第三次作业

                                      组员:寿克霞、宋光能

题目:

  •   请编写一个能自动生成小学四则运算题目的 “软件”。
  •   让程序能接受用户输入答案,并判定对错。 
  •   最后给出总共 对/错 的数量。

     需求分析

  •  编写小学四则运算测试系统,要求完成两位数的加,减,乘,除四则运算。
  •  能根据用户的输入来选择运算种类,用户输入答案后可以判断正误。
  •  做完一次测试后,用户可以决定是否继续进行下一次运算。        

    扩展功能

           -可以出表达式里含有负整数(负整数最小不小于-100)的题目,且负数需要带括号,用户输入的结果不用带括号。

    设计

  • 定义变量i,j,a,b,m其中i表示答对的题数,j表示答错的题数,a,b表示随机产生的数,m接收从键盘输入的运算结果。
  •  分别定义加,减,乘,除子函数,出错和正确都有提示。
  •  主函数用switch来实现用户的输入来选择运算种类,并输出答题的数目和正确的题数
  • 函数声明和加、减、乘、除存放在头文件(.h),测试和实现存放在头文件(.cpp)中。
  • 有加法测试功能

     代码实现

main.cpp(主函数)

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<time.h>
  4 #include"test.h"
  5 int testadd();
  6 
  7 int i=0,j=0;
  8 
  9 void jia()
 10 {
 11     int a,b,m;
 12     srand(time(NULL));
 13     a=1+rand()%100;
 14     b=1+rand()%100;
 15     printf("%d\n",a);
 16     printf("%d\n",b);
 17     printf("a+b=?\n");
 18     int c=add(a,b);
 19     printf("请输入运算结果\n");
 20     scanf("%d",&m);
 21     if(m==c)
 22     {
 23         printf("恭喜你答对了\n");
 24         i++;
 25     }
 26     else
 27     {
 28         printf("你答案有错误\n");
 29         j++;
 30     }
 31     
 32 }
 33 
 34 void jian()
 35 {
 36     int a,b,m;
 37     srand(time(NULL));
 38     a=1+rand()%100;
 39     b=1+rand()%100;
 40     if(a<b)
 41     {
 42         int t=0;
 43         t=a;a=b;b=t;
 44     }
 45     printf("%d\n",a);
 46     printf("%d\n",b);
 47     printf("a-b=?\n");
 48     int c=subtract(a,b);
 49     printf("请输入运算结果\n");
 50     scanf("%d",&m);
 51     if(m==c)
 52     {
 53         printf("恭喜你答对了\n");
 54         i++;
 55     }
 56     else
 57     {
 58         printf("你答案有错误\n");
 59         j++;
 60     }
 61 }
 62 
 63 void chen()
 64 {
 65     int a,b,m;
 66     srand(time(NULL));
 67     a=1+rand()%20;
 68     b=1+rand()%20;
 69     printf("%d\n",a);
 70     printf("%d\n",b);
 71     printf("a*b=?\n");
 72 
 73     float c=multiply(a,b);
 74 
 75     printf("请输入运算结果\n");
 76     scanf("%d",&m);
 77     if(m==c)
 78     {
 79         printf("恭喜你答对了\n");
 80         i++;
 81     }
 82     else
 83     {
 84         printf("你答案有错误\n");
 85         j++;
 86     }
 87     
 88 }
 89 
 90 void chu()
 91 {
 92     int a,b,m;
 93     srand(time(NULL));
 94     a=1+rand()%100;
 95     b=1+rand()%100;
 96     if(b==0)
 97     {
 98         b=1+rand()%100;
 99     }
100     if(a<b)
101     {
102            int t=0;
103         t=a;a=b;b=t;
104     }
105     while(a%b!=0)
106     {
107         a=1+rand()%100;
108         b=1+rand()%100;
109     }
110     printf("%d\n",a);
111     printf("%d\n",b);
112     printf("a/b=?\n");
113 
114     double c=divide(a,b);
115 
116     printf("请输入运算结果\n");
117     scanf("%d",&m);
118     if(m==c)
119     {
120         printf("恭喜你答对了\n");
121         i++;
122     }
123     else
124     {
125         printf("你答案有错误\n");
126         j++;
127     }
128     
129 }
130 
131 int main()
132 
133 { 
134     int m,n=0;
135     int k;
136     k=testadd();
137     if(k==1)
138     {
139         printf("恭喜您,加法测试成功!");
140     }
141 
142     while(1)
143     {
144 
145         printf("1.加法运算\n");
146         printf("2.减法运算\n");
147         printf("3.乘法运算\n");
148         printf("4.除法运算\n");
149         printf("请选择");
150         scanf("%d", &m);
151         switch(m)
152         {
153         case 1:jia();break;
154         case 2:jian();break;
155         case 3:chen();break;
156         case 4:chu();break;
157         }
158         printf("5.请重新选择\n");
159         printf("6.结束\n");
160         scanf("%d", &n);
161         if(n==6) break;
162     }
163     printf("你回答题的总数%d和正确的个数%d\n",i+j,i);
164     return 0;
165 }

 

 

test.cpp(四则运算)

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 #include"test.h"
 5 int add(int a,int b)
 6 {
 7     int c=a+b;
 8     return c;
 9 }
10 
11 int subtract(int a,int b)
12 {
13     int c=a-b;
14     return c;
15 }
16 
17 float multiply(int a,int b)
18 {
19     int c=a*b;
20     return c;
21 }
22 
23 double divide(int a,int b)
24 {
25     int c=a/b;
26     return c;
27 }

 

test.h(声明加、减、乘、除、函数)

1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<time.h>
4 
5 
6 int add(int a,int b);
7 int subtract(int a,int b);
8 float multiply(int a,int b);
9 double divide(int a,int b);

 

cs.cpp(加法测试)

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 #include<assert.h>
 5 #include"test.h"
 6 int testadd()
 7 {
 8     assert(10==add(4,6));
 9     assert(-16==add(4,-20));
10     return 1;
11 }
12     

 四则运算封装成功

 

 

总共 对/错 的数量。

 运行效果如下

 

两人合作步骤:
  • 我们两人进行分析、讨论,提出自己的观点,然后在源程序的基础上进行了修改,使之更加符合代码风格和设计规范的基本要求;
  • 采用模块化的设计思想,将“计算功能封装”起来。如:函数声明和加、减、乘、除存放在头文件(.h),测试和实现存放在头文件(.cpp)中。
  • 有加法测试功能

 

psp耗时

总结:
这次的作业对于我们这个小组还是很难的,我们花了大量的时间来分析和设计,还有模块化的设计思想和,
修改之前的code我们也不知道,编程过程中也遇到很多问题,不过在老师的讲解和上网搜索资料才弄明白,
编程过程中也遇到很多问题,不过最终还是按时完成作业,有什么不足之处,望老师指点,以后加油!

 

posted @ 2015-04-23 15:51  寿克霞  阅读(114)  评论(1编辑  收藏  举报