首页 > 编程语言 >结对编程——300题四则运算

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

时间:2023-04-10 22:55:06浏览次数:33  
标签:case rand 结对 return 300 四则运算 break int char

一、程序介绍

  本次我和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:这其实也是没事找事做)。

三、心得体会

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

标签:case,rand,结对,return,300,四则运算,break,int,char
From: https://www.cnblogs.com/Luvletter2517/p/17304650.html

相关文章

  • 结对编程-四则运算
     本次结对编程作业由我和2152714完成源代码:/**Createdbyzouranon2023/4/10.*Description:四则运算*prams:*/#include<ctime>#include<random>#include"iostream"usingnamespacestd;//存储运算符classOperations{private:charoperation[4]{}......
  • 结对编程-小学生四则运算题目生成
    这次结对编程我是跟学号为2152520的朋友一起进行的四则运算题目生成的编程的。这次我们采用的编程语言是c++编程要求为:题目均为两次的运算,大小限制在一百以内的数字,且答案需要坐落在0~100之间(不显示出答案)。代码演示:#include<iostream>#include<cstdlib>usingnamespacest......
  • 结对编程-----四则运算
    本次结对编程我与2152710一起进行了四则运算的编程。这次采用python作为编程语音。小学生四则运算:两次运算,100 以内的数字,确保答案在 0..100 之间。以下是代码展示importrandomforiinrange(100):  a=random.randint(1,100)  b=random.randint(1,100......
  • 结对编程
    一、实验目的   本次学习的任务是通过两人结对编程的方式,来体会团队合作的过程。整个任务是由2152222和我共同完成。具体是编写一个出四则运算的程序。   要求如下:小学老师要每周给同学出300道四则运算练习题这个程序有很多种实现方式:C/C++C#/VB.net/JavaExce......
  • 结对编程——小学生四则运算
    一、实验准备本次实验的内容是结对编程,我和2152426一起组队完成这次实验。实验的具体题目是小学生四则运算,其具体要求如下:多种实现方式(C/C++、C#/VB.net/Java等)两个运算符,100以内的数字,不需要写答案需要检查答案是否正确,并且保证答案在0..100之间尽可能多的设置......
  • 结对编程
    本次结对编程作业由我和2152701一起完成1.源代码#include<cstdio>#include<cstdlib>#include<time.h>usingnamespacestd;charoperation(intoperation);floatcreateEquation();floatcalculate(intnumber1,intnumber2,intoperation);constintplus=1......
  • 结对编程——四则运算题目生成程序
    在本次结对编程中,我和2152618徐成阳一起完成了四则运算题目生成程序的编写,在这次结对编程中收获良多。在一起完成一个项目时,首先应该进行明确的分工,根据自己的特长进行分工可以大大提高效率,结对编程可以培养我们团队合作的意识,让我们更好地完成工作。以下是程序的源代码:#include......
  • 第七周实验——结对编程
    结对编程-四则运算练习题一、结对成员2152102&2152108 二、实验目的通过两位同学组队用结对编码(一位同学coding,另一个同学在旁边审核代码,之后再交换角色)的方式完成本次实验。本次实验需要设计一个四则运算练习题的随机出题程序,可以通过C/C++/Java/Python等语言进行实现,四......
  • 结对编程--随机四则运算生产
    在本次作业中,与我一同搭档完成结对编程的同学学号为2152434。在讨论后,我们决定基于c++进行本次程序开发。在该系统中,如何产生随机数及使用随机数产生随机符号为开发关键点。以下为程序部分代码展示:    而输出结果如下:  实验体会:结对编程中最重要的是调......
  • OMP30023 Computer Systems
    OMP30023:ComputerSystemsProject1:ProcessManagementReleased:March24,2023AEDTDue:9amApril17,2023AESTWeight:15%1OverviewInthisproject,youwillimplementaprocessmanagercapableofallocatingmemorytoprocessesandschedulingthemforexe......