if(!op.empty()){
//用top前必须判断非空!!!
while(mp[s[i]]<=mp[op.top()]&&(!op.empty())){
deal(op.top(),num);
op.pop();
}
}
第一次进入while,op的大小是1,op内容为'+', 执行pop()后,op大小为0,第一次while结束,第二次进入while时候op是空的,所以第二次在while中使用mp[op.top()]时,
op.top()访问失败,再检查下自己的逻辑吧。
这里有个网上的,可以看看
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 200
#pragma warning(disable:4996)
//判断字符是否合理 0-9 . + - * /
bool CheckInput(char c)
{
//0-9
if (0x30 <= c && c <= 0x39)
{
return true;
}
//. + - * /
if (c == 0x2E || c == 0x2B || c == 0x2D || c == 0x2A || c == 0x2F)
{
return true;
}
return false;
}
bool IsNum(char c)
{
//0-9
if (0x30 <= c && c <= 0x39)
{
return true;
}
//. + - * /
if (c == 0x2E)
{
return true;
}
return false;
}
bool IsOperate(char c)
{
// + - * /
if (c == 0x2B || c == 0x2D || c == 0x2A || c == 0x2F)
{
return true;
}
return false;
}
//1.判断表达式是否合理,2.判断数值是否合理
int main(int argc, char**argv)
{
printf("欢迎使用计算器\n");
while (true)
{
printf("==========================\n");
printf("请输入你的计算公式:\n");
char szBuf[MAX_LEN] = { '\0' };
memset(szBuf, 0, MAX_LEN);
scanf("%s", szBuf);
int len = strlen(szBuf);
//校验用户输入字符是否正确
for (int i = 0; i < len; i++)
{
if (false == CheckInput(szBuf[i]))
{
printf("Input Error charater.\n");
return -1;
}
}
printf("You Input:%s\n执行计算中,请稍后...\n", szBuf);
//解析表达式
//计算值
double *pValues = (double *)malloc(sizeof(double));
int iValueIndex = 0;
//操作符
char* pOpera = (char*)malloc(sizeof(char));
int iOperaIndex = 0;
int valIndex = 0;
double fPreValue = 0;
char szTemp[MAX_LEN];
memset(szTemp, 0, MAX_LEN);
for (int i = 0; i < len; i++)
{
if (IsNum(szBuf[i]))
{
szTemp[valIndex++] = szBuf[i];
continue;
}
szTemp[valIndex++] = '\0';
fPreValue = atof(szTemp);
valIndex = 0;
//存储计算值
pValues[iValueIndex] = fPreValue;
iValueIndex++;
pValues = (double *)realloc(pValues, (iValueIndex + 1) * sizeof(double));
//存储操作符
pOpera[iOperaIndex] = szBuf[i];
iOperaIndex++;
pOpera = (char *)realloc(pOpera, (iOperaIndex + 1) * sizeof(char));
}
szTemp[valIndex++] = '\0';
fPreValue = atof(szTemp);
pValues[iValueIndex] = fPreValue;
//执行*/计算 //10*20/23
double dTemp = 0;
for (int i = 0; i < iOperaIndex; i++)
{
if (pOpera[i] == 0x2A) //*
{
dTemp = pValues[i] * pValues[i + 1];
pValues[i] = 0;
pValues[i + 1] = dTemp;
}
else if (pOpera[i] == 0x2F) // /
{
dTemp = pValues[i] / pValues[i + 1];
pValues[i] = 0;
pValues[i + 1] = dTemp;
}
}
//执行+- //10+0*666-9
char cPreOpera = 0;
for (int i = 0; i < iOperaIndex; i++)
{
if (pOpera[i] == 0x2B) //+
{
dTemp = pValues[i] + pValues[i + 1];
pValues[i] = 0;
pValues[i + 1] = dTemp;
cPreOpera = pOpera[i];
}
else if (pOpera[i] == 0x2D) // -
{
dTemp = pValues[i] - pValues[i + 1];
pValues[i] = 0;
pValues[i + 1] = dTemp;
cPreOpera = pOpera[i];
}
else //* /
{
if (cPreOpera == 0x2B) //+
{
dTemp = pValues[i] + pValues[i + 1];
pValues[i] = 0;
pValues[i + 1] = dTemp;
cPreOpera = pOpera[i];
}
else if (cPreOpera == 0x2D) // -
{
dTemp = pValues[i] - pValues[i + 1];
pValues[i] = 0;
pValues[i + 1] = dTemp;
cPreOpera = pOpera[i];
}
}
}
printf("Reslut = %g\n", pValues[iValueIndex]);
delete[] pValues;
delete[] pOpera;
}
// getchar();
return 0;
}