【问题标题】:Assigning variable in IF statement C在 IF 语句 C 中分配变量
【发布时间】:2018-11-05 18:17:53
【问题描述】:

下面的代码是输出蜗牛完成比赛所用的时间。我知道要使用 if 语句,但是我正在努力寻找一种方法来分配我的最终代码-TimeMinutes1 + TimeMinutes2 + TimeMinutes3 + TimeMinutes4,TimeSeconds1 + TimeSeconds2 + TimeSeconds3 + TimeSeconds4 一个可以与 IF 语句结合使用的变量?

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char SquirrelName [20]; 
    int TimeMinutes1;
    int TimeMinutes2;
    int TimeMinutes3;
    int TimeMinutes4;
    int TimeSeconds1;
    int TimeSeconds2;
    int TimeSeconds3;
    int TimeSeconds4;
    int TotalSeconds1;
    int TotalSeconds2;
    int TotalSeconds3;
    int TotalSeconds4;

    printf("What is the name of the squirrel? \n");
    scanf("%s", SquirrelName);

    printf("How long did it take to complete the first lap in Seconds? \n");
    scanf("%d", &TotalSeconds1);

    TimeMinutes1 = TotalSeconds1 / 60;
    TimeSeconds1 = TotalSeconds1 % 60;

    printf("Lap 1 finished in %d minutes and %d seconds\n", TimeMinutes1, TimeSeconds1);

    printf("How long did it take to complete the second lap in Seconds? \n");
    scanf("%d", &TotalSeconds2);

    TimeMinutes2 = TotalSeconds2 / 60;
    TimeSeconds2 = TotalSeconds2 % 60;

    printf("Lap 2 finished in %d minutes and %d seconds\n", TimeMinutes2, TimeSeconds2);

    printf("How long did it take to complete the third lap in Seconds? \n");
    scanf("%d", &TotalSeconds3);

    TimeMinutes3 = TotalSeconds3/ 60;
    TimeSeconds3 = TotalSeconds3 % 60;

    printf("Lap 3 finished in %d minutes and %d seconds\n", TimeMinutes3, TimeSeconds3);

    printf("How long did it take to complete the fourth lap in Seconds? \n");
    scanf("%d", &TotalSeconds4);

    TimeMinutes4 = TotalSeconds4 / 60;
    TimeSeconds4 = TotalSeconds4 % 60;

    printf("Lap 4 finished in %d minutes and %d seconds\n", TimeMinutes4, TimeSeconds4);

    printf("The total time it took for the course to complete was %d minutes and %d seconds\n", TimeMinutes1 + TimeMinutes2 + TimeMinutes3 + TimeMinutes4, TimeSeconds1 + TimeSeconds2 + TimeSeconds3 + TimeSeconds4);

    return 0;
}

【问题讨论】:

  • @itsme86 使用 c 制作了这个程序
  • 你能给我们举个例子来说明 if 语句的作用吗? (特别是你需要做什么但你不能?)你可以将它们存储在单独的变量中,如果你只想要一秒钟的总测试(或者只是加起来),你可以将分钟 * 60 添加到秒原来的 TotalSeconds)
  • 你能用文字描述你想做什么吗?
  • 那么我认为最简单的方法就是将 TotalSeconds1+2+3+4 相加并检查是否小于 4*60。但是如上所述,您可能仍然需要这样做,以便您可以打印正确划分为分钟和秒的总时间,如果您确实以分钟和秒为单位计算总时间,您可以改为测试分钟
  • totalSeconds = TotalSeconds1 + TotalSeconds2 + TotalSeconds3 + TotalSeconds4; if (totalSeconds &lt; 240) { printf("Snail or Squirrel or whatever qualified!\n"); }

标签: c variables if-statement


【解决方案1】:

只要有多个/多个以数字结尾的变量,例如foo1foo2foo3 等,这表明我们应该使用数组[和循环而不是复制代码] .

如果我们有多个并行数组被同一个索引变量索引,比如:

#define LAPCOUNT    4
int time_tot[LAPCOUNT];
int time_min[LAPCOUNT];
int time_sec[LAPCOUNT];

这表明我们应该创建一个struct 并拥有这些结构的数组。

以下是使用这些想法的代码版本:

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int time_tot;
    int time_min;
    int time_sec;
} lap_t;

#define LAPCOUNT    4

