四则运算二
本次作业要求:
老师又给二柱子增加了几个条件,对程序再做进一步的改进,要求满足如下条件:
1.题目避免重复。
2.可制定。(数量/打印方式)
3.可以控制下列参数:
- 是否有乘除法
- 是否有括号(最多可支持10个数参与计算)
- 数值范围
- 加减有无负数
- 乘除有无余数
设计思路:
本次作业要求功能较多,对于我来说实现起来还是有一定难度,最终决定去掉其中几个个人看来较为困难的功能,最终实现的代码如下(用的if-else结构太多,脑子都不好使了):
1 //随机生成四则运算题目,李青,2016/3/11 2 #include<iostream> 3 #include<stdlib.h> 4 #include<time.h> 5 using namespace std; 6 void main() 7 { 8 srand((int)time(NULL));//时间种子 9 int amount, mul_div, brackets, range, negative_number, remainder,i; 10 int number1, number2,exchange1; 11 cout << "请输入需要生成的运算题目数量:" << endl; 12 cin >> amount; 13 cout << "请输入生成的题目中数值范围(不大于该数):" << endl; 14 cin >> range; 15 cout << "生成的题目中是否有括号?1:有,2:没有 (没有实现该功能) " << endl; 16 cin >> brackets; 17 if (brackets == 2) 18 { 19 cout << "生成的题目中是否有乘除法?1:有, 2:没有" << endl; 20 cin >> mul_div; 21 while (mul_div == 1) 22 { 23 cout << "除法是整除吗?1:是,2:不是" << endl; 24 cin >> remainder; 25 break; 26 } 27 cout << "减法有负数吗?1:有,2:没有" << endl; 28 cin >> negative_number; 29 if (negative_number == 2)//无负数 30 { 31 if (mul_div == 1)//有乘除法 32 { 33 if (remainder == 1)//无余数 34 { 35 for (i = 0; i < amount; i++) 36 { 37 char operation[4] = { '+', '-', '*', '/' };//对运算符号的定义 38 int rand_op_index = rand() % 4; 39 char current_op = operation[rand_op_index]; 40 number1 = rand() % range; 41 number2 = rand() % range; 42 if (current_op == '/') 43 { 44 while (number1 < number2)//确保除数比被除数小 45 { 46 exchange1 = number1; 47 number1 = number2; 48 number2 = exchange1; 49 } 50 while (number2 == 0 || number1 % number2 != 0)//确保是整除且除数不为0 51 { 52 number2 = rand() % (number1 - 1) + 1; 53 } 54 cout << number1 << current_op << number2 << "=" << endl; 55 } 56 else if (current_op == '-') 57 { 58 while (number1 < number2)//确保不会算出负数 59 { 60 exchange1 = number1; 61 number1 = number2; 62 number2 = exchange1; 63 } 64 cout << number1 << current_op << number2 << "=" << endl; 65 } 66 else 67 { 68 cout << number1 << current_op << number2 << "=" << endl; 69 } 70 } 71 } 72 else//有余数 73 { 74 for (i = 0; i < amount; i++) 75 { 76 char operation[4] = { '+', '-', '*', '/' };//对运算符号的定义 77 int rand_op_index = rand() % 4; 78 char current_op = operation[rand_op_index]; 79 number1 = rand() % range; 80 number2 = rand() % range; 81 if (current_op == '/') 82 { 83 while (number2 == 0)//确保除数不为0 84 { 85 number2 = rand() % (number1 - 1) + 1; 86 } 87 cout << number1 << current_op << number2 << "=" << endl; 88 } 89 else if (current_op == '-') 90 { 91 while (number1 < number2)//确保不会算出负数 92 { 93 exchange1 = number1; 94 number1 = number2; 95 number2 = exchange1; 96 } 97 cout << number1 << current_op << number2 << "=" << endl; 98 } 99 else 100 { 101 cout << number1 << current_op << number2 << "=" << endl; 102 } 103 } 104 } 105 } 106 else//无乘除法 107 { 108 for (i = 0; i < amount; i++) 109 { 110 char operation[4] = { '+', '-' };//对运算符号的定义 111 int rand_op_index = rand() % 2; 112 char current_op = operation[rand_op_index]; 113 number1 = rand() % range; 114 number2 = rand() % range; 115 if (current_op == '-') 116 { 117 while (number1 < number2)//确保不会算出负数 118 { 119 exchange1 = number1; 120 number1 = number2; 121 number2 = exchange1; 122 } 123 cout << number1 << current_op << number2 << "=" << endl; 124 } 125 else 126 { 127 cout << number1 << current_op << number2 << "=" << endl; 128 } 129 } 130 } 131 } 132 else//有负数 133 if (mul_div == 1)//有乘除法 134 { 135 if (remainder == 1)//无余数 136 { 137 for (i = 0; i < amount; i++) 138 { 139 char operation[4] = { '+', '-', '*', '/' };//对运算符号的定义 140 int rand_op_index = rand() % 4; 141 char current_op = operation[rand_op_index]; 142 number1 = rand() % range; 143 number2 = rand() % range; 144 if (current_op == '/') 145 { 146 while (number1 < number2)//确保除数比被除数小 147 { 148 exchange1 = number1; 149 number1 = number2; 150 number2 = exchange1; 151 } 152 while (number2 == 0 || number1 % number2 != 0)//确保是整除且除数不为0 153 { 154 number2 = rand() % (number1 - 1) + 1; 155 } 156 cout << number1 << current_op << number2 << "=" << endl; 157 } 158 else 159 { 160 cout << number1 << current_op << number2 << "=" << endl; 161 } 162 } 163 } 164 else//有余数 165 { 166 for (i = 0; i < amount; i++) 167 { 168 char operation[4] = { '+', '-', '*', '/' };//对运算符号的定义 169 int rand_op_index = rand() % 4; 170 char current_op = operation[rand_op_index]; 171 number1 = rand() % range; 172 number2 = rand() % range; 173 if (current_op == '/') 174 { 175 while (number2 == 0)//确保除数不为0 176 { 177 number2 = rand() % (number1 - 1) + 1; 178 } 179 cout << number1 << current_op << number2 << "=" << endl; 180 } 181 else 182 { 183 cout << number1 << current_op << number2 << "=" << endl; 184 } 185 } 186 } 187 } 188 else//无乘除法 189 { 190 for (i = 0; i < amount; i++) 191 { 192 char operation[4] = { '+', '-' };//对运算符号的定义 193 int rand_op_index = rand() % 2; 194 char current_op = operation[rand_op_index]; 195 number1 = rand() % range; 196 number2 = rand() % range; 197 cout << number1 << current_op << number2 << "=" << endl; 198 } 199 } 200 } 201 else//有括号的运算题目的生成,未实现 202 { 203 204 } 205 }
对不同的条件的测试结果如下:
总结与体会:
这次的作业对我来说是一场持久战,从一开始的毫无头绪,到最后实现了部分功能,虽然有的语句是照抄网上的语句,但是经过自己的运用后总会掌握的,希望以后能不断地提高自己的水平,实现更多的功能。
项目计划总结:
日期&&任务 | 听课 | 编程 | 阅读相关书籍 | 网上查找资料 | 日总计 |
周一 | 100 | 20 | 15 | 10 | 145 |
周二 | 30 | 10 | 40 | ||
周三 | 100 | 30 | 20 | 150 | |
周四 | 100 | 30 | 130 | ||
周五 | 30 | 20 | 50 | ||
周六 | 30 | 30 | 60 | ||
周日 | |||||
周总结 | 200 | 260 | 75 | 60 | 595 |
时间记录日志:
日期 | 开始时间 | 结束时间 | 中断时间 | 净时间 | 活动 | 备注 |
3/7 | 14:00 | 15:50 | 10 | 100 | 听课 | 软件工程上课 |
16:00 | 16:20 | 0 | 20 | 编程 | ||
16:30 | 16:45 | 0 | 15 | 阅读相关书籍 | 《构建之法》 | |
17:00 | 17:10 | 0 | 10 | 网上查找资料 | ||
3/8 | 19:00 | 19:30 | 0 | 30 | 编程 | |
19:40 | 19:50 | 0 | 10 | 网上查找资料 | ||
3/9 | 14:00 | 16:00 | 20 | 100 | 编程 | |
16:30 | 17:00 | 0 | 30 | 阅读相关书籍 | 《梦断代码》 | |
17:10 | 17:30 | 0 | 20 | 网上查找资料 | ||
3/10 | 14:00 | 15:50 | 10 | 100 | 听课 | 软件工程上课 |
19:00 | 19:30 | 0 | 30 | 编程 | ||
3/11 | 19:00 | 19:30 | 0 | 30 | 编程 | |
20:00 | 20:20 | 0 | 20 | 网上查找资料 | ||
3/12 | 9:00 | 9:30 | 0 | 30 | 编程 | |
10:00 | 10:30 | 0 | 30 | 阅读相关书籍 | 《构建之法》 |
缺陷记录日志:
日期 | 编号 | 类型 | 引入阶段 | 排除阶段 | 修复时间 | 备注 |
3/9 | 1 | 20 | 编码 | 编译 | 2min | if后面的else只加了一个{ |
3/11 | 2 | 20 | 编码 | 编译 | 1min | 少写了一个功能 |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步