第6周-项目1-分数类的雏形

来源:互联网 发布:linux 防御xss 编辑:程序博客网 时间:2024/06/05 07:51

问题及代码:

/* *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 r,x,y;    x=nume;    y=deno;    while(y!=0)    {        r=x%y;        x=y;        y=r;    }    nume=nume/x;    deno=deno/x;}void CFraction::amplify(int n){    nume=nume*n;    cout<<nume<<'/'<<deno<<endl;}void CFraction::output(int style){    int b;    double x;   switch(style)   {   case 0:    cout<<nume<<'/'<<deno<<endl;    break;   case 1:    CFraction::simplify();    cout<<nume<<'/'<<deno<<endl;    break;   case 2:    b=nume%deno;    if(b==0)        cout<<nume<<endl;      else      {         x=nume-deno;         cout<<b<<'('<<x<<'/'<<deno<<')'<<endl;      }      break;   case 3:    x=(float)nume/(float)deno;    cout<<x<<endl;    break;   }}int main(){    CFraction CF(8,6);    CF.output(0);    CF.output(1);    CF.output(2);    CF.output(3);    CFraction CFC(8,6);    CFC.amplify(2);    return 0;}


运行结果:




0 0
原创粉丝点击