成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

C語(yǔ)言構(gòu)造有理數(shù)的函數(shù) c++有理數(shù)

求高手解答,用C++設(shè)計(jì)一個(gè)有理數(shù)運(yùn)算

#includeiostream

公司主營(yíng)業(yè)務(wù):網(wǎng)站制作、成都網(wǎng)站制作、移動(dòng)網(wǎng)站開(kāi)發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。成都創(chuàng)新互聯(lián)公司是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開(kāi)放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。成都創(chuàng)新互聯(lián)公司推出珙縣免費(fèi)做網(wǎng)站回饋大家。

using namespace std;

//numerator 分子

//denominator分母

int gcd(int m,int n) //求最大公約數(shù)函數(shù),用于約分

{

if(n==0)return m;

return gcd(n,m%n);

}

class fraction

{

private:

int numer;

int denom;

public:

fraction(int n=0,int d=1)//帶默認(rèn)參數(shù)的構(gòu)造函數(shù),整數(shù)與分?jǐn)?shù)的運(yùn)算迎刃而解

{

numer=n;

denom=d;

}

void rev(fraction a){numer=a.denom;denom=a.numer;}//倒數(shù)

friend ostream operator(std::ostream, const fraction); //重載輸出

friend istream operator(std::istream, fraction); //重載輸入

friend bool operator==(const fraction, const fraction); //判斷有理數(shù)是否相等,以下類似

friend bool operator(const fraction, const fraction);

friend bool operator(const fraction, const fraction);

friend bool operator=(const fraction, const fraction);

friend bool operator=(const fraction, const fraction);

friend fraction operator +(const fraction,const fraction);//重載運(yùn)算符

friend fraction operator -(const fraction,const fraction);

friend fraction operator *(const fraction,const fraction);

friend fraction operator /(const fraction,const fraction);

friend bool operator!=(const fraction, const fraction);

};

fraction operator +(const fraction a,const fraction b)

{

fraction p;

p.numer=a.numer*b.denom+b.numer*a.denom;

p.denom=a.denom*b.denom;

return p;

}

fraction operator -(const fraction a,const fraction b)

{

fraction p;

p.numer=a.numer*b.denom-b.numer*a.denom;

p.denom=a.denom*b.denom;

return p;

}

fraction operator *(const fraction a,const fraction b)

{

fraction p;

p.numer=a.numer*b.numer;

p.denom=a.denom*b.denom;

return p;

}

fraction operator /(const fraction a,const fraction b)

{

fraction p;

p.numer=a.numer*b.denom;

p.denom=a.denom*b.numer;

return p;

}

ostream operator(ostream output,const fraction num)

{

int a=gcd(num.numer,num.denom);

if((num.denom/a)==1)

outputnum.numer/a;

else if(num.denom/a0)

output-num.numer/a"/"-num.denom/a;

else outputnum.numer/a"/"num.denom/a;

return output;

}

istream operator(istreaminput, fraction num)

{

inputnum.numernum.denom;

return input;

}

bool operator==(const fractionnum1, const fractionnum2)

{

return (num1.numer*num2.denom)==(num2.numer*num1.denom);

}

bool operator!=(const fraction num1, const fraction num2)

{

return (num1.numer*num2.denom)!=(num2.numer*num1.denom);

}

bool operator(const fractiona, const fractionb)

{

return (a.numer*b.denom)(b.numer*a.denom);

}

bool operator(const fractiona, const fractionb)

{

return (a.numer*b.denom)(b.numer*a.denom);

}

bool operator=(const fractiona, const fractionb)

{

return (a.numer*b.denom)=(b.numer*a.denom);

}

bool operator=(const fractiona, const fractionb)

{

return (a.numer*b.denom)=(b.numer*a.denom);

}

int main()

{

fraction zrf(1,7),ssh(26,65);

fraction p;

p.rev(ssh);

cout"ssh="ssh"\n1/ssh="pendl;

cout"zrf="zrf"\nssh="ssh'\n' ;

cout"(zrf==ssh)="(zrf==ssh)"\n(zrfssh)="(zrfssh)endl ;

cout"zrf+ssh="zrf+sshendl;

cout"zrf*ssh="zrf*sshendl;

cout"ssh-zrf="ssh-zrfendl;

cout"zrf-ssh="zrf-sshendl;

cout"zrf/ssh="zrf/sshendl;

return 0;

}

