使用stack 报错.
在vc6.0中编译没报错,但运行有问题,在vs2003中编译报错.
下面 是代码.
这是一个表达式求解的程序.(利用运算符的优先级)
其中#是整个表达式求解完毕的标志.
#pragma warning(disable:4786)
#include<string>
#include<map>
#include<stack>
#include<iostream>
#include<ctype.h>
using namespace std;
void main()
{
map<string, char>table;
table["++"]='>';
table["+-"]='>';
table["+*"]='<';
table["+/"]='<';
table["+("]='<';
table["+)"]='>';
table["+#"]='>';
table["-+"]='>';
table["--"]='>';
table["-*"]='<';
table["-/"]='<';
table["-("]='<';
table["-)"]='>';
table["-#"]='>';
table["*+"]='>';
table["*-"]='>';
table["**"]='>';
table["*/"]='>';
table["*("]='<';
table["*)"]='>';
table["*#"]='>';
table["/+"]='>';
table["/-"]='>';
table["/*"]='>';
table["//"]='>';
table["/("]='<';
table["/)"]='>';
table["/#"]='>';
table[")+"]='>';
table[")-"]='>';
table[")*"]='>';
table[")/"]='>';
table["))"]='>';
table[")#"]='>';
table["(+"]='<';
table["(-"]='<';
table["(*"]='<';
table["(/"]='<';
table["(("]='<';
table["()"]='=';
table["#+"]='<';
table["#-"]='<';
table["#*"]='<';
table["#/"]='<';
table["#("]='<';
table["##"]='=';
stack<float> num;
stack<char> alp;
alp.push('#');
string s="2+4-(4-4*3)/4";
s+='#';
int i=0;
char c=s[i++];
while(c!='#'||(alp.top)!='#')//这句报错.
{
if(isdigit(c))
{
num.push((float)c-48);
c=s[i++];
}
else
{
string s1;
s1+=alp.top();
s1+=c;
char c1=table[s1];
switch(c1)
{
case'<':
alp.push(c);
c=s[i++];
break;
case'=':
alp.pop();
c=s[i++];
break;
case'>':
float x=num.top();
num.pop();
float y=num.top();
num.pop();
char c2=alp.top();
alp.pop();
switch(c2)
{
case'+':
x=x+y;
break;
case'-':
x=x-y;
break;
case'*':
x=x*y;
break;
case'/':
x=x/y;
break;
}
num.push(x);
break;
}
}
}
cout<<num.top();
}