如何计算人员完成C程序的速度有多快?

时间:2015-04-27 17:25:17

标签: c math time

我正在尝试做一个测验来帮助我的儿子掌握他的时间表。是否可以添加一个函数来计算他完成问题的速度(以秒或分钟为单位)。

另外想问一下:为什么程序适用于任何没有零作为第一个数字的总和?例如(3 * 5 = 15或5 * 0 = 0,但当问题为0 * 5时,输入0时输入不正确。)

感谢您的帮助。这是代码的主要部分。

#include <stdio.h>
#include <time.h>
#include <conio.h>
int main(void){
    int response=0;
    int correctAnswers=0;
    int incorrectAnswers=0;
    int i;//counter
    int percentage;
    int product;
    time_t t;
    printf ("Enter number of sums you want to attempt:\n");
    scanf ("%d",&response);

    if (response==0){
                    printf ("Thanks for playing\n");
                    return 0;
                    }
    srand((unsigned) time(&t));
    for (i=0;i<=response;i++){
        int answer=0;
        int a=rand()%12;
        int b=rand()%12;
        product=a*b;
    printf ("%d * %d=\n",a,b);
    scanf("%d",&answer);
    if ((product=answer)){
                       printf("That's correct\n");
                       correctAnswers++;
                       }
    else {
         printf ("That's wrong!\n");
         incorrectAnswers++;
         }

1 个答案:

答案 0 :(得分:3)

if条件:将此更改为if ((product=answer)){if ((product==answer)){

此外,对于计算时间,您可以使用time_t库中的clocktime.h。请参阅此问题:How to use timer in C?

如何使用计时器的示例(不需要函数 - 它只是几个语句):(您可以将STARTING TIMEENDING TIME放在代码中的任何位置)

#include <stdio.h>
#include <time.h>
#include <conio.h>
#include<stdlib.h>
int main(void){
int response=0;
int correctAnswers=0;
int incorrectAnswers=0;
int i;//counter
int percentage;
int product,msec;
time_t t;
clock_t before = clock();   // STARTING TIME
printf ("Enter number of sums you want to attempt:\n");
scanf ("%d",&response);

if (response==0){
                printf ("Thanks for playing\n");
                return 0;
                }
srand((unsigned) time(&t));
for (i=0;i<=response;i++){
    int answer=0;
    int a=rand()%12;
    int b=rand()%12;
    product=a*b;
printf ("%d * %d=\n",a,b);
scanf("%d",&answer);
if ((product==answer)){
                   printf("That's correct\n");
                   correctAnswers++;
                   }
else {
     printf ("That's wrong!\n");
     incorrectAnswers++;
}

 }

// ENDING TIME
   clock_t difference = clock() - before;
msec = difference * 1000 / CLOCKS_PER_SEC;
printf("Time taken %d seconds %d milliseconds \n",
msec/1000, msec%1000);
}