c++定義一個(gè)有理數(shù)類,實(shí)現(xiàn)有理數(shù)的加減乘除以及化簡(jiǎn)成最簡(jiǎn)分?jǐn)?shù)等,要代碼。問(wèn)題解決了還會(huì)加分。

//Rational.h

#ifndef RATIONAl_CLASS

#define RATIONAl_CLASS

class Rational

{

private:

int P,Q;

void Standardize(); //將有理數(shù)化為標(biāo)準(zhǔn)形式

int gcd (int m,int n); //求兩個(gè)數(shù)的最大公約數(shù)

public:

Rational (int p=0, int q=1); //構(gòu)造有理數(shù)分?jǐn)?shù),分子默認(rèn)為0,分母默認(rèn)為1

Rational (Rational r); //拷貝構(gòu)造函數(shù)

Rational (double ra); //構(gòu)造函數(shù),將實(shí)數(shù)轉(zhuǎn)換為標(biāo)準(zhǔn)形式的有理數(shù)

virtual ~Rational (void); //虛析構(gòu)函數(shù)

void Reduce (); //將分?jǐn)?shù)化為最簡(jiǎn)形式

int GetNumerator(); //取分?jǐn)?shù)的分子

int GetDenominator(); //取分?jǐn)?shù)的分母

Rational operator= (Rational r); //將一個(gè)有理數(shù)對(duì)象賦值給另一個(gè)對(duì)象

Rational operator= (double ra); //將一個(gè)實(shí)數(shù)賦值給一個(gè)有理數(shù)對(duì)象

Rational operator- (); //將一個(gè)有理數(shù)取反

friend double Float(Rational r); //強(qiáng)制轉(zhuǎn)換一個(gè)有理數(shù)對(duì)象為一個(gè)實(shí)數(shù)

//以下為兩個(gè)有理數(shù)對(duì)象、一個(gè)有理數(shù)對(duì)象和一個(gè)實(shí)數(shù)、一個(gè)實(shí)數(shù)和一個(gè)有理數(shù)對(duì)象

//之間的關(guān)系運(yùn)算符的重載

int operator== (Rational r) const;

int operator== (double ra) const;

friend int operator== (double ra, Rational r);

int operator!= (Rational r) const;

int operator!= (double ra) const;

friend int operator!= (double ra, Rational r);

int operator (Rational r) const;

int operator (double ra) const;

friend int operator (double ra, Rational r);

int operator= (Rational r) const;

int operator= (double ra) const;

friend int operator= (double ra, Rational r);

int operator (Rational r) const;

int operator (double ra) const;

friend int operator (double ra, Rational r);

int operator= (Rational r) const;

int operator= (double ra) const;

friend int operator= (double ra, Rational r);

//以下兩個(gè)有理數(shù)對(duì)象、一個(gè)有理數(shù)對(duì)象和一個(gè)實(shí)數(shù)

//之間的普通四則運(yùn)算的重載

Rational operator+ (Rational r) const;

Rational operator+ (double ra) const;

void operator+= (Rational r);

void operator+= (double ra);

Rational operator- (Rational r) const;

Rational operator- (double ra) const;

void operator-= (Rational r);

void operator-= (double ra);

Rational operator* (Rational r) const;

Rational operator* (double ra) const;

void operator*= (Rational r);

void operator*= (double ra);

Rational operator/ (Rational r) const;

Rational operator/ (double ra) const;

void operator/= (Rational r);

void operator/= (double ra);

//輸入輸出流的重載,輸出為最簡(jiǎn)形式

friend ostream operator(ostream output,Rational obj);

friend istream operator(istream input,Rational obj);

int Iegal(void) const; //判斷有理數(shù)對(duì)象的值是否合法

};

//一個(gè)實(shí)數(shù)和一個(gè)有理數(shù)對(duì)象之間的四則運(yùn)算重載

Rational operator+ (double ra, Rational r);

Rational operator- (double ra, Rational r);

Rational operator* (double ra, Rational r);

