有组合框的窗口程序

在窗口中建立一个下拉菜单式的组件,计算分数的四则运算!

 

头文件CFraction.h

class CFraction
{
private:
	int nume;  // 分子
	int deno;  // 分母
	int gcd(int nu, int de);
public:
	//构造函数及运算符重载的函数声明
	CFraction(int nu = 0, int de = 1);         //构造函数
	friend CFraction operator + (CFraction &c1,CFraction &c2);
	friend CFraction operator - (CFraction &c1,CFraction &c2);
	friend CFraction operator * (CFraction &c1,CFraction &c2);
	friend CFraction operator / (CFraction &c1,CFraction &c2);
	int get_nume();
	int get_deno();
	void Simplify();                    //化简(使分子分母没有公因子)
};


MyCFraction.cpp

 

#include"stdafx.h"
#include"CFraction.h"
using namespace std;

CFraction::CFraction(int nu, int de)
{
	nume = nu;
	deno = de;
}

int CFraction::gcd(int nu, int de)    
{    
	int t, r, a, b;   

	a = nu;  

	b = de;  

	if (a < b)       
	{    
		t = a;    
		a = b;                                     
		b = t;    
	}     

	while (b != 0)    
	{    
		r = a % b;    
		a = b;    
		b = r;    
	}    

	return a;    
}  

void CFraction::Simplify() 
{  
	int n ;

	if(nume < 0)
	{
		n = gcd(-nume, deno);
	}
	else
	{
		n = gcd(nume, deno);
	}

	nume = nume / n;  

	deno = deno / n; 

}  

CFraction operator + (CFraction &c1,CFraction &c2)
{
	CFraction c;

	c.nume = c1.nume * c2.deno + c2.nume * c1.deno;

	c.deno = c1.deno * c2.deno;

	return c;
}

CFraction operator - (CFraction &c1,CFraction &c2)
{
	CFraction c;

	c.nume = c1.nume * c2.deno - c2.nume * c1.deno;

	c.deno = c1.deno * c2.deno;

	return c;
}

CFraction operator * (CFraction &c1,CFraction &c2)
{
	CFraction c;

	c.nume = c1.nume * c2.nume;

	c.deno = c1.deno * c2.deno;

	return c;
}

CFraction operator / (CFraction &c1,CFraction &c2)
{
	CFraction c;

	c.nume = c1.nume * c2.deno;

	c.deno = c1.deno * c2.nume;

	return c;
}

int CFraction::get_nume()
{
	return this->nume;
}

int CFraction::get_deno()
{
	return this->deno;
}


buttonj按钮的程序:

 

不要忘记头文件:#include"CFraction.h"

void C分数拓展Dlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码

		UpdateData(); //将把界面上各控件输入的值“捕获”到与之关联的变量中
	CFraction c1(cf1_nume, cf1_deno), c2(cf2_nume, cf2_deno), c(0, 1);

	if(sign == '+')
	{
		c = c1 + c2;
	}
	else if(sign == '-')
	{
		c = c1 - c2;
	}
	else if(sign == '*')
	{
		c = c1 * c2;
	}
	else if(sign == '/')
	{
		c = c1 / c2;
	}
	c.Simplify();

	c_nume = c.get_nume();
	c_deno = c.get_deno();

	UpdateData(FALSE);
}


运行窗口:

加法:

 

减法:

 

乘法:

 

除法:

 

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页