mfc课程设计计算器
⑴ vc++课程设计计算器四则混合运算程序
#include "stdafx.h"
#include "计算器.h"
#include "计算器Dlg.h"
#include "math.h"
#include "time.h" //可以不用 ,屏蔽srand()
#include "stdlib.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
C计算器Dlg::C计算器Dlg(CWnd* pParent /*=NULL*/)
: CDialog(C计算器Dlg::IDD, pParent)
, n(0)
, setxiaoshu(false)
, xiaoshui(0)
, innum(0)
, n_2(0)
, operate(0)
, setnew(false)
BEGIN_MESSAGE_MAP(C计算器Dlg, CDialog)
……. //系统消息和控件的函数
主要实现代码:(dlg.cpp文件)://注:下面所有控件名称使用加粗字体和下划线表示该按键
void C计算器Dlg::OnPaint() //重写OnPaint(),实现,界面颜色的改变
{
int c_r,c_g,c_b,c; //3个变量分别代表R/G/B
// srand(time(0)); //控制随机函数rand()的取值时间(间隔1s左右更新值),后来为实现每次按键都变色把这句去掉了
c_r=rand()%256; // rand()%256代表变量取值为0-255之间的整数,下同
c_g=rand()%256;
c_b=rand()%256;
c=rand()%4; //c=0-3,即分为4种情况,分别绘制不同的颜色方案.实现更多色彩.
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (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
{
if (c==0) //蓝色渐变,其他两种颜色随机
{
CPaintDC dc(this);
CRect rc;
GetClientRect(&rc);
int nHeight=rc.Height();
for(int i=0;nHeight>=0;nHeight--,i++)
{
CPen pen;
pen.CreatePen(PS_SOLID,1,RGB(c_r,c_g,i));
CPen *pOldPen=dc.SelectObject(&pen);
dc.MoveTo(rc.left,i);
dc.LineTo(rc.right,i);
dc.SelectObject(pOldPen);
pen.DeleteObject();
}
}
else if(c==1) //绿色渐变, 其他两种颜色随机
{
CPaintDC dc(this);
CRect rc;
GetClientRect(&rc);
int nHeight=rc.Height();
for(int i=0;nHeight>=0;nHeight--,i++)
{
CPen pen;
pen.CreatePen(PS_SOLID,1,RGB(c_r,i,c_b));
CPen *pOldPen=dc.SelectObject(&pen);
dc.MoveTo(rc.left,i);
dc.LineTo(rc.right,i);
dc.SelectObject(pOldPen);
pen.DeleteObject();
}
}
else if(c==2) //红色渐变, 其他两种颜色随机
{
CPaintDC dc(this);
CRect rc;
GetClientRect(&rc);
int nHeight=rc.Height();
for(int i=0;nHeight>=0;nHeight--,i++)
{
CPen pen;
pen.CreatePen(PS_SOLID,1,RGB(i,c_g,c_r));
CPen *pOldPen=dc.SelectObject(&pen);
dc.MoveTo(rc.left,i);
dc.LineTo(rc.right,i);
dc.SelectObject(pOldPen);
pen.DeleteObject();
}
}
else //都不渐变,所有随机
{
CPaintDC dc(this);
CRect rc;
GetClientRect(&rc);
int nHeight=rc.Height();
for(int i=0;nHeight>=0;nHeight--,i++)
{
CPen pen;
pen.CreatePen(PS_SOLID,1,RGB(c_r,c_g,c_r));
CPen *pOldPen=dc.SelectObject(&pen);
dc.MoveTo(rc.left,i);
dc.LineTo(rc.right,i);
dc.SelectObject(pOldPen);
pen.DeleteObject();
}
}
}
}
void C计算器Dlg::OnBnClickedButton31() // 控件pi对应的函数 按键pi的处理
n=3.14159265358979; // 把pi值3.14159265358979赋给n
UpdateData(false);
Invalidate(); //重新执行OnPaint()函数实现每一次按键,界面自动变色.下面各控件对应函数也调用这函数,如控件对应函数//没有直接显示,就是在被调用的函数中有该函数
}
////////////////////////////////////////以下部分处理方式相似(数字录入)//////////////////
void C计算器Dlg::OnBnClickedButton0() //控件0对应的函数.调用inum()产生预期的数.包括整数/小数/正数/负数.下面1 - 9同
{ 按键0的处理
if (setnew) C计算器Dlg::OnBnClickedButton23(); else Invalidate(); // setnew为true,清除所有控制变量, 在上次计算出结果后
innum=0; // 把0赋给变量inum,执行相应的+++- //可以不用按C键清除直接按数字键开始下一个运
C计算器Dlg::inum(innum); //完成数据的输入过程. //算.setnew=true会在每个单元运算符和等号中出现. setnew
UpdateData(false); //在屏幕上显示当前n的值,下同 //为false,继续输入该数据. setnew=false出现在每个双元运算
} //符中.因OnBnClickedButton23()中已包含Invalidate(),但OnBnClickedButton23()不是 每次都执行, //为避免代码的重复执行,采用if else形式.
void C计算器Dlg::OnBnClickedButton1() //控件1对应的函数 按键1的处理
{
if (setnew) C计算器Dlg::OnBnClickedButton23(); else Invalidate();
innum=1;
C计算器Dlg::inum(innum);
UpdateData(false);
}
void C计算器Dlg::OnBnClickedButton2() //控件2对应的函数 按键2的处理
{
if (setnew) C计算器Dlg::OnBnClickedButton23(); else Invalidate();
innum=2;
C计算器Dlg::inum(innum);
UpdateData(false);
}
void C计算器Dlg::OnBnClickedButton3() //控件3对应的函数 按键3的处理
{
if (setnew) C计算器Dlg::OnBnClickedButton23(); else Invalidate();
innum=3;
C计算器Dlg::inum(innum);
UpdateData(false);
}
void C计算器Dlg::OnBnClickedButton4() //控件4对应的函数 按键4的处理
{
if (setnew) C计算器Dlg::OnBnClickedButton23(); else Invalidate();
innum=4;
C计算器Dlg::inum(innum);
UpdateData(false);
}
void C计算器Dlg::OnBnClickedButton5() //控件5对应的函数 按键5的处理
{
if (setnew) C计算器Dlg::OnBnClickedButton23(); else Invalidate();
innum=5;
C计算器Dlg::inum(innum);
UpdateData(false);
}
void C计算器Dlg::OnBnClickedButton6() //控件6对应的函数 按键6的处理
{
if (setnew) C计算器Dlg::OnBnClickedButton23(); else Invalidate();
innum=6;
C计算器Dlg::inum(innum);
UpdateData(false);
}
void C计算器Dlg::OnBnClickedButton7() //控件7对应的函数 按键7的处理
{
if (setnew) C计算器Dlg::OnBnClickedButton23(); else Invalidate();
innum=7;
C计算器Dlg::inum(innum);
UpdateData(false);
}
void C计算器Dlg::OnBnClickedButton8() //控件8对应的函数 按键8的处理
{
if (setnew) C计算器Dlg::OnBnClickedButton23(); else Invalidate();
innum=8;
C计算器Dlg::inum(innum);
UpdateData(false);
}
void C计算器Dlg::OnBnClickedButton9() //控件9对应的函数 按键9的处理
{
if (setnew) C计算器Dlg::OnBnClickedButton23(); else Invalidate();
innum=9;
C计算器Dlg::inum(innum);
UpdateData(false);
}
////////////////////////////////////////////////结束////////////////////////////////////////////////////////
///////////////////////////////////以下部分处理方式相似(双元运算)////////////////// /////
void C计算器Dlg::OnBnClickedButton16() //控件+对应的函数. 下面的 - * / x^y 同 +符号的处理
{
C计算器Dlg::OnBnClickedIsButton(); // 自动完成上次未完成的运算.当上一次计算完双元运算没有按等号键时候,如只按 //下1 + 1而没有按=的时候,直接按+.可以实现连续计算,从而省去每次都按=的麻烦, //更符合使用的习惯.
n_2=n; // 因为是双元运算,按下+后,把输入值存入另一个数据中,开始输入新的数据
C计算器Dlg::setempty(); // 置空n的各项参数,便于以后重新输入新n值时不影响
setnew=false; //setnew=false(由数字录入部分做出相应).不是在重新输入数据,下面按数字键时接着录入部分执行相应操作
operate='+'; // 操作符为+,,由OnBnClickedIsButton()处理加法运算
}
void C计算器Dlg::OnBnClickedButton15() //控件-对应的函数. -符号的处理
{
C计算器Dlg::OnBnClickedIsButton();
n_2=n;
C计算器Dlg::setempty();
setnew=false;
operate='-';
}
void C计算器Dlg::OnBnClickedButton14() //控件*对应的函数. *符号的处理
{
C计算器Dlg::OnBnClickedIsButton();
n_2=n;
C计算器Dlg::setempty();
setnew=false;
operate='*';
}
void C计算器Dlg::OnBnClickedButton13() //控件/对应的函数. /符号的处理
{
C计算器Dlg::OnBnClickedIsButton();
n_2=n;
C计算器Dlg::setempty();
setnew=false;
operate='/';
}
void C计算器Dlg::OnBnClickedButton27() //控件x^y对应的函数. x^y符号的处理
{
C计算器Dlg::OnBnClickedIsButton();
n_2=n;
C计算器Dlg::setempty();
operate='y';
setnew=false;
}
////////////////////////////////////////////////结束///////////////////////////////////////////////////////
////////////////////////////////////////以下部分处理方式相似(单元运算)//////////////////
void C计算器Dlg::OnBnClickedButton19() //控件1/x对应的函数. 下面符号处理类似 1/x符号的处理
{
operate='x'; //下面执行1/x的运算
C计算器Dlg::operation(operate); // 单元运算,按下该符号直接进行运算
UpdateData(false);
operate='0'; //在这个运算符算出的数据后,输入另外一个数据时候,按下双元运算符因为会先执行OnBnClickedIsButton(),操 //作符为0,什么也不执行
setnew=true; //在数字录入部分做出判断, setnew=true按下数字键时候,执行OnBnClickedButton23(),清空变量.开始新的运算
void C计算器Dlg::OnBnClickedButton17() // 控件sqrt对应的函数. sqrt符号的处理
{
operate='q';
C计算器Dlg::operation(operate);
UpdateData(false);
operate='0';
setnew=true;
}
void C计算器Dlg::OnBnClickedButton24() // 控件sin对应的函数. sin符号的处理
{
operate='s';
C计算器Dlg::operation(operate);
UpdateData(false);
operate='0';
setnew=true;
}
void C计算器Dlg::OnBnClickedButton25() // 控件cos对应的函数. cos符号的处理
{
operate='c';
C计算器Dlg::operation(operate);
UpdateData(false);
operate='0';
setnew=true;
}
void C计算器Dlg::OnBnClickedButton26() // 控件tan对应的函数. tan符号的处理
{
operate='t';
C计算器Dlg::operation(operate);
UpdateData(false);
operate='0';
setnew=true;
}
void C计算器Dlg::OnBnClickedButton29() // 控件ln对应的函数. ln符号的处理
{
operate='n';
C计算器Dlg::operation(operate);
UpdateData(false);
operate='0';
setnew=true;
}
void C计算器Dlg::OnBnClickedButton30() // 控件lg对应的函数. lg符号的处理
{
operate='g';
C计算器Dlg::operation(operate);
UpdateData(false);
operate='0';
setnew=true;
}
////////////////////////////////////////////////结束////////////////////////////////////////////////
////////////////////////////////////////以下为主要处理函数//////////////////////////////
void C计算器Dlg::OnBnClickedIsButton() // 控件=对应的函数. =符号的处理
{
C计算器Dlg::operation(operate); // 完成之前输入数据的计算
operate='0'; // 置空当前输入符号,避免连续+等操作,采用错误操作符
setnew=true; // 开始执行新的操作了
UpdateData(false);
}
void C计算器Dlg::OnBnClickedButton21() // 控件Backspace对应的函数. 删除一个字符 Backspace符号的处理
{
if (n==0) {xiaoshui=0;} // n=0时候,把小数点置空.
else if (xiaoshui>0) // n为小数时候的处理方案
{
n=(long (n*pow(10.0,(xiaoshui-1))))*pow(10.0,-(xiaoshui-1)); // 把该数乘以(原小数位数-1)得到数取整后在扩大,
xiaoshui=xiaoshui-1; // 每次执行删除一位,小数点要前移一位. // 相同倍数即间接删除小数的最后一位了
if (xiaoshui==0) setxiaoshu=false; // 如果小数点为0了,执行setxiaoshu=false,结束小数输入过程
setnew=false; // 不是重新输入数据,这点同所有的setnew=false
}
else //n为整数
{
n=long(n/10); // n除以10,取整.
xiaoshui=0; // 小数点为0
setnew=false; // 可以继续输入数据
}
UpdateData(false);
Invalidate(); // 同所有的Invalidate(),重新执行:OnPaint(),生成新的颜色
}
void C计算器Dlg::setempty(void) // 置空中间变量
{
xiaoshui=0; // 小数部分的位数变量为0
setxiaoshu=false; // 使默认输入变成整数
n=0; // 重置n为0,继续输入第二个运算的数据
}
void C计算器Dlg::OnBnClickedButton22() // 控件CE对应的函数. CE符号的处理: 清除当前输入 CE符号的处理
{
C计算器Dlg::setempty(); //见上函数
UpdateData(false);
Invalidate();
}
void C计算器Dlg::OnBnClickedButton23() //控件C对应的函数. C符号的处理:所有置空 ,C和CE区别就是C清空所有变
{ //量,CE仅清空当前输入的//数据 C符号的处理
C计算器Dlg::setempty();
n_2=0;
operate='0';
UpdateData(false);
Invalidate();
}
void C计算器Dlg::OnBnClickedButton11() //控件+/-对应的函数. 改变正负号 +/-的处理
{
n=-n; //按一次改变n为-n
UpdateData(false);
Invalidate();
}
void C计算器Dlg::inum(int i) // 输入字符为预期数据,i为0-9的变量
{
if (n==fabs(n)) // 判断n的正负(n为正数), 因为负数在录入时候会出现如按下2 – 2时应该显示”-22”但结果却显示”-18”的 { // 情况,分开讨论
if (!setxiaoshu) // 判断是否在输入小数(非按下小数点情况)(n为正数)
n=10*n+i;
else // 按下小数点情况(n为正数)
{
xiaoshui=xiaoshui+1; // 小数正在输入的位数,每输入一次xiaoshui+1 , xiaoshui初始值为0
n=n+pow(10.0,-xiaoshui)*i; // 产生预期的n,
}
}
else // n为负数
{
if (!setxiaoshu) // 非按下小数点情况(n为负数)
{
n=-10*n+i; // 先把负数变为正数,按整数处理,避免负数录入出错
n=-n; // 变成负数
}
else // 按下小数点情况(n为负数)
{
xiaoshui=xiaoshui+1;
n=-n+pow(10.0,-xiaoshui)*i;
n=-n;
}
}
setnew=false;
}
void C计算器Dlg::OnBnClickedButton12() // 控件.对应的函数. 小数点的判断 .的处理
{
setxiaoshu=true; // 按下开始输入小数部分
Invalidate();
}
void C计算器Dlg::operation(char ope) //执行选定运算
{
switch (ope)
{
case '+':{n=n_2+n;}break;
case '-':{n=n_2-n;}break;
case '*':{n=n_2*n;}break;
case '/':{n=n_2/n;}break;
case 'x':{n=1/n;}break;
case 'q':{if (n>0) n=sqrt(n); else n=0;}break;
case 's':{n=sin(double (n*3./180));}break;
case 'c':{n=cos(double (n*3./180));}break;
case 't':{n=tan(double (n*3./180));}break;
case 'n':{if (n>0)n=log(n);else n=0;}break;
case 'g':{if (n>0) n=log(n)/log(10.0);else n=0;}break;
case 'y':{n=pow(n_2,n);}break;
default:; //operate='0',不执行任何代码
}
Invalidate(); //这里加上该函数可以使包含该函数的函数不再重复添加该函数就实现每次按键变色效果
}
试一下吧,应该还行!
⑵ 我刚学mfc,课程设计要求用c++做一个mfc四则运算计算器
我写过一个Qt的计算器,基本逻辑都写出来了,给你参考一下回(没学过MFC),链接在答下面
cout<<"https://github.com/Charles-Neil/NaiveCalculator"
⑶ C语言高手来! MFC 多则运算计算器(用栈实现)
#pragma warning (disable : 4786)
#include <iostream>
#include <string>
#include <map>
#include <cctype>
using namespace std;
enum Token_value
{
NAME,
NUMBER,
END,
PLUS = '+',
MINUS = '-',
MUL = '*',
DIV = '/',
PRINT = ';',
ASSIGN = '=',
LP = '(',
RP = ')',
};
Token_value curr_tok = PRINT;
map<string, double> table;
double term(bool get);
double prim(bool get);
Token_value get_token();
double error(const string &s);
double expr(bool get)
{
double left = term(get);
for(;;)
{
switch (curr_tok)
{
case PLUS:
left += term(true);
break;
case MINUS:
left -= term(true);
break;
default:
return left;
}
}
}
double term(bool get)
{
double left = prim(get);
for(;;)
{
switch(curr_tok)
{
case MUL:
left *= prim(true);
break;
case DIV:
if(double d = prim(true))
{
left /= d;
break;
}
return error("divide by 0");
default:
return left;
}
}
}
double number_value;
string string_value;
double prim(bool get)
{
if(get) get_token();
switch(curr_tok)
{
case NUMBER:
{
double v = number_value;
get_token();
return v;
}
case NAME:
{
double &v = table[string_value];
if(get_token() == ASSIGN) v = expr(true);
return v;
}
case MINUS:
return -prim(true);
case LP:
{
double e = expr(true);
if(curr_tok != RP) return error(")expected");
get_token();
return e;
}
default:
return error("primary expected");
}
}
int no_of_errors;
double error(const string &s)
{
no_of_errors++;
cerr << "error:" << s << '\n';
return 1;
}
Token_value get_token()
{
char ch = 0;
cin >> ch;
switch( ch )
{
case 0:
return curr_tok = END;
case ';':
case '*':
case '/':
case '+':
case '-':
case '(':
case ')':
case '=':
{
return curr_tok = Token_value(ch);
}
case '0': case '1': case '2':
case '3': case '4': case '5':
case '6': case '7': case '8':
case '9':
case '.':
{
cin.putback(ch);
cin >> number_value;
return curr_tok = NUMBER;
}
default:
{
if(isalpha(ch))
{
cin.putback(ch);
cin >> string_value;
return curr_tok = NAME;
}
error("bad token");
return curr_tok = PRINT;
}
}
}
int sum (int a, int b)
{
int c = 0;
c = a + b;
return c;
}
int main()
{
while(cin)
{
get_token();
if(curr_tok == END) break;
if(curr_tok == PRINT) continue;
cout << expr(false) << endl;
}
return no_of_errors;
getchar();
return 0;
}
输入方式如:(1+4)*(2+3)= 最后按回车计算出结果
⑷ 使用MFC制作简单计算器
void CCalDlg::OnButtonadd()
{
// TODO: Add your control notification handler code here
UpdateData();
isresult=TRUE;
num1 = m_edit;
m_edit="";
UpdateData(false);
oper=1;
}
void CCalDlg::OnButtondeng()
{
// TODO: Add your control notification handler code here
UpdateData();
num2 = m_edit;
double result=0;
double firnum=atof(num1);
double secnum=atof(num2);
switch(oper)
{case 1:result=firnum+secnum;
m_edit.Format("%g",result);
UpdateData(false);
break;}
}
在使用m_edit前,要先updatedata更新控件内容到变量。
⑸ vc++6.0课程设计计算器 跪求!!!急!急!急!
可以看下C++ in action 这本书,上面有个很好的例子
⑹ 求计算器MFC制作的程序 谢谢各位高手 急用!
MFC计算器概要
-----大四上学期课程设计笔记
(1)预定义相关变量
(2)小数点的设定
void MyDialog_CAL_2::OnPoint()
{
point=TRUE; //表示.后面的输入部分为小数部分(BOOL类型)
}
(3)数字键1的处理
void MyDialog_CAL_2::OnButton1()
{
if(point)
{
input_point=input_point+1/count; //输入小数部分
count=count*10;
}
else input_int=input_int*10+1; //输入整数部分
//以下主要是处理在编辑框显示的问题罢了,于以后的计算无关!
value=input_int+input_point;
_gcvt(value,10,middle); //value转化为middle(char类型)
m_Edit=(LPCTSTR)middle; //将输入的数据现实在编辑框中
UpdateData(FALSE); //更新:把数据传给对话框
} //其余键位类似
(4)除号键位的设置
void MyDialog_CAL_2::OnDev()
{
id=4;
input1=input_int+input_point;
point=FALSE;
input_int=0;
input_point=0;
count=10;
}
(5)等号键位的设置
void MyDialog_CAL_2::OnResult()
{
input2=input_int+input_point;
point=FALSE;
input_int=0;
input_point=0;
count=10;
switch(id)
{
case 1:
value=input1+input2;
break;
case 2:
value=input1-input2;
break;
case 3:
value=input1*input2;
break;
case 4:
value=input1/input2;
break;
}
_gcvt(value,10,middle);
m_Edit=(LPCTSTR)middle;
UpdateData(FALSE);
}
(5)清空编辑框的键位设置
void MyDialog_CAL_2::OnEmp() //清空编辑框
{
_gcvt(0,10,middle);
m_Edit=(LPCTSTR)middle;
UpdateData(FALSE);
}
⑺ 跪求一个MFC制作的简单计算器带报告 T T
计算机系统设计报告
一、 计算器系统开发设计思想
1、 试验目的
运用所学知识,通过实践加强对所学知识的理解和巩固,增强对相关知识的认识,提高应用所学知识在世界中发现问题、分析问题和解决问题的能力。
2、 试验内容
设计一个多功能计算软件实现功能:
1)具备整型数据、浮点型数据的算术(加、减、乘、除)运算功能。依次输入第一个运算数、运算符(+,-,*,/)、第二个运算数,然后输出结果。结果可以作为下一个运算的第一运算数。按‘C’清屏,按‘R’返回 菜单。
例如:输入:2
+
5
输出:7
2)实现单运算符表达式计算的功能。输入的操作数可以包含整数或浮点数。输入表达式如下:
例如:输入:2+5在
输出:7
二、 计算器系统功能及系统设计介绍
1、 界面设计
创建一个基本对话框的MFC应用程序,在对话框窗体上创建一个计算器所需的按钮。并修改按钮的属性,将个按钮的ID改为向对应的符号,如将 的ID改为IDC_zhengfu。并对编辑添加成员变量m_result用以显示结果。
2、对主要成员函数编写代码
(1) 对头文件jsDlg.h编写代码
在头文件中手动添加成员函数与成员函数变量,代码如下:
//定义两个euum枚举类型Operator, CalcError结构
enum Operator { OpNone, OpAdd, OpSubtract, OpMultiply, OpDivide };
enum CalcError { ErrNone, ErrDivideByZero };
double m_operand; //存储当前输入的操作数
double m_accum; //存储当前的计算处理结束
BOOL m_bCoff; //标识当前输入是否是小数
double m_coff; //小数输入时的系数
Operator m_operator; //定义 枚举变量 m_operator 用以标识当前运算符
CalcError m_errorState; // 定义 枚举 变量m_errorState 用以标识当前运算状态
BOOL m_bOperandAvail; //标识当前输入是否是新输入数字
void UpdateDisplay(); //成员函数处理显示
void Calculate(); //成员函数处理计算
void OnOperandInput(int a); //成员函数处理数字输入
(2)对原文件jsDlg.cpp编写代码
因为该系统运算过程中需要使用平方跟函数,所以在头文件中要添加头文件#include “math.h”。然后对声明变量进行初始化:
jsDlg::jsDlg(CWnd* pParent /*=NULL*/)
: CDialog(jsDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCaaDlg)
m_result = _T(“”); //默认为m_result变量初始化
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_coff=0.1; //为变量进行初始化
m_bCoff=0; 为变量进行初始化
m_errorState = ErrNone; //为变量进行初始化
m_bOperandAvail=FALSE; //为变量进行初始化
m_operator=OpNone; //为变量进行初始化
}
(3)对各控件编写代码
void jsDlg ::On0() //处理“ 0”按钮
{
// TODO: Add your control notification handler code here
OnOperandInput(0);
}
void jsDlg::On1() //处理“ 1”按钮
// TODO: Add your control notification handler code here
OnOperandInput(1);
}
数字“0-9”控件的代码同上类似
void CJsDlg::Onjia()
{
// TODO: Add your control notification handler code here
Calculate();
m_operator = OpAdd;
}
void CJsDlg::Onjian()
{
// TODO: Add your control notification handler code here
Calculate();
m_operator = OpSubtract;
}
void CJsDlg::Oncheng()
{
// TODO: Add your control notification handler code here
Calculate();
m_operator = OpMultiply;
}
void CJsDlg::Onchu()
{
// TODO: Add your control notification handler code here
Calculate();
m_operator = OpDivide;
}
void CJsDlg::Onzhengfu()
{
// TODO: Add your control notification handler code here
m_operand*=-1;
UpdateDisplay();
}
void CJsDlg::Ondian()
{
// TODO: Add your control notification handler code here
m_bCoff=1;
UpdateDisplay();
}
void CJsDlg::Ondengyu()
{
// TODO: Add your control notification handler code here
Calculate();
m_operator = OpNone;
}
void CJsDlg::Onpingfang()
{
// TODO: Add your control notification handler code here
m_operand*=m_operand;
UpdateDisplay();
}
void CJsDlg::Onsqrt()
{
// TODO: Add your control notification handler code here
m_operand=sqrt(m_operand);
UpdateDisplay();
}
void CJsDlg::Onqinglin()
{
// TODO: Add your control notification handler code here
m_operator = OpNone;
m_operand = 0;
m_accum = 0;
m_bOperandAvail = FALSE;
m_errorState = ErrNone;
m_coff=0.1;
m_bCoff=0;
UpdateDisplay();
}
void CJsDlg::OnR()
{
// TODO: Add your control notification handler code here
OnOK(); // 退出对话框程序
}
(4)编写用于实现计算的自定义函数
在jsDlg.cpp中编写:
void jsDlg ::OnOperandInput(int a) //处理0-9 数字的输入函数
{
if (m_errorState != ErrNone) //判断当前运算是否有误,若有则返回
return;
if (!m_bOperandAvail) //判断是否输入新的数字,1-是 0-否
m_operand = 0;
if(!m_bCoff) //判断是否是小数输入,1-是 0-否
m_operand=m_operand*10+(a);
else
{
m_operand=m_operand+(a)*m_coff;
m_coff*=0.1;
}
m_bOperandAvail=TRUE;
UpdateDisplay(); //更新显示
}
(5)编写处理计算函数
在jsDlg.cpp中编写:
void jsDlg ::Calculate()
{
if (m_errorState != ErrNone)
return;
if (m_bOperandAvail)
{ //如果没有计算符就将m_operand值赋予m_accum
if (m_operator == OpNone)
m_accum = m_operand;
else if (m_operator == OpMultiply)
//否则按计算符进行计算
m_accum *= m_operand;
else if (m_operator == OpDivide)
{
if (m_operand == 0)
m_errorState = ErrDivideByZero;
else
m_accum /= m_operand;
}
else if (m_operator == OpAdd)
m_accum += m_operand;
else if (m_operator == OpSubtract)
m_accum -= m_operand;
}
m_bOperandAvail = FALSE;
m_bCoff=0;
m_coff=0.1;
UpdateDisplay();
}
(6)编写处理显示函数
在jsDlg.cpp中编写:
Void CjsDlg::UpdateDisplay() //处理显示函数
{
if (GetSafeHwnd() == NULL)
return;
if (m_errorState != ErrNone)
m_result="除数不能为零";
else //如果当前计算无错误那么进行显示
{
//如果是输入计算数,那么显示输入情况,如果按下计算符,则显示结果
float lVal = (m_bOperandAvail) ? m_operand : m_accum;
m_result.Format(_T("%f"), lVal); //将float型转化为CString型
int i=m_result.GetLength();
while(m_result.GetAt(i-1)=='0')
{
m_result.Delete(i-1,1);
i-=1;
}
}
//用编辑控件变量m_result改变编辑控件的值,更新显示
UpdateData(FALSE);
}
3、 检查创建工程并运行
三、 计算器系统开发的体会
本次MFC计算器的制作,学到了MFC基本的编程方法,增加了编写程序的能力,对VC++可视化用户界面的理解进一步加深。对类的类的封装和设计也有了一种新的认识,为以后的学习积累了经验。不过,通过这次设计实践,也发现了自己的不足之处。在编写过程中也遇到了困难,请教了许多同学,反复检查之后也终于不问题解决了,做出了自己想要的成果。总之,这次实践然我学到了许多东西,有很大的进步。
这个...嫌长了就自己简略一下...
⑻ 如何用MFC编写一个计算器
嗯,实现如抄下:
用visual stdio 或者 visual c++ 6.0建一个MFC的工程项袭目,默认会自动有一个面板。
然后在面板上添加按钮(button(包括数值和计算的法则)),并为每个button关联一个变量(0-9),对于=则右击选择添加函数,执行具体操作。
再在面板上添加一个可编辑框,用于显示结果,关联到一个变量。
在点击=之后,执行操作的时候,将获取的变量序列通过数据结构(清华大学版中有)中的介绍栈的实例就是讲解如果将计算式表达为后缀表达式,并计算结果,这个就涉及到具体怎么实现算法了,你需要哪一步怎么用的时候再上网查下怎么做,这样即能做出来,而且以后还知道怎么做了。
⑼ C++课程设计(计算器)
我有一个 c#的
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
double a, b, c; int d=0;
bool IsDigit = true;
bool IsNew = true;
public Form1()
{
InitializeComponent();
}
private void btnval0_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (IsNew)
{ txtvalue.Text = btn.Text; IsNew = false; }
else
txtvalue.Text += btn.Text;
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (IsDigit) BtnResult_Click(sender, e);
d = 1;
a = double.Parse(txtvalue.Text);
IsNew = true;
}
private void btnsub_Click(object sender, EventArgs e)
{
if (IsDigit) BtnResult_Click(sender, e);
d = 2;
a = double.Parse(txtvalue.Text);
IsNew = true;
}
private void btnMulti_Click(object sender, EventArgs e)
{
if (IsDigit) BtnResult_Click(sender, e);
d = 3;
a = double.Parse(txtvalue.Text);
IsNew = true;
}
private void btnDiv_Click(object sender, EventArgs e)
{
if (IsDigit) BtnResult_Click(sender, e);
d = 4;
a = double.Parse(txtvalue.Text);
IsNew = true;
}
private void BtnResult_Click(object sender, EventArgs e)
{
b = double.Parse(txtvalue.Text);
switch (d)
{
case 1: c = a + b; break;
case 2: c = a - b; break;
case 3: c = a * b; break;
case 4: c = a / b; break;
}
if(d!=0)txtvalue.Text = c.ToString();
IsNew = true;
}
}
}
⑽ 求一个用vs2010中mfc编写的一个简易计算器,只需要加减乘除即可
http://..com/question/1817825401306349388.html?oldq=1