swich关于添加default: cout<<"A wrong mathematical symbol!"<

Max_inner 2016-03-03 12:19:45
#include "..\数据结构\seqstack.h"
const int SIZE= 20;
int icp(char &c)
{
switch(c)
{
case '#': return 0;
case '(': return 7;
case '*':
case '/': return 4;
case '+':
case '-': return 2;
case ')': return 1;
// default: cout<<"A wrong mathematical symbol!"<<endl;return -1;
}
}
int isp(char& c)
{
switch(c)
{
case '#': return 0;
case '(': return 1;
case '*':
case '/': return 5;
case '+':
case '-': return 3;
case ')': return 7;
// default: cout<<"A wrong mathematical symbol!"<<endl;return -1;
}
}
void InfixToPostfix()
{
SeqStack <char> s(SIZE);
char ch,y;
s.Push('#');
while(cin>>ch,ch!='#'){
if (isdigit(ch)||isalpha(ch)) cout<<ch; //扫描到操作数直接输出
else if (ch == ')') //扫描到右括号时的处理
for (s.Top(y),s.Pop();y!='(';s.Top(y),s.Pop()) cout<<y;
else{ //扫描到其他操作符时的处理
if( icp(ch) == -1|| isp(y) == -1 ) break;
else{
for(s.Top(y),s.Pop();icp(ch)<=isp(y);s.Top(y),s.Pop()) //弹出栈顶操作符
cout<<y; //刚弹出的栈顶操作符的优先级高时输出
s.Push(y); //当刚弹出的栈顶操作符的优先级低时,将其重新压回栈中
s.Push(ch); //然后扫描到的操作符进栈
}
}
}
while(!s.IsEmpty()){ //输出栈中剩余的操作符
s.Top(y);
s.Pop();
if( y!= '#') cout<<y;;
}
cout<<endl;
}

加上//default 就会出错 无论输入什么运算符号 都会出现A wrong mathematical symbol!
...全文
143 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dustpg 2016-03-03
  • 打赏
  • 举报
回复
断点调试啊,还有为什么参数用char&, 画蛇添足啊
Max_inner 2016-03-03
  • 打赏
  • 举报
回复
这是stack类 #include<iostream> using namespace std; template <class T> class Stack { public: virtual bool IsEmpty() const=0; virtual bool IsFull() const=0; virtual bool Top(T &x) const=0; virtual bool Push(T x)=0; virtual bool Pop()=0; virtual void Clear()=0; }; 这是stack.h #include "..\数据结构\stack.h" template<class T> class SeqStack:public Stack<T> { public: SeqStack(int mSize); ~SeqStack() {delete []s;} bool IsEmpty() const {return top == -1;} bool IsFull() const {return top == maxTop; } bool Top(T &x) const; bool Push(T x); bool Pop(); void Clear(){ top = -1; } private: int top; //栈顶指针 int maxTop; //最大栈顶指针 T* s; }; template<class T> SeqStack<T>::SeqStack(int mSize) { maxTop=mSize-1; s=new T[mSize]; top=-1; } template<class T> bool SeqStack<T>::Top(T & x) const { if(IsEmpty()){ cout<<"Empty"<<endl; return false; } x=s[top]; return true; } template<class T> bool SeqStack<T>::Push(T x) { if(IsFull()){ //溢出处理 cout<<"Overflow"<<endl; return false; } s[++top]=x; return true; } template <class T> bool SeqStack<T>::Pop() { if(IsEmpty()){ //空栈处理 cout<<"Underflow"<<endl; return false; } top--; return true; } 这是主函数 #include "..\InfixToPostfix.h" int main() { InfixToPostfix(); return 0; } 我只是想能够正确运行 InfixToPostfix() win10,codeblocks 13.12 ,GCC 其它无所谓
qq_22654809 2016-03-03
  • 打赏
  • 举报
回复
添加break
paschen 2016-03-03
  • 打赏
  • 举报
回复
那出现什么错误呢 是否#include <iostream> 并引用了名称空间
天外怪魔 2016-03-03
  • 打赏
  • 举报
回复
贴出完整代码
YXTS122 2016-03-03
  • 打赏
  • 举报
回复
这代码的主函数main能写出来吗?楼主

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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