int
main(void)
{
    char SquirrelName[20];
    int lapidx;
    lap_t *lap;
    lap_t laplist[LAPCOUNT];
    lap_t laptot;

    printf("What is the name of the squirrel? \n");
    scanf("%s", SquirrelName);

    for (lapidx = 0;  lapidx < LAPCOUNT;  ++lapidx) {
        lap = &laplist[lapidx];

        printf("How long did it take to complete lap %d in Seconds? \n",
            lapidx + 1);

        scanf("%d", &lap->time_tot);

        lap->time_min = lap->time_tot / 60;
        lap->time_sec = lap->time_tot % 60;

        printf("Lap %d finished in %d minutes and %d seconds\n",
             lapidx + 1,lap->time_min, lap->time_sec);
    }

    laptot.time_min = 0;
    laptot.time_sec = 0;
    laptot.time_tot = 0;

    for (lapidx = 0;  lapidx < LAPCOUNT;  ++lapidx) {
        lap = &laplist[lapidx];
        laptot.time_min += lap->time_min;
        laptot.time_sec += lap->time_sec;
        laptot.time_tot += lap->time_tot;
    }

#if 0
    printf("The total time it took for the course to complete was %d minutes and %d seconds\n",
        laptot.time_min,laptot.time_sec);
#else
    laptot.time_min = laptot.time_tot / 60;
    laptot.time_sec = laptot.time_tot / 60;
    printf("The total time it took for the course to complete was %d minutes and %d seconds\n",
        laptot.time_min,laptot.time_sec);
#endif

    return 0;
}

更新:

谢谢,我一定会更多地研究这个

这是未来的一个重要概念。

如果圈数更大,例如 100 圈而不是 4 圈,使用数组变得更加明显。

在设计算法时,要问自己的一个重要问题是:我的解决方案是否“扩展”[up]?

struct 类似于表单或 [数据库] 记录。它将所有相关的东西放在一起。表格是一张包含给定主题的所有信息的单张纸(例如税表或人事记录)。

考虑一个简单的人事记录:

typedef struct {
    char person_name[100];
    char person_street[100];
    char person_city[100];
    char person_state[2];
    char person_telno[10];
    int person_age;
    float person_salary;
} person;

这就像每个人只有一个页面,而员工档案包含所有这些页面。

如果没有struct 的想法,我们需要为上述每个记录“字段”设置单独的文件夹

姓名:

Smith, John
Jones, Fred
Miller, Mary

街道:

123 Main St
235 Elm St
63 Oak Ave

城市:

New York
Chicago
Los Angeles

使用struct,我们的组织看起来更像:

Smith, John     123 Main St     New York
Jones, Fred     235 Elm St      Chicago
Miller, Mary    63 Oak Ave      Los Angeles

当然,在现实世界中,这种数据组织似乎是显而易见的。但是,在编码时,尤其是对于更抽象的事物,有时可能会被问题的复杂性所掩盖。

能够细化/减少代码 [通常我确保使用的数据结构最少且完整],可以使代码保持简单、干净和健壮。

这甚至可能发生在有经验的程序员身上。在我从事的一个真正的商业产品中,我发现使用了“并行阵列”。我重构了代码以使用我创建的新struct 数组。我这样做只是为了简化/清理代码。在此过程中,我发现并修复了至少五个在清理之前隐藏且不明显的错误。

【讨论】:

  • 谢谢,我一定会进一步调查
【解决方案2】:

如果我理解正确,你想要做的是这样的:

如果总时间少于 4 分钟,则显示一条消息

当你像这样用英语表达它时,它有助于更​​容易翻译成代码。首先我们需要计算总时间。你可以通过一个作业来做到这一点:

int total = TotalSeconds1 + TotalSeconds2 + TotalSecond3 + TotalSeconds4;

现在我们可以编写一个 if 语句。一种方法是将 4 分钟转换为秒,但是我懒得手工这样做,所以我在代码中明确写了:

if (total <= 4 * 60) {
    printf("You qualified!")
}

当然,您可以在此处更改消息以满足您的需要。

一些建议:

  1. 使用有意义的变量名。在大多数情况下,您做得很好。我在这里唯一的建议是使用lap1lap2 等,而不是TotalSeconds1

  2. 了解循环。我了解您是编程新手。如果您还没有了解 forwhile 循环,我相信他们会在课堂上出现。这些都是很棒的工具,可以让您为重复的任务编写更少的代码。

  3. 了解数组。与 #2 一样,数组允许您使用单个变量存储数据列表。每当您发现自己以数字结尾命名变量时,您很可能应该使用数组来代替。

【讨论】:

    猜你喜欢
    • 2018-07-31
    • 2014-03-05
    • 2018-04-02
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-02
    • 1970-01-01
    相关资源
    最近更新 更多