![:D](./images/smilies/icon_biggrin.gif)
![:D](./images/smilies/icon_biggrin.gif)
Moderator: Board moderators
Code: Select all
#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
int main()
{
long long t,k;
cin>>t;
k=t;
getchar();
getchar();
while(t--)
{
if(t!=k-1)
cout<<endl<<endl;
stack<char> st;
long long n;
char infix;
char postfix[51]="",infix_ex[51]="";
long long i=0,j;
while(1)
{
infix=getchar();
if(infix=='\n')
break;
getchar();
infix_ex[i++]=infix;
}
infix_ex[i++]=')';
n=i;
st.push('(');
j=0;
for(i=0;i<n;i++)
{
if( infix_ex[i]=='0' || infix_ex[i]=='1' || infix_ex[i]=='2' || infix_ex[i]=='3' || infix_ex[i]=='4' || infix_ex[i]=='5' || infix_ex[i]=='6' || infix_ex[i]=='7' || infix_ex[i]=='8' || infix_ex[i]=='9' )
{
postfix[j++]=infix_ex[i];
}
if(infix_ex[i]=='(' )
{
st.push('(');
}
if(infix_ex[i]=='*' || infix_ex[i]=='/')
{
while(st.top()=='*' || st.top()=='/')
{
char temp=st.top();
st.pop();
postfix[j++]=temp;
}
st.push(infix_ex[i]);
}
if(infix_ex[i]=='+' || infix_ex[i]=='-')
{
while(st.top()=='*' || st.top()=='/' || st.top()=='+' || st.top()=='-')
{
char temp=st.top();
st.pop();
postfix[j++]=temp;
}
st.push(infix_ex[i]);
}
if(infix_ex[i]==')' )
{
while(st.top()!='(')
{
char temp=st.top();
st.pop();
postfix[j++]=temp;
}
st.pop();
}
}
cout<<postfix;
}
return 0;
}
Code: Select all
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
using namespace std;
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
size_t i;
int n, run=0;
bool first=true;
string str;
cin >> n;
getchar();
getline(cin, str);
while(run < n){
vector<string> eq;
stack<char> op;
if(!first) cout << endl;
first=false;
while(getline(cin, str) && str.size()!=0){
eq.push_back(str);
}
for(i=0 ; i<eq.size() ; ++i){
if(eq[i][0]=='('){
op.push(eq[i][0]);
}
else if(eq[i][0]==')'){
while(op.top()!='('){
cout << op.top();
op.pop();
}
op.pop();
if(!op.empty()){
if(op.top()=='*'||op.top()=='/'){
cout << op.top();
op.pop();
}
}
}
else if(eq[i][0]<='9' && eq[i][0]>='0'){
cout << eq[i];
}
else{
if(op.empty() || (!op.empty() && op.top()=='(')){
op.push(eq[i][0]);
}
else{
if(eq[i][0]=='+'||eq[i][0]=='-'){
if(op.top()=='*'||op.top()=='/'){
cout << op.top();
op.pop();
if(!op.empty()){
cout << op.top();
op.pop();
}
}
else{
cout << op.top();
op.pop();
}
op.push(eq[i][0]);
}
else if(eq[i][0]=='*'||eq[i][0]=='/'){
if(!op.empty() && (op.top()=='+'||op.top()=='-')){
op.push(eq[i][0]);
}
else{
cout << op.top();
op.pop();
op.push(eq[i][0]);
}
}
}
}
}
while(!op.empty()){
cout << op.top();
op.pop();
}
cout << endl;
run++;
}
return 0;
}
Code: Select all
1
1
*
2
(
1
+
2
)
1
+
2
Code: Select all
1212+1*2+
Sorry! I'll post in the exist thread next time.brianfry713 wrote:Next time post in the existing thread. For this input:My AC code prints:Code: Select all
1 1 * 2 ( 1 + 2 ) 1 + 2
Code: Select all
1212+1*2+
Code: Select all
if(!op.empty() && op.top()!='(')
Code: Select all
Code Removed
Got Ac :P
It seems that it's possible that your oper stack gets cleared under some circumstances when it shouldn't, causing the while at line 41 to be skipped (because the stack is empty), but then you pop from oper anyway (line 46), which causes all the troubles.gr81 wrote:getting RE, please find the code at http://ideone.com/yvuQZE
Code: Select all
1
(
5
/
3
+
5
+
0
)
-
4
*
(
2
)
*
0
/
2
-
5
*
5
/
3
/
6
*
2
+
7
-
9
-
2
/
3
Code: Select all
53/5+0+42*0*2/-55*3/6/2*-7+9-23/-