分数计算器项目设计,供大家参考,具体内容如下
一、问题描述及功能要求
1、分数计算器程序的每种功能都可以用菜单选项列出,用户可以根据需要选择相应的菜单项,从而执行不同的子程序以完成相应的功能
2、增加运算符重载功能,使所设计的分数计算器可以进行四则运算&幂运算&逻辑运算。四则运算&幂运算可以用菜单选项列出,用户可以根据需要选择相应的运算。
3、程序具有判断功能,当有非法的输入时(如分母等于零等),能给出提示信息并退出运算
4、可将分数化为十进制小数和带分数
5、设计逻辑功能的函数使其可以进行逻辑比较,比如大于、小于的逻辑比较。
6、设计数字选择功能程序,用户可以输入数字键选择进行四则运算&幂运算或逻辑比较,运算结果为TRUE或FLASE。
7、要求界面提示清楚、便于操作
二、代码实现 带有注释
废话不说,直接代码,欢迎指正。
大家CV可能有不兼容的情况,可以跟我滴滴,尽可能解决问题地回复。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
using namespace std;
void jiafa1()
{
system ( "cls" );
while (1)
{
long long int a,c,d,f;
char b,e;
cin>>a>>b>>c>>d>>e>>f;
a=a*f+d*c;
c=c*f;
int flag=1;
if ((a<0&&c<0)||(a>0&&c>0)) flag=0;
a= abs (a);c= abs (c);
if (a%c==0)
{
if (flag==0){cout<<a/c<<endl;}
if (flag==1){cout<< "-" <<a/c<<endl;}
}
else if (a>c)
{
int x,y;
x=a;y=c;
int z=c;
while (a%c!=0)
{
z=a%c;
a=c;
c=z;
}
a=x/z;c=y/z;
if (flag==0){cout<<a<< "/" <<c<<endl;}
if (flag==1){cout<< "-" <<a<< "/" <<c<<endl;}
}
else if (c>a)
{
int x,y;
x=a;y=c;
int z=a;
while (c%a!=0)
{
z=c%a;
c=a;
a=z;
}
c=y/z;a=x/z;
if (flag==0){cout<<a<< "/" <<c<<endl;}
if (flag==1){cout<< "-" <<a<< "/" <<c<<endl;}
}
cout<< "1继续使用,0返回上一级" <<endl;
int g;cin>>g;
if (g==1) continue ;
if (g==0) break ;
}
}
void jiafa2()
{
system ( "cls" );
while (1)
{
long long int a,c,d;
char b;
cin>>a>>b>>c>>d;
a=a+d*c;
cout<<a<< "/" <<c<<endl;
cout<< "1继续使用,0返回上一级" <<endl;
int g;cin>>g;
if (g==1) continue ;
if (g==0) break ;
}
}
void jiafa()
{
while (1)
{
system ( "cls" );
cout<< "(1) 两个分数" <<endl;
cout<< "(2) 分数和整数" <<endl;
cout<< "(3) 返回上一级" <<endl;
int a;cin>>a;
if (a==1){jiafa1();}
if (a==2){jiafa2();}
if (a==3){ break ;}
}
}
void jianfa1()
{
system ( "cls" );
while (1)
{
int a,c,d,f;
char b,e;
cin>>a>>b>>c>>d>>e>>f;
a=a*f-d*c;
c=c*f;
int flag=1;
if ((a<0&&c<0)||(a>0&&c>0)) flag=0;
a= abs (a);c= abs (c);
if (a%c==0)
{
if (flag==0){cout<<a/c<<endl;}
if (flag==1){cout<< "-" <<a/c<<endl;}
}
else if (a>c)
{
int x,y;
x=a;y=c;
int z=c;
while (a%c!=0)
{
z=a%c;
a=c;
c=z;
}
a=x/z;c=y/z;
if (flag==0){cout<<a<< "/" <<c<<endl;}
if (flag==1){cout<< "-" <<a<< "/" <<c<<endl;}
}
else if (c>a)
{
int z=a;
int x,y;
x=a;y=c;
while (c%a!=0)
{
z=c%a;
c=a;
a=z;
}
a=x/z;c=y/z;
if (flag==0){cout<<a<< "/" <<c<<endl;}
if (flag==1){cout<< "-" <<a<< "/" <<c<<endl;}
}
cout<< "1继续使用,0返回上一级" <<endl;
int g;cin>>g;
if (g==1) continue ;
if (g==0) break ;
}
}
void jianfa2()
{
system ( "cls" );
while (1)
{
long long int a,c,d;
char b;
cin>>a>>b>>c>>d;
a=a-d*c;
cout<<a<< "/" <<c<<endl;
cout<< "1继续使用,0返回上一级" <<endl;
int g;cin>>g;
if (g==1) continue ;
if (g==0) break ;
}
}
void jianfa()
{
while (1)
{
system ( "cls" );
cout<< "(1) 两个分数" <<endl;
cout<< "(2) 分数和整数" <<endl;
cout<< "(3) 返回上一级" <<endl;
int a;cin>>a;
if (a==1){jianfa1();}
if (a==2){jianfa2();}
if (a==3){ break ;}
}
}
void chengfa1()
{
system ( "cls" );
while (1)
{
long long int a,c,d,f;
char b,e;
cin>>a>>b>>c>>d>>e>>f;
a=a*d;c=c*f;
int flag=1;
if ((a<0&&c<0)||(a>0&&c>0)) flag=0;
a= abs (a);c= abs (c);
if (a%c==0)
{
if (flag==0){cout<<a/c<<endl;}
if (flag==1){cout<< "-" <<a/c<<endl;}
}
else if (a>c)
{
int z=c;
int x,y;x=a,y=c;
while (a%c!=0)
{
z=a%c;
a=c;
c=z;
}
a=x/z;c=y/z;
if (flag==0){cout<<a<< "/" <<c<<endl;}
if (flag==1){cout<< "-" <<a<< "/" <<c<<endl;}
}
else if (c>a)
{
int z=a;
int x,y;x=a;y=c;
while (c%a!=0)
{
z=c%a;
c=a;
a=z;
}
a=x/z;c=y/z;
if (flag==0){cout<<a<< "/" <<c<<endl;}
if (flag==1){cout<< "-" <<a<< "/" <<c<<endl;}
}
cout<< "1继续使用,0返回上一级" <<endl;
int g;cin>>g;
if (g==1) continue ;
if (g==0) break ;
}
}
void chengfa2()
{
system ( "cls" );
while (1)
{
long long int a,c,d;
char b;
cin>>a>>b>>c>>d;
int flag=1;
if ((a<0&&d<0)||(a>0&&d>0)) flag=0;
a= abs (a);d= abs (d);
if (d%c==0) {a=a*(d/c);
if (flag==0) {cout<<a<<endl;}
if (flag==1) {cout<< "-" <<a<<endl;}
}
else if (c%d==0)
{
if (flag==0) {cout<<a<< "/" <<c/d<<endl;}
if (flag==1) {cout<< "-" <<a<< "/" <<c/d<<endl;}
}
else
{
if (flag==0){cout<<a*d<< "/" <<c<<endl;}
if (flag==1){cout<< "-" <<a*d<< "/" <<c<<endl;}
}
cout<< "1继续使用,0返回上一级" <<endl;
int g;cin>>g;
if (g==1) continue ;
if (g==0) break ;
}
}
void chengfa()
{
while (1)
{
system ( "cls" );
cout<< "(1) 两个分数" <<endl;
cout<< "(2) 分数和整数" <<endl;
cout<< "(3) 返回上一级" <<endl;
int a;cin>>a;
if (a==1){chengfa1();}
if (a==2){chengfa2();}
if (a==3){ break ;}
}
}
void chufa1()
{
system ( "cls" );
while (1)
{
int a,c,d,f;
char b,e;
cin>>a>>b>>c>>d>>e>>f;
int flag=1;
a=a*f;c=c*d; if ((a<0&&c<0)||(a>0&&c>0)) flag=0;
a= abs (a);c= abs (c);
if (a%c==0)
{
if (flag==0) cout<<a/c<<endl;
if (flag==1) cout<< "-" <<a/c<<endl;
}
else if (a>c)
{
int z=c;
int x,y;x=a;y=c;
while (a%c!=0)
{
z=a%c;
a=c;
c=z;
}
a=x/z;c=y/z;
if (flag==0){cout<<a<< "/" <<c<<endl;}
if (flag==1){cout<< "-" <<a<< "/" <<c<<endl;}
}
else if (c>a)
{
int z=a;
int x,y;x=a;y=c;
while (c%a!=0)
{
z=c%a;
c=a;
a=z;
}
a=x/z;c=y/z;
if (flag==0){cout<<a<< "/" <<c<<endl;}
if (flag==1){cout<< "-" <<a<< "/" <<c<<endl;}
}
cout<< "1继续使用,0返回上一级" <<endl;
int g;cin>>g;
if (g==1) continue ;
if (g==0) break ;
}
}
void chufa2()
{
system ( "cls" );
while (1)
{
long long int a,c,d;
char b;
cin>>a>>b>>c>>d;
int flag=1;
if ((a<0&&d<0)||(a>0&&d>0)) flag=0;
a= abs (a);d= abs (d);
if (d%c==0) {a=a*(d/c); if (flag==0){cout<<a<<endl;} if (flag==1){cout<< "-" <<a<<endl;}}
else if (c%d==0)
{
if (flag==0) {cout<<a<< "/" <<c/d<<endl;}
if (flag==1) {cout<< "-" <<a<< "/" <<c/d<<endl;}
}
else
{
if (flag==0) {cout<<a*d<< "/" <<c<<endl;}
if (flag==1) {cout<< "-" <<a*d<< "/" <<c<<endl;}
}
cout<< "1继续使用,0返回上一级" <<endl;
int g;cin>>g;
if (g==1) continue ;
if (g==0) break ;
}
}
void chufa()
{
while (1)
{
system ( "cls" );
cout<< "(1) 两个分数" <<endl;
cout<< "(2) 分数和整数" <<endl;
cout<< "(3) 返回上一级" <<endl;
int a;cin>>a;
if (a==1){chufa1();}
if (a==2){chufa2();}
if (a==3){ break ;}
}
}
void chengfang1()
{
system ( "cls" );
while (1)
{
long long int a,c,d;
char b;
cin>>a>>b>>c>>d;
int flag=1;
if (a<0&&d%2==0) flag=0;
if (a>0) flag=0;
a= abs (a);
long int sum1=1;
long int sum2=1;
while (d--)
{
sum1*=a;
sum2*=c;
}
if (flag==0){cout<<sum1<< "/" <<sum2<<endl;}
if (flag==1){cout<< "-" <<sum1<< "/" <<sum2<<endl;}
cout<< "1继续使用,0返回上一级" <<endl;
int g;cin>>g;
if (g==1) continue ;
if (g==0) break ;
}
}
void chengfang()
{
while (1)
{
system ( "cls" );
cout<< "(1) 分数和整数" <<endl;
cout<< "(2) 返回上一级" <<endl;
int a;cin>>a;
if (a==1){chengfang1();}
if (a==2){ break ;}
}
}
void sizeyunsuan()
{
while (1)
{
system ( "cls" );
cout<< "(1) 加法运算" <<endl;
cout<< "(2) 减法运算" <<endl;
cout<< "(3) 乘法运算" <<endl;
cout<< "(4) 除法运算" <<endl;
cout<< "(5) 乘方运算" <<endl;
cout<< "(6) 返回上一级" <<endl;
int a;cin>>a;
if (a==1) {jiafa();}
if (a==2){jianfa();}
if (a==3){chengfa();}
if (a==4){chufa();}
if (a==5){chengfang();}
if (a==6) break ;
}
}
void jiemian()
{
cout<< "(1) 四则运算及幂运算" <<endl;
cout<< "(2) 退出程序" <<endl;
int xxx;cin>>xxx;
if (xxx==1){sizeyunsuan();}
if (xxx==2){ system ( "cls" );cout<< "蟹蟹你的使用,欢迎下次使用哦" <<endl; exit (0);}
}
int main()
{
while (1)
{
system ( "cls" );
jiemian();
}
return 0;
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/m0_46525584/article/details/106966900