在IF语句C中分配变量

时间:2018-11-05 18:17:54

标签: c variables if-statement

以下代码用于输出蜗牛完成比赛所需的时间。我知道要使用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;
}

2 个答案:

答案 0 :(得分: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,那么使用数组将会变得更加明显。

在设计算法时,要问自己的一个重要问题是:我的解决方案是否“规模化” [向上]?

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的数组。我这样做只是为了简化/清理代码。在此过程中,我能够发现并修复至少五个潜在的,直到清理才明显的错误。

答案 1 :(得分:0)

如果我正确理解您想要做的事情,像这样:

  

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

当您用英语这样说时,它有助于简化翻译成代码的过程。首先,我们需要计算总时间。您可以通过一次分配来做到这一点:

int total = TotalSeconds1 + TotalSeconds2 + TotalSecond3 + TotalSeconds4;

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

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

当然,您可以在此处更改此消息以适合您的需求。

一些建议:

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

  2. 了解循环。我了解您是编程新手。如果您尚未了解forwhile循环,那么我敢肯定它们会在课堂上出现。这些出色的工具可让您为重复执行的任务编写更少的代码。

  3. 了解数组。与#2相同,数组允许您使用单个变量存储数据列表。每当发现自己在变量的末尾都用数字命名时,就应该使用数组代替。

相关问题