第十五周C++任务【二】拓展

MyCFraction.cpp
#include "stdafx.h"
//#include "iostream"
#include"MyCFraction.h"
int CFraction::gcd(int x,int y)
{
	int r;
	while(y!=0)
	{
		r=x%y;
		x=y;
		y=r;
	}
	return x;
}
void CFraction::simplify()
{
	int n = gcd(nume,deno);
	nume = nume/n;
	deno = deno/n;
}
CFraction CFraction::operator + (const CFraction &c)
{
	CFraction c1;
	c1.nume = nume * c.deno + c.nume *deno;
	c1.deno = deno * c.deno;
	c1.simplify();
	return c1;
}
CFraction CFraction::operator - (const CFraction &c)
{
	CFraction c1;
	c1.nume = nume * c.deno - c.nume *deno;
	c1.deno = deno * c.deno;
	c1.simplify();
	return c1;
}
CFraction CFraction::operator * (const CFraction &c)
{
	CFraction c1;
	c1.nume = nume * c.nume;
	c1.deno = deno * c.deno;
	c1.simplify();
	return c1;
}
CFraction CFraction::operator / (const CFraction &c)
{
	CFraction c1;
	c1.nume = nume * c.deno;
	c1.deno = deno * c.nume;
	c1.simplify();
	return c1;
}
int CFraction::get_nume()
{
	return nume;
}
int CFraction::get_deno()
{
	return deno;
}

MyCFraction.h
class CFraction
{
private:
	int nume;
	int deno;
public:
	CFraction (int nu=0, int de=1):nume(nu),deno(de){}
	void simplify();
	int gcd(int x,int y);
	int get_nume();
    int get_deno();

	CFraction operator + (const CFraction &c);
	CFraction operator - (const CFraction &c);
	CFraction operator * (const CFraction &c);
	CFraction operator / (const CFraction &c);
};


button
void CCFrationDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(); //将把界面上各控件输入的值“捕获”到与之关联的变量中
	CFraction c1(c_1,c_2),c2(c_4,c_5),c;
	if(c_3=='+')
	{
		c=c1+c2;
	}
	else if(c_3=='-')
	{
		c=c1-c2;
	}
	else if(c_3=='*')
	{
		c=c1*c2;
		
	}
	else
	{
		c=c1/c2;
		}
		c_6=c.get_nume();
			c_7=c.get_deno();
	UpdateData(FALSE); //更新界面上对应的控件的值并实现显示

}


 

 

 

感言:偷笑大笑

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值