Rational operator/ (double ra, Rational r);

void PrintMixedNumber(Rational X); //將一個(gè)有理數(shù)對(duì)象轉(zhuǎn)換為帶分?jǐn)?shù)形式

Rational SolveEquation(Rational a,Rational b,Rational c); //求解一般的分?jǐn)?shù)方程

#endif

二、函數(shù)實(shí)現(xiàn)部分:

//Rational.cpp

#include iostream

using namespace std;

#include "Rational.h"

void Rational::Standardize () //將有理數(shù)化為標(biāo)準(zhǔn)形式

{

if(Q == 0)

{

cout"Error!"endl;

exit(1);

}

else

if(Q 0)

{

P= -P;

Q= -Q;

}

}

int Rational::gcd (int m,int n) //求兩個(gè)數(shù)的最大公約數(shù)

{

int i,t;

if(m==0 || n==0)

{

cout"Error!"endl;

exit(1);

}

if(nm || n-m)

{

t=n;

n=m;

m=t;

}

i=m%n;

if(i!=0)

do

{

m=n;

n=i;

i=m%n;

}while(m%n!=0);

return ((n0)? n : -n);

}

Rational::Rational (int p, int q) //構(gòu)造有理數(shù)分?jǐn)?shù),分子默認(rèn)為0,分母默認(rèn)為1

{

P=p;

Q=q;

Standardize();

}

Rational::Rational (Rational r) //拷貝構(gòu)造函數(shù)

{

P=r.P;

Q=r.Q;

Standardize();

}

Rational::Rational (double ra) //構(gòu)造函數(shù),將實(shí)數(shù)轉(zhuǎn)換為標(biāo)準(zhǔn)形式的有理數(shù)

{

int base=1;

while(ra-(int)ra !=0)

{

ra=ra*10;

base=base*10;

}

P=ra;

Q=base;

}

Rational::~Rational (void) //虛析構(gòu)函數(shù)

{}

void Rational::Reduce () //將分?jǐn)?shù)化為最簡(jiǎn)形式

{

int max=gcd(P,Q);

P= P/max;

Q= Q/max;

}

int Rational::GetNumerator() //取分?jǐn)?shù)的分子

{

return P;

}

int Rational::GetDenominator() //取分?jǐn)?shù)的分母

{

return Q;

}

Rational Rational::operator= (Rational r) //將一個(gè)有理數(shù)對(duì)象賦值給另一個(gè)對(duì)象

{

P=r.GetNumerator();

Q=r.GetDenominator();

return *this;

}

Rational Rational::operator= (double ra) //將一個(gè)實(shí)數(shù)賦值給一個(gè)有理數(shù)對(duì)象

{

Rational temp(ra);

*this=temp;

return *this;

}

Rational Rational::operator- () //將一個(gè)有理數(shù)取反

{

Rational temp(-GetNumerator(),GetDenominator());

return temp;

}

double Float(Rational r) //強(qiáng)制轉(zhuǎn)換一個(gè)有理數(shù)對(duì)象為一個(gè)實(shí)數(shù)

{

return (1.0*r.GetNumerator()/r.GetDenominator());

}

//以下為兩個(gè)有理數(shù)對(duì)象、一個(gè)有理數(shù)對(duì)象和一個(gè)實(shí)數(shù)、一個(gè)實(shí)數(shù)和一個(gè)有理數(shù)對(duì)象

//之間的關(guān)系運(yùn)算符的重載

int Rational::operator== (Rational r) const

{

int r_p=r.GetNumerator(), r_q=r.GetDenominator();

return (P*r_q==r_p*Q);

}

int Rational::operator== (double ra) const

{

Rational temp(ra);

int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();

return (P*temp_q==temp_p*Q);

}

int operator== (double ra, Rational r)

{

Rational temp(ra);

int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();

int r_p=r.GetNumerator(), r_q=r.GetDenominator();

r_p=r_p*temp_q;

temp_p=temp_p*r_q;

return (r_p==temp_p);

}

int Rational::operator!= (Rational r) const

{

int r_p=r.GetNumerator(), r_q=r.GetDenominator();

return !(P*r_q==r_p*Q);

}

