分数类的雏形
来源:互联网 发布:惠普一体机扫描软件 编辑:程序博客网 时间:2024/05/21 15:06
/*
*Copyright (c) 2013 ,烟台大学计算机学院
*All rights reserved.
*作者:张凤宁
*完成日期:2014年4月15
*版本号:v1.0
*问题描述:
*样例输入:
*样例输出:
*问题分析:用简单的方法,学会活学活用
*/
#include <iostream>
#include <Cmath>
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(); //按照"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;
//默认方式0
};
CFraction::CFraction(int nu,int de)
{
nume=nu;
deno=de;
}
void CFraction::set(int nu,int de)
{
nume=nu;
deno=de;
}
void CFraction::input()
{
char c;
cin>>nume>>c>>deno;
while(c!='/')
{
cout<<"输入格式错误,请重新输入。"<<endl;
cin>>nume>>c>>deno;
}
}
void CFraction::simplify()
{
int i,j;
if(nume>deno)
j=deno;
else
j=nume;
for(i=2; i<=j; i++)
if(nume%i==0&&deno%i==0)
{
nume/=i;
deno/=i;
}
}
void CFraction::amplify(int n)
{
nume=nume*n;
}
void CFraction::output(int style)
{
if(style==1)
{
cout<<"原样:"<<nume<<"/"<<deno<<endl;
}
else if(style==2)
{
simplify();
cout<<"化简后形式为:"<<nume<<"/"<<deno<<endl;
}
else if(style==3)
{
cout<<"输出形式为1(1/3):"<<endl;
simplify();
cout<<int(nume/deno)<<"("<<nume-deno*(nume/deno)<<"/"<<deno<<")"<<endl;
}
else if(style==4)
{
cout<<"小数形式输出为:"<<double(nume/deno)<<endl;
}
else if(style==5)
{
int n;
cin>>n;
amplify(n);
cout<<"放大n倍后"<<nume<<endl;
}
else
{
cout<<"输入有误,即将退出程序。"<<endl;
}
}
int main()
{
CFraction c1;
int a;
cout<<"请输入一个分数:"<<endl;
c1.input();
cout<<"输出方式如下:"<<endl;
cout<<"1.原样输出 2.化简后输出"<<endl;
cout<<"3.以1(1/3)形式输出 4.小数形式输出"<<endl;
cout<<"5.放大n倍后"<<endl;
cin>>a;
c1.output(a);
return 0;
}
运行结果:
心得体会:好纠结呀。。。。。。
- 分数类的雏形
- 分数类的雏形
- 分数类的雏形
- 分数类的雏形
- 分数类的雏形
- 分数类的雏形
- 分数类的雏形
- 分数类的雏形
- 分数类的雏形
- 分数类的雏形
- 分数类的雏形
- 分数类的雏形
- 分数类的雏形
- 分数类的雏形
- 分数类的雏形
- 分数类的雏形
- 分数类的雏形
- 分数类的雏形
- eclipse导入工程中文乱码问题
- 春天来了
- 兰州水污染源:发现27年为何仍未根治
- Eclipse上GIT插件EGIT使用手册之五_查看历史记录
- 将tomcat添加为linux系统服务
- 分数类的雏形
- ios之在子线程中使用定时器
- 总结:C# Bitmap保存的问题,eg:GDI+中发生一般性错误 .jpg 正由另一个进程使用 ,该进程无法访问文件
- cuda文件编译成动态库
- solr中一个问题——顺便讨论java中关于中包括main函数以及涉及资源(变量)操作中的类
- 关于PJSIP 添加视频的思路和想法
- 春天里
- 第一周作业1
- 自己写的用java生成XML代码,不足之处请大家指正