/* (程序头部注释开始)
* 程序的版权和版本声明部分
* 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: 在此添加控件通知处理程序代码
}