public class Complex{ private int real, imag;public Complex(int real,this.real = real;this.imag = imag; public Complex() this(e,o); public string tostring(){ return "("+this.real+"+"+this.imag+"i)"; public void add(Complex c){this.real += c.real;this.imag +=c.imag; public static Complex subtract(Complex c1, Complex c2){ return new Complex(c1.real-c2.real, c1.imag-c2.imag);public class Complex{ private int real, imag;public Complex(int real,this.real = real;this.imag = imag; public Complex() this(e,o); public string tostring(){ return "("+this.real+"+"+this.imag+"i)"; public void add(Complex c){this.real += c.real;this.imag +=c.imag; public static Complex subtract(Complex c1, Complex c2){ return new Complex(c1.real-c2.real, c1.imag-c2.imag);运行结果
时间: 2024-02-23 17:00:00 浏览: 113
这段代码是有问题的,因为有语法错误,无法通过编译。具体来说,以下几行代码存在语法错误:
```
public Complex(int real,this.real = real;this.imag = imag;
public Complex() this(e,o);
public string tostring(){
```
其中,构造方法 `Complex(int real,` 缺少了右大括号 `}`;在构造方法和默认构造方法中,缺少了参数列表;`tostring()` 方法的返回类型应该是 `String` 而不是 `string`。
此外,这段代码中存在重复定义的问题,因为 `public class Complex{...}` 这一行代码出现了两次。
由于这段代码无法通过编译,因此无法给出运行结果。如果您有其他问题或需要进一步帮助,请随时提出。
相关问题
把下面代码的运算符重载改为友元函数形式#include<iostream> using namespace std; class complex { private: double real; double imag; public: complex(double r = 0.0, double i = 0.0); void print(); complex operator -=(complex c); complex operator *=(complex c); complex operator /=(complex c); complex operator ++(); complex operator ++(int); }; complex::complex(double r, double i) { real = r; imag = i; } complex complex::operator -=(complex c) { complex temp; temp.real = real - c.real; temp.imag = imag - c.imag; real = temp.real; imag = temp.imag; return temp; } complex complex::operator *=(complex c) { complex temp; temp.real = real * c.real - imag * c.imag; temp.imag = real * c.imag + imag * c.real; real = temp.real; imag = temp.imag; return temp; } complex complex::operator /=(complex c) { complex temp; double d; d = c.real * c.real + c.imag * c.imag; temp.real = (real * c.real + imag * c.imag) / d; temp.imag = (c.real * imag - real * c.imag) / d; real = temp.real; imag = temp.imag; return temp; } complex complex::operator ++() { complex temp; temp.real = ++real; temp.imag = ++imag; return temp; } complex complex::operator ++(int) { complex temp(real, imag); real++; imag++; return temp; } void complex::print() { cout << real; if (imag >= 0) cout << '+'; cout << imag << 'i' << endl; } int main() { complex A(30, 40), B(15, 30),C; C = A.operator++(1); cout << "C=A++后,C为:"; C.print(); cout << "A为:"; A.print(); C = A.operator++(); cout << "C=++A后,C为:"; C.print(); cout << "A为:"; A.print(); A *= B; cout << "A*=B后,A为:"; A.print(); A /= B; cout << "A/=B后,A为: "; A.print(); cout << "B为:"; B.print(); return 0; }
重载运算符改为友元函数的形式如下:
```cpp
#include<iostream>
using namespace std;
class complex {
private:
double real;
double imag;
public:
complex(double r = 0.0, double i = 0.0);
void print();
friend complex operator -=(complex& c1, const complex& c2);
friend complex operator *=(complex& c1, const complex& c2);
friend complex operator /=(complex& c1, const complex& c2);
friend complex operator ++(complex& c);
friend complex operator ++(complex& c, int);
};
complex::complex(double r, double i) {
real = r;
imag = i;
}
complex operator -=(complex& c1, const complex& c2) {
complex temp;
temp.real = c1.real - c2.real;
temp.imag = c1.imag - c2.imag;
c1.real = temp.real;
c1.imag = temp.imag;
return temp;
}
complex operator *=(complex& c1, const complex& c2) {
complex temp;
temp.real = c1.real * c2.real - c1.imag * c2.imag;
temp.imag = c1.real * c2.imag + c1.imag * c2.real;
c1.real = temp.real;
c1.imag = temp.imag;
return temp;
}
complex operator /=(complex& c1, const complex& c2) {
complex temp;
double d;
d = c2.real * c2.real + c2.imag * c2.imag;
temp.real = (c1.real * c2.real + c1.imag * c2.imag) / d;
temp.imag = (c2.real * c1.imag - c1.real * c2.imag) / d;
c1.real = temp.real;
c1.imag = temp.imag;
return temp;
}
complex operator ++(complex& c) {
complex temp;
temp.real = ++c.real;
temp.imag = ++c.imag;
return temp;
}
complex operator ++(complex& c, int) {
complex temp(c.real, c.imag);
c.real++;
c.imag++;
return temp;
}
void complex::print() {
cout << real;
if (imag >= 0) cout << '+';
cout << imag << 'i' << endl;
}
int main() {
complex A(30, 40), B(15, 30), C;
C = operator++(A, 1);
cout << "C=A++后,C为:";
C.print();
cout << "A为:";
A.print();
C = operator++(A);
cout << "C=++A后,C为:";
C.print();
cout << "A为:";
A.print();
A *= B;
cout << "A*=B后,A为:";
A.print();
A /= B;
cout << "A/=B后,A为: ";
A.print();
cout << "B为:";
B.print();
return 0;
}
```
需要注意的是,友元函数的参数中第一个参数需要加上引用符号 &,因为需要修改该对象的值。而第二个参数一般为 const 引用形式,以保证不会对该参数进行修改。另外,在调用友元函数时需要在函数名前加上 operator。
用C++给下面的代码写运行部分。class COMPLEX {private: double real; double imag;public: COMPLEX(double r = 0, double i = 0) : real(r), imag(i) {} COMPLEX operator+(const COMPLEX& rhs) const { return COMPLEX(real + rhs.real, imag + rhs.imag); } COMPLEX operator-(const COMPLEX& rhs) const { return COMPLEX(real - rhs.real, imag - rhs.imag); } COMPLEX& operator=(const COMPLEX& rhs) { real = rhs.real; imag = rhs.imag; return *this; }};
以下是用C++编写的运行部分:
```c++
#include <iostream>
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& rhs) const {
return COMPLEX(real + rhs.real, imag + rhs.imag);
}
COMPLEX operator-(const COMPLEX& rhs) const {
return COMPLEX(real - rhs.real, imag - rhs.imag);
}
COMPLEX& operator=(const COMPLEX& rhs) {
real = rhs.real;
imag = rhs.imag;
return *this;
}
};
int main() {
COMPLEX a(1, 2), b(3, 4), c;
c = a + b;
cout << c.real << " + " << c.imag << "i" << endl;
c = a - b;
cout << c.real << " + " << c.imag << "i" << endl;
return 0;
}
```
在上述代码中,我们定义了一个名为`COMPLEX`的类,它表示一个复数,具有实部和虚部两个成员变量。我们使用构造函数初始化这两个成员变量,并且使用运算符重载函数`operator+`和`operator-`分别实现了复数的加法和减法。同时,我们也定义了一个赋值运算符`operator=`,用于将一个复数赋值给另一个复数。
在`main()`函数中,我们创建了两个复数对象`a`和`b`,并将它们相加和相减,将结果分别赋值给复数对象`c`。最后,我们输出了`c`的实部和虚部,以验证我们的代码是否正确。
阅读全文