/*
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称:C++第十五周【任务2】
* 作 者: 李洪悬
* 完成日期: 2012 年 5 月 30 日
* 对任务及求解方法的描述部分
* 输入描述:输入分数
* 问题描述:进行分数的加减乘除
* 程序输出:输出分数
*/
【任务2】设计分数类,开发一个窗口式程序,可以完成分数的四则运算(参考第8 周任务3)
CFraction.h
using namespace std;
int gcd(int m, int n);
class CFraction
{
private:
int nume; // 分子
int deno; // 分母
public:
//构造函数及运算符重载的函数声明
CFraction(int nu=0,int de=1); //构造函数,初始化用
void Simplify(); //化简(使分子分母没有公因子)
void output(); //输出:以8/6为例,style为0时,输出8/6;
int get_nume();
int get_deno();
CFraction operator+(CFraction &c);
CFraction operator-(CFraction &c);
CFraction operator*(CFraction &c);
CFraction operator/(CFraction &c);
};
CFraction.cpp
#include "stdafx.h"
#include <iostream>
#include"CFrration.h"
CFraction::CFraction(int nu,int de) //构造函数,初始化用
{
if (de!=0)
{
nume=nu;
deno=de;
}
else
{
cerr<<"初始化中发生错误,程序退出\n";
system("pause");
exit(0);
}
}
void CFraction::Simplify() //化简(使分子分母没有公因子)
{
int n;
if(nume < 0)
{
n = gcd(-nume, deno);
}
else
{
n = gcd(nume, deno);
}
nume = nume / n;
deno = deno / n;
}
// 求m,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;
}
void CFraction::output() //输出:以8/6为例,style为0时,输出8/6;
{
cout<<nume<<'/'<<deno<<endl;
}
CFraction CFraction::operator+(CFraction &c)
{
CFraction c2,c3,c4;
c2.nume =nume*c.deno ;
c3.nume =c.nume *deno;
c2.deno =deno*c.deno ;
c3.deno =c.deno *deno;
c4.nume=c2.nume +c3.nume ;
c4.deno =c2.deno ;
c4.Simplify ();
return c4;
}
CFraction CFraction::operator-(CFraction &c)
{
CFraction c2,c3,c4;
c2.nume =nume*c.deno ;
c3.nume =c.nume *deno;
c2.deno =deno*c.deno ;
c3.deno =c.deno *deno;
c4.nume=c2.nume -c3.nume ;
c4.deno =c2.deno ;
c4.Simplify ();
return c4;
}
CFraction CFraction::operator*(CFraction &c)
{
CFraction c2,c3,c4;
c2.nume =nume*c.nume ;
c2.deno =deno*c.deno ;
c2.Simplify ();
return c2;
}
CFraction CFraction::operator/(CFraction &c)
{
CFraction c2,c3;
c2.nume =c.deno ;
c2.deno =c.nume ;
c3.nume =nume*c2.nume ;
c3.deno =deno*c2.deno ;
c3.Simplify ();
return c3;
}
int CFraction::get_nume()
{
return nume;
}
int CFraction::get_deno()
{
return deno;
}
cfrationDlg.cpp
// cfrationDlg.cpp : implementation file
//
#include "stdafx.h"
#include "cfration.h"
#include "cfrationDlg.h"
#include "CFrration.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
// CcfrationDlg dialog
CcfrationDlg::CcfrationDlg(CWnd* pParent /*=NULL*/)
: CDialog(CcfrationDlg::IDD, pParent)
, c_1(0)
, c_2(0)
, c_3(_T(""))
, c_4(0)
, c_5(0)
, c_6(0)
, c_7(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CcfrationDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, c_1);
DDX_Text(pDX, IDC_EDIT2, c_2);
DDX_Text(pDX, IDC_EDIT3, c_3);
DDX_Text(pDX, IDC_EDIT4, c_4);
DDX_Text(pDX, IDC_EDIT5, c_5);
DDX_Text(pDX, IDC_EDIT6, c_6);
DDX_Text(pDX, IDC_EDIT7, c_7);
}
BEGIN_MESSAGE_MAP(CcfrationDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON1, &CcfrationDlg::OnBnClickedButton1)
END_MESSAGE_MAP()
// CcfrationDlg message handlers
BOOL CcfrationDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CcfrationDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CcfrationDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CcfrationDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CcfrationDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
UpdateData();
CFraction f1(c_1,c_2);
CFraction f2(c_4,c_5);
CFraction f3;
if(c_3=='+')
f3=f1+f2;
else if(c_3=='-')
f3=f1-f2;
else if(c_3=='*')
f3=f1*f2;
else if(c_3=='/')
f3=f1/f2;
c_6=f3.get_nume();
c_7=f3.get_deno();
UpdateData(FALSE);
}
MFC输出:
经验积累:
重复做啊!做啊!做啊!还是做出来啦!