复数运算(友元函数)

复数运算(友元函数)复数运算 友元函数 题目描述复数类的声明如下 要求如下 1 实现复数类和友元函数 addCom 和 outCom

大家好,欢迎来到IT知识分享网。

 复数运算(友元函数)

题目描述

复数类的声明如下:

复数运算(友元函数)

要求如下:

1.     实现复数类和友元函数addCom和outCom。

2.   参考addCom函数为复数类增加一个友元函数minusCom,用于实现两个复数的减法

3.     在main函数中,通过友元函数,实现复数的加减法和复数的输出。

输入

第1行:第1个复数的实部和虚部

第2行:需进行运算的次数,注意:是连续运算。具体结果可参考样例

  第3行开始,每行输入运算类型,以及参与运算的复数的实部与虚部。“+”表示复数相加,“-”表示复数相减。

输出

 每行输出复数运算后的结果,复数输出格式为“(实部,虚部)”。

样例输入

10 10
3
+ 20 10
– 15 5
+ 5 25

样例输出

(30,20)
(15,15)
(20,40)

我的代码

#include <iostream> using namespace std; class complex{ private: double real; double imag; public: complex(); complex(double r, double i); friend complex addCom(complex c1, complex c2); friend complex minusCom(complex c1, complex c2); friend void outCom(complex c); }; complex::complex() {} complex::complex(double r, double i) { real = r; imag = i; } complex addCom(complex c1, complex c2) { c1.real = c1.real + c2.real; c1.imag = c1.imag + c2.imag; return c1; } complex minusCom(complex c1, complex c2) { c1.real = c1.real - c2.real; c1.imag = c1.imag - c2.imag; return c1; } void outCom(complex c) { cout << "(" << c.real << "," << c.imag << ")" << endl; } int main() { double real, imag; cin >> real >> imag; complex a(real, imag); int t; cin >> t; while(t--){ char s; cin >> s; if(s == '-') { cin >> real >> imag; complex b(real, imag); a = minusCom(a, b); outCom(a); } else if(s == '+'){ cin >> real >> imag; complex b(real, imag); a = addCom(a, b); outCom(a); } } return 0; }

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/134266.html

(0)
上一篇 2025-07-13 19:10
下一篇 2025-07-13 19:20

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信