第十五周作业2

来源:互联网 发布:windows 添加输入法 编辑:程序博客网 时间:2024/04/30 09:08
// stdafx.cpp : 只包括标准包含文件的源文件// cfraction.pch 将作为预编译头// stdafx.obj 将包含预编译类型信息#include "stdafx.h"

#include "stdafx.h"#include "CFrac.h"using namespace std;CFraction CFraction :: operator+(CFraction &c1){ CFraction c2; c2.deno = deno * c1.deno; c2.nume = nume * c1.deno + c1.nume * deno; c2.output(); return c2; } CFraction CFraction :: operator-(CFraction &c1){ CFraction c2; c2.deno = deno * c1.deno; c2.nume = nume * c1.deno - c1.nume * deno;c2.output(); return c2; } CFraction CFraction :: operator*(CFraction &c1){ CFraction c2; c2.deno = deno * c1.deno; c2.nume = nume * c1.nume; c2.output(); return c2; } CFraction CFraction :: operator/(CFraction &c1){ CFraction c2; c2.deno = deno * c1.nume; c2.nume = nume * c1.deno; c2.output(); return c2; } void CFraction :: output() { int i; if(nume >= 0){if(deno < nume)i = deno; else i = nume; for(; i > 0; --i) {if(nume % i == 0 && deno % i == 0) {nume = nume / i; deno = deno / i; break; } } }else {nume = -nume;if(deno < nume)i = deno; else i = nume; for(; i > 0; --i) {if(nume % i == 0 && deno % i == 0) {nume = nume / i; deno = deno / i; break; } } nume = -nume;}}

// cfraction.cpp : 定义应用程序的类行为。//#include "stdafx.h"#include "cfraction.h"#include "cfractionDlg.h"#ifdef _DEBUG#define new DEBUG_NEW#endif// CcfractionAppBEGIN_MESSAGE_MAP(CcfractionApp, CWinApp)ON_COMMAND(ID_HELP, &CWinApp::OnHelp)END_MESSAGE_MAP()// CcfractionApp 构造CcfractionApp::CcfractionApp(){// TODO: 在此处添加构造代码,// 将所有重要的初始化放置在 InitInstance 中}// 唯一的一个 CcfractionApp 对象CcfractionApp theApp;// CcfractionApp 初始化BOOL CcfractionApp::InitInstance(){// 如果一个运行在 Windows XP 上的应用程序清单指定要// 使用 ComCtl32.dll 版本 6 或更高版本来启用可视化方式,//则需要 InitCommonControlsEx()。否则,将无法创建窗口。INITCOMMONCONTROLSEX InitCtrls;InitCtrls.dwSize = sizeof(InitCtrls);// 将它设置为包括所有要在应用程序中使用的// 公共控件类。InitCtrls.dwICC = ICC_WIN95_CLASSES;InitCommonControlsEx(&InitCtrls);CWinApp::InitInstance();AfxEnableControlContainer();// 标准初始化// 如果未使用这些功能并希望减小// 最终可执行文件的大小,则应移除下列// 不需要的特定初始化例程// 更改用于存储设置的注册表项// TODO: 应适当修改该字符串,// 例如修改为公司或组织名SetRegistryKey(_T("应用程序向导生成的本地应用程序"));CcfractionDlg dlg;m_pMainWnd = &dlg;INT_PTR nResponse = dlg.DoModal();if (nResponse == IDOK){// TODO: 在此放置处理何时用//  “确定”来关闭对话框的代码}else if (nResponse == IDCANCEL){// TODO: 在此放置处理何时用//  “取消”来关闭对话框的代码}// 由于对话框已关闭,所以将返回 FALSE 以便退出应用程序,//  而不是启动应用程序的消息泵。return FALSE;}

// cfractionDlg.cpp : 实现文件//#include "stdafx.h"#include "cfraction.h"#include "cfractionDlg.h"#include "CFrac.h"#ifdef _DEBUG#define new DEBUG_NEW#endif// 用于应用程序“关于”菜单项的 CAboutDlg 对话框class CAboutDlg : public CDialog{public:CAboutDlg();// 对话框数据enum { IDD = IDD_ABOUTBOX };protected:virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持// 实现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()// CcfractionDlg 对话框CcfractionDlg::CcfractionDlg(CWnd* pParent /*=NULL*/): CDialog(CcfractionDlg::IDD, pParent), m_nu1(0), m_du1(0), m_a(_T("")), m_nu2(0), m_de2(0), m_nu3(0), m_de3(0){m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);}void CcfractionDlg::DoDataExchange(CDataExchange* pDX){CDialog::DoDataExchange(pDX);DDX_Text(pDX, IDC_EDIT1, m_nu1);DDX_Text(pDX, IDC_EDIT2, m_du1);DDX_Text(pDX, IDC_EDIT3, m_a);DDX_Text(pDX, IDC_EDIT4, m_nu2);DDX_Text(pDX, IDC_EDIT5, m_de2);DDX_Text(pDX, IDC_EDIT6, m_nu3);DDX_Text(pDX, IDC_EDIT7, m_de3);}BEGIN_MESSAGE_MAP(CcfractionDlg, CDialog)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()//}}AFX_MSG_MAPON_BN_CLICKED(IDC_BUTTON1, &CcfractionDlg::OnBnClickedButton1)END_MESSAGE_MAP()// CcfractionDlg 消息处理程序BOOL CcfractionDlg::OnInitDialog(){CDialog::OnInitDialog();// 将“关于...”菜单项添加到系统菜单中。// IDM_ABOUTBOX 必须在系统命令范围内。ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL){CString strAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if (!strAboutMenu.IsEmpty()){pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);}}// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动//  执行此操作SetIcon(m_hIcon, TRUE);// 设置大图标SetIcon(m_hIcon, FALSE);// 设置小图标// TODO: 在此添加额外的初始化代码return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE}void CcfractionDlg::OnSysCommand(UINT nID, LPARAM lParam){if ((nID & 0xFFF0) == IDM_ABOUTBOX){CAboutDlg dlgAbout;dlgAbout.DoModal();}else{CDialog::OnSysCommand(nID, lParam);}}// 如果向对话框添加最小化按钮,则需要下面的代码//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,//  这将由框架自动完成。void CcfractionDlg::OnPaint(){if (IsIconic()){CPaintDC dc(this); // 用于绘制的设备上下文SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);// 使图标在工作区矩形中居中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;// 绘制图标dc.DrawIcon(x, y, m_hIcon);}else{CDialog::OnPaint();}}//当用户拖动最小化窗口时系统调用此函数取得光标//显示。HCURSOR CcfractionDlg::OnQueryDragIcon(){return static_cast<HCURSOR>(m_hIcon);}void CcfractionDlg::OnBnClickedButton1(){// TODO: 在此添加控件通知处理程序代码UpdateData();//将界面上的各控件输入的值“捕获”到与之关联的变量中CFraction t1(m_nu1, m_du1);CFraction t2(m_nu2, m_de2);CFraction t3;if(m_a == "+")t3 = t1.operator+(t2);else if(m_a == "-")t3 = t1.operator-(t2);else if(m_a == "*")t3 = t1.operator*(t2);else if(m_a == "/")t3 = t1.operator/(t2);m_nu3 = t3.getnume();m_de3 = t3.getdeno();UpdateData(FALSE);//用m_area的值更新界面上对应的控件的值并实现显示}


逐渐有点眉目了