第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

以下代碼是關(guān)于C++的提問 ,麻煩幫忙看看該怎么解決好一點(diǎn)?

以下代碼是關(guān)于C++的提問 ,麻煩幫忙看看該怎么解決好一點(diǎn)?

人到中年有點(diǎn)甜 2021-12-31 09:05:43
Design a composite class represents complex numbers whose real and imaginary partsare Fractions.1. Write appropriate constructors for this class;2. Fractions should be able to add, subtract, multiple and divide.
查看完整描述

2 回答

?
大話西游666

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊

#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
#include <iomanip>
class Complex
{
public:
Complex(double _real,double _imag = 0.0):real(_real),imag(_imag){} //構(gòu)造函數(shù),初始化列表和默認(rèn)參數(shù)
Complex(std::istream &is){is >> *this;}; //輸入構(gòu)造函數(shù),調(diào)用自身的>>操作符
void SetReal(double _real); //更改實(shí)部的值
void SetImag(double _imag); //更改虛部的值
void SetVal(double _real,double _imag); //更改整個(gè)復(fù)數(shù)
inline double GetReal() const; //獲取實(shí)部,常成員函數(shù)
inline double GetImag() const; //獲取虛部,常成員函數(shù)
Complex& operator+=(const Complex &val); //成員操作符+=
Complex& operator*=(const Complex &val); //成員操作符-=
friend bool operator==(const Complex &lhs,const Complex &rhs); //友元函數(shù),需訪問私有數(shù)據(jù)
friend std::istream& operator>>(std::istream &,Complex &); //友元輸入操作符,需私有數(shù)據(jù)
friend std::ostream& operator<<(std::ostream &,const Complex &); //友元輸出操作符,需私有數(shù)據(jù)
private:
double real;
double imag;
};

Complex operator+(const Complex &lhs, const Complex &rhs); //普通函數(shù),實(shí)現(xiàn)兩個(gè)復(fù)數(shù)+操作
Complex operator*(const Complex &lhs, const Complex &rhs); //普通函數(shù),實(shí)現(xiàn)兩個(gè)復(fù)數(shù)*操作

//========================分割線,此線上面為定義代碼,此線下面是實(shí)現(xiàn)代碼===============================
inline bool operator==(const Complex &lhs,const Complex &rhs)
{
return (lhs.real == rhs.real) && (lhs.imag == rhs.imag);
}

inline bool operator!=(const Complex &lhs,const Complex &rhs)
{
return !(lhs==rhs);
}

inline Complex& Complex::operator+=(const Complex &val)
{
real += val.real;
imag += val.imag;
return *this;
}

inline Complex operator+(const Complex &lhs,const Complex &rhs)
{
Complex ret(lhs);
ret += rhs;
return ret;
}

inline Complex& Complex::operator*=(const Complex &val)
{
double tReal = real;
double tImag = imag;
real = tReal*val.real - tImag*val.imag;
imag = tReal*val.imag + tImag*val.real;
return *this;
}

inline Complex operator*(const Complex &lhs,const Complex &rhs)
{
Complex ret(lhs);
ret *= rhs;
return ret;
}

inline std::istream& operator>>(std::istream &is,Complex &com)
{
std::cout << "請(qǐng)輸入實(shí)數(shù)部分:" ;
is >> com.real;
if(is)
{
std::cout << "請(qǐng)輸入虛數(shù)部分:" ;
is >> com.imag;
if(is)
{
return is;
}
else
{
com = Complex(0.0);
}
}
else
{
com = Complex(0.0);
}
return is;
}

inline std::ostream& operator<<(std::ostream &os, const Complex &com)
{
os << "復(fù)數(shù)為:" << com.real << std::showpos << com.imag << "i" << std::endl;
return os;
}

inline double Complex::GetReal() const
{
return real;
}

inline double Complex::GetImag() const
{
return imag;
}

void Complex::SetReal(double _real)
{
real = _real;
}

void Complex::SetImag(double _imag)
{
imag = _imag;
}

void Complex::SetVal(double _real,double _imag)
{
real = _real;
imag = _imag;
}
#endif



查看完整回答
反對(duì) 回復(fù) 2022-01-03
?
Smart貓小萌

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊

只實(shí)現(xiàn)了要求的基本加減乘除供參考


#include <iostream>using namespace std; class complex {public:    complex(double r = 0, double i = 0):real(r), imaginary(i){}    complex operator+(complex &a)    {        complex t(*this);        t.real += a.real;        t.imaginary += a.imaginary;        return t;    }    complex operator-(complex &a)    {        complex t(*this);        t.real -= a.real;        t.imaginary -= a.imaginary;        return t;    }    complex operator*(complex &a)    {        complex t;        t.real = a.real*real - a.imaginary*imaginary;        t.imaginary = a.real*imaginary + a.imaginary*real;        return t;    }    complex operator/(complex &a)    {        complex t, t1(a);        double div;        t1.imaginary*= -1;        div = a.real*a.real - a.imaginary * a.imaginary;        t = a * *this;        t.real /= div;        t.imaginary /= div;        return t;    }    friend ostream &operator << (ostream & out, const complex & a);private:    double real, imaginary;}; ostream &operator << (ostream & out, const complex & a){    out << '(' << a.real;    if(a.imaginary>0)        out<<'+';    out << a.imaginary << "i)";    return out;}int main(){    complex a(1,2), b(3,4);    cout << a << '+' << b << '=' << a+b<< endl;    cout << a << '-' << b << '=' << a-b<< endl;    cout << a << '*' << b << '=' << a*b<< endl;    cout << a << '/' << b << '=' << a/b<< endl;}



查看完整回答
反對(duì) 回復(fù) 2022-01-03
  • 2 回答
  • 0 關(guān)注
  • 231 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)