四则运算3(四则运算2程序的扩展)

题目:选一个方向,将四则运算2程序进行扩展;

方向:让程序能够接受用户输入的答案,并判定对错,最后输出做对题的数量;

思路:将每道题的正确结果保存在数组daan[]中,将用户给出的答案定义为result.当(result==daan[])时,输出“用户所给答案正确”,否则输出“答案错误,正确答案为daan[]”

具体程序代码为:

  1 #include<iostream.h>
  2 #include<stdlib.h>//使用随机函数
  3 #include<time.h>//题目避免重复
  4 #include<conio.h>
  5 int x1[100],x2[100],op,i;
  6 int daan[100];
  7 void yunsuan1()
  8 {
  9     if(op==0)
 10     {
 11         cout<<x1[i]<<"+"<<x2[i]<<"="<<endl;
 12         daan[i]=x1[i]+x2[i];
 13     }
 14     if(op==1)
 15     {
 16         cout<<x1[i]<<"-"<<x2[i]<<"="<<endl;
 17         daan[i]=x1[i]-x2[i];
 18     }
 19     if(op==2)
 20     {
 21         cout<<x1[i]<<"*"<<x2[i]<<"="<<endl;
 22         daan[i]=x1[i]*x2[i];
 23     }
 24     if(op==3)
 25     {
 26         cout<<x1[i]<<"/"<<x2[i]<<"="<<endl;
 27         daan[i]=x1[i]/x2[i];
 28     }
 29 }
 30 void yunsuan2()
 31 {
 32     if(op==0)
 33         cout<<x1[i]<<"+"<<x2[i]<<"="<<"	";
 34     if(op==1)
 35         cout<<x1[i]<<"-"<<x2[i]<<"="<<"	";
 36     if(op==2)
 37         cout<<x1[i]<<"*"<<x2[i]<<"="<<"	";
 38     if(op==3)
 39         cout<<x1[i]<<"/"<<x2[i]<<"="<<"	";
 40 }
 41 
 42 
 43 int main()
 44 {
 45     srand(time(NULL));//题目避免重复
 46     int num,temp;//题目的数量num
 47     int fanwei,out,div,fushu,jixu;//数值的范围,输出方式,是否有乘除运算,加减法是否有负数
 48     int result;//用户输入的答案
 49     //int score=0;//用户得分,初始化为0
 50     int t=0;//用户答对题的数量
 51     cout<<"********欢迎使用小学四则运算定制系统********"<<endl;
 52     cout<<"本系统支持定制,祝您使用愉快!"<<endl;
 53     cout<<"请输入题目的数量num:(最大数量为100)"<<endl;
 54     cin>>num;
 55     cout<<"请选择数值范围(0--*)fanwei"<<endl;
 56     cin>>fanwei;
 57     cout<<"请选择打印方式out(0,空行打印 1,空格打印)"<<endl;
 58     cin>>out;
 59     cout<<"请选择有无乘除运算div(0,没有乘除运算 1,有乘除运算)"<<endl;
 60     cin>>div;
 61     cout<<"请选择加减运算有无负数fushu(0,没有负数 1,有负数)"<<endl;
 62     cin>>fushu;
 63     for(i=1;i<=num;i++)
 64     {
 65         x1[i]=rand()%fanwei;//输出两个用户制定范围内的数
 66         x2[i]=rand()%fanwei;//输出两个用户制定范围内的数
 67         op=rand()%4;
 68         if(out==0)
 69         {
 70             if(div==0&&fushu==0)
 71             {
 72                 if(op>=2)
 73                 {
 74                     op=op-2;
 75                     yunsuan1();
 76                 }
 77                 else
 78                 {
 79                     yunsuan1();
 80                 }
 81             }
 82             if(div==1&&fushu==1)
 83             {
 84                 if(x1[i]>x2[i])
 85                 {
 86                     temp=x1[i];
 87                     x1[i]=x2[i];
 88                     x2[i]=temp;
 89                 }
 90             
 91                 yunsuan1();
 92             }
 93             if(div==0&&fushu==1)
 94             {
 95                 
 96                    if(op>=2)
 97                 {
 98                     op=op-2;
 99                     yunsuan1();
100                 }
101                 else
102                 {
103                     yunsuan1();
104                 }                 
105             }
106             if(div==1&&fushu==0)
107             {
108               yunsuan1();
109             }
110         }
111         else if(out==1)
112         {
113             if(div==0&&fushu==0)
114             {
115                 if(op>=2)
116                 {
117                     op=op-2;
118                     yunsuan2();
119                 }
120                 else
121                 {
122                     yunsuan2();
123                 }
124             }
125             if(div==1&&fushu==1)
126             {
127                   if(x1[i]>x2[i])
128                 {
129                     temp=x1[i];
130                     x1[i]=x2[i];
131                     x2[i]=temp;
132                 }
133                 
134 
135                 yunsuan2();
136             }
137             if(div==0&&fushu==1)
138             {
139                 if(op>=2)
140                 {
141                     op=op-2;
142                     yunsuan2();
143                 }
144                 else
145                 {
146                     yunsuan2();
147                 }        
148 
149             }
150             if(div==1&&fushu==0)
151             {
152                 yunsuan2();
153             }
154         }
155 
156     }
157     
158     cout<<"请输入你的答案(result):"<<endl;
159     for(int j=1;j<=num;j++)
160     {
161     cin>>result;
162     if(result==daan[j])
163     {
164         t=t+1;
165         cout<<"答对,请继续作答"<<endl;
166         
167     }
168     else
169     {
170         cout<<""<<j<<"题回答有误,正确结果为:"<<daan[j]<<endl;
171     }
172     
173     
174     }
175     cout<<"每道题目一分,您答对的题数为:"<<t<<"总得分为"<<t<<endl;
176     cout<<"要继续吗?jixu(0, 退出 1,继续)"<<endl;
177     cin>>jixu;
178     if(jixu==1)
179     {
180         cout<<endl;
181         main();
182     }
183     else
184     {
185         return 0;
186     }
187     getch();
188     return 0;
189     
190 }

程序运行结果为:

总结:一开始程序代码有错误:1,程序智能存储最后一道题的答案;

                                        2,输出的题号总是比正确的题号小1;

       解决方案:1,将题的答案保存在数组daan[]中;

        2,发现变量j的初始值应该设为1,才能符合题目的要求。

原文地址:https://www.cnblogs.com/xiaojin123/p/4347593.html