(2)有理数的功能界面:
assign 设置有理数表示的分子和分母
= 有理数的赋值
X= 有理数的复合赋值(+、-、*、/)
关系运算 ==、!=、>、>=、<、<=
算术运算 +、-、*、/
real 有理数转换为浮点数
输入输出 >>和<<
simplify 有理数的化简(分子、分母的最大公约数gcd;同除gcd;考虑负
号处理)
3 个解决方案
#1
用C++实现有理数类,在大多数据结构教材中都能找到:)
#2
我这里没有数据结构的书籍,不知哪里能找到源码,呵呵
#3
源码来了!有问题就说吧 :) (给我加点分吧)
#include <assert.h>
#include <iostream.h>
//最大公约数函数
int gcd(int m,int n){
int r;
if(m==0&&n==0) return 0;
if(m<0) m=-m;
if(n<0) n=-n;
if(m==0) return n;
if(n==0) return m;
r=m%n;
while(r!=0){
m=n;
n=r;
r=m%n;}
return n;}
//符号函数
int sign(int value){
if(value>0) return 1;
else if(value==0) return 0;
else return -1;}
//有理数类的规范说明
class rational{
public:
//构造函数
rational();
rational(int);
rational(int,int);
rational(const rational &);
//访问函数
int num() const;
int den() const;
//赋值函数
void operator =(const rational &);
void operator +=(const rational &);
void operator -=(const rational &);
void operator *=(const rational &);
void operator /=(const rational &);
//转换函数
operator float() const;
//比较
int compare(const rational &)const;
private:
//数据域
int numerator;
int denominator;
//正规化函数(约分)
void normalize();
};
//访问函数
int rational::num()const{return numerator;}
int rational::den()const{return denominator;}
//构造函数
rational::rational():numerator(0),denominator(1){}
rational::rational(int num):numerator(num),denominator(1){}
rational::rational(int num,int den):numerator(num),denominator(den)
{normalize();}
rational::rational(const rational &value):
numerator(value.numerator),denominator(value.denominator){}
//赋值函数
void rational::operator =(const rational & right){
numerator=right.numerator;
denominator=right.denominator;}
void rational::operator +=(const rational & right){
numerator=numerator*right.denominator+denominator*right.numerator;
denominator*=right.denominator;
normalize();}
void rational::operator -=(const rational & right){
numerator=numerator*right.denominator-denominator*right.numerator;
denominator*=right.denominator;
normalize();}
void rational::operator *=(const rational & right){
numerator*=right.numerator;
denominator*=right.denominator;
normalize();}
void rational::operator /=(const rational & right){
int temp=right.numerator;
numerator*=right.denominator;
denominator*=temp;
normalize();}
//转换函数
rational::operator float() const{return (float)(numerator/denominator);}
//正规化函数
void rational::normalize(){
int signal=sign(numerator)*sign(denominator);
assert(denominator!=0);
int d=gcd(numerator,denominator);
numerator=signal*(numerator/d);
denominator=denominator/d;}
//普通运算符
rational operator +(const rational &a,const rational &b){
return rational(a.num()*b.den()+a.den()*b.num(),a.den()*b.den());}
rational operator -(const rational &a,const rational &b){
return rational(a.num()*b.den()-a.den()*b.num(),a.den()*b.den());}
rational operator *(const rational &a,const rational &b){
return rational(a.num()*b.num(),a.den()*b.den());}
rational operator /(const rational &a,const rational &b){
return rational(a.num()*b.den(),a.den()*b.num());}
rational operator -(const rational &a){
return rational(-a.num(),a.den());}
//比较
int rational::compare(const rational & right) const{
return (numerator*right.den()-denominator*right.num())
*sign(denominator)*sign(right.den());}
int operator ==(const rational & left,const rational & right){
return left.compare(right)==0;}
int operator !=(const rational & left,const rational & right) {
return left.compare(right)!=0;}
int operator >=(const rational & left,const rational & right){
return left.compare(right)>=0;}
int operator <=(const rational & left,const rational & right){
return left.compare(right)<=0;}
int operator >(const rational & left,const rational & right){
return left.compare(right)>0;}
int operator <(const rational & left,const rational & right){
return left.compare(right)<0;}
//有理数流的输出
ostream & operator <<(ostream & out,const rational & value){
out<<value.num()<<'/'<<value.den();
return out;}
//有理数流的输入
istream & operator >>(istream & in,rational &r){
int a,b;
in>>a;
char c;
in>>c;
if(c=='/')
in>>b;
else{
in.putback(c);
b=1;}
rational newValue(a,b);
r=newValue;
return in;}
//有理数的绝对值
rational abs(const rational & rat){
int numerator=rat.num();
if(numerator<0) numerator=-numerator;
return rational(numerator,rat.den());
}
void main(){return;}
#include <assert.h>
#include <iostream.h>
//最大公约数函数
int gcd(int m,int n){
int r;
if(m==0&&n==0) return 0;
if(m<0) m=-m;
if(n<0) n=-n;
if(m==0) return n;
if(n==0) return m;
r=m%n;
while(r!=0){
m=n;
n=r;
r=m%n;}
return n;}
//符号函数
int sign(int value){
if(value>0) return 1;
else if(value==0) return 0;
else return -1;}
//有理数类的规范说明
class rational{
public:
//构造函数
rational();
rational(int);
rational(int,int);
rational(const rational &);
//访问函数
int num() const;
int den() const;
//赋值函数
void operator =(const rational &);
void operator +=(const rational &);
void operator -=(const rational &);
void operator *=(const rational &);
void operator /=(const rational &);
//转换函数
operator float() const;
//比较
int compare(const rational &)const;
private:
//数据域
int numerator;
int denominator;
//正规化函数(约分)
void normalize();
};
//访问函数
int rational::num()const{return numerator;}
int rational::den()const{return denominator;}
//构造函数
rational::rational():numerator(0),denominator(1){}
rational::rational(int num):numerator(num),denominator(1){}
rational::rational(int num,int den):numerator(num),denominator(den)
{normalize();}
rational::rational(const rational &value):
numerator(value.numerator),denominator(value.denominator){}
//赋值函数
void rational::operator =(const rational & right){
numerator=right.numerator;
denominator=right.denominator;}
void rational::operator +=(const rational & right){
numerator=numerator*right.denominator+denominator*right.numerator;
denominator*=right.denominator;
normalize();}
void rational::operator -=(const rational & right){
numerator=numerator*right.denominator-denominator*right.numerator;
denominator*=right.denominator;
normalize();}
void rational::operator *=(const rational & right){
numerator*=right.numerator;
denominator*=right.denominator;
normalize();}
void rational::operator /=(const rational & right){
int temp=right.numerator;
numerator*=right.denominator;
denominator*=temp;
normalize();}
//转换函数
rational::operator float() const{return (float)(numerator/denominator);}
//正规化函数
void rational::normalize(){
int signal=sign(numerator)*sign(denominator);
assert(denominator!=0);
int d=gcd(numerator,denominator);
numerator=signal*(numerator/d);
denominator=denominator/d;}
//普通运算符
rational operator +(const rational &a,const rational &b){
return rational(a.num()*b.den()+a.den()*b.num(),a.den()*b.den());}
rational operator -(const rational &a,const rational &b){
return rational(a.num()*b.den()-a.den()*b.num(),a.den()*b.den());}
rational operator *(const rational &a,const rational &b){
return rational(a.num()*b.num(),a.den()*b.den());}
rational operator /(const rational &a,const rational &b){
return rational(a.num()*b.den(),a.den()*b.num());}
rational operator -(const rational &a){
return rational(-a.num(),a.den());}
//比较
int rational::compare(const rational & right) const{
return (numerator*right.den()-denominator*right.num())
*sign(denominator)*sign(right.den());}
int operator ==(const rational & left,const rational & right){
return left.compare(right)==0;}
int operator !=(const rational & left,const rational & right) {
return left.compare(right)!=0;}
int operator >=(const rational & left,const rational & right){
return left.compare(right)>=0;}
int operator <=(const rational & left,const rational & right){
return left.compare(right)<=0;}
int operator >(const rational & left,const rational & right){
return left.compare(right)>0;}
int operator <(const rational & left,const rational & right){
return left.compare(right)<0;}
//有理数流的输出
ostream & operator <<(ostream & out,const rational & value){
out<<value.num()<<'/'<<value.den();
return out;}
//有理数流的输入
istream & operator >>(istream & in,rational &r){
int a,b;
in>>a;
char c;
in>>c;
if(c=='/')
in>>b;
else{
in.putback(c);
b=1;}
rational newValue(a,b);
r=newValue;
return in;}
//有理数的绝对值
rational abs(const rational & rat){
int numerator=rat.num();
if(numerator<0) numerator=-numerator;
return rational(numerator,rat.den());
}
void main(){return;}
#1
用C++实现有理数类,在大多数据结构教材中都能找到:)
#2
我这里没有数据结构的书籍,不知哪里能找到源码,呵呵
#3
源码来了!有问题就说吧 :) (给我加点分吧)
#include <assert.h>
#include <iostream.h>
//最大公约数函数
int gcd(int m,int n){
int r;
if(m==0&&n==0) return 0;
if(m<0) m=-m;
if(n<0) n=-n;
if(m==0) return n;
if(n==0) return m;
r=m%n;
while(r!=0){
m=n;
n=r;
r=m%n;}
return n;}
//符号函数
int sign(int value){
if(value>0) return 1;
else if(value==0) return 0;
else return -1;}
//有理数类的规范说明
class rational{
public:
//构造函数
rational();
rational(int);
rational(int,int);
rational(const rational &);
//访问函数
int num() const;
int den() const;
//赋值函数
void operator =(const rational &);
void operator +=(const rational &);
void operator -=(const rational &);
void operator *=(const rational &);
void operator /=(const rational &);
//转换函数
operator float() const;
//比较
int compare(const rational &)const;
private:
//数据域
int numerator;
int denominator;
//正规化函数(约分)
void normalize();
};
//访问函数
int rational::num()const{return numerator;}
int rational::den()const{return denominator;}
//构造函数
rational::rational():numerator(0),denominator(1){}
rational::rational(int num):numerator(num),denominator(1){}
rational::rational(int num,int den):numerator(num),denominator(den)
{normalize();}
rational::rational(const rational &value):
numerator(value.numerator),denominator(value.denominator){}
//赋值函数
void rational::operator =(const rational & right){
numerator=right.numerator;
denominator=right.denominator;}
void rational::operator +=(const rational & right){
numerator=numerator*right.denominator+denominator*right.numerator;
denominator*=right.denominator;
normalize();}
void rational::operator -=(const rational & right){
numerator=numerator*right.denominator-denominator*right.numerator;
denominator*=right.denominator;
normalize();}
void rational::operator *=(const rational & right){
numerator*=right.numerator;
denominator*=right.denominator;
normalize();}
void rational::operator /=(const rational & right){
int temp=right.numerator;
numerator*=right.denominator;
denominator*=temp;
normalize();}
//转换函数
rational::operator float() const{return (float)(numerator/denominator);}
//正规化函数
void rational::normalize(){
int signal=sign(numerator)*sign(denominator);
assert(denominator!=0);
int d=gcd(numerator,denominator);
numerator=signal*(numerator/d);
denominator=denominator/d;}
//普通运算符
rational operator +(const rational &a,const rational &b){
return rational(a.num()*b.den()+a.den()*b.num(),a.den()*b.den());}
rational operator -(const rational &a,const rational &b){
return rational(a.num()*b.den()-a.den()*b.num(),a.den()*b.den());}
rational operator *(const rational &a,const rational &b){
return rational(a.num()*b.num(),a.den()*b.den());}
rational operator /(const rational &a,const rational &b){
return rational(a.num()*b.den(),a.den()*b.num());}
rational operator -(const rational &a){
return rational(-a.num(),a.den());}
//比较
int rational::compare(const rational & right) const{
return (numerator*right.den()-denominator*right.num())
*sign(denominator)*sign(right.den());}
int operator ==(const rational & left,const rational & right){
return left.compare(right)==0;}
int operator !=(const rational & left,const rational & right) {
return left.compare(right)!=0;}
int operator >=(const rational & left,const rational & right){
return left.compare(right)>=0;}
int operator <=(const rational & left,const rational & right){
return left.compare(right)<=0;}
int operator >(const rational & left,const rational & right){
return left.compare(right)>0;}
int operator <(const rational & left,const rational & right){
return left.compare(right)<0;}
//有理数流的输出
ostream & operator <<(ostream & out,const rational & value){
out<<value.num()<<'/'<<value.den();
return out;}
//有理数流的输入
istream & operator >>(istream & in,rational &r){
int a,b;
in>>a;
char c;
in>>c;
if(c=='/')
in>>b;
else{
in.putback(c);
b=1;}
rational newValue(a,b);
r=newValue;
return in;}
//有理数的绝对值
rational abs(const rational & rat){
int numerator=rat.num();
if(numerator<0) numerator=-numerator;
return rational(numerator,rat.den());
}
void main(){return;}
#include <assert.h>
#include <iostream.h>
//最大公约数函数
int gcd(int m,int n){
int r;
if(m==0&&n==0) return 0;
if(m<0) m=-m;
if(n<0) n=-n;
if(m==0) return n;
if(n==0) return m;
r=m%n;
while(r!=0){
m=n;
n=r;
r=m%n;}
return n;}
//符号函数
int sign(int value){
if(value>0) return 1;
else if(value==0) return 0;
else return -1;}
//有理数类的规范说明
class rational{
public:
//构造函数
rational();
rational(int);
rational(int,int);
rational(const rational &);
//访问函数
int num() const;
int den() const;
//赋值函数
void operator =(const rational &);
void operator +=(const rational &);
void operator -=(const rational &);
void operator *=(const rational &);
void operator /=(const rational &);
//转换函数
operator float() const;
//比较
int compare(const rational &)const;
private:
//数据域
int numerator;
int denominator;
//正规化函数(约分)
void normalize();
};
//访问函数
int rational::num()const{return numerator;}
int rational::den()const{return denominator;}
//构造函数
rational::rational():numerator(0),denominator(1){}
rational::rational(int num):numerator(num),denominator(1){}
rational::rational(int num,int den):numerator(num),denominator(den)
{normalize();}
rational::rational(const rational &value):
numerator(value.numerator),denominator(value.denominator){}
//赋值函数
void rational::operator =(const rational & right){
numerator=right.numerator;
denominator=right.denominator;}
void rational::operator +=(const rational & right){
numerator=numerator*right.denominator+denominator*right.numerator;
denominator*=right.denominator;
normalize();}
void rational::operator -=(const rational & right){
numerator=numerator*right.denominator-denominator*right.numerator;
denominator*=right.denominator;
normalize();}
void rational::operator *=(const rational & right){
numerator*=right.numerator;
denominator*=right.denominator;
normalize();}
void rational::operator /=(const rational & right){
int temp=right.numerator;
numerator*=right.denominator;
denominator*=temp;
normalize();}
//转换函数
rational::operator float() const{return (float)(numerator/denominator);}
//正规化函数
void rational::normalize(){
int signal=sign(numerator)*sign(denominator);
assert(denominator!=0);
int d=gcd(numerator,denominator);
numerator=signal*(numerator/d);
denominator=denominator/d;}
//普通运算符
rational operator +(const rational &a,const rational &b){
return rational(a.num()*b.den()+a.den()*b.num(),a.den()*b.den());}
rational operator -(const rational &a,const rational &b){
return rational(a.num()*b.den()-a.den()*b.num(),a.den()*b.den());}
rational operator *(const rational &a,const rational &b){
return rational(a.num()*b.num(),a.den()*b.den());}
rational operator /(const rational &a,const rational &b){
return rational(a.num()*b.den(),a.den()*b.num());}
rational operator -(const rational &a){
return rational(-a.num(),a.den());}
//比较
int rational::compare(const rational & right) const{
return (numerator*right.den()-denominator*right.num())
*sign(denominator)*sign(right.den());}
int operator ==(const rational & left,const rational & right){
return left.compare(right)==0;}
int operator !=(const rational & left,const rational & right) {
return left.compare(right)!=0;}
int operator >=(const rational & left,const rational & right){
return left.compare(right)>=0;}
int operator <=(const rational & left,const rational & right){
return left.compare(right)<=0;}
int operator >(const rational & left,const rational & right){
return left.compare(right)>0;}
int operator <(const rational & left,const rational & right){
return left.compare(right)<0;}
//有理数流的输出
ostream & operator <<(ostream & out,const rational & value){
out<<value.num()<<'/'<<value.den();
return out;}
//有理数流的输入
istream & operator >>(istream & in,rational &r){
int a,b;
in>>a;
char c;
in>>c;
if(c=='/')
in>>b;
else{
in.putback(c);
b=1;}
rational newValue(a,b);
r=newValue;
return in;}
//有理数的绝对值
rational abs(const rational & rat){
int numerator=rat.num();
if(numerator<0) numerator=-numerator;
return rational(numerator,rat.den());
}
void main(){return;}