
2个回答
展开全部
代码如下,顺便把约分处理也写了。我vc++6.0编译通过。其中假分数没有处理成带分数。
#include <iostream>
using namespace std;
typedef struct{
int a,b;
} frac;
int gcd(int a,int b)
{
if(b==0)return a;
else return gcd(b,a%b);
}
frac operator + (const frac x,const frac y)
{
frac z;
int a,b,t=1;
a=x.b*y.a+x.a*y.b;
b=x.b*y.b;
if(a!=0)t=a<b?gcd(b,a):gcd(a,b);
z.a=a/t;z.b=b/t;
return z;
}
frac operator - (const frac x,const frac y)
{
frac z;
int a,b,t=1;
a=-x.b*y.a+x.a*y.b;
b=x.b*y.b;
if(a!=0)t=a<b?gcd(b,a):gcd(a,b);
z.a=a/t;z.b=b/t;
return z;
}
frac operator * (const frac x,const frac y)
{
frac z;
int a,b,t=1;
a=x.a*y.a;
b=x.b*y.b;
if(a!=0)t=a<b?gcd(b,a):gcd(a,b);
z.a=a/t;z.b=b/t;
return z;
}
frac operator / (const frac x,const frac y)
{
frac z;
int a,b,t=1;
if(y.a==0)throw (-1);
else
{
a=x.a*y.b;
b=x.b*y.a;
if(a!=0)t=a<b?gcd(b,a):gcd(a,b);
z.a=a/t;z.b=b/t;
}
return z;
}
bool operator < (const frac x,const frac y)
{
return(x.a*y.b<x.b*y.a);
}
bool operator > (const frac x,const frac y)
{
return(x.a*y.b>x.b*y.a);
}
bool operator == (const frac x,const frac y)
{
return(x.a*y.b==x.b*y.a);
}
ostream& operator <<(ostream& out, frac x)
{
if(x.a==0)out<<"0";
else if(x.b==1)out<<x.a;
else out<<x.a<<"/"<<x.b;
return out;
}
istream& operator >>(istream& in, frac& x)
{
in>>x.a;getchar();in>>x.b;
if(x.b==0)throw(-1);
return in;
}
int main()
{
cout<<"请按照m/n的形式输入两个分数"<<endl;
frac x,y;
try
{
cin>>x>>y;
if(x<y)cout<<x<<"<"<<y<<endl;
if(x>y)cout<<x<<">"<<y<<endl;
if(x==y)cout<<x<<"="<<y<<endl;
cout<<x<<" + "<<y<<"="<<x+y<<endl;
cout<<x<<" - "<<y<<"="<<x-y<<endl;
cout<<x<<" * "<<y<<"="<<x*y<<endl;
cout<<x<<" / "<<y<<"="<<x/y<<endl;
}
catch(int n)
{
if(n==-1)
cout<<"Invalid"<<endl;
}
system("pause");
return 0;
}
#include <iostream>
using namespace std;
typedef struct{
int a,b;
} frac;
int gcd(int a,int b)
{
if(b==0)return a;
else return gcd(b,a%b);
}
frac operator + (const frac x,const frac y)
{
frac z;
int a,b,t=1;
a=x.b*y.a+x.a*y.b;
b=x.b*y.b;
if(a!=0)t=a<b?gcd(b,a):gcd(a,b);
z.a=a/t;z.b=b/t;
return z;
}
frac operator - (const frac x,const frac y)
{
frac z;
int a,b,t=1;
a=-x.b*y.a+x.a*y.b;
b=x.b*y.b;
if(a!=0)t=a<b?gcd(b,a):gcd(a,b);
z.a=a/t;z.b=b/t;
return z;
}
frac operator * (const frac x,const frac y)
{
frac z;
int a,b,t=1;
a=x.a*y.a;
b=x.b*y.b;
if(a!=0)t=a<b?gcd(b,a):gcd(a,b);
z.a=a/t;z.b=b/t;
return z;
}
frac operator / (const frac x,const frac y)
{
frac z;
int a,b,t=1;
if(y.a==0)throw (-1);
else
{
a=x.a*y.b;
b=x.b*y.a;
if(a!=0)t=a<b?gcd(b,a):gcd(a,b);
z.a=a/t;z.b=b/t;
}
return z;
}
bool operator < (const frac x,const frac y)
{
return(x.a*y.b<x.b*y.a);
}
bool operator > (const frac x,const frac y)
{
return(x.a*y.b>x.b*y.a);
}
bool operator == (const frac x,const frac y)
{
return(x.a*y.b==x.b*y.a);
}
ostream& operator <<(ostream& out, frac x)
{
if(x.a==0)out<<"0";
else if(x.b==1)out<<x.a;
else out<<x.a<<"/"<<x.b;
return out;
}
istream& operator >>(istream& in, frac& x)
{
in>>x.a;getchar();in>>x.b;
if(x.b==0)throw(-1);
return in;
}
int main()
{
cout<<"请按照m/n的形式输入两个分数"<<endl;
frac x,y;
try
{
cin>>x>>y;
if(x<y)cout<<x<<"<"<<y<<endl;
if(x>y)cout<<x<<">"<<y<<endl;
if(x==y)cout<<x<<"="<<y<<endl;
cout<<x<<" + "<<y<<"="<<x+y<<endl;
cout<<x<<" - "<<y<<"="<<x-y<<endl;
cout<<x<<" * "<<y<<"="<<x*y<<endl;
cout<<x<<" / "<<y<<"="<<x/y<<endl;
}
catch(int n)
{
if(n==-1)
cout<<"Invalid"<<endl;
}
system("pause");
return 0;
}
2011-07-06
展开全部
#include <iostream.h>
class complex
{
public:
complex()
complex(double r, double i)
{
real = r, imag = i;
}
complex operator +(const complex &c);
complex operator -(const complex &c);
complex operator *(const complex &c);
complex operator /(const complex &c);
friend void print(const complex &c);
private:
double real, imag;
};
inline complex complex::operator +(const complex &c)
{
return complex(real + c.real, imag + c.imag);
}
inline complex complex::operator -(const complex &c)
{
return complex(real - c.real, imag - c.imag);
}
inline complex complex::operator *(const complex &c)
{
return complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real);
}
inline complex complex::operator /(const complex &c)
{
return complex((real * c.real + imag + c.imag) / (c.real * c.real + c.imag * c.imag),
(imag * c.real - real * c.imag) / (c.real * c.real + c.imag * c.imag));
}
void print(const complex &c)
{
if(c.imag<0)
cout<<c.real<<c.imag<<'i';
else
cout<<c.real<<'+'<<c.imag<<'i';
}
void main()
{
complex c1(2.0, 3.0), c2(4.0, -2.0), c3;
c3 = c1 + c2;
cout<<"\nc1+c2=";
print(c3);
c3 = c1 - c2;
cout<<"\nc1-c2=";
print(c3);
c3 = c1 * c2;
cout<<"\nc1*c2=";
print(c3);
c3 = c1 / c2;
cout<<"\nc1/c2=";
print(c3);
c3 = (c1+c2) * (c1-c2) * c2/c1;
cout<<"\n(c1+c2)*(c1-c2)*c2/c1=";
print(c3);
cout<<endl;
}
另外,站长团上有产品团购,便宜有保证
class complex
{
public:
complex()
complex(double r, double i)
{
real = r, imag = i;
}
complex operator +(const complex &c);
complex operator -(const complex &c);
complex operator *(const complex &c);
complex operator /(const complex &c);
friend void print(const complex &c);
private:
double real, imag;
};
inline complex complex::operator +(const complex &c)
{
return complex(real + c.real, imag + c.imag);
}
inline complex complex::operator -(const complex &c)
{
return complex(real - c.real, imag - c.imag);
}
inline complex complex::operator *(const complex &c)
{
return complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real);
}
inline complex complex::operator /(const complex &c)
{
return complex((real * c.real + imag + c.imag) / (c.real * c.real + c.imag * c.imag),
(imag * c.real - real * c.imag) / (c.real * c.real + c.imag * c.imag));
}
void print(const complex &c)
{
if(c.imag<0)
cout<<c.real<<c.imag<<'i';
else
cout<<c.real<<'+'<<c.imag<<'i';
}
void main()
{
complex c1(2.0, 3.0), c2(4.0, -2.0), c3;
c3 = c1 + c2;
cout<<"\nc1+c2=";
print(c3);
c3 = c1 - c2;
cout<<"\nc1-c2=";
print(c3);
c3 = c1 * c2;
cout<<"\nc1*c2=";
print(c3);
c3 = c1 / c2;
cout<<"\nc1/c2=";
print(c3);
c3 = (c1+c2) * (c1-c2) * c2/c1;
cout<<"\n(c1+c2)*(c1-c2)*c2/c1=";
print(c3);
cout<<endl;
}
另外,站长团上有产品团购,便宜有保证
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询