使用stack 报错.

wzjall 2005-08-14 11:19:09
在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();
}



...全文
212 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
K 2005-08-15
  • 打赏
  • 举报
回复
呵呵,以后要小心
yuchengliu 2005-08-15
  • 打赏
  • 举报
回复
UP
lxk_cool 2005-08-14
  • 打赏
  • 举报
回复
呵呵,以后要小心
snowbirdfly 2005-08-14
  • 打赏
  • 举报
回复
希望下次不会这样!
顶一下~~~
wzjall 2005-08-14
  • 打赏
  • 举报
回复
谢谢楼上的两位,确实太粗心 了.我都检查了好几遍,怎么就没发现呢????
foochow 2005-08-14
  • 打赏
  • 举报
回复
好粗心;-)
Jagen在路上 2005-08-14
  • 打赏
  • 举报
回复
楼主,我无话可说,可能是你的粗心大意。
alp.top --〉alp.top()

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