随机生成30道四则运算程序分析
题目要求:随机生成30到四则运算题,其中不能超过2位运算,包含真分数。
问题细化:
1、循环30次
2、四则运算方式的表示,+,-,*,/
3、真分数的产生
4、随机产生真分数之间或者整数之间以及真分数和整数之间的运算
细化分析:
1、循环30次
2、在循环中随机产生一个1-4的随机数,表示整数运算,真分数和整数之间的运算,还是真分数之间的运算
3、在确定后,在随机产生一个1-4的随机数表示使用哪种规则,+,-,*,/
4、真分数表示使用两个随机数,要求分子比分母小,然后用“分子/分母”表示
以下是我的代码:
1 #include<iostream> 2 #include"time.h" 3 using namespace std; 4 5 6 //运算符两边都是整数 7 void IntegerCount(){ 8 int first, second, work; //work表示运算规则,first表示第一个数,second表示第二个数 9 first = rand() % 100; 10 work = 1 + rand() % 4; 11 if (1 == work){ //1表示+,2表示-,3表示*,4表示/ 12 second = rand() % 100; 13 cout << first << " + " << second << " = " << endl; 14 } 15 if (2 == work){ 16 second = rand() % 100; 17 cout << first << " - " << second << " = " << endl; 18 } 19 if (3 == work){ 20 second = rand() % 100; 21 cout << first << " * " << second << " = " << endl; 22 } 23 if (4 == work){ 24 second = rand() % 100; 25 while (0 == second){ 26 second = rand() % 100; 27 } 28 cout << first << " / " << second << " = " << endl; 29 } 30 } 31 //运算符左边是整数右边是真分数 32 void LeftIntegerAndDecimal(){ 33 int first; 34 int secondTop, secondBase; 35 int work; 36 first = rand() % 100; 37 secondTop = rand() % 100; 38 secondBase = 1 + rand() % 100; 39 while (secondTop > secondBase){ 40 secondTop = rand() % 100; 41 secondBase = 1 + rand() % 100; 42 } 43 work = 1 + rand() % 4; 44 if (1 == work){ 45 cout << first << " + " << secondTop << "/" << secondBase << " = " << endl; 46 } 47 if (2 == work){ 48 cout << first << " - " << secondTop << "/" << secondBase << " = " << endl; 49 } 50 if (3 == work){ 51 cout << first << " * " << secondTop << "/" << secondBase << " = " << endl; 52 } 53 if (4 == work){ 54 cout << first << " / " << secondTop << "/" << secondBase << " = " << endl; 55 } 56 } 57 //运算符左边是真分数左边是整数 58 void RightIntegerAndDecimal(){ 59 int second; 60 int firstTop, firstBase; 61 int work; 62 second = 1 + rand() % 100; 63 firstTop = rand() % 100; 64 firstBase = 1 + rand() % 100; 65 while (firstTop > firstBase){ 66 firstTop = rand() % 100; 67 firstBase = 1 + rand() % 100; 68 } 69 work = 1 + rand() % 4; 70 if (1 == work){ 71 cout << firstTop << "/" << firstBase << " + " << second << " = " << endl; 72 } 73 if (2 == work){ 74 cout << firstTop << "/" << firstBase << " - " << second << " = " << endl; 75 } 76 if (3 == work){ 77 cout << firstTop << "/" << firstBase << " * " << second << " = " << endl; 78 } 79 if (4 == work){ 80 cout << firstTop << "/" << firstBase << " / " << second << " = " << endl; 81 } 82 } 83 //运算符两边都是真分数 84 void DecimalCount(){ 85 int fTop, fBase; 86 int sTop, sBase; 87 int work; 88 fTop = 1 + rand()%100; 89 fBase = fTop + rand()%100; 90 sTop = 1 + rand()%100; 91 sBase = sTop + rand()%100; 92 work = 1 + rand() % 4; 93 if (1 == work){ 94 cout << fTop << "/" << fBase << " + " << sTop << "/" << sBase << " = " << endl; 95 } 96 if (2 == work){ 97 cout << fTop << "/" << fBase << " - " << sTop << "/" << sBase << " = " << endl; 98 } 99 if (3 == work){ 100 cout << fTop << "/" << fBase << " * " << sTop << "/" << sBase << " = " << endl; 101 } 102 if (4 == work){ 103 cout << fTop << "/" << fBase << " / " << sTop << "/" << sBase << " = " << endl; 104 } 105 } 106 int main(){ 107 int choose; //表示运算的四种种方式1、整数之间2、整数和真分数其中该方式有2种3、真分数和真分数 108 srand((unsigned)time(NULL)); 109 110 int i = 0; 111 //四种情况的运算 112 while (true){ 113 choose = rand() % 4; 114 if (choose == 1){ 115 IntegerCount(); 116 i = i + 1; 117 //循环30次 118 if (i == 30){ 119 break; 120 } 121 } 122 if (choose == 2){ 123 LeftIntegerAndDecimal(); 124 i = i + 1; 125 if (i == 30){ 126 break; 127 } 128 } 129 if (choose == 3){ 130 RightIntegerAndDecimal(); 131 i = i + 1; 132 if (i == 30){ 133 break; 134 } 135 } 136 if (choose == 4){ 137 DecimalCount(); 138 i = i + 1; 139 if (i == 30){ 140 break; 141 } 142 } 143 } 144 return 0; 145 }
运行结果:
感悟:
使用细化方法可以使问题变得简单,可以使一些原先很难的问题得已解决。