C++(分数类,进行加减乘除的运算并化简)

要求
(1)定义整数类和分数类。其中,包括构造函数、析构函数、显示函数等。
(2)输入 / 输出:对流提取和流插入运算符进行重载。
(3)计算功能:可进行分数的加、减、乘和除法运算。
(4)化简功能:将分数化简为最简分数。
(5)异常处理功能:分数中分母不能为零。
(6)菜单功能:每种功能的操作都是在菜单中进行相应选择。
程序:

#include<iostream>
#include<cmath>
using namespace std;
//整数 
class Integer{
	protected:
		int num;
	public:
		Integer(){
		}
		Integer(int n){
			num=n;
		}
		void display(){
			cout<<num<<endl;
		}
}; 
//分数 
class fraction:public Integer{
	private:
		int nume,deno;//分子,分母 
	public:
		fraction(){
			nume=0;
			deno=0;
		}
		fraction(int a,int b){
			nume=a;
			deno=b;
		}
		
		int outnume(){
			return nume;
		}
		int outdeno(){
			return deno;
		}
		
		void simple(fraction &f){
			int p,q;
			p=max(f.deno,f.nume);
			q=min(f.deno,f.nume);		
			if(f.deno==f.nume){
				f.deno=1;
				f.nume=1;
			}else if(f.nume==0){
				f.deno=0;
				f.nume=0;
			}
			else{
				for(int i=0;;i++)      
				{
					if(p%q==0)      
					{
						f.deno=f.deno/q;
						f.nume=f.nume/q;
						break;
					}
					else
					{
						int t=p;
						p=q;
						q=t%q;
					}
				}
			}
				
		}
		
		
		
		friend istream &operator >>(istream &, fraction &);
		friend ostream &operator <<(ostream &, fraction &);
		fraction operator +(fraction &f1);
		fraction operator -(fraction &f1);
		fraction operator *(fraction &f1);
		fraction operator /(fraction &f1);
};
//-------------------------------------------------------------------
ostream &operator<<(ostream &output, fraction &f){
	if(f.deno==1&&f.nume==1){
		output<<1;
	}else if(f.nume==0&&f.deno==0){
		output<<0;
	}else{
		output<<f.nume<<"/"<<f.deno;
	}
	return output;
}

istream &operator>>(istream &input,fraction &f){
	cout<<" 请输入分子分母:";
	input>>f.nume>>f.deno;
	if(f.deno<=0){
		throw(f.deno);
	}
	return input;
}
//----------------------------------------------------------------
fraction fraction::operator +(fraction &f1){
	fraction f;
	int t1=deno/f1.deno,t2=f1.deno/deno;
	if(deno==f1.deno){
		f.deno=deno;
		f.nume=nume+f1.nume;
	} 
	else if(deno>f1.deno){
		f1.deno=t1*f1.deno;
		f1.nume=t1*f1.nume;
		f.deno=deno;
		f.nume=nume+f1.nume;
	}
	else if(deno<f1.deno){
		deno=t2*deno;
		nume=t2*nume;
		f.deno=f1.deno;
		f.nume=nume+f1.nume;
	}
	simple(f);
	return f;
}

fraction fraction::operator -(fraction &f1){
	fraction f;
	int t1=deno/f1.deno,t2=f1.deno/deno;
	if(deno==f1.deno){
		f.deno=deno;
		f.nume=nume-f1.nume;
	} 
	else if(deno>f1.deno){
		f1.deno=t1*f1.deno;
		f1.nume=t1*f1.nume;
		f.deno=deno;
		f.nume=nume-f1.nume;
	}
	else if(deno<f1.deno){
		deno=t2*deno;
		nume=t2*nume;
		f.deno=f1.deno;
		f.nume=nume-f1.nume;
	}
	simple(f);
	return f;
}

fraction fraction::operator *(fraction &f1){
	fraction f;
	f.deno=deno*f1.deno;
	f.nume=nume*f1.nume;
	simple(f);
	return f;
}

