第十五周 任务二

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

* 作    者:         刘向一                     
* 完成日期:     2012 年5月29日
* 版 本 号:       V1.0   
* 对任务及求解方法的描述部分
* 输入描述:
* 问题描述:
* 程序输出:

* 程序头部的注释结束

*/

头文件

class CFraction  
{  
private:  
    int nume;  
    int deno;  
public:  
    CFraction(int nu=0,int de=1):nume(nu),deno(de){}  
    void simplify();   
    void display();  
    CFraction operator +(CFraction &c1);  
    CFraction operator -(CFraction &c1);  
    CFraction operator *(CFraction &c1);  
    CFraction operator /(CFraction &c1);  
    bool operator >(CFraction &c1);  
    bool operator <(CFraction &c1);  
    bool operator ==(CFraction &c1);  
    bool operator !=(CFraction &c1);  
    bool operator >=(CFraction &c1);  
    bool operator <=(CFraction &c1);  
    CFraction operator +();  
    CFraction operator -();  
    int getnu();  
    int getde();  
};


源文件

#include "stdafx.h"  
#include "123.h"  
#include <iostream>  
using namespace std;  
void CFraction::simplify()  
{  
    int x;  
    int max;  
    if(nume>deno)  
        x=nume;  
    else   
        x=deno;  
    for(int i=2;i<x;i++)  
        if(nume%i==0&&deno%i==0)  
        {  
            deno=deno/i;  
            nume=nume/i;  
            max=i;  
        }  
}  
  
void CFraction::display()    
{    
    cout<<"("<<nume<<"/"<<deno<<")"<<endl;    
}   
  
CFraction CFraction::operator +(CFraction &c1)  
{  
    CFraction c2;  
  
    c2.deno=deno*c1.deno;  
    c2.nume=nume*c1.deno+c1.nume*deno;  
    c2.simplify();  
  
    return (c2);  
}  
  
CFraction CFraction::operator -(CFraction &c1)  
{  
    CFraction c2;  
  
    c2.deno=deno*c1.deno;  
    c2.nume=nume*c1.deno-c1.nume*deno;  
    c2.simplify();  
  
    return (c2);  
}  
  
CFraction CFraction::operator *(CFraction &c1)  
{  
    CFraction c2;  
  
    c2.deno=deno*c1.deno;  
    c2.nume=nume*c1.nume;  
    c2.simplify();  
  
    return (c2);  
}  
  
CFraction CFraction::operator /(CFraction &c1)  
{  
    CFraction c2;  
  
    c2.deno=deno*c1.nume;  
    c2.nume=nume*c1.deno;  
    c2.simplify();  
  
    return (c2);  
}  
  
bool CFraction::operator >(CFraction &c1)  
{  
    CFraction  c2,c3;  
    c2.nume=nume*c1.deno;  
    c3.nume=c1.nume*deno;  
  
    if(c2.nume>c3.nume)  
        return true;  
    else  
        return false;  
}  
  
bool CFraction::operator <(CFraction &c1)  
{  
    CFraction  c2,c3;  
    c2.nume=nume*c1.deno;  
    c3.nume=c1.nume*deno;  
  
    if(c2.nume<c3.nume)  
        return true;  
    else  
        return false;  
}  
  
bool CFraction::operator ==(CFraction &c1)  
{  
    if( operator >(c1)!=1&& operator <(c1)!=1)  
        return true;  
    else  
        return false;  
}  
  
bool CFraction::operator !=(CFraction &c1)  
{  
    if(operator >(c1)==1|| operator <(c1)==1)  
        return true;  
    else   
        return false;  
}  
  
bool CFraction::operator >=(CFraction &c1)  
{  
    if( operator <(c1)!=1)  
        return true;  
    else   
        return false;  
}  
  
bool CFraction::operator <=(CFraction &c1)  
{  
    if(operator >(c1)!=1)  
        return true;  
    else  
        return false;  
}  
CFraction CFraction::operator +()  
{  
    CFraction c2;  
  
    c2.nume=nume;  
    c2.deno=deno;  
  
    return (c2);  
}  
  
CFraction CFraction::operator -()  
{  
    CFraction c2;  
  
    c2.nume=-nume;  
    c2.deno=deno;  
  
    return (c2);  
}  
int  CFraction::getnu()  
{  
    return nume;  
}  
int  CFraction::getde()  
{  
    return deno;  
}  


按钮文件

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: 在此添加控件通知处理程序代码  
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值