大一下 C语言大作业

Hi, 这篇文章是关于大一年级下学期C语言最后的大作业,年代久远,仅供参考哦~

题目:

Coursework 2016 DeadLine June 27th

Write a program that will help elementary school pupils practice math.
a) The program will first ask the user for his/her ID number (including two letters & 4 digits), e.g.
Please input your four digit ID no: AB1234
The program should have input validation.
Then the program prompts three choices:
(1) Start a test
(2) Check scores
(3) Exit

Test: the program will give 10 math problems, e.g.:

12 * 3 = 36
48 + 32 = 80

56 / 28 = 2

Note:
i) Pupils will answer each problem before the next one is given.

ii) The problems should include addition, subtraction, multiplication and division. They are randomly generated.

iii) Randomly generates numbers for problems. However, must ensure that both the problem and the result are no larger than two digits. The problem and the result should be greater than or equal to zero. The divisor cannot be zero.

iv) After ten problems are finished, record the time used by the student to do the ten problems.

v) Gives a score to each student. Saves this student’s ID, his/her score and the time used into a file named ‘record.txt’.

vi) Print the following information on the screen:
Prob. | Correct Answ. | Ur Answ

c) Check scores: Searches the file ‘record.txt’ and lists all the historical scores for this student, e.g.:

Your previous records are:
AB1234 80 150 seconds
AB1234 50 182 seconds
AB1234 90 98 seconds

You will be marked based on your program’s:
(1) Correctiveness
(2) Readability
(3) Robustness
(4) Conciseness

代码:

注意当年我在大作业里用了goto,但后来我得知goto会影响健壮性,会扣分的,所以建议不要用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <string.h>

main()
{
char ID[7],option[2],symbol[10],convert[3],answer[10][3];
int n,i,a[10],b[10],result[10],score=0,grade,timeduation;
time_t start, end;
FILE *data;
void check(char[]);

do{
printf("Please input your four digit ID (e.g.AB1234): ");
scanf("%s",ID);
}while(!(ID[6]=='\0'&&isalpha(ID[0])&&isalpha(ID[1])&&isdigit(ID[2])&&isdigit(ID[3])&&isdigit(ID[4])&&isdigit(ID[5])));//to inspect whether the user type in the correct ID

choice://use goto function to return as a loop
printf("\nPlease type in the corresponding number to continue.\n(1) Start a test\n(2) Check scores\n(3) Exit\n");
scanf("%s",option);
if(option[1]!='\0'||isdigit(option[0])==0)
{
printf("Wrong Input!");
goto choice;
}//to inspect whether user type in the correct number
switch(option[0])//use ASCII to get the correspoding funtion
{
case 49:getchar();
printf("---------------------------------------\n"); //start a test
srand((unsigned)time(NULL));
time(&start);
for(i=0;i<10;i++)
{
n=rand()%4;
do{
a[i]=rand()%100;
b[i]=rand()%100;
switch(n){
case 0:symbol[i] = '+';
result[i] = a[i] + b[i];
break;
case 1:symbol[i] = '-';
result[i] = a[i] - b[i];
break;
case 2:symbol[i] = '*';
result[i] = a[i] * b[i];
break;
case 3:symbol[i] = '/';
result[i] = a[i] / b[i];
}
}while(result[i] > 99 || result[i] < 0 || n==3 && a[i] % b[i] != 0);
printf("%2d %c %2d = ", a[i], symbol[i], b[i]);
itoa(result[i], convert, 10);
gets(answer[i]);
if (!strcmp(convert, answer[i])) {
score += 10;
}
}
time(&end);
printf("---------------------------------------\nBravo! You have got %d score(s) in %ld seconds!\n",score,end-start);
printf("Prob. | Correct Answ. | Ur Answ\n");
for(i=0;i<10;i++)
printf("(%-2d) %-2d %c %-2d = %-3d %s\n",i+1,a[i],symbol[i],b[i],result[i],answer[i]);
data = fopen("record.txt", "a+"); //save the information into a file
fprintf(data,"%s %d %d\n",ID,score,end-start);
fclose(data);
goto choice;//return to the option menu
break;
case 50:check(ID);
goto choice;//return to the option menu
break;
case 51:printf("Thanks for testing! This program is about to quit.\n");//quit the program
break;
default:printf("Wrong Input! ");
goto choice;
}
system("pause");
}

