您的位置:首页 > 其它

1272 面向对象程序设计上机练习十(运算符重载)

2017-12-18 11:08 253 查看
1272 面向对象程序设计上机练习十(运算符重载)

Time Limit: 1000MS Memory
Limit: 65536KB


Problem Description

定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。例如:c1+c2、i+c1、c1+i均合法。(其中i是整数,c1、c2是复数),编程实现求2个复数之和、整数与复数之和。


Input

输入有三行:第1行是第1个复数c1的实部和虚部,以空格分开。第2行是第2个复数c2的实部和虚部,以空格分开。第3行是1个整数i的值。


Output

输出有三行:
第1行是2个复数c1和c2的和,显示方式:实部+虚部i
第2行是第1个复数c1加i的值,显示方式:实部+虚部i 
第3行是i加第
4000
1个复数c1的值,显示方式:实部+虚部i


Example Input

2 33 510


Example Output

5+8i12+3i12+3i

#include <iostream>
using namespace std;

class Complex
{
public:
Complex(int x = 0, int y = 0)
{
real = x;
imag = y;
}

void get_in1(int x = 0, int y = 0)
{
cin >> x >>y;
real = x;
imag = y;
}

void get_in2(int x = 0, int y = 0)
{
cin >> x;
real = x;
imag = y;
}

void put_out()
{
cout << real <<'+'<< imag <<'i'<<endl;
}

Complex operator + (Complex &t)
{
Complex x;
x.real = t.real + real;
x.imag = t.imag + imag;
return x;
}

private:
int real,imag;
int a,b;
};
int main()
{
Complex c1,c2,c3,c4,c5,c6;
c1.get_in1();
c2.get_in1();
c3.get_in2();

c4 = c1 + c2;
c5 = c1 + c3;
c6 = c3 + c1;

c4.put_out();
c5.put_out();
c6.put_out();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: