您的位置:首页 > 编程语言 > C语言/C++

2012年 上半年 第九周 C++程序设计 (二十七)

2012-04-21 22:48 453 查看
#include <iostream>
using namespace std;
class CFraction
{
private:
int nume;  // 分子
int deno;  // 分母
public:
CFraction(int nu=0,int de=1):nume(nu),deno(de){}
void simplify();
void display();

friend ostream& operator<<(ostream &, CFraction &);
friend istream& operator>>(istream &, CFraction &);

CFraction operator+(const CFraction &c);
CFraction operator-(const CFraction &c);
CFraction operator*(const CFraction &c);
CFraction operator/(const CFraction &c);
CFraction operator+();
CFraction operator-();
bool operator>(const CFraction &c);
bool operator<(const CFraction &c);
bool operator==(const CFraction &c);
bool operator>=(const CFraction &c);
bool operator<=(const CFraction &c);
};
// 分数化简
void CFraction::simplify()
{

for ( int i = nume; i > 0; i-- )
{
if ( nume % i == 0)

if ( deno % i == 0 )
{
nume /=  i;
deno /=  i;
break;
}

}

}

ostream& operator<<(ostream &output, CFraction &t)
{
output << t.nume << "/" << t.deno << endl;
return output;
}
istream& operator>>(istream &input, CFraction &t)
{
char a,b,c;
input >> a >> t.nume >> b >> t.deno >> c;
while(1)
{
if ( a == '(' && b== ',' && c == ')' )
break;
else
cout << " 输入错误,请重新输入" << endl;
}
return input;
}

CFraction CFraction::operator+(const CFraction &c)
{
CFraction  t;
t.deno = c.deno * deno;
t.nume = deno * c.nume + c.deno * nume;
t.simplify();
return t;
}

CFraction CFraction::operator-(const CFraction &c)
{
CFraction  t;
t.deno = c.deno * deno;
t.nume = c.deno * nume - deno * c.nume;
t.simplify();
return t;
}

CFraction CFraction::operator*(const CFraction &c)
{
CFraction  t;
t.deno = c.deno * deno;
t.nume = nume * c.nume;
t.simplify();
return t;
}

CFraction CFraction::operator/(const CFraction &c)
{
CFraction  t;
t.deno = c.nume * deno;
t.nume = nume * c.deno;
t.simplify();
return t;
}

CFraction CFraction::operator+()
{
CFraction t;
t.deno = deno;
t.nume = nume;
t.simplify();
return t;
}
CFraction CFraction::operator-()
{
CFraction t;
t.deno = deno;
t.nume = -nume;
t.simplify();
return t;
}

bool CFraction::operator>(const CFraction &c)
{
int t1_nume,t2_nume;
t1_nume = deno * c.nume ;
t2_nume = c.deno * nume ;
if(t1_nume  < t2_nume)
return true;
else
return false;
}
bool CFraction::operator<(const CFraction &c)
{
int t1_nume ,t2_nume ;
t1_nume = deno * c.nume ;
t2_nume = c.deno * nume ;
if(t1_nume > t2_nume)
return true;
else
return false;
}

bool CFraction::operator==(const CFraction &c)
{
int t1_nume ,t2_nume ;
t1_nume = deno * c.nume ;
t2_nume = c.deno * nume ;
if(t1_nume == t2_nume)
return true;
else
return false;
}

bool CFraction::operator>=(const CFraction &c)
{
int t1_nume ,t2_nume ;
t1_nume = deno * c.nume ;
t2_nume = c.deno * nume ;
if(t1_nume < t2_nume )
return true;
else
return false;
}
bool CFraction::operator<=(const CFraction &c)
{
int t1_nume ,t2_nume ;
t1_nume = deno * c.nume ;
t2_nume = c.deno * nume ;
if( t1_nume >= t2_nume)
return true;
else
return false;
}
int main()
{
CFraction c1(1,2),c2,t;

cout <<"please input c2 (X,X)"<< endl;
cin >> c2;

t = c1 + c2;
cout<<"c1 + c2 = "<<t;

t = c1 - c2;
cout<<"c1 - c2 = "<<t;

t = c1 * c2;
cout<<"c1 * c2 = "<<t;

t = c1 / c2;
cout<<"c1 / c2 = "<<t;

cout << "取反" << endl;

t = c1;
cout<<"t = "<<t;

t = -c1;
cout<<"-t = "<<t;

cout << "比较大小" << endl;
if(c1 > c2) cout<< " c1 > c2 " << endl;
if(c1 < c2) cout<<"  c1 < c2 " << endl;
if(c1 == c2) cout<<" c1 == c2 " << endl;
if(c1 >= c2) cout<<" c1 >= c2 " << endl;
if(c1 <= c2) cout<<" c1 <= c2 " << endl;

system("pause");

return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息