//win32gui版计算器
//Main.cpp
// jsq.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include "MainDlg.h"
#include <COMMCTRL.H>
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
//Enable IPAddress Calendar.etc
InitCommonControls();
DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, Main_Proc);
return 0;
}
//MainDlg.cpp
#include "stdafx.h"
#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#include "MainDlg.h"
#include "stdio.h"
//#include "string.h"
#include "stdlib.h"
int top=0,topop=1,msgtop=0;
char optr[256]="#";//初始化操作符栈;
char msg[256]="#";//初始表达式栈;
double result[256]={-1};//操作数栈,计算结束后的结果放在了第0个下标中;
double jieguo=0;
void pushmsg(char ch)
{
msg[msgtop++]=ch;//将按键字符数据送入表达式栈msg;
}
void backspace(HWND hwnd)
{
if(0==msgtop)
{
EnableWindow(GetDlgItem(hwnd,IDC_BUTTON20),FALSE);
}
else
{
msg[(--msgtop)--]='\0';//按下backspace后删除前一个字符;
++msgtop;
}
}
double gettopnd()//取得操作数栈顶符号
{
return result[top-1];
}
char gettopop()//取得操作符栈顶符号
{
return optr[topop-1];
}
void pushnd(double ch)//操作数入栈
{
result[top++]=ch;
}
void pushop(char ch)//操作符入栈
{
optr[topop++]=ch;
}
double popnd()//弹出操作数
{
return result[--top];
}
char popop()//弹出操作符
{
return optr[--topop];
}
char IsOprator(char m )//判断字符是否是操作符,是则返回操作符assci,否为0;
{
switch(m)
{
case '+':
case '-':
case '*':
case '/':
case '(':
case ')':
case '#':return m;break;
default: return 0;
}
}
int FmtStr(char msg[],int N)//格式化表达式,主要是将负数(-x)转化为(0-x)的形式以统一使用后续算法,同理可增加新的计算功能;
{
int tmp=0;//临时变量,主要是保存表达式增加的位数(表达式增加的长度)
for (int i=0;i<N-1;i++)//总共循环表达式的长度那么多次;
{
if(msg[i]=='-'&&msg[i-1]=='(')//取当前字符并判断该字符是否是负号且它的上一个字符是左括号,确定这个符号为负而不是减;
{
for(int t=N+1;t!=i;t--)//若为负号,则向后取数直到遇到右括号时终止;
{
msg[t]=msg[t-1];//将负号后,右括号前的所有字符作为一个操作数;
}
m