计算器的设计C++

计算器的设计
1、设计内容
该程序是一个小型实用复数计算器,可以完成复数的加减操作
2、程序设计要求
1)增加运算符重载功能,可以重载+=、- =、*、++、- -、> =、<=、!=运算符,其中,> =、<=是针对复数的模运算。
2)完善计算器测试程序,加减法要求在两位数内进行,而且减法结果不能是负数。增加乘法的测试,乘法要求为一位数的运算。
3)完善输入重载函数,要求可以接收从键盘输入的a+i*b形式的负数,在程序中可以识别出实部、虚部并可以正确赋值。

刚刚写完, 先附上截图程序太长, 附上部分程序
// 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 handlersBOOL 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++编写的”输入两个运算数和四则运算符,输出计算结果”计算器程序代码具体如下:include<stdio.h> void main(){int a,b,d;char c;printf("请输入一种运算符:\\n");scanf("%c",&c);printf("请输入两个数:\\n");scanf("%d",&a);scanf("%d",&b);switch(c){ case '+':d=a+...

[源码和文档分享]基于C++实现的多项式计算器系统
实验内容涉及设计并实现一个多项式计算器系统。利用C++的类及运算符重载特性,编写源代码,最终生成可执行程序,以实现对简单多项式的计算操作。实现步骤如下:1. 定义多项式类,包含多项式的系数和指数,并实现运算符重载以支持多项式的加、减、乘、除等基本运算。2. 设计用户接口,允许用户输入多项式表达式...

C++ mfc做计算器 具体步骤 急!!!
1.首先做计算器界面类,界面上要用0-9, +,-,*,\/,=等(复杂点还可以做的和XP一样的科学计算器)2.添加计算逻辑,每个按建点下后,在输出栏的显示我这里有源代码,你如果要请提供邮箱附部分核心代码:1.首先做计算器界面类,界面上要用0-9, +,-,*,\/,=等(复杂点还可以做的和XP一样的科学计算器)2.添加...

如何用c++,定义计算器类calculator?
include<iostream> using namespace std;class calculator { public:calculator(double a=0,double b=0):a(a),b(b){ } void set(double a,double b){ this->a=a;this->b=b;} double add(){ return a+b;} double subtract(){ return a-b;} double multiply(){ return a*b;} do...

c++分数计算器程序设计 帮忙看一下这个程序应该怎么改进,为什么无法_百...
c++分数计算器程序设计帮忙看一下这个程序应该怎么改进,为什么无法运行,显示打开input.txt失败呢#include<iostream>#include<fstream>#include<cmath>usingnamespacestd;\/\/---... c++分数计算器程序设计帮忙看一下这个程序应该怎么改进,为什么无法运行,显示打开input.txt失败呢#include<iostream>#include<fstream>#include...

求一个用C++编过计算器的,就是那种加减乘除三角函数可以写一排算的
}\/\/将中缀表达式转换为后缀表达式(逆波兰式)void trans( char a[], char b[] ){ char stock[128]={0}; int top=0; int len=0; int i=0; int j=0; top = -1; j = -1; len = strlen(a); for ( i=0; i<len; i++ ) { switch( a[...

用C++设计一简单的计算器模拟程序
模拟简单运算器的工作。假设计算器只能进行加减乘除运算,运算数和结果都是整数,4种运算符的优先级相同,按从左到右的顺序计算。输入格式:输入在一行中给出一个四则运算算式,没有空格,且至少有一个操作数。遇等号”=”说明输入结束。输出格式:在一行中输出算式的运算结果,或者如果除法分母为0或有...

用c++设计一个计算器程序
\/\/ but no namespaces and \/\/ no exceptions \/\/ pp 107-119, sec 6.1, A Desk Calculator \/\/ uses += rather than push_back() for string \/\/ to work around standard library bug \/\/ uses istrstream from <strstream> rather than istringstream from <sstream> \/\/ to work around ...

c++使用宏的计算器?
a+b#define MINUS(a,b) a-b#define MULTI(a,b) a*b#define DIVIDE(a,b) a\/bint main(){float a,b;scanf("%f %f",&a,&b);printf("%f\\n",PLUS(a,b));printf("%f\\n",MINUS(a,b));printf("%f\\n",MULTI(a,b));printf("%f\\n",DIVIDE(a,b));return 0;} 截图:

用C++写一个计算器,要求:+,-,*,\/机算;进制转换,三角函数计算
include<stdio.h> main(){float a,b,d;char c;scanf("%f,%f,%c",&a,&b,&c)if(c=='+')d=a+b;else if(c=='-')d=a-b;else if(c=='*')d=a*b;else if(c=='\/')d=a\/b;printf("%f",d);} 进制暂时想不起来!不好意思!!!

相似回答