使用scanf为数组(C)中的每个值分配时间的程序

时间:2018-11-10 16:42:43

标签: c arrays time while-loop scanf

我正在为将数字和名称存储到数组中进行分配。我们必须存储4个数字,以秒为单位表示时间到数组中的每个数字中。然后将其汇总并以分钟和秒的格式输出每个英文句子中的数字。

这是我所拥有的东西,它很小,我什至不确定它能达到预期的效果:

#include <stdio.h>

int main()
{
    int i = 0; 
    int person[8] = { 0 };

    int leg1;
    int leg2;
    int leg3;
    int leg4;

    while (i < 8) {
        printf("Hello, please enter the number of the person.:\n"); 
        scanf("%d", &person[i]); 
        i = i + 1;
    }

    printf("%d ", i);
    return 0;
}

理想情况下,我想为数组中的每个人记录一个名字,而不是数字-总共8个名字,但即使是我认为不起作用的数字。

我不确定是否可以使用scanf在代码4的时间值中分配每个数字。

这里有腿可以存储4个时间值,然后我将它们相加并将其转换为秒,然后整体输出每个值的时间。我只是不知道如何对数组进行操作。

edit:我不太习惯这种编辑的工作方式,我写的一半已经消失了,但是没关系,大部分都还是超大型的。这是我记录时间并对时间进行分类的其他代码:

#include <stdio.h>

int main()
{
    char name[31]; 
    int leg1;
    int leg2; 
    int leg3;
    int leg4;
    int totalTime;

    printf("Hello, please enter the name of the person.:\n");
    scanf("%30s", &name);

    printf("Now please enter the time of the person for the first leg in seconds.:\n");
    scanf("%d", &leg1); 

    printf("Now please enter the time of the person for the second leg in seconds.:\n"); 
    scanf("%d", &leg2); 

    printf("Now please enter the time of the person for the third leg in seconds.:\n");
    scanf("%d", &leg3); 

    printf("Now please enter the time of the person for the final leg in seconds.:\n");
    scanf("%d", &leg4); 

    totalTime = leg1 + leg2 + leg3 + leg4; 

    int minutes = totalTime / 60;
    int seconds = totalTime % 60; 

    if (minutes < 4)
    {
        printf("\t%30s qualified for the International Tournament with a time of %d minutes and %d seconds", name, minutes, seconds);
    }
    else if (minutes >= 4 && minutes < 12)
    {
        printf("\t%30s qualified for the Natonal Race Meeting with a time of %d minutes and %d seconds", name, minutes, seconds);
    }
    else if (minutes >= 12 && minutes < 30)
    {
        printf("\t%30s qualified for the Beginner's League with a time of %d minutes and %d seconds", name, minutes, seconds);
    }
    else if (minutes >= 30)
    {
        printf("\t%30s did not qualify for any league with a really shit time of %d minutes and %d seconds", name, minutes, seconds);
    }

    return 0;

}

1 个答案:

答案 0 :(得分:1)

由于您是为某项作业而做的,并且尚未被引入struct中,因此您可能想避免使用它。但这是将相关信息分组为记录的方式:

struct Racer {
    char name[31]; 
    int leg1;
    int leg2; 
    int leg3;
    int leg4;
};

int main(int argc, char *argv) {
    struct Racer racers[8];
    int i;
    for (i = 0; i < 8; ++i) {
        scanf("%30s", &racers[i].name);
        scanf("%d", &racers[i].leg1);
        /* ... */
    }
    int totalTime = 0;
    for (i = 0; i < 8; ++i) {
        totalTime = totalTime + racers[i].leg1 + /* ... */
        /* ... */
    }
    /* ... */
}

为避免此时使用结构,您可以只使用并行数组。另请参见"Store multiple strings in array"

int main(int argc, char *argv) {
    char names[8][31];
    int legs1[8];
    int legs2[8];
    int legs3[8];
    int legs4[8];
    int i;
    for (i = 0; i < 8; ++i) {
        scanf("%30s", &names[i]);
        scanf("%d", &legs1[i]);
        /* ... */
    }
    int totalTime = 0;
    for (i = 0; i < 8; ++i) {
        totalTime = totalTime + legs1[i] + /* ... */
        /* ... */
    }
    /* ... */
}

(旁注:我会提到这两种方法在运行时并不等效,因为它们在内存上的布局有所不同。在某些情况下,您可能会选择单独的数组是有原因的。要逐个遍历数组,最好将数据紧密结合在一起,而不是将其拆分为多个记录中的元素。)