第9周--项目1-Complex类
生活随笔
收集整理的這篇文章主要介紹了
第9周--项目1-Complex类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*
* Copyright (c) 2011, 煙臺大學計算機學院
* All rights reserved.
* 作 者:王靜
* 完成日期:2013 年 5 月 5 日
* 版 本 號:v1.0
* 輸入描述:
* 問題描述:定義Complex類中的<<和>>運算符的重載,實現輸入和輸出,改造原程序中對運算結果顯示方式,使程序讀起來更自然。
* 程序輸出:
* 問題分析:
* 算法設計:略
*/
#include <iostream>
#include <Cstring>
using namespace std;
class Complex
{
public:Complex( ){real=0;imag=0;}//Complex(double r,double i){real=r;imag=i;}friend Complex operator+(Complex &c1,Complex &c2);friend Complex operator-(Complex &c1,Complex &c2);friend Complex operator*(Complex &c1,Complex &c2);friend Complex operator/(Complex &c1,Complex &c2);friend istream& operator>>(istream &cin,Complex&c);friend ostream& operator<<(ostream &cout,Complex&c);
private:double real;double imag;
};Complex operator+(Complex &c1,Complex &c2)
{Complex c;c.real=c1.real+c2.real;c.imag=c1.imag+c2.imag;return c;
}
Complex operator-(Complex &c1,Complex &c2)
{Complex c;c.real=c1.real-c2.real;c.imag=c1.imag-c2.imag;return c;
}
Complex operator*(Complex &c1,Complex &c2)
{Complex c;c.real=c1.real*c2.real-c1.imag*c2.imag;c.imag=c1.imag*c2.real+c1.real*c2.imag;return c;
}
Complex operator/(Complex &c1,Complex &c2)
{Complex c;c.real=(c1.real/c2.real+c1.imag/c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);c.imag=(c1.imag/c2.real-c1.real/c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);return c;
}
istream & operator>>(istream&cin,Complex&c)
{cout<<"請輸入復數的實部與虛部"<<endl;cin>>c.real>>c.imag;return cin;
}
ostream & operator<<(ostream &cout,Complex&c)
{cout<<"(";cout<<c.real;cout<<","<<c.imag<<"i)"<<endl;return cout;
}
int main( )
{Complex c1,c2,c3;cin>>c1>>c2;cout<<c1<<c2<<endl;c3=c1+c2;cout<<"c1+c2="<<c3<<endl;c3=c1-c2;cout<<"c1-c2="<<c3<<endl;c3=c1*c2;cout<<"c1*c2="<<c3<<endl;c3=c1/c2;cout<<"c1/c2="<<c3<<endl;//system("pause");return 0;
}
?
運行結果:
(貼圖)
心得體會:
?
總結
以上是生活随笔為你收集整理的第9周--项目1-Complex类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RecyclerView+自定义Inde
- 下一篇: 敏捷团队章程