1.题目:
Problem Description
定义描述复数complex的类
(前缀减)operator-- (成员)
(前缀加)operator++ (友元)
(后缀减)operator-- (成员)
(后缀加)operator++ (友元)
(前缀减)operator-- (成员)
(前缀加)operator++ (友元)
(后缀减)operator-- (成员)
(后缀加)operator++ (友元)
Input
输入多个测试实例,每个测试实例占一行,表示一个复数
Output
每个输出实例占一行。如下输出所示,每行有四组复数,分别是对输入的复数对象进行前缀减,前缀加,后缀减,后缀加。每个复数之间用空格隔开。
Sample Input
1 1
2 2
3 3
Sample Output
(0,0) (1,1) (0,0) (1,1)
(1,1) (2,2) (1,1) (2,2)
(2,2) (3,3) (2,2) (3,3)
2.参考代码:
#include <iostream>
using namespace std;
class Complex
{
private:
int real, image;
public:
Complex(int r = 0, int i = 0);
Complex operator--();
friend Complex operator++(Complex&);
Complex operator--(int);
friend Complex operator++(Complex&, int);
void show();
};
Complex::Complex(int r, int i)
{
real = r;
image = i;
}
Complex Complex::operator--()
{
--real;
--image;
return *this;
}
Complex operator++(Complex& c)
{
++c.real;
++c.image;
return c;
}
Complex Complex::operator--(int)
{
real--;
image--;
return *this;
}
Complex operator++(Complex& c, int)
{
c.real++;
c.image++;
return c;
}
void Complex::show()
{
cout << "(" << real << "," << image << ")";
}
int main()
{
int r, i;
while (cin >> r >> i) {
Complex x(r, i);
--x;
x.show();
cout << ' ';
++x;
x.show();
cout << ' ';
x--;
x.show();
cout << ' ';
x++;
x.show();
cout << endl;
}
return 0;
}