分数运算是解决了,但我一直不知道怎么解决输入识别的问题
比如最好能以2/5+1/7这样的形式输入,恳请高手指教!
谢谢!
程序如下:
//分数运算
#include
class Euclid//用于求最大公约数的两数
{
private:
int x,y;
public:
Euclid(int xi,int yi)
{
x=xi; //分子
y=yi; //分母
}
friend int gcd(Euclid&a,int n,int d);
int mgcd(Euclid&a)
{
return gcd(a,x,y);
}
};
int gcd(Euclid&a,int n,int d)
{
if(d==0) return n;
else
return gcd(a,d,n%d);
}
class fraction
{
public:
fraction(int n,int d){numer=n;denom=d;}//构造函数
fraction(fraction &p){numer=p.numer;denom=p.denom;}//拷贝构造函数
fraction operator + (fraction c2);//重定义+
fraction operator - (fraction c2);//重定义-
fraction operator * (fraction c2);//重定义*
fraction operator / (fraction c2);//重定义/
int getn(){return numer;}//取得分子值
int getd(){return denom;}//取得分母值
private:
int numer;//分子
int denom;//分母
};
fraction fraction::operator + (class fraction c2)
{
numer=numer*c2.denom+denom*c2.numer;
denom=denom*c2.denom;
Euclid m(numer,denom);
numer/=m.mgcd(m);
denom/=m.mgcd(m);
return fraction(numer,denom);
}
fraction fraction::operator - (class fraction c2)
{
numer=numer*c2.denom-denom*c2.numer;
denom=denom*c2.denom;
Euclid m(numer,denom);
numer/=m.mgcd(m);
denom/=m.mgcd(m);
return fraction(numer,denom);
}
fraction fraction::operator * (class fraction c2)
{
numer=numer*c2.numer;
denom=denom*c2.denom;
Euclid m(numer,denom);
numer/=m.mgcd(m);
denom/=m.mgcd(m);
return fraction(numer,denom);
}
fraction fraction::operator / (class fraction c2)
{
numer=numer*c2.denom;
denom=denom*c2.numer;
Euclid m(numer,denom);
numer/=m.mgcd(m);
denom/=m.mgcd(m);
return fraction(numer,denom);
}
void main()
{
int x,y;
cout<<"分子:";
cin>>x;
cout<<"分母:";
cin>>y;
fraction a(x,y);
cout<<"分子:";
cin>>x;
cout<<"分母:";
cin>>y;
fraction b(x,y);
fraction c=a/b;
cout<}
--------------------next---------------------
阅读(991) | 评论(0) | 转发(0) |