如何计时人完成 C 程序的速度?
How to time how quickly person completes C program?
我正在尝试做一个小测验来帮助我儿子掌握他的乘法表。是否可以添加一个函数来计算他完成问题的速度(以秒或分钟为单位)。
还想问:为什么程序对第一个数字不是零的任何和都有效?例如(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++;
}
if
条件:将此:if ((product=answer)){
更改为:if ((product==answer)){
此外,对于计算时间,您可以使用 time.h
库中的 time_t
和 clock
。看到这个问题:How to use timer in C?
如何使用计时器的示例(不需要函数 - 它只是几个语句):(您可以将 STARTING TIME
和 ENDING 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);
}
我正在尝试做一个小测验来帮助我儿子掌握他的乘法表。是否可以添加一个函数来计算他完成问题的速度(以秒或分钟为单位)。
还想问:为什么程序对第一个数字不是零的任何和都有效?例如(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++;
}
if
条件:将此:if ((product=answer)){
更改为:if ((product==answer)){
此外,对于计算时间,您可以使用 time.h
库中的 time_t
和 clock
。看到这个问题:How to use timer in C?
如何使用计时器的示例(不需要函数 - 它只是几个语句):(您可以将 STARTING TIME
和 ENDING 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);
}