int Rational::operator!= (double ra) const

{

Rational temp(ra);

int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();

return !(P*temp_q==temp_p*Q);

}

int operator!= (double ra, Rational r)

{

Rational temp(ra);

int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();

int r_p=r.GetNumerator(), r_q=r.GetDenominator();

r_p=r_p*temp_q;

temp_p=temp_p*r_q;

return !(r_p==temp_p);

}

int Rational::operator (Rational r) const

{

int r_p=r.GetNumerator(), r_q=r.GetDenominator();

return (P*r_qr_p*Q);

}

int Rational::operator (double ra) const

{

Rational temp(ra);

int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();

return (P*temp_qtemp_p*Q);

}

int operator (double ra, Rational r)

{

Rational temp(ra);

int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();

int r_p=r.GetNumerator(), r_q=r.GetDenominator();

r_p=r_p*temp_q;

temp_p=temp_p*r_q;

return (r_ptemp_p);

}

int Rational::operator= (Rational r) const

{

int r_p=r.GetNumerator(), r_q=r.GetDenominator();

return (P*r_q=r_p*Q);

}

int Rational::operator= (double ra) const

{

Rational temp(ra);

int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();

return (P*temp_q=temp_p*Q);

}

int operator= (double ra, Rational r)

{

Rational temp(ra);

int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();

int r_p=r.GetNumerator(), r_q=r.GetDenominator();

r_p=r_p*temp_q;

temp_p=temp_p*r_q;

return (r_p=temp_p);

}

int Rational::operator (Rational r) const

{

int r_p=r.GetNumerator(), r_q=r.GetDenominator();

return (P*r_qr_p*Q);

}

int Rational::operator (double ra) const

{

Rational temp(ra);

int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();

return (P*temp_qtemp_p*Q);

}

int operator (double ra, Rational r)

{

Rational temp(ra);

int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();

int r_p=r.GetNumerator(), r_q=r.GetDenominator();

r_p=r_p*temp_q;

temp_p=temp_p*r_q;

return (r_ptemp_p);

}

int Rational::operator= (Rational r) const

{

int r_p=r.GetNumerator(), r_q=r.GetDenominator();

return (P*r_q=r_p*Q);

}

int Rational::operator= (double ra) const

{

Rational temp(ra);

int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();

return (P*temp_q=temp_p*Q);

}

int operator= (double ra, Rational r)

{

Rational temp(ra);

int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();

int r_p=r.GetNumerator(), r_q=r.GetDenominator();

r_p=r_p*temp_q;

temp_p=temp_p*r_q;

return (r_p=temp_p);

}

//以下為兩個(gè)有理數(shù)對(duì)象、一個(gè)有理數(shù)對(duì)象和一個(gè)實(shí)數(shù)

//之間的普通四則運(yùn)算的重載

Rational Rational::operator+ (Rational r) const

{

int r_p=r.GetNumerator(), r_q=r.GetDenominator();

Rational obj(P*r_q+r_p*Q,Q*r_q);

return obj;

}

Rational Rational::operator+ (double ra) const

{

Rational temp(ra);

int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();

Rational obj(P*temp_q+temp_p*Q,Q*temp_q);

return obj;

}

void Rational::operator+= (Rational r)

{

P=P+r.GetNumerator();

Q=Q+r.GetDenominator();

}

void Rational::operator+= (double ra)

{

Rational temp(ra);

int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();

P=P*temp_q+temp_p*Q;

Q=temp_q*Q;

}

Rational Rational::operator- (Rational r) const

{

int r_p=r.GetNumerator(), r_q=r.GetDenominator();

Rational obj(P*r_q-r_p*Q,Q*r_q);

return obj;

}

Rational Rational::operator- (double ra) const

{

Rational temp(ra);

int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();

Rational obj(P*temp_q-temp_p*Q,Q*temp_q);

return obj;

}

void Rational::operator-= (Rational r)

{

P=P-r.GetNumerator();

Q=Q-r.GetDenominator();

}

void Rational::operator-= (double ra)

{

Rational temp(ra);

int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();

P=P*temp_q-temp_p*Q;

Q=temp_q*Q;

}

Rational Rational::operator* (Rational r) const

