2020-04-09 17:44

# 这是我找到的一个代码，这个问题怎么处理好

calculate.h
#include
class Integer//整数类
{
protected:
int fz;
intfm;
public:
Integer(int a=0,int b=1):fz(a),fm(b){}
~Integer(){}
void display();

};
class Fraction:public Integer
{
public:
Fraction(int a=0,int b=1):Integer(a,b){}
friend istream & operator>>(istream & ,Fraction & );//重载输入流
friend ostream & operator<<(ostream & ,Fraction & );
Fraction operator+(Fraction &c);
Fraction operator+(int n);
friend Fraction operator-(int n,Fraction &c);
Fraction operator*(Fraction &c);
Fraction operator*(int n);
Fraction operator/(Fraction &c);
Fraction operator/(int n);
friend Fraction operator/(int n,Fraction &c);
Fraction predigest();
void display();
};
calculate.cpp
#include
#include"calculate.h"
void Integer::display()
{
cout< cout cout cout cout cout cout }
Fraction Fraction::predigest()
{
int n,d,t,sign=1;
Fraction temp;
if(fz {
fz=-fz;
fm=-fm;
}
if(fz>0 && fm {
fm=-fm;
sign=-1;
}
if(fz0)
{
fz=-fz;
sign=-1;
}
n=fz;
d=fm;

``````int r;
if(n<d)
{
t=n;n=d;d=t;
}
r=n%d;
while(r!=0)
{
n=d;
d=r;
r=n%d;
}
temp.fz=sign*(fz/d);
temp.fm=fm/d;
return temp;
``````

}
Fraction Fraction::operator+(Fraction &c)
{
Fraction temp;
temp.fz=fz*c.fm+fm*c.fz;
temp.fm=fm*c.fm;
return temp;
}
Fraction Fraction::operator+(int n)
{
Fraction temp;
temp.fz=fz+fm*n;
temp.fm=fm;
return temp;
}
Fraction operator-(int n,Fraction &c)
{
Fraction temp;
temp.fz=c.fm*n-c.fz;
temp.fm=c.fm;
return temp;
}
Fraction Fraction::operator-(int n)
{
Fraction temp;
temp.fz=fz-fm*n;
temp.fm=fm;
return temp;
}
Fraction Fraction::operator-(Fraction &c)
{
Fraction temp;
temp.fz=fz*c.fm-fm*c.fz;
temp.fm=fm*c.fm;
return temp;
}
Fraction Fraction::operator*(Fraction &c)
{
Fraction temp;
temp.fz=fz*c.fz;
temp.fm=fm*c.fm;
return temp;

}
Fraction Fraction::operator*(int n)
{
Fraction temp;
temp.fz=fz*n;
temp.fm=fm;
return temp;
}
Fraction Fraction::operator/(Fraction &c)
{
Fraction temp;
temp.fz=fz*c.fm;
temp.fm=fm*c.fz;
return temp;
}
Fraction Fraction::operator/(int n)
{
Fraction temp;
temp.fz=fz;
temp.fm=fm*n;
return temp;
}
Fraction operator/(int n,Fraction &c)
{
Fraction temp;
temp.fz=n*c.fm;
temp.fm=c.fz;
return temp;
}
ostream&operator<<(ostream &output,Fraction &t)
{
int a,b,c;
a=t.fz;
b=t.fm;
if(a==0||b==0)
cout<<"0";
if(b==1)
cout< if(a>b&&b!=1)
{
c=a/b;
a=a%b;
cout< if(a!=0)
cout }
if(ab&&b!=1)
{
c=-a/b;
a=-a&b;
c=-c;
cout<<'='< if(a!=0)
cout }
if(t.fz>0&&t.fz output if(t.fz output return output;
}
istream&operator {
int a,c;
char b;
input>>a>>b>>C;
if(b=='/'&&c!=0)
{
t.fz=a;
t.fm=c;
}
else
cout<<"错误！请退出重新输入！"< return input;
}
int main()
{
Integer dis;
Fraction a,b,result,real;
int m,z;
char c;
dis.display();
while(m!=0)
{
cout cin>>m;
if(m==1)
{
cout< cin>>a;
cout< cin>>c;
cout< cin>>z;
if(c=='+')
result=a+z;
if(c=='-')
result=a-z;
if(c=='*')
result=a*z;
if(c=='/')
result=a/z;
real=result.predigest();
cout<<"运算式为："<
cout }
if(m==2)
{
cout cout cin>>z;
cout< cin>>c;
cout< cin>>b;
if(c=='+')
result=b+z;
if(c=='-')
result=z-b;
if(c=='*')
result=b*z;
if(c=='/')
result=z/b;
real=result.predigest();
cout<<"运算式为："< cout }
if(m==3)
{
cout cout cin>>a;
cout< cin>>c;
cout< cin>>b;
if(c=='+')
result=b+a;
if(c=='-')
result=a-b;
if(c=='*')
result=b*a;
if(c=='/')
result=a/b;
real=result.predigest();
cout<<"运算式为："<<a<<c<<b<<'=' <<a.predigest()<<c<<b.predigest()<<'='<<real<<endl;
cout<<endl;
}
}
return 0;
}

• 点赞
• 写回答
• 关注问题
• 收藏
• 复制链接分享
• 邀请回答

#### 1条回答

• franzhong 1年前

你把多个cpp内容拷贝到一个文件里面了，第30行的意思是，以下内容为calculate.cpp文件中的内容，前面是.h的头文件，你要弄两个文件，有包含关系的，名称要起一致

点赞 2 评论 复制链接分享