服务器之家

服务器之家 > 正文

C++实现小型复数计算器

时间:2021-11-16 15:17     来源/作者:名名名名

小型复数计算器项目设计,供大家参考,具体内容如下

一、问题描述及功能要求

1、实现复数的设置和显示。
2、声明一个复数类Complex,重载运算符 “+”、 “-”、 “*”、 “/”,使之能用于复数的加、减、乘、除,运算符重载函数作为Complex类的成员函数。
3、声明一个复数类Complex,重载运算符 “+”,使之能用于复数的加法运算。参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。例如:c1+c2,i+c1, c1+i,均合法(设i为整数,c1,c2为复数),分别求两个复数之和、整数和复数之和,并显示运算结果。
4、实现用运算符 = = 进行复数的相等比较,并显示比较结果。
5、在程序中还应该有构造函数,析构函数,并要用到友元函数。

二、代码实现 带有注释

废话不说,直接代码,欢迎指正。

?
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
#include <iostream>
#include <process.h>
using namespace std;//标准命名空间
void home_page()//打印首页
{
 
    cout<<"\t\t\t--------------欢迎来到复数四则计算器----------------"<<endl;
    cout<<"\t\t\t|                                                  |"<<endl;
    cout<<"\t\t\t|                  服务类型:(1~4):             |"<<endl;
    cout<<"\t\t\t|                1.实数与实数运算;                |"<<endl;
    cout<<"\t\t\t|                2.实数与虚数运算;                |"<<endl;
    cout<<"\t\t\t|                3.虚数与虚数运算;                |"<<endl;
    cout<<"\t\t\t|                4.退出程序。                      |"<<endl;
    cout<<"\t\t\t|                                                  |"<<endl;
    cout<<"\t\t\t----------------------------------------------------"<<endl;
}
void second_page()//打印次页
{
    cout<<"\t\t\t----------------------------------------------------"<<endl;
    cout<<"\t\t\t|                                                  |"<<endl;
    cout<<"\t\t\t|选择你要进行的运算类型(1~6):                 |"<<endl;
    cout<<"\t\t\t|                1.进行加法运算;                  |"<<endl;
    cout<<"\t\t\t|                2.进行减法运算;                  |"<<endl;
    cout<<"\t\t\t|                3.进行乘法运算;                  |"<<endl;
    cout<<"\t\t\t|                4.进行除法运算;                  |"<<endl;
    cout<<"\t\t\t|                5.进行比较运算;                  |"<<endl;
    cout<<"\t\t\t|                6.返回上层目录;                  |"<<endl;
    cout<<"\t\t\t|                                                  |"<<endl;
    cout<<"\t\t\t----------------------------------------------------"<<endl;
}
void third_page()//程序退出提示语句
{
    cout<<"程序已安全退出,感谢您的光顾,为达您的满意,我们力求下次做得更好!"<<endl;
}
class Complex
{
private:
    double real;//定义虚数的实部
    double imag;//定义虚数的虚部
public:
 