fraction fraction::operator /(fraction &f1){
	fraction f;
	f.deno=deno*f1.nume;
	f.nume=nume*f1.deno;
	simple(f);
	return f;
}
//------------------------------------------------
int main(){
	fraction f1(0,0),f2(1,2),f3;
	int item,x=f1.outdeno(),y=f1.outnume();
	for(;;){
	cout<<"--------------------"<<endl;
	cout<<"请选择你需要的功能:"<<endl; 
	cout<<"--------------------"<<endl;
	cout<<"	(1)输入一个分数"<<endl; 
	cout<<"	(2)输出一个分数"<<endl; 
	cout<<"	(3)两个分数相加"<<endl; 
	cout<<"	(4)两个分数相减"<<endl; 
	cout<<"	(5)两个分数相乘"<<endl; 
	cout<<"	(6)两个分数相除"<<endl; 
	cout<<"	(0)结束程序"<<endl; 
	cout<<"--------------------"<<endl;
	cout<<" 你的指令为:";
	cin>>item;
	if(item<8&&item>=0){
	
	switch(item){
		case(0):{
			exit(1);
			break;
		}
		case(1):{
			try{
				cin>>f1;
				x=f1.outdeno();y=f1.outnume();
			}catch(int){
				cout<<"--------------------"<<endl;
				cout<<" 数据错误,请重新输入"<<endl;
				continue;
			}
			break;
		}
		case(2):{
			if(x==0&&y==0){
				cout<<"由于你尚未输入分数,所以输出初始分数"<<f2<<endl;
			}else{
			cout<<" 输入的分数为:"<<f1<<endl;
			}
			break;
		}
		case(3):{
			f3=f1+f2; 
			cout<<" "<<f1<<"与"<<f2<<"相加的结果为:"<<f3<<endl;
			break;
		}
		case(4):{
			f3=f1-f2;
			cout<<" "<<f1<<"与"<<f2<<"相减的结果为:"<<f3<<endl;
			break;
		}
		case(5):{
			f3=f1*f2;
			cout<<" "<<f1<<"与"<<f2<<"相乘的结果为:"<<f3<<endl;
			break;
		}
		case(6):{
			f3=f1/f2;
			cout<<" "<<f1<<"与"<<f2<<"相除的结果为:"<<f3<<endl;
			break;
		}
	}
}else {
	cout<<"--------------------"<<endl;
	cout<<" 指令无效,请重新输入"<<endl;
	cout<<"--------------------"<<endl;
	continue; 
};
}
	return 1;
}

运行结果:
在这里插入图片描述

  • 11
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是一个简单的 C++ 分数的设计: ```c++ #include <iostream> using namespace std; class Fraction { private: int numerator; // 分子 int denominator; // 分母 public: Fraction(int n = 0, int d = 1) : numerator(n), denominator(d) {} // 构造函数 void print() const { // 打印分数 cout << numerator << '/' << denominator << endl; } void simplify() { // 约分 int gcd = getGcd(numerator, denominator); numerator /= gcd; denominator /= gcd; } Fraction operator+(const Fraction& f) const { // 加法运算符重载 int n = numerator * f.denominator + f.numerator * denominator; int d = denominator * f.denominator; return Fraction(n, d); } Fraction operator-(const Fraction& f) const { // 减法运算符重载 int n = numerator * f.denominator - f.numerator * denominator; int d = denominator * f.denominator; return Fraction(n, d); } Fraction operator*(const Fraction& f) const { // 乘法运算符重载 int n = numerator * f.numerator; int d = denominator * f.denominator; return Fraction(n, d); } Fraction operator/(const Fraction& f) const { // 除法运算符重载 int n = numerator * f.denominator; int d = denominator * f.numerator; return Fraction(n, d); } bool operator==(const Fraction& f) const { // 相等运算符重载 return numerator == f.numerator && denominator == f.denominator; } bool operator!=(const Fraction& f) const { // 不等运算符重载 return !(*this == f); } private: int getGcd(int a, int b) const { // 求最大公约数 return b == 0 ? a : getGcd(b, a % b); } }; int main() { Fraction f1(1, 2); Fraction f2(2, 3); Fraction f3 = f1 + f2; f3.print(); // 输出 7/6 Fraction f4 = f1 - f2; f4.print(); // 输出 -1/6 Fraction f5 = f1 * f2; f5.print(); // 输出 1/3 Fraction f6 = f1 / f2; f6.print(); // 输出 3/4 if (f1 == f2) { cout << "f1 == f2" << endl; } else { cout << "f1 != f2" << endl; } // 输出 f1 != f2 return 0; } ``` 在上面的代码中,我们定义了一个 `Fraction` ,其中包含了分子和分母两个私有成员变量,以及一些公有方法和运算符重载函数。在构造函数中,我们设置了默认参数为 0 和 1,这样就可以创建一个值为 0 的分数对象。在 `print()` 方法中,我们输出了分数的字符串表示,如 `1/2`。在 `simplify()` 方法中,我们使用欧几里得算法求出了分子和分母的最大公约数,并将其约分。在运算符重载函数中,我们分别实现了加、减、乘、除、相等和不等运算符重载。最后,在 `main()` 函数中,我们创建了两个分数对象 `f1` 和 `f2`,并对它们进行了加、减、乘、除和相等性判断的运算
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

FFFPAG

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

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

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

打赏作者

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

抵扣说明:

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

余额充值