⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 分数类
💻 CPP
字号:
#include "fraction.h"
#include "integer.h"
#include <cstdlib>

int main()
{
	Fraction f1,f2,f3;       
	Fraction* pf ;
	Integer i1,i2,i3;
	int j=2;
	try{
		cin>>f1>>f2;		    	//输入f1,f2;
		f3=f1+f2;                   //Fraction与Fraction运算
		cout<<f3<<endl;
		f3=f1-f2;
		cout<<f3<<endl;
		f3=f1*f2;
		cout<<f3<<endl;
		f3=f1/f2;
		cout<<f3<<endl;            
							
		f3=j+f1;					//Fraction与 int 运算
		cout<<f3<<endl;
		f3=j*f1;
		cout<<f3<<endl;

		cout<<-f1<<endl;                 //取负
		cout<<f1.Reciprocal()<<endl;     //倒数
		cout<<f1.Reduce()<<endl;         //约分
		f1.tf(f2);                       //通分

		cin>>i1>>i2;                //Integer 与 Integer 运算

		i3=i1+i2;
		cout<<i3<<endl;
		i3=i1*i2;
		cout<<i3<<endl;

		cout<<-i1<<endl;           //取负

		i3=i1-j;                   //Integer 与 int 运算
		cout<<i3<<endl;
		i3=j/i1;
		cout<<i3<<endl;

		f3=i1+i2;                  //整数值转换成分数类
		cout<<f3<<endl;
 
		i1=f1+f2;                   // 分数值转换成整数类
 		cout<<i1<<endl;
	}
	catch(exception &error)
	{
	   cout << error.what()<< endl;
	}

	if(f1 >= f2)
	{
		pf=&f1;
		cout << "digital number of f1 is " << pf->digit_number() << endl;            //测试虚函数
	}
	else if(f1<f2)
	{
		pf=&i1;
		cout<< "digital number of i1 is " << pf->digit_number() << endl;    
	}

	try                 //测试异常
	{
		Fraction f4 = f2 / 0;
		cout<<f4<<endl;
	}
	catch(exception &error)
	{
	   cout << error.what()<< endl;
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -