输出错误

伊桑·亨特

我的输出不是应该的

第一行应说明计时器应运行多少时间,第二行将显示正在运行的计时器,第三行将显示得分

我不知道为什么计时器会跳到2147344384

#include <stdio.h>
#include <conio.h>
#include <time.h>

//main
int main(){
    //function declaration

int score(int c , int d);
int timer(int a , int b);

    //function declaration

    // variable declaration;
    time_t t1;
    int e ;
    //variable declaration;

printf("how many seconds\n");
scanf("%d",e);
(void) time(&t1);// time starts
while(1){

    printf("timer will run for %d seconds\n",e);
    printf("Time remaining = %d\n",timer(t1,e));
    printf("Current Score = %d\n",score(timer(t1,e), e));
    if (timer(t1,e) == 0){break;}
    system("cls");

}

}
//main


//timer
int timer(int starttime, int fortime){
    int tori = 0;
    // (void) time(&t1); // paste this where u have to start time
time_t t2;

   while(1){
    (void) time(&t2);
     tori=fortime-(t2-starttime);
     return (tori);
   }

}
//timer

// score
int score(int timerem, int time){
   int k , ka ;
    ka = ((timerem/time)*100);


if (ka >90 && ka <= 100){
    k = 10;
}
else if (ka >80 && ka <= 90){
    k = 9;
}
else if (ka >70 && ka <= 80){
    k = 8;
}
else if (ka >60 && ka <= 70){
    k = 7;
}
else if (ka >50 && ka <= 60){
    k = 6 ;
}
else if (ka >40 && ka <= 50){
    k = 5;
}
else if (ka >30 && ka <= 40){
    k = 4;
}
else if (ka >20 && ka <= 30){
    k = 3;
}
else if (ka >10 && ka <= 20){
    k = 2;
}
else if (ka >50 && ka <= 10){
    k = 1;
}
else if (ka >0 && ka <= 5){
    k = 0;
}
return (k);
}
//score
用户名
there were two significant bugs in the posted code:
-- the timer function, calculation of percent remaining time always yielded 0
-- the score function, 'k' was not initialized when percent remaining time was 0

there is a the 'problem' that the outputs to the screen are at a very fast rate.
suggest adding a nanosleep() call of about 1/2 second in the main while loop

// <-- corrected included files for portability 
// <-- (and so will compile on my linux system)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>   // time()


// prototypes  // <-- moved function prototypes to file globally visible location
               // <-- otherwise compiler (except inside main()) will use default 
               // <-- return and parameter typing
int score(int c , int d);
int timer(int a , int b);

//main
int main()
{
    // variable declaration;
    time_t t1;
    int e ;

    printf("how many seconds\n");
    if( 1 != scanf(" %d", &e) ) // <-- corrected problems with statement
    { // then scanf failed      // <-- added error checking
        perror( "scanf for seconds failed" );
        return(-1);
    }

    // implied else, scanf successful

    t1 = time(NULL);// time starts // <-- modified for clarity

    while(1)
    {
        // <-- modified loop so all functions use same time value
        // <-- modified loop to eliminate sudden loss of initial/subsequent screen display
        time_t t2 = timer(t1,e);

        //system( "cls" );   // uncomment for windows
        //system( "clear" ); // uncomment for linux
        printf("timer will run for %d seconds\n",e);
        printf("Time remaining = %d\n",(int)t2);
        printf("Current Score = %d\n",score(t2, e));
        if (!t2) { break; }
    } // end while

    return(0);
} // end function: main


//timer
int timer(int starttime, int fortime)
{
    int tori = 0;
    // (void) time(&t1); // paste this where u have to start time
    time_t t2 = time(NULL);      // <-- modified for clarity
                                 // <-- removed unneeded/do nothing while loop
    tori=fortime-(t2-starttime);
    return (tori);
} // end function: timer


// score
int score(int timerem, int time)
{
    int k;   // returned value
    int ka;  // calculated percentage not expired

    ka = (timerem*100)/time; // <-- corrected this calculation


    if (ka >90 && ka <= 100)
    {
        k = 10;
    }

    else if (ka >80 && ka <= 90)
    {
        k = 9;
    }

    else if (ka >70 && ka <= 80)
    {
        k = 8;
    }

    else if (ka >60 && ka <= 70)
    {
        k = 7;
    }

    else if (ka >50 && ka <= 60)
    {
        k = 6 ;
    }

    else if (ka >40 && ka <= 50)
    {
        k = 5;
    }

    else if (ka >30 && ka <= 40)
    {
        k = 4;
    }

    else if (ka >20 && ka <= 30)
    {
        k = 3;
    }

    else if (ka >10 && ka <= 20)
    {
        k = 2;
    }

    else if (ka >50 && ka <= 10)
    {
        k = 1;
    }

    else if (ka > 0 && ka <= 5)
    {
        k = 0;
    }

    else // <-- added this code block, which eliminates display of random value at end
    {
        k = 0;
    }
    return (k);
} // end function: score

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章