C++-------实现计算器

头文件声明

 

class CFraction  
{  
private:  
          
    int nu;  
    int de;  
      
public:  
   CFraction(int nu = 0,int de = 1);  
    
	CFraction operator +(CFraction &a);

	CFraction operator -(CFraction &a);

	CFraction operator *(CFraction &a);

	CFraction operator /(CFraction &a);

	CFraction operator -();

	void simplify(); 

	int get_nu()
	{
		simplify();
		return nu;
	}

	int get_de()
	{
		simplify();
		return de;
	}
};


源程序

 

#include "stdafx.h"
#include <cmath>
#include "string"
#include "Mycount.h"
using namespace std;
CFraction::CFraction(int n,int d)  
{  
                         nu = n;  
                         de = d;  
} 

CFraction CFraction::operator +(CFraction &a)
{
	CFraction c;
	c.nu = a.nu*de+nu*a.de;
	c.de = a.de*de;
	return c;
}

CFraction CFraction::operator -(CFraction &a)
{
	CFraction c;
	c.nu = nu*a.de - a.nu*de;
	c.de = a.de*de;
	return c;
}

CFraction CFraction::operator *(CFraction &a)
{
	CFraction c;
	c.nu = nu*a.nu;
	c.de = de*a.de;
	return c;
}

CFraction CFraction::operator /(CFraction &a)
{
	CFraction c;
	c.nu = a.nu*de;
	c.de = a.de*nu;
	return c;
}

CFraction CFraction::operator -()
{
	CFraction c;
	c.de = -de;
	c.nu = -nu;
	return c;
}

void  CFraction::simplify()
{
	int x;  
    if(nu>de)  
        x = de;  
    else   
        x = nu;  
      
    for(int i = 2; i<=x;i++)  
    {  
        if(nu%i==0&& de%i==0)  
        {  
            nu = nu/i;  
            de = de/i;  
                        i = 1;  
        }  
    }  
}  


button 按键程序

 

void C分数的计算Dlg::OnBnClickedButton1()
{
	UpdateData();
	CFraction c1(c1_nu,c1_de),c2(c2_nu,c2_de),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_nu = c.get_nu();
	c_de = c.get_de();
	
	UpdateData(FALSE);
	// TODO: 在此添加控件通知处理程序代码
}


下拉列那个还没找到怎么弄。谁懂得告诉下吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值