程序头部注释开始
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: 窗口程序
* 作 者: 王智凯
* 完成日期: 2012年 5月29日
* 版 本 号: 凯子
MyCFraction.h
class CFraction
{
private:
int nume; // 分子
int deno; // 分母
public:
CFraction(int nu = 0, int de = 1); //构造函数
friend CFraction operator + (CFraction &c1,CFraction &c2);
friend CFraction operator - (CFraction &c1,CFraction &c2);
friend CFraction operator * (CFraction &c1,CFraction &c2);
friend CFraction operator / (CFraction &c1,CFraction &c2);
int get_nume(){return nume;}
int get_deno(){return deno;}
void Simplify();//化简
int gcd(int nu, int de);
};
MyCFraction.cpp
#include"stdafx.h"
#include"MyCFraction.h"
#include <cmath>
int CFraction::gcd(int nu, int de)
{
int t, r, a, b;
a = nu;
b = de;
if (a < b)
{
t = a;
a = b;
b = t;
}
while (b != 0)
{
r = a % b;
a = b;
b = r;
}
return a;
}
void CFraction::Simplify()
{
int n ;
if(nume < 0)
{
n = gcd(-nume, deno);
}
else
{
n = gcd(nume, deno);
}
nume = nume / n;
deno = deno / n;
}
CFraction::CFraction(int nu, int de)
{
nume = nu;
deno = de;
}
CFraction operator + (CFraction &c1,CFraction &c2)
{
CFraction c;
c.nume = c1.nume * c2.deno + c2.nume * c1.deno;
c.deno = c1.deno * c2.deno;
return c;
}
CFraction operator - (CFraction &c1,CFraction &c2)
{
CFraction c;
c.nume = c1.nume * c2.deno - c2.nume * c1.deno;
c.deno = c1.deno * c2.deno;
return c;
}
CFraction operator * (CFraction &c1,CFraction &c2)
{
CFraction c;
c.nume = c1.nume * c2.nume;
c.deno = c1.deno * c2.deno;
return c;
}
CFraction operator / (CFraction &c1,CFraction &c2)
{
CFraction c;
c.nume = c1.nume * c2.deno;
c.deno = c1.deno * c2.nume;
return c;
}
button:
void CwangzhikaiDlg::OnBnClickedButton1()
{
UpdateData();
CFraction c1(a_1, a_2), c2(a_3,a), c;
if(sign == '+')
{
c = c1 + c2;
}
else if(sign == '-')
{
c = c1 - c2;
}
else if(sign == '*')
{
c = c1 * c2;
}
else if(sign == '/')
{
c = c1 / c2;
}
c.Simplify();
x = c.get_nume();
y = c.get_deno();
UpdateData(FALSE);
}
上机感言:下拉式窗口果真很霸气。。。