void check(char ID[])
{
FILE *file;
int getgrade,gettime,j=0;
char getid[6];

file=fopen("record.txt","r");
if(file==NULL)
printf("You have never done the test before, so there is no record.");
fscanf(file,"%s %d %d",getid,&getgrade,&gettime);
printf("Your previous records are:\n");//print the previous grades
while(!feof(file))
{
if(strncmp(ID,getid,6)==0)
{
printf("%s %d %d \n\n",ID,getgrade,gettime);
j=1;
}
fscanf(file,"%s %d %d",getid,&getgrade,&gettime);
}
if(j==0)
printf("Sorry, there is no previou grade.\n");
fclose(file);
}

后来又随便帮别人写了一份差不多的,也放上来吧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#include <string.h>
//This is a program that will help elementary school pupils practice math.

int main()
{
char fid[10],ID[10],symbol[10],convert[3],answer[10][3];
int j=0,i,op,n,a[10],b[10],result[10],score=0,grade,timeduation;
FILE *data;
time_t tstart, tend;

do{
printf("Please input your four digit ID number such as AB1234: ");
scanf("%s",ID);
if(!(ID[6]=='\0'&&isalpha(ID[0])&&isalpha(ID[1])&&isdigit(ID[2])&&isdigit(ID[3])&&isdigit(ID[4])&&isdigit(ID[5])))
printf("Sorry, invalid ID. ");
}while(!(ID[6]=='\0'&&isalpha(ID[0])&&isalpha(ID[1])&&isdigit(ID[2])&&isdigit(ID[3])&&isdigit(ID[4])&&isdigit(ID[5])));//To check the ID invalidation

choose:
printf("\nPlease type in the number to continue.\n(1) Start a test\n(2) Check scores\n(3) Exit\n");
scanf("%d",&op);
switch(op)//let user to choose what function they want
{
case 1:
getchar();
printf("\n");
srand((unsigned)time(NULL));
time(&tstart);
for(i=0;i<10;i++)//to start the test
{
n=rand()%4;
do{
a[i]=rand()%100;
b[i]=rand()%100;
switch(n){
case 0:symbol[i] = '+';
result[i] = a[i] + b[i];
break;
case 1:symbol[i] = '-';
result[i] = a[i] - b[i];
break;
case 2:symbol[i] = '*';
result[i] = a[i] * b[i];
break;
case 3:symbol[i] = '/';
result[i] = a[i] / b[i];
}
}while(result[i] > 99 || result[i] < 0 || n==3 && a[i] % b[i] != 0);
printf("%2d %c %2d = ", a[i], symbol[i], b[i]);
itoa(result[i], convert, 10);
gets(answer[i]);//gather the user's answers
if (!strcmp(convert, answer[i])) {
score += 10;
}
}
time(&tend);//time ends
printf("\nYou have got %d score(s) in %ld seconds!\n",score,tend-tstart);
printf("Prob. | Correct Answ. | Ur Answ\n"); //print the test information
for(i=0;i<10;i++)
{
printf("(%2d) %2d %c %2d = %3d %s\n",i+1,a[i],symbol[i],b[i],result[i],answer[i]);
}
data = fopen("record.txt", "a+");
fprintf(data,"%s %3d %3ld\n",ID,score,tend-tstart);
fclose(data);
goto choose;
break;
case 2:
data=fopen("record.txt","r");
if(data==NULL)
{
printf("You have never done the test before.");
goto choose;
}
fscanf(data,"%s %d %d",fid,&grade,&timeduation);//scan the user's information
printf("Your previous records are:\n");
while(!feof(data))
{
if(strncmp(ID,fid,6)==0)//check the user's name
{
printf("%s %d %d \n\n",fid,grade,timeduation);
j=1;
}
fscanf(data,"%s %d %d",fid,&grade,&timeduation);
}
if(j==0)
{
printf("Sorry, there is no previous record.\n");
}
fclose(data);
goto choose;
break;
case 3:
printf("It's about to quit! \n");
break; //quit
default:
printf("Wrong Number! ");
goto choose;
}
return 0;
}

附录:我们那年的注意事项

截止日期是6月27日,迟交每日扣10%,最多扣至50%。至期末考试关闭提交系统

源代码为CPP文件,且已通过TC3编译,没有任何编译错误
源文件以完整学号作为文件名,CPP作为扩展名,不得压缩
将原文件作为附件以bupt.edu.cn邮箱发送至[email protected]。邮件主题为完整学号
提交后自行到课程网站确认提交结果
文件名和邮件主题,不能是全角数字
不要重复提交,每重复提交一次,扣减10%