第六周项目一 分数类的雏形

/*   
*Copyright (c)2016,烟台大学计算机与控制工程学院   
*All rights reserved.   
*文件名称:main.cpp   
*作    者:刘默涵  
*完成日期:2016年4月7日   
*版 本 号:v1.0   

完成分数类的设计,并在main()函数中自行定义对象,调用各成员函数,完成基本的测试。

<span><span class="preprocessor">#include <</span></span>iostream>  
  
using namespace std;  
class CFraction  
{  
private:  
    int nume;  
    int deno;  
public:  
    CFraction(int nu=0,int de=1);  
    void set(int nu=0,int de=1);  
    void input();  
    void simplify();  
    void amplify(int n);  
    void output(int style);  
  
};  
 CFraction::CFraction(int nu,int de):nume(nu),deno(de){}  
  
void CFraction::simplify()  
{  
    int z=0,x,y;  
    x=nume;  
    y=deno;  
  
    while(y!=0)  
    {  
        z=x%y;  
        x=y;  
        y=z;  
  
    }  
    nume=nume/x;  
    deno=deno/x;  
    cout<<nume<<'/'<<deno<<endl;  
  
}  
void CFraction::amplify(int n)  
{  
  
  
    nume=nume*n;  
    cout<<nume<<'/'<<deno<<endl;  
  
}  
void CFraction::output(int style)  
{  
    int b,x,a;  
    float c;  
    x=nume;  
    switch(style)  
    {  
        case 0:cout<<nume<<'/'<<deno<<endl;break;  
        case 1:CFraction::simplify();break;  
        case 2:b=x%deno;  
               if(b==0)  
               {  
                   a=x/deno;  
                  cout<<a<<endl;  
               }  
               else  
               {  
                   a=x/deno;  
                   cout<<a<<'('<<b<<'/'<<deno<<')'<<endl;  
               }  
               break;  
  
        case 3:  
              c=(float)x/(float)deno;  
              cout<<c<<endl;  
              break;  
  
    }  
  
}  
int main()  
{  
    int n;  
    CFraction fenshu(8,6);  
    fenshu.output(0);  
    fenshu.output(1);  
    fenshu.output(2);  
    fenshu.output(3);  
    CFraction fenshu1(8,6);  
    cout<<"放大倍数为"<<endl;  
    cin>>n;  
    fenshu1.amplify(n);  
  
    return 0;  
} 
运行结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值