#include<iostream> using namespace std; class Fract { int n; int d; public: Fract():n(0),d(1){}; Fract(int an,int ad):n(an),d(ad) { reduce(); } void reduce() { if(n<0){d=-d;n=-n;} if(d==0){cout<<"d==0!"<<endl;} int absn=n<0?-n:n; for(int i=d;i>1;i--) { if(n%i==0&&d%i==0) n/=i,d/=i; break; } } void show() { cout<<n<<'/'<<d<<endl; } double value() { return (double)n/d; } }; class Dai:public Fract { int i; public: Dai():i(0){} Dai(int ai,int an,int ad):i(ai),Fract(an,ad){} void show() { cout<<i<<'('; Fract::show(); } double value() { return i+Fract::value(); } }; int main() { Dai d1; Dai d2(2,12,16); d1.show(); cout<<"d1="<<d1.value()<<endl; d2.show(); cout<<"d2="<<d2.value()<<endl; cout<<"================================="<<endl; Fract f1(12,16); Fract f2; f1.show(); cout<<"f1="<<f1.value()<<endl; f2.show(); cout<<"f2="<<f2.value()<<endl; }
时间: 2024-09-07 03:19:16