break;
}
OPTR.pop();
if(OPTR.empty()){
break;
}
r = ranks[OPTR.top()];
}
//如果是(,就直接加入,不用做任何处理
if(c == ')'){
char t = OPTR.top();
r = ranks[OPTR.top()];
while(t != '('){
if(r >= 2){//不加入其他字符
posfix[j++] = OPTR.top();
}
OPTR.pop();
t = OPTR.top();
r = ranks[OPTR.top()];
}
OPTR.pop();//弹出'('
}else{
OPTR.push(c);
}
}
}
}
void print_chars(char* chars){
cout << chars[0];
for(int i = 1; chars[i] != '\0'; i++){
cout << '!' << chars[i];
}
cout << endl;
}
int main(){
// char test1[10] = {'9','-','5','*','(','4','+','3',')','#'};
// char result1[50];
// InfixToPostfix(test1, result1);
// print_chars(result1);
/*output 9!5!4!3!+!*!- */
char test2[14] = {'2','*','(','3','+','4',')','/','(','1','+','1',')','#'};
char result2[50];
InfixToPostfix(test2, result2);
print_chars(result2);
/*output 2!3!4!+!*!1!1!+!/ */
return 0;
}