    Complex(double r=0,double i=0):real(r),imag(i) {} //构造函数初始化虚数的实部和虚部
    friend Complex operator +(Complex &c1,Complex &c2)//实现两虚数(或实数)相加
    {
        return Complex(c1.real+c2.real,c1.imag+c2.imag);
    }
    friend Complex operator -(Complex &c1,Complex &c2)//实现两虚数(或实数)相减
    {
        return Complex(c1.real-c2.real,c1.imag-c2.imag);
    }
    friend Complex operator *(Complex &c1,Complex &c2)//实现两虚数(或实数)相乘
    {
        return Complex(c1.real*c2.real-(c1.imag*c2.imag),
                       c1.real*c2.imag+c2.real*c1.imag);
    }
    friend Complex operator /(Complex &c1,Complex &c2)//实现两虚数(或实数)相除
    {
        return Complex(((c1.real*c2.real)-(c1.imag*c2.imag))/
                       ((c2.real*c2.real)+(c2.imag*c2.imag)),
                       ((c1.real*c2.imag)+(c2.real*c1.imag))/
                       ((c2.real*c2.real)+(c2.imag*c2.imag)));
    }
    friend bool operator ==(Complex &c1,Complex &c2)//实现两虚数(或实数)相比较
    {
        if(c1.real==c2.real&&c1.imag==c2.imag)
        {
            return true;
        }
        else
            return false;
    }
    void set_real()//实现给实部赋值
    {
        cin>>real;
    }
    void set_imag()//实现给虚部赋值
    {
        cin>>imag;
    }
    void display()//显示运算结果
    {
        if(imag==0)
        {
            cout<<"结果为:"<<real<<endl;
        }
        else
            cout<<"结果为:"<<real<<"+"<<imag<<"i"<<endl;
    }
    ~Complex(){}//析构函数
} ;
int main()//主函数
{
    int choice,choice1;//定义选择标志
    while(1)
    {
        home_page();
        cout<<"请选择所需的服务:"<<endl;
        cin>>choice;
 
        switch(choice)
        {
        case 1:
            second_page();
            cout<<"请选择你要进行的运算:"<<endl;
            cin>>choice1;
 
            switch(choice1)
            {
 
            case 1:
            {
                Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0);
                cout<<"请输入第一个实数"<<endl;
                c1.set_real();
                cout<<"请输入第二个实数"<<endl;
                c2.set_real();
                c3=c1+c2;//+号为已重载运算符,下同
                c3.display();
                return main();//返回主函数,类似于递归,下同
            }
            break;
            case 2:
            {
                Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0);
                cout<<"请输入第一个实数"<<endl;
                c1.set_real();
                cout<<"请输入第二个实数"<<endl;
                c2.set_real();
                c3=c1-c2;
                c3.display();
                return main();
            }
            break;
            case 3:
            {
                Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0);
                cout<<"请输入第一个实数"<<endl;
                c1.set_real();
                cout<<"请输入第二个实数"<<endl;
                c2.set_real();
                c3=c1*c2;
                c3.display();
                return main();
            }
            break;
            case 4:
            {
                Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0);
                cout<<"请输入第一个实数"<<endl;
                c1.set_real();
                cout<<"请输入第二个实数"<<endl;
                c2.set_real();
                c3=c1/c2;
                c3.display();
                return main();
            }
            break;
            case 5:
            {
                Complex c1(0.0,0.0),c2(0.0,0.0);
                cout<<"请输入第一个实数"<<endl;
                c1.set_real();
                cout<<"请输入第二个实数"<<endl;
                c2.set_real();
                if(c1==c2)
                    cout<<"两数相等"<<endl;
                else
                    cout<<"两数不相等"<<endl;
                    return main();
            }
            case 6:
            {
 
                return main();
            }
            }
            break;
        case 2:
            second_page();
            cout<<"请选择你要进行的运算:"<<endl;
            cin>>choice1;
 
            switch(choice1)
            {
            case 1:
            {
                Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0);
                cout<<"请输入第一个实数"<<endl;
                c1.set_real();
                cout<<"请输入第二个虚数的实部"<<endl;
                c2.set_real();
                cout<<"请输入第二个虚数的虚部"<<endl;
                c2.set_imag();
                c3=c1+c2;
                c3.display();
                return main();
            }
            break;
            case 2:
            {
                Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0);
                cout<<"请输入第一个实数"<<endl;
                c1.set_real();
                cout<<"请输入第二个虚数的实部"<<endl;
                c2.set_real();
                cout<<"请输入第二个虚数的虚部"<<endl;
                c2.set_imag();
                c3=c1-c2;
                c3.display();
                return main();
            }
            break;
            case 3:
            {
                Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0);
                cout<<"请输入第一个实数"<<endl;
                c1.set_real();
                cout<<"请输入第二个虚数的实部"<<endl;
                c2.set_real();
                cout<<"请输入第二个虚数的虚部"<<endl;
                c2.set_imag();
                c3=c1*c2;
                c3.display();
                return main();
            }
            break;
            case 4:
            {
                Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0);
                cout<<"请输入第一个实数"<<endl;
                c1.set_real();
                cout<<"请输入第二个虚数的实部"<<endl;
                c2.set_real();
                cout<<"请输入第二个虚数的虚部"<<endl;
                c2.set_imag();
                c3=c1/c2;
                c3.display();
                return main();
            }
            break;
            case 5:
            {
                Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0);
                cout<<"请输入第一个实数"<<endl;
                c1.set_real();
                cout<<"请输入第二个虚数的实部"<<endl;
                c2.set_real();
                cout<<"请输入第二个虚数的虚部"<<endl;
                c2.set_imag();
                if(c1==c2)
                    cout<<"两数相等"<<endl;
                else
                    cout<<"两数不相等"<<endl;
                    return main();
            }
            break;
            case 6:
            {
 
                return main();
            }
            }
 
        case 3:
            second_page();
            cout<<"请选择你要进行的运算:"<<endl;
            cin>>choice1;
 
            switch(choice1)
            {
            case 1:
            {
                Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0);
                cout<<"请输入第一个虚数的实部"<<endl;
                c1.set_real();
                cout<<"请输入第一个虚数的虚部"<<endl;
                c1.set_imag();
                cout<<"请输入第二个虚数的实部"<<endl;
                c2.set_real();
                cout<<"请输入第二个虚数的虚部"<<endl;
                c2.set_imag();
                c3=c1+c2;
                c3.display();
                return main();
            }
            case 2:
            {
                Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0);
                cout<<"请输入第一个虚数的实部"<<endl;
                c1.set_real();
                cout<<"请输入第一个虚数的虚部"<<endl;
                c1.set_imag();
                cout<<"请输入第二个虚数的实部"<<endl;
                c2.set_real();
                cout<<"请输入第二个虚数的虚部"<<endl;
                c2.set_imag();
                c3=c1-c2;
                c3.display();
                return main();
            }
            break;
            case 3:
            {
                Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0);
                cout<<"请输入第一个虚数的实部"<<endl;
                c1.set_real();
                cout<<"请输入第一个虚数的虚部"<<endl;
                c1.set_imag();
                cout<<"请输入第二个虚数的实部"<<endl;
                c2.set_real();
                cout<<"请输入第二个虚数的虚部"<<endl;
                c2.set_imag();
                c3=c1*c2;
                c3.display();
                return main();
            }
            break;
            case 4:
            {
                Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0);
                cout<<"请输入第一个虚数的实部"<<endl;
                c1.set_real();
                cout<<"请输入第一个虚数的虚部"<<endl;
                c1.set_imag();
                cout<<"请输入第二个虚数的实部"<<endl;
                c2.set_real();
                cout<<"请输入第二个虚数的虚部"<<endl;
                c2.set_imag();
                c3=c1/c2;
                c3.display();
                return main();
            }
            break;
            case 5:
            {
                Complex c1(0.0,0.0),c2(0.0,0.0),c3(0.0,0.0);
                cout<<"请输入第一个虚数的实部"<<endl;
                c1.set_real();
                cout<<"请输入第一个虚数的虚部"<<endl;
                c1.set_imag();
                cout<<"请输入第二个虚数的实部"<<endl;
                c2.set_real();
                cout<<"请输入第二个虚数的虚部"<<endl;
                c2.set_imag();
                if(c1==c2)
                    cout<<"两数相等"<<endl;
                else
                    cout<<"两数不相等"<<endl;
                    return main();
            }
            case 6://返回上层目录
            {
 
                return main();
            }
            }
            break;
 
        case 4://退出程序
 
            third_page();
            break;
        }
        break;
    }
    return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/m0_46525584/article/details/106672490

标签:

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部