#include<iostream>
using namespace std;
class CFraction
{
private:
int nume; // 分子
int deno; // 分母
void simplify(); //化简(使分子分母没有公因子)
public:
CFraction(int nu = 0, int de = 1); //构造函数,初始化用
void set(int nu , int de); //置值,改变值时用
void input(); //按照"nu/de"的格式,如"5/2"的形式输入
void amplify(int n); //放大n倍,如2/3放大5倍为10/3
void output(int style);
//输出:以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 = 0, int de = 1)
{
nume = nu;
deno = de;
}
void CFraction::input()
{
cout << "按照 分子/分母 的格式,如 5/2 的形式输入." << endl << "请输入: ";
char a;
cin >> nume >> a >> deno;
while (a != ‘/‘||deno == 0)
{
cout << "按照 分子/分母 的格式,如 5/2 的形式输入." << endl << "请重新输入: ";
cin >> nume >> a >> deno;
}
}
void CFraction::simplify()
{
if (nume == 0)
cout << "无需化简" << endl;
else if (nume == deno)
{
nume == deno == 1;
}
else
{
int r, x = nume, y = deno;
if (x > y)
{
r = x;
x = y;
y = r;
}
r = 1;
while (r != 0)
{
r = y%x;
y = x;
x = r;
}//最大公约数为y
nume = nume / y;
deno = deno / y;
}
}
void CFraction::amplify(int n)
{
nume = nume*n;
deno = deno*n;
}
void CFraction::output(int style=0)
{
if (nume == 0)
cout<<"结果为: "<< 0;
else if (nume == deno)
{
cout << "结果为: " << 1;
}
else
{
switch (style)
{
case 0: cout << "结果为: "<< nume << ‘/‘ << deno; break;
case 1:{simplify(); output(); break; }
case 2:
{
if (nume <= deno)
output(1);
else
{
int a=0;
while (nume > deno)
{
nume = nume - deno;
++a;
}
cout << "结果为: " << a << "(" << nume << ‘/‘ << deno << ")"<<endl;
}
break;
}
case 3:cout << "结果为: " << (double)nume / (double)deno << endl;
}
}
}
int main()
{
CFraction a;
a.input();
int n;
cout << "请输入放大倍数:";
cin >> n;
while (n == 0)
{
cout << "不允许的操作.请重新输入放大倍数: ";
cin >> n;
}
a.amplify(n);
a.output();
cout << endl << "请输入你需要的输出形式(默认形式为 0),以8/6为例:" << endl << endl;
cout << "0.原样输出8/6" << endl;
cout << "1.输出化简后形式4/3" << endl;
cout << "2.输出1(1 / 3)形式,表示一又三分之一" << endl;
cout << "3.用小数形式输出,如1.3333" << endl;
cin >> n;
while (n != 0 && n != 1 && n != 2 && n != 3)
{
cout << "请在0--3中选择:";
cin >> n;
}
a.output(n);
while (1){}
}