四则运算题2
本題新學(xué)知識點:
itoa函數(shù)
char *itoa( int value, char *string,int radix);[1]? 原型說明: value:欲轉(zhuǎn)換的數(shù)據(jù)。 string:目標(biāo)字符串的地址。 radix:轉(zhuǎn)換后的進(jìn)制數(shù),可以是10進(jìn)制、16進(jìn)制等。 程序?qū)嵗? #include?<stdlib.h> #include?<stdio.h> int?main(void) { ????int?number?=?12345; ????char?string[32]; ????itoa(number,?string,?10); ????printf("integer?=?%d?string?=?%s\n",?number,?string); ????return?0; } 1 /* 2 設(shè)計思路: 3 1.判斷是否有乘除法 4 2.規(guī)定數(shù)值范圍:通過random乘數(shù)值 5 3.加減有無負(fù)數(shù):兩個數(shù)值隨機(jī)產(chǎn)生符號 6 4.除法有無余數(shù): 7 */ 8 9 #include<iostream> 10 #include<time.h> 11 #include<string> 12 # include <stdio.h> 13 #include<stdlib.h> 14 #include<iomanip> 15 #include<cmath> 16 using namespace std; 17 18 //1.乘除法 19 int chengchu() 20 { 21 char X; 22 cout << "請選擇要產(chǎn)生幾以內(nèi)的四則運算:(10 or 100)"; 23 int range; 24 cin >> range; 25 26 cout << "1.是否需要乘除法運算?(Y or N)\n"; 27 cin >> X; 28 29 if (X == 'Y') 30 { 31 32 for (int count = 0; count < 5; count++) 33 { 34 int a = 0, b = 0; 35 36 a = rand() % range; 37 b = rand() % range ; 38 39 //隨機(jī)產(chǎn)生四則運算符 40 int sign = 0; 41 sign = (rand() % 100) % 2; 42 string opera_sign[2] = { "*", "/" }; 43 44 //當(dāng)b=0且運算為除法時重新生成 45 while ((b == 0) & (sign == 1)) 46 { 47 b = rand() % range; 48 } 49 50 switch (sign) 51 { 52 case 0:cout << a << opera_sign[sign] << b << "=" << endl; break; 53 case 1:cout << a << opera_sign[sign] << b << "=" << endl; break; 54 } 55 } 56 } 57 if (X == 'N') 58 { 59 60 for (int count = 0; count < 5; count++) 61 { 62 int a = 0, b = 0; 63 64 a = rand() % range; 65 b = rand() % range; 66 67 //隨機(jī)產(chǎn)生四則運算符 68 int sign = 0; 69 sign = (rand() % 100) % 2; 70 string opera_sign[2] = { "+", "-" }; 71 72 switch (sign) 73 { 74 case 0:cout << a << opera_sign[sign] << b << "=" << endl; break; 75 case 1:cout << a << opera_sign[sign] << b << "=" << endl; break; 76 } 77 } 78 } 79 return 0; 80 } 81 82 //2.包含真分?jǐn)?shù) 83 int fenshu() 84 { 85 char Z; 86 cout << "2.是否包含真分?jǐn)?shù)?(Y or N)\n"; 87 cin >> Z; 88 if (Z == 'Y') 89 { 90 for (int count = 0; count < 5; count++) 91 { 92 int a1 = 0, b1 = 0, a2 = 0, b2 = 0; 93 94 a1 = rand() % 100; 95 b1 = rand() % 100; 96 a2 = rand() % 100; 97 b2 = rand() % 100; 98 99 //判斷是否為真分?jǐn)?shù) 100 while (a1>b1 || b1 == 0) 101 { 102 a1 = rand() % 100; 103 b1 = rand() % 100; 104 } 105 106 while (a2 > b2 || b2 == 0) 107 { 108 a2 = rand() % 100; 109 b2 = rand() % 100; 110 } 111 112 //隨機(jī)產(chǎn)生四則運算符 113 int sign = 0; 114 sign = (rand() % 100) % 4; 115 string opera_sign[4] = { "+", "-", "*", "/" }; 116 117 switch (sign) 118 { 119 case 0:cout << "(" << a1 << "/" << b1 << ")" << opera_sign[sign] << "(" << a2 << "/" << b2 << ")" << "=" << endl; break; 120 case 1:cout << "(" << a1 << "/" << b1 << ")" << opera_sign[sign] << "(" << a2 << "/" << b2 << ")" << "=" << endl; break; 121 case 2:cout << "(" << a1 << "/" << b1 << ")" << opera_sign[sign] << "(" << a2 << "/" << b2 << ")" << "=" << endl; break; 122 case 3:cout << "(" << a1 << "/" << b1 << ")" << opera_sign[sign] << "(" << a2 << "/" << b2 << ")" << "=" << endl; break; 123 } 124 } 125 } 126 if (Z == 'N') 127 { 128 for (int count = 0; count < 5; count++) 129 { 130 int a1 = 0, b1 = 0, a2 = 0, b2 = 0; 131 132 a1 = rand() % 100; 133 b1 = rand() % 100; 134 a2 = rand() % 100; 135 b2 = rand() % 100; 136 137 //分母不可為0 138 while (b1 == 0) 139 { 140 b1 = rand() % 100; 141 } 142 while (b2 == 0) 143 { 144 b2 = rand() % 100; 145 } 146 147 //隨機(jī)產(chǎn)生四則運算符 148 int sign = 0; 149 sign = (rand() % 100) % 4; 150 string opera_sign[4] = { "+", "-", "*", "/" }; 151 152 switch (sign) 153 { 154 case 0:cout << "(" << a1 << "/" << b1 << ")" << opera_sign[sign] << "(" << a2 << "/" << b2 << ")" << "=" << endl; break; 155 case 1:cout << "(" << a1 << "/" << b1 << ")" << opera_sign[sign] << "(" << a2 << "/" << b2 << ")" << "=" << endl; break; 156 case 2:cout << "(" << a1 << "/" << b1 << ")" << opera_sign[sign] << "(" << a2 << "/" << b2 << ")" << "=" << endl; break; 157 case 3:cout << "(" << a1 << "/" << b1 << ")" << opera_sign[sign] << "(" << a2 << "/" << b2 << ")" << "=" << endl; break; 158 } 159 } 160 } 161 return 0; 162 } 163 164 //把數(shù)字轉(zhuǎn)換成字符串型 165 string int_string(int number) 166 { 167 char str1[200]; 168 itoa(number, str1, 10); 169 string str_ = str1; 170 return str_; 171 } 172 173 int kuohao() 174 { 175 char K; 176 cout << "3.是否包含括號?(Y or N)\n"; 177 cin >> K; 178 if (K == 'Y') 179 { 180 string stri[4]; 181 for (int i = 0; i < 4; i++) 182 { 183 int a = 0, b = 0; 184 a = rand() % 100; 185 b = rand() % 100; 186 187 int sign = 0; 188 sign = (rand() % 100) % 4; 189 string opera_sign[4] = { "+", "-", "*", "/" }; 190 191 //當(dāng)b=0且運算為除法時重新生成 192 while ((b == 0) & (sign == 3)) 193 { 194 b = rand() % 100; 195 } 196 string str,ch1, ch2; 197 198 ch1 = int_string(a); 199 ch2 = int_string(b); 200 //string stri[4]; 201 202 switch (sign) 203 { 204 case 0: 205 { 206 str = "(" + ch1 + opera_sign[sign] + ch2 + ")"; 207 } break; 208 209 case 1: 210 { 211 str = "(" + ch1 + opera_sign[sign] + ch2 + ")"; 212 }break; 213 214 case 2: 215 { 216 str = "(" + ch1 + opera_sign[sign] + ch2 + ")"; 217 } break; 218 219 case 3: 220 { 221 str = "(" + ch1 + opera_sign[sign] + ch2 + ")"; 222 }break; 223 } 224 stri[i]= str; 225 } 226 227 string str5; 228 // str5 = "((" + stri[0] + stri[1] + ")" +stri[2] + ")" + stri[3]; 229 cout << "((" <<stri[0] <<"+"<<stri[1] << ")*"<<stri[2] <<")" <<"/"<< stri[3]; 230 } 231 232 else 233 { 234 cout << endl; 235 } 236 237 238 } 239 //主函數(shù) 240 int main() 241 { 242 //以現(xiàn)在的系統(tǒng)時間作為隨機(jī)數(shù)的種子來產(chǎn)生隨機(jī)數(shù) 243 srand(time(NULL)); 244 cout << "請您選擇四則運算題的條件:\n"; 245 int result1 = chengchu(); 246 int result2 = fenshu(); 247 int result3 = kuohao(); 248 return 0; 249 }轉(zhuǎn)載于:https://www.cnblogs.com/X-knight/p/5269542.html
總結(jié)
- 上一篇: [转]用Whois获得电信运营商的IP地
- 下一篇: 各类型数据的导入与导出