3 solutions
-
4
这道题了解中缀转后缀的运算规则即可: 如遇数字直接输出,若遇左括号直接入栈,若遇右括号则持续输出栈中内容直到遇到左括号为止(左右括号均不输出仅需在栈中删除)若遇到操作符需持续输出栈中内容直到遇到比当前操作符的优先级低的为止再将该操作符压入栈中,最后读到末尾将栈中所有内容输出即可。
#include<bits/stdc++.h> using namespace std; int main() { char i[1005]; stack<char> pp; scanf("%s",i); for(int c=0;c<strlen(i);c++){ int k=i[c]-'0'; if(k>=0&&k<=9) cout<<k; else{ if(pp.empty()) pp.push(i[c]); else if(i[c]=='+'||i[c]=='-'){ while(pp.top()!='('){ cout<<pp.top(); pp.pop(); if(pp.empty()) break; } pp.push(i[c]); }else if(i[c]=='*'||i[c]=='/'){ while(pp.top()=='*'||pp.top()=='/'){ cout<<pp.top(); pp.pop(); if(pp.empty()) break; } pp.push(i[c]); }else if(i[c]=='(') pp.push(i[c]); else if(i[c]==')'){ while(pp.top()!='('){ cout<<pp.top(); pp.pop(); } pp.pop(); } } } while(!pp.empty()){ cout<<pp.top(); pp.pop(); } return 0; }
-
1
可以通过自己//几行来看运行步骤 写得不好,仅供参考
#include <bits/stdc++.h> using namespace std; int main() { char a[1005]; cin>>a; int len=strlen(a); stack<char>s; for(int i=0;i<len;i++){ if(a[i]>=48&&a[i]<=58) cout<<a[i]; else{ if(a[i]=='('){ s.push(a[i]); } else if(a[i]=='+'||a[i]=='-'){ if(s.empty()||s.top()=='('){ // cout<<" in "; s.push(a[i]); } else{ while(!s.empty()){ if(s.top()!='('){ // cout<<" chu "; cout<<s.top(); s.pop();} // cout<<"g";} else{ // cout<<"qaq"; break; } } // cout<<" ru "; s.push(a[i]); } } else if(a[i]=='*'||a[i]=='/'){ if(s.empty()||s.top()=='('||s.top()=='+'||s.top()=='-') s.push(a[i]); else{ while(s.top()!='+'&&s.top()!='-'&&s.top()!='('&&!s.empty()){ cout<<s.top(); // cout<<" chengchu "; s.pop(); } s.push(a[i]); } } else if(a[i]==')'){ while(s.top()!='('){ cout<<s.top(); s.pop(); } if(s.top()=='('){ s.pop();} } } } while(!s.empty()){ cout<<s.top(); s.pop(); } return 0; }
-
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; }
- 1
Information
- ID
- 294
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- 6
- Tags
- # Submissions
- 83
- Accepted
- 27
- Uploaded By