3 solutions
-
0
#include<stdio.h> #include<string.h> #include<stack> #include<queue> using namespace std; int main() { char arr[1000]; scanf("%s",arr); stack<char>room; for(int i = 0; i < strlen(arr); i ++) { if(arr[i] >= 48 && arr[i] <= 57) { printf("%c",arr[i]); }//从左到右扫描每一个字符。如果扫描到的字符是操作数(如a、b等),就直接输出这些操作数 //遇到括号 else if(arr[i] == ')' || arr[i] == '(') { if(arr[i] == '(')room.push(arr[i]); else { while(room.top() != '(') { if(room.size() == 0)break; printf("%c",room.top()); room.pop(); } room.pop(); } } else { if(room.size() == 0) { room.push(arr[i]);//如果堆栈是空的,直接将操作符存储到堆栈中 } else { //如果该操作符的优先级大于堆栈出口的操作符, //就直接将操作符存储到堆栈中 if(arr[i] == '*' || arr[i] == '/')room.push(arr[i]); else if(arr[i] == '+' ) { if(room.top() == '+' || room.top() == '-'||room.top() == '(')room.push(arr[i]); else { while(room.size() != '+' || room.top() != '-') { printf("%c",room.top()); room.pop(); if(room.size() == 0)break; } room.push(arr[i]); } } else { if(room.top() == '-' || room.top() == '(')room.push(arr[i]); else { while(room.top() != '-'&&room.top() != '(') { printf("%c",room.top()); room.pop(); if(room.size() == 0)break; } room.push(arr[i]); } } } } } while(room.size() != 0) { printf("%c",room.top()); room.pop(); if(room.size() == 0)break; } return 0; }
Information
- ID
- 294
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- 6
- Tags
- # Submissions
- 83
- Accepted
- 27
- Uploaded By