C++的实验

来源:互联网 发布:淘宝买家不评价怎么办 编辑:程序博客网 时间:2024/06/08 14:55

编程:建立一个分数类。分数类的数据成员包括分子和分母,操作包括约分、通分、加、减、乘、除、求倒数、比较、显示和输入。分数类的定义如下:­

//l.h ­

class fraction{ ­

int above; ­

int below; ­

    void reduction(); ­

void makecommond(fraction); ­

public: ­

fraction(int a=0,int b=1); ­

fraction add(fraction); ­

fraction sub(fraction); ­

fraction mul(fraction); ­

fraction div(fraction); ­

fraction reciprocal(); ­

bool equal(fraction); ­

bool greaterthan(fraction); ­

bool lessthan(fraction); ­

void display(); ­

void input(); ­

}; ­

//l.cpp ­

#include "l.h" ­

#include <iostream> ­

using namespace std; ­

void fraction::reduction(){ ­

int a=above,b=below,c; ­

for(c=a%b;c!=0;a=b,b=c,c=a%b); ­

    above=above/b; ­

below=below/b; ­

} ­

void fraction::makecommond(fraction f){ ­

int b=below,b1=f.below,c; ­

for(c=b1%b;c!=0;b1=b,b=c,c=b1%b); ­

    below=(f.below*below/b); ­

    above=(f.below*below/b)/below*above; ­

} ­

fraction::fraction(int a,int b){ ­

    above=a; ­

below=b; ­

} ­

fraction fraction::add(fraction f){ ­

fraction t; ­

    makecommond(f); ­

t.above=above+f.above*below/f.below; ­

t.below=below; ­

    t.reduction(); ­

return t; ­

} ­

fraction fraction::sub(fraction f){ ­

fraction t; ­

    makecommond( f); ­

t.above=above-f.above*below/f.below; ­

t.below=below; ­

    t.reduction(); ­

return t; ­

} ­

fraction fraction::mul(fraction f){ ­

    fraction t; ­

    t.above=f.above*above; ­

t.below=f.below*below; ­

    t.reduction(); ­

return t; ­

} ­

fraction fraction::div(fraction f){ ­

    fraction t; ­

    t.below=f.above*below; ­

t.above=f.below*above; ­

    t.reduction(); ­

return t; ­

} ­

fraction fraction::reciprocal(){ ­

fraction t; ­

t.above=below; ­

t.below=above; ­

    t.reduction(); ­

return t; ­

} ­

bool fraction::equal(fraction f){ ­

    makecommond(f); ­

    if(above==f.above*below/f.below) ­

return true; ­

} ­

bool fraction::greaterthan(fraction f){ ­

    makecommond(f); ­

if(above>f.above*below/f.below) ­

return true; ­

} ­

bool fraction::lessthan(fraction f){ ­

    makecommond(f); ­

if(above<f.above*below/f.below) ­

return true; ­

} ­

void fraction::display(){ ­

cout<<above<<"/"<<below<<endl; ­

} ­

void fraction::input(){ ­

cout<<"请输入above="<<endl; ­

cin>>above; ­

cout<<"请输入below="<<endl; ­

cin>>below; ­

} ­

//main.cpp ­

#include "l.h" ­

#include <iostream> ­

using namespace std; ­

int main(){ ­

fraction f1,f2,f3; ­

int a,b,c; ­

f1.input(); ­

f2.input(); ­

f3=f1.add(f2); ­

cout<<"加法结果:"<<endl; ­

f3.display(); ­

f3=f1.sub(f2); ­

    cout<<"减法结果:"<<endl; ­

    f3.display(); ­

f3=f1.mul(f2); ­

    cout<<"乘法结果:"<<endl; ­

f3.display(); ­

f3=f1.div(f2); ­

    cout<<"除法结果:"<<endl; ­

f3.display(); ­

    f3=f1.reciprocal(); ­

cout<<"求倒结果:"<<endl; ­

f3.display(); ­

a=f1.equal(f2); ­

b=f1.greaterthan(f2); ­

c=f1.lessthan(f2); ­

if(a==1) ­

cout<<"两数相等!"<<endl; ­

    else if(b==1) ­

cout<<"前数要大!"<<endl; ­

    else if(c==1) ­

cout<<"后数要大!"<<endl; ­

return 0; ­

原创粉丝点击