交通信号程序C Basic

时间:2014-12-26 04:27:07

标签: c

我做了一个小型的基本交通灯计划。它没有给出我期待的输出。我希望它随着时间的推移打印所有五个案例,但是在第二个if if情况下,它打印"红灯红灯"而不是"红灯红灯"然后开始眨眼同样是第五个if else。什么似乎是问题?

#include <time.h>
#include <conio.h>
#include <stdio.h>
int main()
{
    time_t strt, t;
    long difference = 0;
    int a, progrun;
    char start;
    printf("Press 1 to turn on the Traffic Light\n");
    scanf_s("%d", &progrun);
    if (progrun == 1)
    {
        strt = time(NULL);
        printf("East-West Signal   North-East Signal\n");
        while (difference < 15)
        {
            t = time(NULL);
            difference = t - strt;
            int light(difference);
            if (difference <= 3)
            {
                printf("Red Light   Green Light\r");
                fflush stdout;
            }
            else if (difference > 3 && difference <= 6)
            {
                printf("Red Light   Yellow Light\r");
                fflush stdout;
            }
            else if (difference > 5 && difference <= 8)
            {
                printf("Red Light   Red Light\r");
                fflush stdout;
            }
            else if (difference > 8 && difference <= 10)
            {
                printf("Yellow Light   Red Light\r");
                fflush stdout;
            }
            else if (difference > 10 && difference <= 12)
            {
                printf("Green Light   Red Light\r");
                fflush stdout;
            }
            else if (difference > 12 && difference <= 14)
            {
                printf("Yellow Light   Yellow Light\r");
                fflush stdout;
            }
        }
    }
    _getch();
    return 0;
}

1 个答案:

答案 0 :(得分:0)

回车只是将光标移回到行的开头。然后当你打印时,它会覆盖字符,但它不一定会覆盖整行。

这意味着最简单的解决方法是在打印行的末尾添加一些空格。

printf("Red Light   Red Light   \r");