一、设计思路
将答案存入数组a,余数存入数组b中。与用户输入的答案进行比较。
二、设计代码
1 /*程序名:四则运算*/ 2 #include<iostream.h> 3 #include<stdlib.h> 4 #include<iomanip.h> 5 int answer(int a[],int b[],int length) //判断正误 6 { 7 int answer[100]; 8 int yushu[100]; 9 int sum=0; 10 for(int i=0;i<length;i++) 11 { 12 if(b[i]!=0) 13 { 14 cout<<"请输入第"<<i+1<<"道题商和余数:"; 15 cin>>answer[i]>>yushu[i]; 16 if(a[i]==answer[i]&&b[i]==yushu[i]) 17 { 18 sum=sum+1; 19 } 20 } 21 else 22 { 23 cout<<"请输入第"<<i+1<<"道题结果:"; 24 cin>>answer[i]; 25 if(a[i]==answer[i]) 26 { 27 sum=sum+1; 28 } 29 } 30 } 31 return sum; 32 } 33 int jisuan(char a,int op1,int op2) //计算题目正确结果 34 { 35 int correct; 36 switch(a) 37 { 38 case '+':correct=op1+op2;break; 39 case '-':correct=op1-op2;break; 40 case '*':correct=op1*op2;break; 41 case '/':correct=op1/op2;break; 42 } 43 return correct; 44 } 45 46 int main() 47 { 48 int a[100]; 49 int b[100]={0}; 50 int op1,op2; 51 int min,max; 52 int judgeopera; //有无乘除判断 53 int judgesign; //有无正负判断 54 int judgeremain; //有无余数判断 55 int count; //题目数量 56 int yunsuan; //确定运算符 57 int j; 58 int row; //行间隔 59 int column; 60 int flag=0; 61 srand((unsigned)time(0)); 62 cout<<"请输入数值范围:"; 63 cin>>min>>max; 64 cout<<"请输入输出行间隔:"; 65 cin>>row; 66 cout<<"请输入输出列数:"; 67 cin>>column; 68 char yun[5]={'+','-','*','/'}; 69 cout<<"输入题目数量:"; 70 cin>>count; 71 cout<<"是否含有乘除(1表示Yes,2表示No)"; 72 cin>>judgeopera; 73 while(judgeopera!=1&&judgeopera!=2) //如果输入不符合,重新输入 74 { 75 cout<<"重新输入:"; 76 cin>>judgeopera; 77 } 78 cout<<"是否允许负数存在(1表示Yes,2表示No)"; 79 cin>>judgesign; 80 while(judgesign!=1&&judgesign!=2) 81 { 82 cout<<"重新输入:"; 83 cin>>judgesign; 84 } 85 if(judgeopera==1) 86 { 87 cout<<"有乘除法时是否允许余数存在(1表示Yes,2表示No)"; 88 cin>>judgeremain; 89 while(judgeremain!=1&&judgeremain!=2) 90 { 91 cout<<"重新输入:"; 92 cin>>judgeremain; 93 } 94 } 95 char q; 96 int sum; 97 for(int i=0;i<count;i++) 98 { 99 100 op1=int(rand()%(max-min+1))+min; 101 op2=int(rand()%(max-min+1))+min; 102 if(judgeopera==1) 103 { 104 yunsuan=int(rand()%4); 105 if(judgesign==1) 106 { 107 if(judgeremain==1) //有乘除,有负数,有余数部分 108 { 109 cout<<i+1<<"、"<<op1<<yun[yunsuan]<<op2<<" "; 110 flag=flag+1; 111 if(yunsuan==3) 112 { 113 a[i]=op1/op2; 114 b[i]=op1%op2; 115 } 116 else 117 { 118 a[i]=jisuan(yun[yunsuan],op1,op2); 119 } 120 } 121 else //由乘除,有负数,无余数部分 122 { 123 if(yunsuan==3) 124 { 125 while(op1%op2!=0) 126 { 127 128 op1=int(rand()%(max-min+1))+min; 129 op2=int(rand()%(max-min+1))+min; 130 } 131 cout<<i+1<<"、"<<op1<<yun[yunsuan]<<op2<<" "; 132 flag=flag+1; 133 a[i]=jisuan(yun[yunsuan],op1,op2); 134 } 135 else 136 { 137 cout<<i+1<<"、"<<op1<<yun[yunsuan]<<op2<<" "; 138 flag=flag+1; 139 a[i]=jisuan(yun[yunsuan],op1,op2); 140 } 141 } 142 } 143 else //有乘除,无负数, 144 { 145 if(yunsuan==1) 146 { 147 while(op1<op2) 148 { 149 150 op1=int(rand()%(max-min+1))+min; 151 op2=int(rand()%(max-min+1))+min; 152 } 153 cout<<i+1<<"、"<<op1<<yun[yunsuan]<<op2<<" "; 154 flag=flag+1; 155 a[i]=jisuan(yun[yunsuan],op1,op2); 156 } 157 else 158 { 159 if(judgeremain==1) //有乘除,无负数,有余数部分 160 { 161 cout<<i+1<<"、"<<op1<<yun[yunsuan]<<op2<<" "; 162 flag=flag+1; 163 if(yunsuan==3) 164 { 165 a[i]=op1/op2; 166 b[i]=op1%op2; 167 } 168 else 169 { 170 a[i]=jisuan(yun[yunsuan],op1,op2); 171 } 172 } 173 else 174 { 175 if(yunsuan==3) 176 { 177 while(op1%op2!=0) 178 { 179 op1=int(rand()%(max-min+1))+min; 180 op2=int(rand()%(max-min+1))+min; 181 } 182 cout<<i+1<<"、"<<op1<<yun[yunsuan]<<op2<<" "; 183 flag=flag+1; 184 a[i]=jisuan(yun[yunsuan],op1,op2); 185 } 186 else 187 { 188 cout<<i+1<<"、"<<op1<<yun[yunsuan]<<op2<<" "; 189 flag=flag+1; 190 a[i]=jisuan(yun[yunsuan],op1,op2); 191 } 192 } 193 } 194 } 195 } 196 else //无乘除部分 197 { 198 199 yunsuan=int(rand()%2); 200 if(judgesign==1) 201 { 202 cout<<i+1<<"、"<<op1<<yun[yunsuan]<<op2<<" "; 203 flag=flag+1; 204 a[i]=jisuan(yun[yunsuan],op1,op2); 205 } 206 else 207 { 208 if(yunsuan==1) 209 { 210 while(op1<op2) 211 { 212 op1=int(rand()%(max-min+1))+min; 213 op2=int(rand()%(max-min+1))+min; 214 } 215 cout<<i+1<<"、"<<op1<<yun[yunsuan]<<op2<<" "; 216 flag=flag+1; 217 a[i]=jisuan(yun[yunsuan],op1,op2); 218 } 219 else 220 { 221 cout<<i+1<<"、"<<op1<<yun[yunsuan]<<op2<<" "; 222 flag=flag+1; 223 a[i]=jisuan(yun[yunsuan],op1,op2); 224 } 225 } 226 } 227 if(flag==column) 228 { 229 for(int p=0;p<=row;p++) //行间隔设置 230 { 231 cout<<endl; 232 } 233 flag=0; 234 } 235 } 236 cout<<endl; 237 sum=answer(a,b,count); 238 cout<<"正确题数:"<<sum<<endl; 239 cout<<"错误题数:"<<count-sum<<endl; 240 return 0; 241 }
三、截图
四、实验总结
本次实验运用了函数,本来对函数传参是不太懂得。经过这次实验,掌握那么一点点。
本来是输出一道题目,输出一道题目,让用户输入结果,这样导致列数不易控制。于是
统一在题目输完时,输入结果,判断正误。这样更有利于界面完整化、规格化。