结对编程——300题四则运算

一、程序介绍

  本次我和2152524同学结对编程完成的程序是“300道四则运算练习题”,本次程序开发的难点在于2个运算符的各4种形态也即共16种组合方式。我们采用最传统的方式——穷举法,将十六种组合用switch函数一字排开,一个个去输出结果。

二、程序源码

  

复制代码
  1 #include<iostream>
  2 using namespace std;
  3 
  4 int Plus(int a,int b) {
  5     return (a + b);
  6 }
  7 
  8 int Minus(int a, int b) {
  9     return (a - b);
 10 }
 11 
 12 int Multiple(int a, int b) {
 13     return (a * b);
 14 }
 15 
 16 int Divide(int a, int b) {
 17     return (a / b);
 18 }
 19 
 20 char operatorCreate() {
 21     int a;
 22     char c;
 23     a = rand() % 4 + 1;
 24     switch (a)
 25     {
 26     case 1: c = '+'; break;
 27     case 2: c = '-'; break;
 28     case 3: c = '*'; break;
 29     case 4: c = '/'; break;
 30     default:
 31         break;
 32     }
 33     
 34     return c;
 35 }
 36 
 37 int plusPlusCreate(int i,char c1,char c2) {
 38     int x1, x2, x3;
 39     int temp;
 40     x1 = rand() % 100 + 1;
 41     x2 = rand() % 100 + 1;
 42     x3 = rand() % 100 + 1;
 43     cout << i << endl << x1  << c1  << x2 << c2 << x3 << "=" << endl;
 44     temp = Plus(Plus(x1, x2), x3);
 45     return temp;
 46 }
 47 
 48 int plusMinusCreate(int i, char c1, char c2) {
 49     int x1, x2, x3;
 50     int temp;
 51     x1 = rand() % 100 + 1;
 52     x2 = rand() % 100 + 1;
 53     x3 = rand() % 100 + 1;
 54     temp = Minus(Plus(x1, x2), x3);
 55     return temp;
 56 }
 57 
 58 int plusMultipleCreate(int i, char c1, char c2) {
 59     int x1, x2, x3;
 60     int temp;
 61     x1 = rand() % 100 + 1;
 62     x2 = rand() % 100 + 1;
 63     x3 = rand() % 100 + 1;
 64     cout << i << endl << x1 << c1 << x2 << c2 << x3 << "=" << endl;
 65     temp = Multiple(Plus(x1, x2), x3);
 66     return temp;
 67 }
 68 
 69 int plusDivideCreate(int i, char c1, char c2) {
 70     int x1, x2, x3;
 71     int temp;
 72     x1 = rand() % 100 + 1;
 73     x2 = rand() % 100 + 1;
 74     x3 = rand() % 100 + 1;
 75     cout << i << endl << x1 << c1 << x2 << c2 << x3 << "=" << endl;
 76     temp = Divide(Plus(x1, x2), x3);
 77     return temp;
 78 }
 79 
 80 int minusPlusCreate(int i, char c1, char c2) {
 81     int x1, x2, x3;
 82     int temp;
 83     x1 = rand() % 100 + 1;
 84     x2 = rand() % 100 + 1;
 85     x3 = rand() % 100 + 1;
 86     cout << i << endl << x1 << c1 << x2 << c2 << x3 << "=" << endl;
 87     temp = Plus(Minus(x1, x2), x3);
 88     return temp;
 89 }
 90 
 91 int minusMinusCreate(int i, char c1, char c2) {
 92     int x1, x2, x3;
 93     int temp;
 94     x1 = rand() % 100 + 1;
 95     x2 = rand() % 100 + 1;
 96     x3 = rand() % 100 + 1;
 97     cout << i << endl << x1 << c1 << x2 << c2 << x3 << "=" << endl;
 98     temp = Minus(Minus(x1, x2), x3);
 99     return temp;
100 }
101 
102 int minusMultipleCreate(int i, char c1, char c2) {
103     int x1, x2, x3;
104     int temp;
105     x1 = rand() % 100 + 1;
106     x2 = rand() % 100 + 1;
107     x3 = rand() % 100 + 1;
108     cout << i << endl << x1 << c1 << x2 << c2 << x3 << "=" << endl;
109     temp = Multiple(Minus(x1, x2), x3);
110     return temp;
111 }
112 
113 int minusDivideCreate(int i, char c1, char c2) {
114     int x1, x2, x3;
115     int temp;
116     x1 = rand() % 100 + 1;
117     x2 = rand() % 100 + 1;
118     x3 = rand() % 100 + 1;
119     cout << i << endl << x1 << c1 << x2 << c2 << x3 << "=" << endl;
120     temp = Divide(Minus(x1, x2), x3);
121     return temp;
122 }
123 
124 int multiplePlusCreate(int i, char c1, char c2) {
125     int x1, x2, x3;
126     int temp;
127     x1 = rand() % 100 + 1;
128     x2 = rand() % 100 + 1;
129     x3 = rand() % 100 + 1;
130     cout << i << endl << x1 << c1 << x2 << c2 << x3 << "=" << endl;
131     temp = Plus(Multiple(x1, x2), x3);
132     return temp;
133 }
134 
135 int multipleMinusCreate(int i, char c1, char c2) {
136     int x1, x2, x3;
137     int temp;
138     x1 = rand() % 100 + 1;
139     x2 = rand() % 100 + 1;
140     x3 = rand() % 100 + 1;
141     cout << i << endl << x1 << c1 << x2 << c2 << x3 << "=" << endl;
142     temp = Minus(Multiple(x1, x2), x3);
143     return temp;
144 }
145 
146 int multipleMultipleCreate(int i, char c1, char c2) {
147     int x1, x2, x3;
148     int temp;
149     x1 = rand() % 100 + 1;
150     x2 = rand() % 100 + 1;
151     x3 = rand() % 100 + 1;
152     cout << i << endl << x1 << c1 << x2 << c2 << x3 << "=" << endl;
153     temp = Multiple(Multiple(x1, x2), x3);
154     return temp;
155 }
156 
157 int multipleDivideCreate(int i, char c1, char c2) {
158     int x1, x2, x3;
159     int temp;
160     x1 = rand() % 100 + 1;
161     x2 = rand() % 100 + 1;
162     x3 = rand() % 100 + 1;
163     cout << i << endl << x1 << c1 << x2 << c2 << x3 << "=" << endl;
164     temp = Divide(Multiple(x1, x2), x3);
165     return temp;
166 }
167 
168 int dividePlusCreate(int i, char c1, char c2) {
169     int x1, x2, x3;
170     int temp;
171     x1 = rand() % 100 + 1;
172     x2 = rand() % 100 + 1;
173     x3 = rand() % 100 + 1;
174     cout << i << endl << x1 << c1 << x2 << c2 << x3 << "=" << endl;
175     temp = Plus(Divide(x1, x2), x3);
176     return temp;
177 }
178 
179 int divideMinusCreate(int i, char c1, char c2) {
180     int x1, x2, x3;
181     int temp;
182     x1 = rand() % 100 + 1;
183     x2 = rand() % 100 + 1;
184     x3 = rand() % 100 + 1;
185     cout << i << endl << x1 << c1 << x2 << c2 << x3 << "=" << endl;
186     temp = Minus(Divide(x1, x2), x3);
187     return temp;
188 }
189 
190 int divideMultipleCreate(int i, char c1, char c2) {
191     int x1, x2, x3;
192     int temp;
193     x1 = rand() % 100 + 1;
194     x2 = rand() % 100 + 1;
195     x3 = rand() % 100 + 1;
196     cout << i << endl << x1 << c1 << x2 << c2 << x3 << "=" << endl;
197     temp = Multiple(Divide(x1, x2), x3);
198     return temp;
199 }
200 
201 int divideDivideCreate(int i, char c1, char c2) {
202     int x1, x2, x3;
203     int temp;
204     x1 = rand() % 100 + 1;
205     x2 = rand() % 100 + 1;
206     x3 = rand() % 100 + 1;
207     cout << i << endl << x1 << c1 << x2 << c2 << x3 << "=" << endl;
208     temp = Divide(Divide(x1, x2), x3);
209     return temp;
210 }
211 
212 int judge(char c1, char c2) {
213     int t = 0;
214     if (c1 == '+' && c2 == '+') t = 1;
215     else if (c1 == '+' && c2 == '-') t = 2;
216     else if (c1 == '+' && c2 == '*') t = 3;
217     else if (c1 == '+' && c2 == '/') t = 4;
218     else if (c1 == '-' && c2 == '+') t = 5;
219     else if (c1 == '-' && c2 == '-') t = 6;
220     else if (c1 == '-' && c2 == '*') t = 7;
221     else if (c1 == '-' && c2 == '/') t = 8;
222     else if (c1 == '*' && c2 == '+') t = 9;
223     else if (c1 == '*' && c2 == '-') t = 10;
224     else if (c1 == '*' && c2 == '*') t = 11;
225     else if (c1 == '*' && c2 == '/') t = 12;
226     else if (c1 == '/' && c2 == '+') t = 13;
227     else if (c1 == '/' && c2 == '-') t = 14;
228     else if (c1 == '/' && c2 == '*') t = 15;
229     else if (c1 == '/' && c2 == '/') t = 16;
230     return t;
231 }
232 void Create() {
233     int t = 0;
234     int i = 1;
235     while (i <= 300) {    
236         char c1 = operatorCreate();
237         char c2 = operatorCreate();
238         t = judge(c1, c2);
239         switch (t)
240             {
241         case 1:plusPlusCreate(i, c1, c2); i++; break;
242         case 2:plusMinusCreate(i, c1, c2); i++; break;
243         case 3:plusMultipleCreate(i, c1, c2); i++; break;
244         case 4:plusDivideCreate(i, c1, c2); i++; break;
245         case 5:minusPlusCreate(i, c1, c2); i++; break;
246         case 6:minusMinusCreate(i, c1, c2); i++; break;
247         case 7:minusMultipleCreate(i, c1, c2); i++; break;
248         case 8:minusDivideCreate(i, c1, c2); i++; break;
249         case 9:multiplePlusCreate(i, c1, c2); i++; break;
250         case 10:multipleMinusCreate(i, c1, c2); i++; break;
251         case 11:multipleMultipleCreate(i, c1, c2); i++; break;
252         case 12:multipleDivideCreate(i, c1, c2); i++; break;
253         case 13:dividePlusCreate(i, c1, c2); i++; break;
254         case 14:divideMinusCreate(i, c1, c2); i++; break;
255         case 15:divideMultipleCreate(i, c1, c2); i++; break;
256         case 16:divideDivideCreate(i, c1, c2); i++; break;
257             default:
258                 break;
259             }
260     }
261     
262 }
263 
264 int main() {
265     Create();
266     return 0;
267 }
复制代码

    可以看到最基本的加减乘除运算我们也将它们封装进了函数(PS:这纯属闲的没事找事),此外我们也将上述16个组合中每一个组合都做了输出的函数,使得main函数看起来十分简洁(PS:这其实也是没事找事做)。

三、心得体会

  做出这个程序还是比较简单的,我们的程序肯定还有可以优化的地方。在处理更复杂的情况时不可能用的是穷举法,那样会对计算机造成极大的负荷。我们还要不断学习,加油!

posted @   Luvletter2517  阅读(33)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示