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
    点赞
  • 59
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
矩阵加减乘除运算可以通过重载运算符来实现。以下是一个矩阵的定义及其运算符重载实现示例: ```C++ #include <iostream> #include <vector> using namespace std; class Matrix { public: Matrix(int rows, int cols) : rows(rows), cols(cols) { data.resize(rows); for (int i = 0; i < rows; i++) { data[i].resize(cols); } } int getRows() const { return rows; } int getCols() const { return cols; } double& operator()(int i, int j) { return data[i][j]; } const double& operator()(int i, int j) const { return data[i][j]; } Matrix operator+(const Matrix& other) const { Matrix result(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result(i, j) = (*this)(i, j) + other(i, j); } } return result; } Matrix operator-(const Matrix& other) const { Matrix result(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result(i, j) = (*this)(i, j) - other(i, j); } } return result; } Matrix operator*(const Matrix& other) const { Matrix result(rows, other.cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < other.cols; j++) { double sum = 0; for (int k = 0; k < cols; k++) { sum += (*this)(i, k) * other(k, j); } result(i, j) = sum; } } return result; } Matrix operator/(const Matrix& other) const { Matrix result(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { result(i, j) = (*this)(i, j) / other(i, j); } } return result; } private: int rows, cols; vector<vector<double>> data; }; int main() { Matrix m1(2, 3); m1(0, 0) = 1; m1(0, 1) = 2; m1(0, 2) = 3; m1(1, 0) = 4; m1(1, 1) = 5; m1(1, 2) = 6; Matrix m2(3, 2); m2(0, 0) = 7; m2(0, 1) = 8; m2(1, 0) = 9; m2(1, 1) = 10; m2(2, 0) = 11; m2(2, 1) = 12; Matrix m3 = m1 * m2; for (int i = 0; i < m3.getRows(); i++) { for (int j = 0; j < m3.getCols(); j++) { cout << m3(i, j) << " "; } cout << endl; } return 0; } ``` 在这个示例中,矩阵的构造函数接受行数和列数,并动态分配内存来存储矩阵数据。运算符重载实现中,加、减、乘、除分别对应了 `operator+`、`operator-`、`operator*`、`operator/`。`operator()` 用于访问矩阵元素。在 main 函数中,首先创建了两个矩阵 m1 和 m2,然后计算它们的乘积,并输出结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

FFFPAG

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

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

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

打赏作者

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

抵扣说明:

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

余额充值