需求分析:
根据键盘输入数字显示对应的表达式,答完题后给出正确错误题目的个数和题号。
功能设计:
系统内部自主生成表达式,并能对结果进行评判。
设计实现:
先做出一个简单的四则运算,然后在其基础上把答案进行比较并记录此时的题号,最后输出答题结果。
代码说明:
1 #include "stdafx.h"
2 #include<stdlib.h>
3 #include <time.h>
4 #include <string>
5 #include <iostream>
6 using std::string;
7 using namespace std;
8
9 int _tmain(int argc, _TCHAR* argv[])
10 {
11 int a1,a2,b1,b2,c1,c2,d,e,i,n,z;
12 char op1,op2;
13 string s,r,w;
14 string h1,h2;
15 char g[] = " ";
16 int right = 0;
17 int wrong = 0;
18 char r1[] = " ";
19 char w1[] = " ";
20 srand(unsigned(time(0)));
21 cout << "输入题数:";
22 cin >> n;
23 for(i=0;i<n;i++)
24 {
25 a1 = rand()%10;
26 b1 = rand()%10;
27 c1 = rand()%10;
28 d = rand()%4;
29 e = rand()%4;
30 switch(d)
31 {
32 case 0:op1='+';break;
33 case 1:op1='-';break;
34 case 2:op1='*';break;
35 case 3:op1='/';break;
36 }
37 switch(e)
38 {
39 case 0:op2='+';break;
40 case 1:op2='-';break;
41 case 2:op2='*';break;
42 case 3:op2='/';break;
43 }
44 while(op1=='/'&&b1==0)b1 = rand()%10;
45 while(op2=='/'&&c1==0)c1 = rand()%10;
46 if(op1=='+'&&op2=='+')z = a1 + b1 + c1;
47 if(op1=='+'&&op2=='-')z = a1 + b1 - c1;
48 if(op1=='+'&&op2=='*')z = a1 + (b1 * c1);
49 if(op1=='+'&&op2=='/')z = a1 + (b1 / c1);
50 if(op1=='-'&&op2=='+')z = a1 - b1 + c1;
51 if(op1=='-'&&op2=='-')z = a1 - b1 - c1;
52 if(op1=='-'&&op2=='*')z = a1 - (b1 * c1);
53 if(op1=='-'&&op2=='/')z = a1 - (b1 / c1);
54 if(op1=='*'&&op2=='+')z = a1 * b1 + c1;
55 if(op1=='*'&&op2=='-')z = a1 * b1 - c1;
56 if(op1=='*'&&op2=='*')z = a1 * b1 * c1;
57 if(op1=='*'&&op2=='/')z = a1 * b1 / c1;
58 if(op1=='/'&&op2=='+')z = a1 / b1 + c1;
59 if(op1=='/'&&op2=='-')z = a1 / b1 - c1;
60 if(op1=='/'&&op2=='*')z = a1 / b1 * c1;
61 if(op1=='/'&&op2=='/')z = a1 / b1 / c1;
62 cout << i+1 << "." << " ";
63 cout <<a1 << op1 << b1 << op2 << c1 << "=";
64 cin >> s;
65 sprintf(g,"%d",z);
66 h1 += g;
67 h2 += s;
68 if(g == s){
69 right++;
70 sprintf(r1,"%d",i+1);
71 r +=r1;
72 }
73 else{
74 wrong++;
75 sprintf(w1,"%d",i+1);
76 w +=w1;
77 }
78 }
79 cout << "\n正确解答:" << right << endl;
80 cout << "正确题号:";
81 for(i=0;i<right;i++){
82 cout << r[i] << " ";
83 }
84 cout << "\n错误解答:" << wrong << endl;
85 cout << "错误题号:";
86 for(i=0;i<wrong;i++){
87 cout << w[i] << " ";
88 }
89 system("pause");
90 return 0;
91
92 }
测试运行:
PSP:
PSP2.1 |
Personal Software Process Stages |
Time Senior Student |
Time |
Planning |
计划 |
0.5h |
1h |
· Estimate |
估计这个任务需要多少时间 |
2d |
2d |
Development |
开发 |
1d |
1.5d |
· Analysis |
需求分析 (包括学习新技术) |
1h |
2h |
· Design Spec |
生成设计文档 |
0 |
0 |
· Design Review |
设计复审 |
0 |
0 |
· Coding Standard |
代码规范 |
1h |
0.5h |
· Design |
具体设计 |
1h |
1h |
· Coding |
具体编码 |
2d |
1.5d |
· Code Review |
代码复审 |
2h | 1h |
· Test |
测试(自我测试,修改代码,提交修改) |
1h |
2h |
Reporting |
报告 |
1h |
0.5h |
· |
测试报告 |
0 |
0 |
· |
计算工作量 |
0 |
0 |
· |
并提出过程改进计划 |
0 |
0 |
小结:
班级大佬代码强多了,功能做的也比较完善。个人基础比较差,做不到所有功能,这个四则运算在除法上面也做的不够完整。再接下来的日子要好好锻炼自己,争取赶上班级同学的步伐。