设计一个分数类 把可以队分数对象进行各种计算功能(加减乘除等)
比较功能(大于小于及与整数的比较)进行重载
重载输入输出运算符
程序有点问题 请指导一下
// 01.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream.h"
#include "math.h"
#include "stdlib.h"
#include "string.h"
//using namespace std;
class Point
{
public:
int numerator; //分子
int denominator; //分母
char semicolon; //除号
Point(int x=0,int y=1,char z='/')
{
numerator=x;
denominator=y;
semicolon='z';
}
friend ostream& operator <<(ostream & ,const Point &a); //重载<<
friend istream& operator >>(istream & ,const Point &a); //重载>> 这边有点问题
friend Point operator +(const Point &a,const Point &b); //重载+
friend Point operator -(const Point &a,const Point &b); //重载-
friend Point operator *(const Point &a,const Point &b); //重载*
friend Point operator /(const Point &a,const Point &b); //重载/
};
/*class Expression_error:public exception //异常类处理
{
public:
Erpression_error(const string &s):exception(s.c_str){}
};*/
ostream& operator <<(ostream & osobject,const Point &a)
{
osobject<<a.numerator<<a.semicolon<<a.numerator;
return osobject;
}
istream& operator >>(istream & isobject ,const Point &a)
{
isobject>>a.numerator>>a.semicolon>>a.denominator;
return isobject;
}
Point divide_exactly( Point &c) ////判断该分数能否转化为整数
{
if(c.numerator%c.denominator==0)
{
c.numerator= c.numerator/c.denominator ;
c.denominator=1;
return c;
}
return c;
}
int min_multiple(const Point &a,const Point &b) //求分母的最小公倍数
{
int i,j;
for(i=1;;i++)
for(j=1;;j++)
if(a.denominator*i==b.denominator*j)
return a.denominator*i;
}
void compare(const Point &a,const Point &b) //比较两个分数大小
{
Point t1,t2;
if(a.denominator==b.denominator)
{
if(a.numerator>b.numerator)
{
cout<<"最大的数是:";
cout<<a;
}
else if(a.numerator<b.numerator)
{
cout<<"最大的数是";
cout<<b;
}
else
{
cout<<"两个数一样大";
}
}
else
{
t1.denominator=min_multiple(a,b);
t2.denominator=t1.denominator;
t1.numerator=a.numerator*(t1.denominator/a.denominator);
t2.numerator=b.numerator*(t1.denominator/b.denominator);
if(t1.numerator>t2.numerator)
{
cout<<"最大的数是:";
cout<<a;
}
else if(t1.numerator<t2.numerator)
{
cout<<"最大的数是:";
cout<<b;
}
else
{
cout<<"两个数一样大";
}
}
}
void compare(const Point &a,int b) //比较分数和整数的大小
{
Point t1,t2;
t1=a;
t2.numerator=b;
t2.denominator=1;
t2.denominator=t1.denominator;
t2.numerator=t2.numerator*t1.denominator;
if(t1.numerator>t2.numerator)
{
cout<<"最大的数是:";
cout<<a;
}
else if(t1.numerator<t2.numerator)
{
cout<<"最大的数是:";
cout<<b;
}
else
{
cout<<"两个数一样大";
}
}
Point operator +(const Point &a,const Point &b)
{
Point t;
if(a.denominator==b.denominator)
{
t.denominator=a.denominator;
t.numerator=a.numerator+b.numerator;
////////////////////////////////////////t=t.divide_exactly;
return t;
}
else
{
t.denominator=min_multiple(a,b);
t.numerator=a.numerator*(t.denominator/a.denominator)+b.numerator*(t.denominator/b.denominator);
////////////////////////////////////////t=t.divide_exactly;
return t;
}
}
Point operator -(const Point &a,const Point &b)
{
Point t;
if(a.denominator==b.denominator)
{
t.denominator=a.denominator;
t.numerator=a.numerator-b.numerator;
////////////////////////////////////////t=t.divide_exactly;
return t;
}
else
{
t.denominator=min_multiple(a,b);
t.numerator=a.numerator*(t.denominator/a.denominator)-b.numerator*(t.denominator/b.denominator);
////////////////////////////////////////t=t.divide_exactly;
return t;
}
}
Point operator *(const Point &a,const Point &b)
{
Point t;
t.denominator=a.denominator*b.denominator;
t.numerator=a.numerator*b.numerator;
////////////////////////////////////////t=t.divide_exactly;
return t;
}
Point operator /(const Point &a,const Point &b)
{
Point t;
t.denominator=a.denominator*b.numerator;
t.numerator=a.numerator*b.denominator;
//////////////////////////////////////////t=t.divide_exactly;
return t;
}
void Mmenu ()
{
int n, m ;
Point a,b;
cout<<"分数输入格式:a/b"<<endl;
cout<<"本程序分数输出格式为:a/b "<<endl;
cout<<endl<<endl<<endl ;
cout<<"┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓";
cout<<"┃**************** 分数计算程序主菜单界面 *****************┃";
cout<<"┃********** ★★★★★ ★★★★★★★ ★★★★★ ************┃";
cout<<"┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫";
cout<<"┃****************★ ①.分数的加法 ★****************┃";
cout<<"┃****************★ ②.分数的减法 ★****************┃";
cout<<"┃****************★ ③.分数的乘法 ★****************┃";
cout<<"┃****************★ ④.分数的除法 ★****************┃";
cout<<"┃****************★ ⑤.分数与分数比较 ★****************┃";
cout<<"┃****************★ ⑥.分数与整数比较 ★****************┃";
cout<<"┃****************★ ⑦.安全退出软件 ★************** ┃";
cout<<"┃ ┃";
cout<<"┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛";
cout<<"\n" ;
cout<<"chioce(1~7): " ;
cin>> n ;
switch (n)
{
case 1 : cout<<"请输入第一个分数:";
cin>>a;
cout<<"请输入第二个分数:";
cin>>b;
cout<<a+b;
getchar() ;
break ;
case 2 :
cout<<"请输入第一个分数:";
cin>>a;
cout<<"请输入第二个分数:";
cin>>b;
cout<<a-b;
getchar();
break ;
case 3 :
cout<<"请输入第一个分数:";
cin>>a;
cout<<"请输入第二个分数:";
cin>>b;
cout<<a*b;
getchar() ;
break ;
case 4 :
cout<<"请输入第一个分数:";
cin>>a;
cout<<"请输入第二个分数:";
cin>>b;
cout<<a*b;
getchar() ;
break ;
case 5 :
cout<<"请输入第一个分数:";
cin>>a;
cout<<"请输入第二个分数:";
cin>>b;
compare(a,b);
getchar() ;
break ;
case 6 :
cout<<"请输入分数:";
cin>>a;
cout<<"请输入整数:";
cin>>m;
compare(a,m);
getchar() ;
break ;
case 7 : cout<<"谢谢使用"<<endl ;
exit ( 0 ) ;
default : cout<<"输入错误,请重新输入选项\n" ;
}
}
void main(int argc, char* argv[])
{
while( 1 )
{
system("cls"); //清屏
Mmenu();
}
}
[此贴子已经被作者于2007-5-15 16:27:40编辑过]