实验,help帮忙看看是要写什么啊~~本cn毫无头绪啊!!
class Complex //复数类
{
double real, image; //复数的实部和虚部
public:
//构造函数
//使用在操作类内和类外两种方法实现类的以下运算符重载函数
//1、运算符输入>>和输出<<,并能够实现连续运算
//2、运算符乘法*,并能够实现连续运算
//3、运算符下标[],下标值为0时返回实部、为1时返回虚部,其他值提示出错
//4、运算符前置++和后置++,对实部加1
//5、运算符强制类型转换(),把浮点型数强制转换为实部为0虚部为此数的复数
};
void main()
{
Complex c1(2.0, 3.5), c2(5, 8.5), c3, c4;
cin>>c3>>c4;
cout<<c2++*c3<<c4;
cout<<++(Complex)c2[0];
cout<c1+c3+4.7;
}
#include<iostream>
#include<cstdlib>
#include<string>
using namespace std;
class Complex {
double real, image;//复数的实部和虚部
public:
Complex(){}
Complex(double r, double i) :real(r), image(i) {}//构造函数
Complex(double i):real(0),image(i){}
Complex& operator +(Complex& c) {
real += c.real;
image += c.image;
return *this;
}//类内对象与对象相加
Complex& operator*(Complex c) {
double tmp = real;
real = real * c.real - image * c.image;
image = tmp * c.image + image * c.real;
return *this;
}//类内乘法
double& operator[](int i) {
if (i == 0) {
return real;
}
else if (i == 1) {
return image;
}
else
cout << "输入错误!" << endl;
exit(0);
}//类内下标
//[]运算符不能被重载为全局函数
Complex operator++(int) {
Complex tmp = *this;
real += 1;
return tmp;
}//类内后置++
friend ostream& operator<<(ostream& os, const Complex& c);//声明友元函数
friend istream& operator>>(istream& is, Complex& c);
friend Complex operator++(Complex c);
friend Complex& operator+(Complex& c, double i);
};
ostream& operator<<(ostream& os, const Complex& c) {
os << c.real << "+" << c.image << "i";
return os;
}//类外输出
istream& operator>>(istream& is, Complex& c) {
string str;
is >> str;
int pos = str.find("+", 0);
string tmp = str.substr(0, pos);
c.real = atof(tmp.c_str());
tmp = str.substr(pos + 1, str.length() - pos - 2);
c.image = atof(tmp.c_str());
return is;
}//类外输入
Complex operator++(Complex c) {
c.real += 1;
return c;
}//类外前置++
Complex& operator+(Complex& c, double i) {
c.real += i;
return c;
}
int main()
{
Complex c1(2.0, 3.5), c2(5, 8.5), c3, c4;
cin >> c3 >> c4;
cout << c2++ * c3 << " " << c4 << endl;
cout << c2[0] << endl;
cout << ++((Complex)c2[0]) << endl;
cout << c1 + c3 + 4.7;
}
咱俩的实验报告一样的,我也不会,你要是写出来告诉我一声,谢谢了