{

Rational temp(P*r.GetNumerator(), Q*r.GetDenominator());

return temp;

}

Rational Rational::operator* (double ra) const

{

Rational temp(ra);

int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();

Rational obj(P*temp_p,Q*temp_q);

return obj;

}

void Rational::operator*= (Rational r)

{

P=P*r.GetNumerator();

Q=Q*r.GetDenominator();

}

void Rational::operator*= (double ra)

{

Rational temp(ra);

int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();

P=P*temp_p;

Q=temp_q*Q;

}

Rational Rational::operator/ (Rational r) const

{

Rational temp(P*r.GetDenominator(),Q*r.GetNumerator());

return temp;

}

Rational Rational::operator/ (double ra) const

{

Rational temp(ra);

int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();

Rational obj(P*temp_q,Q*temp_p);

return obj;

}

void Rational::operator/= (Rational r)

{

P=P*r.GetDenominator();

Q=Q*r.GetNumerator();

}

void Rational::operator/= (double ra)

{

Rational temp(ra);

int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();

P=P*temp_q;

Q=temp_p*Q;

}

//輸入輸出流的重載,輸出為最簡(jiǎn)形式

ostream operator(ostream output,Rational obj)

{

obj.Reduce();

outputobj.GetNumerator();

output"/"obj.GetDenominator();

return output;

}

istream operator(istream input,Rational obj)

{

int num,den;

char oper;

inputnum;

inputoper;

inputden;

Rational temp(num,den);

obj=temp;

return input;

}

int Rational::Iegal(void) const //判斷有理數(shù)對(duì)象的值是否合法

{

if(Q==0)

return 0;

else

return 1;

}

//一個(gè)實(shí)數(shù)和一個(gè)有理數(shù)對(duì)象之間的四則運(yùn)算重載

Rational operator+ (double ra, Rational r)

{

Rational temp(ra);

temp=temp+r;

return temp;

}

Rational operator- (double ra, Rational r)

{

Rational temp(ra);

temp=temp-r;

return temp;

}

Rational operator* (double ra, Rational r)

{

Rational temp(ra);

int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();

int r_p=r.GetNumerator(), r_q=r.GetDenominator();

Rational obj(temp_p*r_p,r_q*temp_q);

return obj;

}

Rational operator/ (double ra, Rational r)

{

Rational temp(ra);

int temp_p=temp.GetNumerator(), temp_q=temp.GetDenominator();

int r_p=r.GetNumerator(), r_q=r.GetDenominator();

Rational obj(temp_p*r_q,r_p*temp_q);

return obj;

}

void PrintMixedNumber(Rational X) //將一個(gè)有理數(shù)對(duì)象轉(zhuǎn)換為帶分?jǐn)?shù)形式

{

int t;

X.Reduce();

t=X.GetNumerator()/X.GetDenominator();

coutt"("(X.GetNumerator()-t*X.GetDenominator())"/"X.GetDenominator()")"endl;

}

Rational SolveEquation(Rational a,Rational b,Rational c) //求解一般的分?jǐn)?shù)方程

{

Rational temp;

if(a.GetNumerator()==0)

{

cout"Error!"endl;

exit(1);

}

temp=(c-b)/a;

temp.Reduce();

return temp;

}

三、主函數(shù)(測(cè)試部分):

#include iostream

using namespace std;

#include "Rational.h"

void main()

{

Rational A(-4,-8),C,D;

Rational B(10.05);

Rational E(B);

cout"Please input C: ";

cinC;

D=43.9;

cout"A = "Aendl;

cout"B = "Bendl;

cout"E = "Eendl;

cout"B = E ? "(B==E)endl;

cout"A != B ? "(A!=B)endl;

cout"-B = "(-B)endl;

cout"D(43.9) = "Dendl;

cout"A+B = "(A+B)endl;

cout"A-B = "(A-B)endl;

cout"A+1.5 = "(A+1.5)endl;

cout"1.5+A = "(1.5+A)endl;

cout"A*1.5 = "(A*1.5)endl;

cout"1.5*A = "(1.5*A)endl;

cout"A/B = "(A/B)endl;

cout"A/1.5 = "(A/1.5)endl;

cout"1.5/A = "(1.5/A)endl;

A*=1.5;

cout"A*=1.5 = "Aendl;

cout"float(A) = "Float(A)endl;

cout"E 的帶分?jǐn)?shù)形式為: ";

PrintMixedNumber(E);

Rational X;

cout"A*X+B=C 解得: ";

X=SolveEquation(A,C,E);

cout"X = "Xendl;

}

C++編程題 定義一個(gè)有理數(shù)類。

1 有理數(shù)類

class Fraction

{

private:

int _Numerator; //分子

int _Denominator; //分母

public:

Fraction(){}

Fraction(const int Numerator,const int Denominator);

friend Fraction operator+(const Fraction frac1,const Fraction frac2);

friend Fraction operator-(const Fraction frac1,const Fraction frac2);

friend Fraction operator*(const Fraction frac1,const Fraction frac2);

friend Fraction operator/(const Fraction frac1,const Fraction frac2);

bool operator(const Fraction frac);

bool operator(const Fraction frac);

bool operator==(const Fraction frac);

bool operator!=(const Fraction frac);

void simp();

friend ostream operator(ostream out,Fraction frac);

};

2 包含類

class A

{

class B // B類屬于A類的成員

{

//成員

};

//A類的其他成員

};

3 派生類

class A

{

};

//Book是從PrintedDocument中派生的

class B:A

{

//成員表

};

希望對(duì)你有所幫助!

c語(yǔ)言中有理數(shù)怎么表示

結(jié)構(gòu)體定義有理數(shù)及其操作

#includeiostreamusing namespace std;struct RationalT { // 有理數(shù)類型的定義 int num; // 分子 int den; // 分母};RationalT CreateRational(int num, int den) // 創(chuàng)建一個(gè)有理數(shù){ RationalT r; r.num = num; r.den = den; return r;}RationalT AddRational(RationalT r1, RationalT r2) // 有理數(shù)加法{ RationalT r; int fac; r.num = r1.num * r2.den + r2.num * r1.den; r.den = r1.den * r2.den; fac = r.num r.den ? r.num : r.den; // 將結(jié)果化成最簡(jiǎn)分式 while (r.num % fac != 0 || r.den % fac != 0) -- fac; r.num /= fac; r.den /= fac; return r;}RationalT MultiplyRational(RationalT r1, RationalT r2) // 有理數(shù)乘法{ RationalT r; int fac; r.num = r1.num * r2.num; r.den = r1.den * r2.den; fac = r.num r.den ? r.num : r.den; // 將結(jié)果化成最簡(jiǎn)分式 while (r.num % fac != 0 || r.den % fac != 0) -- fac; r.num /= fac; r.den /= fac; return r;}double GetRational(RationalT r) // 將有理數(shù)轉(zhuǎn)換成小數(shù){ return double(r.num) / r.den;}void PrintRational(RationalT r) // 以分?jǐn)?shù)形式輸出有理數(shù){ cout r.num "/" r.den endl;}void main(){ struct RationalT r1,r2,r3,r4; r1 = CreateRational(2,3); r2 = CreateRational(3,8); r3 = AddRational(r1,r2); r4 = MultiplyRational(r1,r2); PrintRational(r3); PrintRational(r4); coutGetRational(r4)endl; cin.get();cin.get();}/*25/241/40.25*/

分享題目:C語(yǔ)言構(gòu)造有理數(shù)的函數(shù) c++有理數(shù)
URL網(wǎng)址:http://jinyejixie.com/article42/dosecec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、定制網(wǎng)站、營(yíng)銷型網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
和平区| 华容县| 万全县| 阜阳市| 罗田县| 全南县| 安福县| 根河市| 万全县| 育儿| 洛隆县| 革吉县| 济宁市| 衡南县| 济南市| 阿合奇县| 普陀区| 新巴尔虎右旗| 晋江市| 唐山市| 白玉县| 民丰县| 辽阳市| 广丰县| 卓尼县| 锦州市| 庐江县| 荔浦县| 云南省| 盐源县| 乡城县| 舞阳县| 山东省| 峡江县| 右玉县| 锦州市| 武强县| 宣威市| 沾化县| 丹阳市| 元江|