#include<iostream> #include<cmath> using namespace std; class Complex{ private: int real; int imag; public: Complex(int r=0,int i=0):real(r),imag(i){} Complex operator+(Complex &b); void display(){ cout<<real<<"+"<<imag<<"i";} }; Complex::Complex operator+(Complex &b){ Complex c; c.real=real+b.real; c.imag=imag+b.imag; return c; } int main(){ Complex c1(1,1),c2(2,2),c3; c3=c2+c1; c3.display(); return 0; }
时间: 2024-04-13 07:29:06 浏览: 13
你提供的代码存在一些语法错误。在Complex类中,你定义了一个重载的加法运算符,但是函数定义的位置有误。以下是修改后的代码:
```cpp
#include<iostream>
#include<cmath>
using namespace std;
class Complex{
private:
int real;
int imag;
public:
Complex(int r=0, int i=0) : real(r), imag(i) {}
Complex operator+(Complex &b);
void display(){
cout << real << "+" << imag << "i";
}
};
Complex Complex::operator+(Complex &b){
Complex c;
c.real = real + b.real;
c.imag = imag + b.imag;
return c;
}
int main(){
Complex c1(1, 1), c2(2, 2), c3;
c3 = c2 + c1;
c3.display();
return 0;
}
```
在这个代码中,我们定义了一个Complex类,表示复数。在类中,我们重载了加法运算符,使得两个Complex对象可以相加。在main函数中,创建了两个Complex对象c1和c2,并将它们相加得到c3。最后输出c3的值。
相关问题
#include <iostream> #include <cmath> using namespace std; class Complex { private: double real, imag; public: void input() { cin >> real >> imag; } double getReal() const { return real; } double getImag() const { return imag; } }; double dist(int x1, int y1) { return sqrt((int)x1*x1 + (int)y1*y1); } double dist(double x1, double y1) { return sqrt((double)x1*x1 + (double)y1*y1); } double dist(const Complex& a, const Complex& b) { int dx = a.getReal() - b.getReal(); int dy = a.getImag() - b.getImag(); return sqrt(dx*dx + dy*dy); } int main() { int a1,b1; double a2,b2; Complex a3,b3; int type; cin>>type; if(type==1) { cin>>a1>>b1; cout<<dist(a1,b1)<<endl; } else if(type==2) { cin>>a2>>b2; cout<<dist(a2,b2)<<endl; } else { a3.input(); b3.input(); cout<<dist(a3,b3)<<endl; } return 0; }修改这个代码使得输入2 2.2 9.9时输出7.7
可以将第一个dist函数的参数类型改为double,同时在计算平方和时也需要使用double类型,代码如下:
```
#include <iostream>
#include <cmath>
using namespace std;
class Complex {
private:
double real, imag;
public:
void input() {
cin >> real >> imag;
}
double getReal() const {
return real;
}
double getImag() const {
return imag;
}
};
double dist(double x1, double y1) {
return sqrt(x1*x1 + y1*y1);
}
double dist(const Complex& a, const Complex& b) {
double dx = a.getReal() - b.getReal();
double dy = a.getImag() - b.getImag();
return sqrt(dx*dx + dy*dy);
}
int main() {
int a1, type;
double a2, b2;
Complex a3, b3;
cin >> type;
if (type == 1) {
cin >> a1 >> b1;
cout << dist(a1, b1) << endl;
}
else if (type == 2) {
cin >> a2 >> b2;
cout << dist(a2, b2) << endl;
}
else {
a3.input();
b3.input();
cout << dist(a3, b3) << endl;
}
return 0;
}
```
使用这个代码,输入2 2.2 9.9时会输出7.7。
请完成Complex类的定义,使得下面的主函数能正常运行。 int main( ) { Complex c1(1,2); cout<<"c1="<<c1; Complex c2(2); cout<<"c2="<<c2<<endl; Complex c3(c1); cout<<"c3="<<c3; c1=c1+c2+c3; // c1=(c1.operator+(c2)).operator+(c3); cout<<"c1=c1+c2+c3="<<c1; c2=-c3; cout<<"c2=-c3="<<c2; c3=c2-c1; cout<<"c3=c2-c1="<< c3; Complex r2=2+c2; cout<<"r2=2+c2="<<r2; Complex c4; cin>>c4; cout<<"c4="<<c4; cout<<"the module of c4 is: "<<c4.GetMagnitude()<<endl; return 0; }
```
#include <iostream>
#include <cmath>
using namespace std;
class Complex {
private:
double real;
double imag;
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
Complex operator+(const Complex& other) const {
return Complex(real + other.real, imag + other.imag);
}
Complex operator-(const Complex& other) const {
return Complex(real - other.real, imag - other.imag);
}
Complex operator-() const {
return Complex(-real, -imag);
}
Complex operator+(double num) const {
return Complex(real + num, imag);
}
Complex operator-(double num) const {
return Complex(real - num, imag);
}
friend Complex operator+(double num, const Complex& c) {
return c + num;
}
friend ostream& operator<<(ostream& os, const Complex& c) {
os << c.real << "+" << c.imag << "i";
return os;
}
friend istream& operator>>(istream& is, Complex& c) {
is >> c.real >> c.imag;
return is;
}
double GetMagnitude() const {
return sqrt(real * real + imag * imag);
}
};
int main() {
Complex c1(1, 2);
cout << "c1=" << c1;
Complex c2(2);
cout << "c2=" << c2 << endl;
Complex c3(c1);
cout << "c3=" << c3;
c1 = c1 + c2 + c3;
cout << "c1=c1+c2+c3=" << c1;
c2 = -c3;
cout << "c2=-c3=" << c2;
Complex c4;
cin >> c4;
cout << "c4=" << c4;
cout << "the module of c4 is: " << c4.GetMagnitude() << endl;
return 0;
}
```