求一个复数计算器,要求实现复数的加减乘除,
1.定义一个类Complex
2.声明所需成员函数和友元函数,输入远算符>>和输出运算符<<,并对加减乘除运算符进行重载
3.定义各类成员函数
跪求了,新手,不怎么理解,求例子
求一个复数计算器,要求实现复数的加减乘除, 1.定义一个类Complex 2.声明所需成员函数和友
答案:1 悬赏:70 手机版
解决时间 2021-03-10 23:39
- 提问者网友:欲望失宠
- 2021-03-10 15:23
最佳答案
- 五星知识达人网友:忘川信使
- 2021-03-10 16:25
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(double r = 0, double i = 0) : real_(r), imag_(i) { }
Complex(const Complex &c):real_(c.real_), imag_(c.imag_) { }
~Complex() {}
double real() const { return real_; }
double imag() const { return imag_; }
void real(double r) { real_ = r; }
void imag(double i) { imag_ = i; }
void reset(double r, double i) { real_ = r; imag_ = i; }
Complex operator+(const Complex &c);
Complex operator-(const Complex &c);
Complex operator*(const Complex &c);
Complex operator/(const Complex &c);
Complex& operator=(const Complex &c);
friend ostream& operator<<(ostream &os, Complex &c);
friend istream& operator>>(istream &is, Complex &c);
private:
double real_;
double imag_;
};
Complex Complex::operator+(const Complex &c) {
return Complex(real_ + c.real_, imag_ + c.imag_);
}
Complex Complex::operator-(const Complex &c) {
return Complex(real_-c.real_, imag_-c.imag_);
}
Complex Complex::operator*(const Complex &c) {
return Complex((real_*c.real_ - imag_*c.imag_), (imag_*c.real_ + real_*imag_));
}
Complex Complex::operator/(const Complex &c) {
return Complex(((real_*c.real_ + imag_*c.imag_) / (c.real_*c.real_+ c.imag_*c.imag_)),
(imag_*c.real_-real_*c.imag_) / (c.real_*c.real_+c.imag_*c.imag_));
}
Complex& Complex::operator=(const Complex &c) {
real_ = c.real_; imag_ = c.imag_;
return *this;
}
ostream& operator<<(ostream &os, Complex &c) {
os << c.real_;
if (c.imag_ >= 0) os << "+";
os << c.imag_ << "i" << endl;
return os;
}
istream& operator>>(istream &is, Complex &c) {
is >> c.real_ >> c.imag_;
return is;
}
int main()
{
Complex c;
cout << c;
Complex c2(1, 20);
cout << c2;
Complex c3(1, -20);
cout << c3;
Complex c4 = c2 * c3;
cout << c4;
Complex c5 = c2 - c3;
cout << c5;
Complex c6;
c6 = c5;
cout << c6;
Complex c7 = c2 + c3;
cout << c7;
Complex c8 = c2 / c3;
cout << c8;
}
这是最简单的例子了。
using namespace std;
class Complex
{
public:
Complex(double r = 0, double i = 0) : real_(r), imag_(i) { }
Complex(const Complex &c):real_(c.real_), imag_(c.imag_) { }
~Complex() {}
double real() const { return real_; }
double imag() const { return imag_; }
void real(double r) { real_ = r; }
void imag(double i) { imag_ = i; }
void reset(double r, double i) { real_ = r; imag_ = i; }
Complex operator+(const Complex &c);
Complex operator-(const Complex &c);
Complex operator*(const Complex &c);
Complex operator/(const Complex &c);
Complex& operator=(const Complex &c);
friend ostream& operator<<(ostream &os, Complex &c);
friend istream& operator>>(istream &is, Complex &c);
private:
double real_;
double imag_;
};
Complex Complex::operator+(const Complex &c) {
return Complex(real_ + c.real_, imag_ + c.imag_);
}
Complex Complex::operator-(const Complex &c) {
return Complex(real_-c.real_, imag_-c.imag_);
}
Complex Complex::operator*(const Complex &c) {
return Complex((real_*c.real_ - imag_*c.imag_), (imag_*c.real_ + real_*imag_));
}
Complex Complex::operator/(const Complex &c) {
return Complex(((real_*c.real_ + imag_*c.imag_) / (c.real_*c.real_+ c.imag_*c.imag_)),
(imag_*c.real_-real_*c.imag_) / (c.real_*c.real_+c.imag_*c.imag_));
}
Complex& Complex::operator=(const Complex &c) {
real_ = c.real_; imag_ = c.imag_;
return *this;
}
ostream& operator<<(ostream &os, Complex &c) {
os << c.real_;
if (c.imag_ >= 0) os << "+";
os << c.imag_ << "i" << endl;
return os;
}
istream& operator>>(istream &is, Complex &c) {
is >> c.real_ >> c.imag_;
return is;
}
int main()
{
Complex c;
cout << c;
Complex c2(1, 20);
cout << c2;
Complex c3(1, -20);
cout << c3;
Complex c4 = c2 * c3;
cout << c4;
Complex c5 = c2 - c3;
cout << c5;
Complex c6;
c6 = c5;
cout << c6;
Complex c7 = c2 + c3;
cout << c7;
Complex c8 = c2 / c3;
cout << c8;
}
这是最简单的例子了。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