码迷,mamicode.com
首页 > 其他好文 > 详细

四则运算题2

时间:2016-03-12 18:31:43      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:

本题新学知识点:

itoa函数

char *itoa( int value, char *string,int radix);[1] 
原型说明:
value欲转换的数据。
string:目标字符串的地址。
radix:转换后的进制数,可以是10进制、16进制等。
 
程序实例:
#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 设计思路:
  3 1.判断是否有乘除法
  4 2.规定数值范围:通过random乘数值
  5 3.加减有无负数:两个数值随机产生符号
  6 4.除法有无余数:
  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 << "请选择要产生几以内的四则运算:(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             //随机产生四则运算符
 40             int sign = 0;
 41             sign = (rand() % 100) % 2;
 42             string opera_sign[2] = { "*", "/" };
 43 
 44             //当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             //随机产生四则运算符
 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.包含真分数
 83 int fenshu()
 84 {
 85     char Z;
 86     cout << "2.是否包含真分数?(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             //判断是否为真分数
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             //随机产生四则运算符
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             //随机产生四则运算符
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 //把数字转换成字符串型
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             //当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 //主函数
240 int main()
241 {
242     //以现在的系统时间作为随机数的种子来产生随机数
243     srand(time(NULL));
244     cout << "请您选择四则运算题的条件:\n";
245     int result1 = chengchu();
246     int result2 = fenshu();
247     int result3 = kuohao();
248     return 0;
249 }

技术分享技术分享技术分享

四则运算题2

标签:

原文地址:http://www.cnblogs.com/X-knight/p/5269542.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!