作者TheJim (TheJim)
看板C_and_CPP
標題Re: [問題] c++實作複數的class
時間Mon May 4 16:23:03 2009
#include <iostream>
#include <iomanip>
using namespace std;
class Complex
{
private:
double RealPart;
double ImaginPart;
public:
Complex operator +(const Complex & );
Complex operator *(const Complex &);
Complex operator /(const Complex &);
friend Complex operator *( double , const Complex &a);
Complex operator -();
Complex(const double a = 0.0, const double b =
0.0):RealPart(a),ImaginPart(b)
{}
~Complex()
{}
friend Complex operator -(const Complex &, const Complex
&);
friend std::ostream & operator << (std::ostream & os ,const
Complex &);
friend std::istream & operator >> (std::istream & is,Complex
&);
};
Complex Complex::operator +(const Complex &t)
{
return Complex(RealPart+t.RealPart , ImaginPart+t.ImaginPart );
}
Complex operator -(const Complex &a, const Complex &b)
{
return Complex(a.RealPart -b.RealPart , a.ImaginPart
-b.ImaginPart );
}
Complex Complex::operator *(const Complex &t)
{
return Complex(RealPart*t.RealPart -ImaginPart*t.ImaginPart ,
RealPart*t.ImaginPart +ImaginPart*t.RealPart );
}
Complex Complex::operator /(const Complex &t)
{
double x = t.RealPart*t.RealPart + t.ImaginPart*t.ImaginPart;
if( x!=0 )
{
//cout<<Complex( (RealPart*t.RealPart +ImaginPart*t.ImaginPart)/x
, (ImaginPart*t.RealPart-RealPart*t.ImaginPart)/x );
return Complex( (RealPart*t.RealPart +ImaginPart*t.ImaginPart)/x
, (ImaginPart*t.RealPart-RealPart*t.ImaginPart)/x );
}
//cout<<"divided by 0!\n";
return Complex(-0.001,0.001);
}
Complex operator *( double n, const Complex &t)
{
return Complex(n*t.RealPart ,n*t.ImaginPart );
}
Complex Complex::operator -()
{
return Complex(-RealPart, -ImaginPart);
}
std:: ostream & operator << (std:: ostream & os, const
Complex & t)
{
if( t.RealPart==-0.001&&t.ImaginPart==0.001 )
cout<<"divided by 0!\n";
else if( t.RealPart==0.001&&t.ImaginPart==-0.001 )
cout<<"undefined operation!\n";
else
os <<"("<< fixed << setprecision(2)<< t.RealPart <<
","<<t.ImaginPart <<")\n";
return os;
}
std:: istream & operator >> (std:: istream & is, Complex
&t )
{
char a;
cin>>a;
is >> t.RealPart ;
cin>>a;
is >> t.ImaginPart ;
cin>>a;
return is;
}
int main()
{
char oper;
Complex* answer[1000];
Complex num1,num2;
int i=0;
while( cin>>num1 ){
answer[i] = new Complex;
cin>>oper;
cin>>num2;
switch( oper ){
case '+':
//cout<<num1+num2;
//cin>>num2;
*answer[i]=num1+num2;
break;
case '-':
//cout<<num1-num2;
//cin>>num2;
*answer[i]=num1-num2;
break;
case '*':
//cout<<num1*num2;
//cin>>num2;
*answer[i]=num1*num2;
break;
case '/':
//num1/num2;
//cin>>num2;
*answer[i]=num1/num2;
break;
default:
*answer[i]=Complex(0.001,-0.001);
//cout<<"undefined operation!\n";
break;
}
/*if( i==10 )
break;*/
i++;
}
for( int j=0;j<i;j++ )
cout<<*answer[j];
system("pause");
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.140.12
1F:推 ledia:如果算出來的答案剛好是 0.001 - 0.001i 怎麼辦? XD 05/04 16:31
2F:→ TheJim:唉唷 那個我試過了 不是那原因 05/04 17:24
3F:推 ledia:那... 如果亂搞的 operator 長度超過 1 byte 會發生什麼事? 05/04 19:17
4F:推 yoco315:setpre 2 ? 有規定說最多兩位嗎? 05/04 20:48
5F:推 yoco315:有規定耶.. 那我也不知道了 05/04 20:56
6F:→ yoco315:哈,第四項你有顧到嗎?undefined 的那個 05/04 20:57
7F:→ yoco315:嗯,有顧到.. -_-" 那真的不知道了 05/04 20:59
8F:→ TheJim:樓上你在搞笑嗎= = 05/04 21:07
9F:推 AppleFox:超強大的自言自語... 05/04 22:55
10F:推 yoco315:大大的程度超過我太多了 qq 05/05 02:16