第六周【项目1-分数类的雏形】

/*
*Copyright (c)2016,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:main.cpp
*作    者:张珩瑞
*完成日期:2016年4月2日
*版 本 号:v1.0
*
*问题描述:分数类的雏形
输入描述:分数
*输出描述:分数的各种表现方式,化简
*/

#include <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;
}

运行结果为

第六周【项目1-分数类的雏形】_第1张图片

你可能感兴趣的:(C++)