当前位置:编程学习 > C/C++ >>

用C++设计一个简单的计算器

要求如下:

1.能实现+ -  * / 功能。

2.  0作除数判别

3.精度要求:小数点后一位数

4.要设计出计算器的界面

5.要求有具体的输入提示等帮助信息

6.需要书名的地方要求加注解

 

哎~~没办法啊,想了好久就是没弄出来,百度上的有些又不符合要求,希望懂的大神帮帮忙,好的话会有追加分的~~~

追问:呵呵,辛苦了,请问这是全部的么?

答案:刚刚写完, 先附上截图

程序太长, 附上部分程序


// XRabbitDlg.cpp : implementation file
//

#include "stdafx.h"
#include "XRabbit.h"
#include "XRabbitDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CXRabbitDlg dialog
VOID ChangeText(VOID);

 

CXRabbitDlg::CXRabbitDlg(CWnd* pParent /*=NULL*/)
 : CDialogEx(CXRabbitDlg::IDD, pParent)
 , XEditStr1(_T(""))
 , XEditStr2(_T(""))
 , XEditStr3(_T(""))
{
 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CXRabbitDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialogEx::DoDataExchange(pDX);
 DDX_Text(pDX, IDC_EDIT3, XEditStr1);
 DDX_Text(pDX, IDC_EDIT4, XEditStr2);
 DDX_Text(pDX, IDC_EDIT5, XEditStr3);
}

BEGIN_MESSAGE_MAP(CXRabbitDlg, CDialogEx)
 ON_WM_PAINT()
 ON_WM_QUERYDRAGICON()
 ON_WM_SHOWWINDOW()
 ON_BN_CLICKED(IDC_RADIO1, &CXRabbitDlg::OnBnClickedRadio1)
 ON_BN_CLICKED(IDC_RADIO2, &CXRabbitDlg::OnBnClickedRadio2)
 ON_BN_CLICKED(IDC_RADIO3, &CXRabbitDlg::OnBnClickedRadio3)
 ON_BN_CLICKED(IDC_RADIO4, &CXRabbitDlg::OnBnClickedRadio4)
 ON_BN_CLICKED(IDC_BUTTON1, &CXRabbitDlg::OnBnClickedButton1)
END_MESSAGE_MAP()


// CXRabbitDlg message handlers

BOOL CXRabbitDlg::OnInitDialog()
{
 CDialogEx::OnInitDialog();

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

// 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 CXRabbitDlg::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
 {
  CDialogEx::OnPaint();
 }
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CXRabbitDlg::OnQueryDragIcon()
{
 return static_cast<HCURSOR>(m_hIcon);
}

 

void CXRabbitDlg::OnShowWindow(BOOL bShow, UINT nStatus)
{
 CDialogEx::OnShowWindow(bShow, nStatus);

 // TODO: Add your message handler code here
 //static_cast<CButton *>(GetDlgItem(IDC_RADIO1))->SetCheck(TRUE);
}


void CXRabbitDlg::OnBnClickedRadio1()
{
 // TODO: Add your control notification handler code here
 ::ChangeText();
}

VOID ChangeText(VOID){
 CONST UINT uiIDC[4] = {IDC_RADIO1, IDC_RADIO2, IDC_RADIO3, IDC_RADIO4};
 CButton *XRadio;
 CString XText, XStr;
 for (int i = 0; i != 4; ++i){
  XRadio = static_cast<CButton *>(CButton::FromHandle(::GetDlgItem(AfxGetApp()->GetMainWnd()->GetSafeHwnd(), uiIDC[i])));
  if (XRadio->GetCheck()){
   XRadio->GetWindowText(XText);
   XStr = XText.GetAt(0) + CString(L"数");
   ::SetWindowText(::GetDlgItem(AfxGetApp()->GetMainWnd()->GetSafeHwnd(), IDC_STATIC3), XStr);
   XStr = CString(L"被") + XText.GetAt(0) + CString(L"数");
   ::SetWindowText(::GetDlgItem(AfxGetApp()->GetMainWnd()->GetSafeHwnd(), IDC_STATIC2), XStr);
   ::SetWindowText(::GetDlgItem(AfxGetApp()->GetMainWnd()->GetSafeHwnd(), IDC_STATIC4), CString(XText.GetAt(0)));
   break;
  }
 }
}

void CXRabbitDlg::OnBnClickedRadio2()
{
 // TODO: Add your control notification handler code here
 ::ChangeText();
}


void CXRabbitDlg::OnBnClickedRadio3()
{
 // TODO: Add your control notification handler code here
 ::ChangeText();
}


void CXRabbitDlg::OnBnClickedRadio4()
{
 // TODO: Add your control notification handler code here
 ::ChangeText();
}


void CXRabbitDlg::OnBnClickedButton1()
{
 // TODO: Add your control notification handler code here
 this->UpdateData(TRUE);
 CString cstrOperator;
 CStatic *XStatic = static_cast<CStatic *>(GetDlgItem(IDC_STATIC4));
 XStatic->GetWindowText(cstrOperator);
 double lfNum1 = _tcstod(XEditStr1, NULL);
 double lfNum2 = _tcstod(XEditStr2, NULL);
 double lfRes = 0.0;
 if (cstrOperator == CString(L"加"))
  lfRes = lfNum1 + lfNum2;
 else if (cstrOperator == CString(L"减"))
  lfRes = lfNum1 - lfNum2;
 else if (cstrOperator == CString(L"乘"))
  lfRes = lfNum1 * lfNum2;
 else if (cstrOperator == CString(L"除")){
  if (lfNum2 == 0)
   AfxMessageBox(_T("除数不为零"));
  else
   lfRes = lfNum1 / lfNum2;
 }
 else
  AfxMessageBox(_T("未知错误"));
 INT iRes = static_cast<int>(lfRes * 10);
 XEditStr3.Format(_T("%i"), iRes);
 XEditStr3.Insert(XEditStr3.GetLength() - 1, _T("."));
 this->UpdateData(FALSE);
}

上一个:c++ 中“内存”问题
下一个:c++程序,200行左右

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,