Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C语言中的倒计时,不冻结用户输入_C - Fatal编程技术网

C语言中的倒计时,不冻结用户输入

C语言中的倒计时,不冻结用户输入,c,C,我是C语言的新手,我即将完成一个程序。有人能教我如何在输入东西的同时制作倒计时吗?我目前正在使用sleep(),正如我所听说的,它不利于线程,并冻结输入。是这样吗 下面是函数: void nowPlaying(SONG * h, SONG * t, int * randPrev, int shuffleCon) { SONG * ptr; ptr = h->next; int random, randCount, mincount = 0, choice = 4,

我是C语言的新手,我即将完成一个程序。有人能教我如何在输入东西的同时制作倒计时吗?我目前正在使用sleep(),正如我所听说的,它不利于线程,并冻结输入。是这样吗

下面是函数:

void nowPlaying(SONG * h, SONG * t, int * randPrev, int shuffleCon)
{
    SONG * ptr;
    ptr = h->next;
    int random, randCount, mincount = 0, choice = 4, tot = 0, tot2 = 0;

    if(h->next == t) {
        printf("No songs to be played. Add some!\n");
    }
    else if(h->next != t) {
        while(ptr->next != t) {
            tot2 = tot = ((ptr->cdown.minutes*60) + ptr->cdown.secs);
            do {
                printf("------------------------YouTunes------------------------\n");
                printf("========================================================\n");
                printf("TITLE : %s                                            \n", ptr->title);
                printf("ARTIST: %s                                            \n", ptr->artist);
                printf("ALBUM : %s                                            \n", ptr->album);
                switch(ptr->genre) {
                case 1:
                    printf("GENRE : POP                                           \n");
                    break;
                case 2:
                    printf("GENRE : OPM                                           \n");
                    break;
                case 3:
                    printf("GENRE : ROCK                                          \n");
                    break;
                case 4:
                    printf("GENRE : R&B                                           \n");
                    break;
                case 5:
                    printf("GENRE : ACOUSTIC                                      \n");
                    break;
                case 6:
                    printf("GENRE : CLASSICAL                                     \n");
                    break;
                }

                while(tot2 >= 60) {
                    tot2 = tot2 - 60;
                    mincount++;
                    //if(time_left % 60 == 0) mincount++;
                }

                printf("TIME: ");

                if(mincount < 10)
                    printf("0%d", mincount);
                else
                    printf("%d", mincount);

                printf(":");

                if(tot2 < 10)
                    printf("0%d", tot2);
                else if(tot2 == 60)
                    printf("00");
                else
                    printf("%d", tot2);

                printf("\n========================================================\n");
                printf("[1] Prev                 [0]Exit                  [2] Next\n");
                printf("Choice: ");
                //scanf("%d", &choice);
                //timeout(500);
                tot--;
                tot2 = tot;
                mincount = 0;
                sleep(1);
                system("clear");

                if(shuffleCon == 0) {
                    if(choice == 1) {
                        if(ptr->prev == h) {
                            //do nothing
                        }
                        else if(ptr->prev != h) {
                            ptr = ptr->prev;
                        }
                    }
                    else if(choice == 2) {
                        if(ptr->next == t) {
                            //do nothing
                        }
                        else if(ptr->next != t) {
                            ptr = ptr->next;
                        }
                    }
                }
                else if(shuffleCon == 1) {
                    if(choice == 1) {
                        random = shuffle(h, t, randPrev);
                        randCount = 0;
                        ptr = h->next;
                        while(randCount != random) {
                            ptr = ptr->next;
                            randCount++;
                        }
                    }
                    else if(choice == 2) {
                        random = shuffle(h, t, randPrev);
                        randCount = 0;
                        ptr = h->next;
                        while(randCount != random) {
                            ptr = ptr->next;
                            randCount++;
                        }

                        if(ptr == t) {
                            ptr = ptr->prev;
                            ptr = ptr->prev;
                        }
                    }
                }
            } while(tot != -1);

            if(shuffleCon == 0)
                ptr = ptr->next;
            else if(shuffleCon == 1) {
                random = shuffle(h, t, randPrev);
                randCount = 0;
                ptr = h->next;
                while(randCount != random) {
                    ptr = ptr->next;
                    randCount++;
                }
            }
        }
    }
}
void nowPlaying(宋*h,宋*t,int*randprov,int shuffleCon)
{
歌曲*ptr;
ptr=h->next;
int random,randCount,mincount=0,choice=4,tot=0,tot2=0;
如果(h->next==t){
printf(“没有要播放的歌曲。添加一些!\n”);
}
否则如果(h->next!=t){
while(ptr->next!=t){
tot2=tot=((ptr->cdown.minutes*60)+ptr->cdown.secs);
做{
printf(“---------------------------YouTunes-----------------\n”);
printf(“==================================================================================\n”);
printf(“标题:%s\n”,ptr->TITLE);
printf(“艺术家:%s\n”,ptr->ARTIST);
printf(“相册:%s\n”,ptr->ALBUM);
开关(ptr->类型){
案例1:
printf(“流派:POP\n”);
打破
案例2:
printf(“类型:OPM\n”);
打破
案例3:
printf(“流派:摇滚乐”);
打破
案例4:
printf(“流派:R&B\n”);
打破
案例5:
printf(“流派:声学”);
打破
案例6:
printf(“流派:古典音乐”);
打破
}
而(tot2>=60){
tot2=tot2-60;
mincount++;
//if(time_left%60==0)mincount++;
}
printf(“时间:”);
如果(最小计数<10)
printf(“0%d”,最小计数);
其他的
printf(“%d”,mincount);
printf(“:”);
如果(tot2<10)
printf(“0%d”,总计2);
否则如果(tot2==60)
printf(“00”);
其他的
printf(“%d”,总计2);
printf(“\n===========================================================================================\n”);
printf(“[1]上一个[0]退出[2]下一个\n”);
printf(“选择:”);
//scanf(“%d”,选择(&C);
//超时(500);
托特--;
tot2=tot;
mincount=0;
睡眠(1);
系统(“清除”);
if(shuffleCon==0){
如果(选项==1){
如果(ptr->prev==h){
//无所事事
}
否则如果(ptr->prev!=h){
ptr=ptr->prev;
}
}
else if(选项==2){
如果(ptr->next==t){
//无所事事
}
否则如果(ptr->next!=t){
ptr=ptr->next;
}
}
}
else if(shuffleCon==1){
如果(选项==1){
随机=随机(h,t,randPrev);
randCount=0;
ptr=h->next;
while(随机计数!=随机){
ptr=ptr->next;
randCount++;
}
}
else if(选项==2){
随机=随机(h,t,randPrev);
randCount=0;
ptr=h->next;
while(随机计数!=随机){
ptr=ptr->next;
randCount++;
}
如果(ptr==t){
ptr=ptr->prev;
ptr=ptr->prev;
}
}
}
}而(tot!=-1);
if(shuffleCon==0)
ptr=ptr->next;
else if(shuffleCon==1){
随机=随机(h,t,randPrev);
randCount=0;
ptr=h->next;
while(随机计数!=随机){
ptr=ptr->next;
randCount++;
}
}
}
}
}

广义上讲,其中一个:

  • 使用
    SIGALRM
    (请参阅
    报警
    手册页),并依靠该信号中断系统调用,以期望用户输入

  • (更好,当然也是更有价值的编程练习),使用
    select
    poll
    编写一个事件循环,并将超时设置为超时之前的剩余时间


  • 对于您正在讨论的应用程序类型,(2)将是一个更好的选择。

    广义而言,以下其中之一:

  • 使用
    SIGALRM
    (请参阅
    报警
    手册页),并依靠该信号中断系统调用,以期望用户输入

  • (更好,当然也是一个更有价值的编程练习),使用
    select
    poll
    编写一个事件循环,并将超时设置为tim之前的剩余时间