64,682
社区成员
发帖
与我相关
我的任务
分享
void MySetWindowText(HWND hwnd,TCHAR* cInput,int idEditBox=IDC_EDT);
////////////////////////////////////////////////////////////////////////////////
// Main_OnCommand
////////////////////////////////////////////变量声明
int integer;
int a,b,res;
char op;
char cExpression[255];
Stack<char> sOperator;
Stack<int> sInteger;
////////////////////////////////////////////////
void Main_OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
//Stack<char> s;
switch(id)
{
case IDC_BTN0:MySetWindowText(hwnd,"0");break;
case IDC_BTN1:MySetWindowText(hwnd,"1");break;
case IDC_BTN2:MySetWindowText(hwnd,"2");break;
case IDC_BTN3:MySetWindowText(hwnd,"3");break;
case IDC_BTN4:MySetWindowText(hwnd,"4");break;
case IDC_BTN5:MySetWindowText(hwnd,"5");break;
case IDC_BTN6:MySetWindowText(hwnd,"6");break;
case IDC_BTN7:MySetWindowText(hwnd,"7");break;
case IDC_BTN8:MySetWindowText(hwnd,"8");break;
case IDC_BTN9:MySetWindowText(hwnd,"9");break;
case IDC_BTNPLUS :MySetWindowText(hwnd,"+");break;
case IDC_BTNMINUS :MySetWindowText(hwnd,"-");break;
case IDC_BTNMULTIPLY:MySetWindowText(hwnd,"*");break;
case IDC_BTNDIVIDE :MySetWindowText(hwnd,"/");break;
case IDC_BTNLpar :MySetWindowText(hwnd,"(");break;
case IDC_BTNRpar :MySetWindowText(hwnd,")");break;
case IDC_BTNEQUALl://这里响应了“=”按键
{
ZeroMemory(cExpression,255);
GetWindowText(GetDlgItem(hwnd,IDC_EDT),cExpression,255);
stringWork(cExpression);
sOperator.push('#');
int i=0;
while(cExpression[i]!='#'||sOperator.retrieve()!='#')//当表达式和栈中元素均为'#'时。结束
{
if(readInteger(cExpression,i,integer))
{
sInteger.push(integer);
i++;//取下一个字符;
}
else
switch(PRICompare(sOperator.retrieve(),cExpression[i]))
{
case -1:
{
sOperator.push(cExpression[i]);
i++;
}
break;
case 0://脱括号,并接受下一字符
{
sOperator.pop(op);
i++;
}
break;
case 1://退栈并将运算结果入栈。
{
sOperator.pop(op);
sInteger.pop(b);
sInteger.pop(a);
calculate(op,a,b,res);
sInteger.push(res);
}
break;
case 2:
MessageBox(NULL,"表达式存在语法错误","语法错误",MB_ICONERROR||MB_OK);
break;
default:break;
}//end switch
}//end while
ZeroMemory(cExpression,255);
itoa(sInteger.retrieve(),cExpression,10);
SetWindowText(GetDlgItem(hwnd,IDC_EDT),cExpression);
}
break;
default:break;
}
}
void MySetWindowText(HWND hwnd,TCHAR* cInput,int idEditBox)
{
TCHAR cCurrent[255];
ZeroMemory(cCurrent,255);
GetWindowText(GetDlgItem(hwnd,idEditBox),cCurrent,255);
lstrcat(cCurrent,cInput);
SetWindowText(GetDlgItem(hwnd,idEditBox),cCurrent);
}
#ifndef _ASSIST_H
#define _ASSIST_H
int ChtoInt(char p);
int PRICompare(char oper1, char oper2);
bool readInteger(const char* cStr, int& index,int& iResult);
void calculate(char oper,int a,int b,int& c);
void stringWork(char* str);
#endif