第15周实验报告任务2

/* (程序头部注释开始)  
* 程序的版权和版本声明部分 
 
* Copyright (c) 2011, 烟台大学计算机学院学生  
 
* All rights reserved.* 文件名称:窗口分数类 
* 作 者: 郭岩岩 

* 完成日期:2012 年5月 29日 

* 版 本 号: vc.1 

* 对任务及求解方法的描述部分 

* 输入描述:  

* 问题描述:  

* 程序输出:  

*程序头部的注释结束 

*/  





class CFraction    
{    
private:    
    int nume;  // 分子     
    int deno; // 分母         
public:    
    CFraction(int nu=0,int de=1);   //构造函数,初始化用         
    void input();               //按照"nu/de"的格式,如"5/2"的形式输入     
    void Simplify();//化简(使分子分母没有公因子)
	int get_nume();
	int get_deno();
    CFraction operator+(CFraction &c);  
    CFraction operator-(CFraction &c);  
    CFraction operator*(CFraction &c);  
    CFraction operator/(CFraction &c);  
};

#include "stdafx.h"   
#include <iostream>   
#include"MyCFraction.h"   

using namespace std;      
int gcd(int,int);    
CFraction::CFraction(int nu,int de)    
{    
    if(de!=0)  
    {  
        nume=nu;    
        deno=de;    
    }  
    else  
    {  
        cerr<<"初始化中发生错误,程序退出\n";    
        exit(0);    
    }  
}      
void CFraction::input()    
{    
  
    cout<<nume<<"/"<<deno<<endl;  
}   
//化简   
void CFraction::Simplify()    
{   
    int n=gcd(nume,deno);  
    nume=nume/n;  
    deno=deno/n;      
}   
int gcd(int m,int n)  
{  
    int r;    
    if (m<n){r=m;m=n;n=r;}    
    while(r=m%n)  // 求m,n的最大公约数(原来求公约数可以这样求啊)    
    {    
        m=n;    
        n=r;    
    }    
    return n;   
} 

int CFraction::get_nume()
{
	return nume;
}

int CFraction::get_deno()
{
	return deno;
}
CFraction CFraction::operator+(CFraction &c)  
{  
    CFraction c2;  
    c2.nume =nume*c.deno +c.nume*deno ;  
    c2.deno =deno*c.deno ;  
    c2.Simplify();  
    return c2;  
}  
CFraction CFraction::operator-(CFraction &c)  
{  
    CFraction c2;  
    c2.nume =nume*c.deno -c.nume*deno ;  
    c2.deno =deno*c.deno ;  
    c2.Simplify ();  
    return c2;  
}  
CFraction CFraction::operator*(CFraction &c)  
{  
    CFraction c2;  
    c2.nume =nume*c.nume;  
    c2.deno =deno*c.deno ;  
    c2.Simplify();  
    return c2;  
}  
CFraction CFraction::operator/(CFraction &c)  
{  
    CFraction c2;  
    c2.nume =nume*c.deno ;  
    c2.deno =deno*c.nume  ;  
    c2.Simplify();  
    return c2;
}

void CCFractionDlg::OnBnClickedButton1()
{
	// TODO: Add your control notification handler code here
	UpdateData();
	CFraction cf1(c_n1,c_d1),cf2(c_n2,c_d2),cf3;
	if(c=="+")
		cf3=cf1+cf2;
	else if(c=="-")
		cf3=cf1-cf2;
	else if(c=="*")
		cf3=cf1*cf2;
	else if(c=="/")
		cf3=cf1/cf2;
	c_n3=cf3.get_nume();
	c_d3=cf3.get_deno();
	UpdateData(FALSE);  

	
}


 

 

上机感言:看着不再是黑黑的窗口感觉舒服多了!

经验积累:可以增加两个get函数来得到分数和分母各为多少;

                   在cpp函数中要加上#include"stdafx.h";

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值