# 第十五周任务二

源文件：
#include "stdafx.h"
#include <iostream>
#include "fenshu.h"
using namespace std;

int CFraction::get_nu()
{
return nu;
}
int CFraction::get_de()
{
return de;
}

void CFraction::input()
{
cout<<"输入分数按照nu/de格式"<<endl;
cin>>nu;
cin>>c;
cin>>de;
}

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;
}
}
}

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;
}

class CFraction
{
private:

int nu;
int de;
char c;

public:
CFraction(int nu = 0,int de = 1);

void set(int nu = 0,int de  = 1);

void input();

void simplify();

void output();

CFraction operator +(CFraction &a);

CFraction operator -(CFraction &a);

CFraction operator *(CFraction &a);

CFraction operator /(CFraction &a);

CFraction operator -();

int get_nu();

int get_de();

};

Button1定义：
void CfenshujisuanDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData();
CFraction C1(a_1,a_2),C2(b_1,b_2),C;
if(fuhao == '+')
{
C = C1 + C2;
}
else if(fuhao == '-')
{
C = C1 - C2;
}
else if(fuhao == '*')
{
C = C1 * C2;
}
else if(fuhao == '/')
{
C = C1 / C2;
}
C.simplify();
c_1 = C.get_nu();
c_2 = C.get_de();
UpdateData(FALSE);

}


• 本文已收录于以下专栏：

举报原因： 您举报文章：第十五周任务二 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)