“+ - * /”重载

/* 
* Copyright (c) 2011, 烟台大学计算机学院
* All rights reserved.
* 作    者:刘明亮
* 完成日期:2013  年 4  月 24 日
* 版 本 号:v1.0
* 输入描述:
* 问题描述:分数类中的对象可以和整型数进行四则运算,且运算符合交换律。
* 例如:CFraction a(1,3),b;int i=2; 可以完成b=a+i;。
* 同样,可以完成i+a, 45+a, a*27, 5/a等各种运算。
* 程序输出:
* 问题分析:
* 算法设计:略
*/
#include<iostream>
#include<cmath>
using namespace std;
class CFraction
{private:
int nume;  // 分子
int deno;  // 分母
public:
	CFraction(int nu=0,int de=1);    //置值,改变值时用
	CFraction simplify();        //化简(使分子分母没有子)
	//比较运算符(二目)的重载
	bool operator > (CFraction &t);
	bool operator < (CFraction &t);
	bool operator == (CFraction &t);
	//二目运算符的重载
	CFraction operator+(CFraction &n);
	CFraction operator-(CFraction &n);
	CFraction operator*(CFraction &n);
	CFraction operator/(CFraction &n);
	CFraction operator+(int i);
	CFraction operator-(int i);
	CFraction operator*(int i);
	CFraction operator/(int i);
	void display();
};

CFraction::CFraction(int nu,int de)    //置值
{
    nume=nu;
    deno=de;
}
CFraction CFraction::simplify()   //化简(使分子分母没有公因子)
{
    CFraction n;
	n.nume=nume;n.deno=deno;
	int i;
	if(nume%deno==0){
		n.deno=1;
		n.nume=nume/deno;
	}else{
		if(nume>deno){
			for(i=2;i<=n.deno;i++){
				if(nume%i==0&&deno%i==0){
					n.nume=n.nume/i;
					n.deno=n.deno/i;
				}
			}
		}else{
			for(i=2;i<=n.nume;i++){
                if(n.nume%i==0&&n.deno%i==0){
                    n.nume=n.nume/i;
                    n.deno=n.deno/i;
                }
			}
		}
	}
    return n;
}
bool CFraction::operator > (CFraction &t)
{
	if(nume>t.nume||(nume==t.nume&&deno>t.deno)){
		return true;
	}else{
		return false;
	}
}
bool CFraction::operator < (CFraction &t)
{
	if(nume<t.nume||(nume==t.nume&&deno<t.deno)){
		return true;
	}else{
		return false;
	}
}
bool CFraction::operator == (CFraction &t)
{
	if(nume==t.nume&&deno==t.deno){
		return true;
	}else{
		return false;
	}
}
//二目运算符的重载
CFraction CFraction::operator+(CFraction &n)
{
	CFraction t;
	t.deno=deno*n.deno;
	t.nume=(n.nume)*deno+nume*(n.deno);
	return t.simplify();
}

CFraction CFraction::operator-(CFraction &n)
{
	CFraction t;
	t.deno=deno*n.deno;
	t.nume=nume*n.deno-n.nume*deno;
	return t.simplify();
}
CFraction CFraction::operator*(CFraction &n)
{
	CFraction t;
	t.deno=deno*n.deno;
	t.nume=nume*n.nume;
	return t.simplify();
}
CFraction CFraction::operator/(CFraction &n)
{
	CFraction t;
	t.deno=deno*n.nume;
	t.nume=nume*n.deno;
	return t.simplify();
}
CFraction CFraction::operator+(int i)
{
	CFraction t;
	t.deno=deno;
	t.nume=nume+i*deno;
	return t.simplify();
}

CFraction CFraction::operator-(int i)
{
	CFraction t;
	t.deno=deno;
	t.nume=nume-i*deno;
	return t.simplify();
}
CFraction CFraction::operator*(int i)
{
	CFraction t;
	t.deno=deno;
	t.nume=nume*i;
	return t.simplify();
}
CFraction CFraction::operator/(int i)
{
	CFraction t;
	t.deno=deno*i;
	t.nume=nume;
	return t.simplify();
}
void CFraction::display()
{
	if(deno==1){
		cout<<nume<<endl;
	}else{
		cout<<nume<<"/"<<deno<<endl;
	}
}
int main()
{
    CFraction n1(3,2),n2(1,2),n;//构造函数,初始化用
	cout<<"n1为:";
	n1.display();
	cout<<"n2为:";
	n2.display();
	cout<<"下面比较两个时间大小:\n";
	if (n1>n2) cout<<"n1>n2"<<endl;
	if (n1<n2) cout<<"n1<n2"<<endl;
	if (n1==n2) cout<<"n1=n2"<<endl;
	cout<<endl;
	//下面自行设计对其他运算符的重载的测试
	cout<<"n1+n2=";
	n=n1+n2;
	n.display();
	cout<<"n1-n2=";
	n=n1-n2;
	n.display();
	cout<<"n1*n2=";
	n=n1*n2;
	n.display();
	cout<<"n1/n2=";
	n=n1/n2;
	n.display();
	cout<<"n1+3=";
	n=n1.operator+(3);
	n.display();
	cout<<"n1-3=";
	n=n1.operator-(3);
	n.display();
	cout<<"n1*3=";
	n=n1.operator*(3);
	n.display();
	cout<<"n1/3=";
	n=n1.operator/(3);
	n.display();
	return 0;
}

好的,以下是一个示例: ```c++ #include <iostream> using namespace std; struct MyStruct { int a, b; MyStruct(int a = 0, int b = 0) : a(a), b(b) {} // 加法运算符重载 MyStruct operator+(const MyStruct& other) const { return MyStruct(a + other.a, b + other.b); } // 减法运算符重载 MyStruct operator-(const MyStruct& other) const { return MyStruct(a - other.a, b - other.b); } // 乘法运算符重载 MyStruct operator*(const MyStruct& other) const { return MyStruct(a * other.a, b * other.b); } // 除法运算符重载 MyStruct operator/(const MyStruct& other) const { return MyStruct(a / other.a, b / other.b); } // 赋值函数 MyStruct& operator=(const MyStruct& other) { a = other.a; b = other.b; return *this; } // 拷贝函数 MyStruct(const MyStruct& other) { a = other.a; b = other.b; } // 友元函数 friend ostream& operator<<(ostream& os, const MyStruct& obj) { os << "a: " << obj.a << ", b: " << obj.b; return os; } }; int main() { MyStruct a(1, 2), b(3, 4), c; // 测试加法运算符重载 c = a + b; cout << "a + b = " << c << endl; // 测试减法运算符重载 c = a - b; cout << "a - b = " << c << endl; // 测试乘法运算符重载 c = a * b; cout << "a * b = " << c << endl; // 测试除法运算符重载 c = a / b; cout << "a / b = " << c << endl; // 测试赋值函数 MyStruct d = a; cout << "d = " << d << endl; // 测试拷贝函数 MyStruct e(a); cout << "e = " << e << endl; return 0; } ``` 输出结果: ``` a + b = a: 4, b: 6 a - b = a: -2, b: -2 a * b = a: 3, b: 8 a / b = a: 0, b: 0 d = a: 1, b: 2 e = a: 1, b: 2 ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值