Re: to doncare
※ 引述《log56 (chaos)》之銘言:
: ※ 引述《g400 (真的貴,好厲害)》之銘言:
: : 這不素粉簡單嗎???
: : 1.分割字串
: : 2.通分
: : 3.分子分母作運算
: : 4.約分
: 再兩個小時就要交作業了…
: 難產…
這只有做到分數相加...剩下的自己改吧....
#include <iostream.h>
class fraction{
private:
int p,q;
int gcd(int,int);
public:
void set_value(int a,int b){
p=a; q=b;
}
void reduction();
void add(fraction );
void show();
};
int fraction::gcd(int x,int y){
int z=x%y;
while (z!=0){
x=y;
y=z;
z=x%y;
}
return y;
}
void fraction::reduction(){
int r=gcd(p,q);
p/=r;
q/=r;
}
void fraction::show(){
cout <<q<<"/"<<p;
}
void fraction::add(fraction x){
int a=x.p*p;
int b=x.p*q+x.q*p;
p=a;
q=b;
reduction();
}
void main(){
fraction a,b;
int a1,a2;
cout << "Denominator 1="; cin >> a1;
cout << "Numerator 1="; cin >> a2;
a.set_value(a1,a2);
cout << "Denominator 2="; cin >>a1;
cout << "Numerator 2="; cin >>a2;
b.set_value(a1,a2);
a.add(b);
a.show();
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 219.68.15.158
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 4 之 4 篇):