swich关于添加default: cout<<"A wrong mathematical symbol!"<
#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!