/*
*Copyright (c)2016,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:main.cpp
*作 者:徐聪
*完成日期:2016年4月5日
*版 本 号:v1.0
*Copyright (c)2016,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:main.cpp
*作 者:徐聪
*完成日期:2016年4月5日
*版 本 号:v1.0
*
完成分数类的设计,并在main()函数中自行定义对象,调用各成员函数,完成基本的测试。
#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;
}
运行结果