分数

/*
* 程序的版权和版本声明部分
* Copyright (c)2012, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称: x.cpp
* 作者:徐本锡
* 完成日期: 2013年 4 月3  日
* 版本号: v1.0
* 输入描述:

* 问题描述:   分数

* 程序输出:

*/
//我的代码:
#include <iostream>
#include <stdlib.h>
using namespace std;
int n=1;                  
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();				//按照"nu/de"的格式,如"5/2"的形式输入
	void simplify();				//化简(使分子分母没有公因子)
	void amplify(int n);			//放大n倍,如2/3放大5倍为10/3
	void output(int style=0);		//输出:以8/6为例,style为0时,原样输出8/6;
								//style为1时,输出化简后形式4/3;
								//style为2时,输出1(1/3)形式,表示一又三分之一;
								//style为3时,用小数形式输出,如1.3333;
								//不给出参数和非1、2,认为是方式0
};
CFraction::CFraction(int nu ,int de )               
{
    if(de!=0)
    {
        nume=nu;
        deno=de;
    }
	else
	{
    cout<<"输入有误"<<endl;

    }
}
void CFraction::set(int nu,int de)   
{           
    if(de!=0)
    {
        nume=nu;
        deno=de;
    }
	else
	{
    cout<<"输入有误!"<<endl;
    }
}
void CFraction::input()                     
{
    int q,w;
    char a;
    cout<<"请按照(a/b)输入:"<<endl;
    cin>>q>>a>>w;
    if(a!='/'||deno==0)
    {
        cout<<"输入有误"<<endl;
    }else{
        nume=q;
        deno=w;
    }
}
void CFraction::simplify()                  
{
    int min=deno;
    if(deno>nume)
    {
        min=nume;
    }
    while(1)
    {
        if(deno%min==0&&nume%min==0)
        {
            n=min;
			break;
        }
		else
        {
           -- min;
        }
    }

}
void CFraction::amplify(int n)              
{
    nume*=n;
}
void CFraction::output(int style)           
{

    switch(style)
    {
    case 0:
        cout<<"原样输出为:"<<nume<<"/"<<deno<<endl;
        break;
    case 1:
        simplify();
        cout<<"化简后的分数为:"<<(nume/n)<<"/"<<(deno/n)<<endl;
        break;
    case 2:
        simplify();
        cout<<"带分数为:"<<(nume/n)/(deno/n)<<"("<<(nume/n)-(deno/n)*((nume/n)/(deno/n))<<"/"<<deno/n<<")"<<endl;
        break;
    case 3:
        cout<<"小数形式为:"<<double(nume)/double(deno)<<endl;
        break;
    default :
        cout<<"输出原样为:"<<nume<<"/"<<deno<<endl;
        break;
    }
}
int main()
{
    CFraction s(9,54);
    cout<<"您的分数测试为:"<<endl;
    s.output(0);
    s.output(1);
    s.output(2);
    s.output(3);
    cout<<"输入分数: "<<endl;
    s.input();
    s.output(0);
    s.output(1);
    s.output(2);
    s.output(3);
    return 0;
}


 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十&年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值