Why I am facing run time error in ansi c ?
Here's the code:
#include<stdio.h>
#include<string.h>
char stack[100];
int top = -1;
push(char symb)
{
stack[++top] = symb;
}
char pop()
{
return (stack[top--]);
}
int pr(char n)
{
switch (n)
{
case '#':
return 0;
case '(':
return 1;
case '+':
case '-':
return 2;
case '*':
case '/':
return 3;
}
}
int check(char n)
{
if((n>=48&&n<=57)||(n>=65&&n<=90)||(n>=97&&n<=122))
return 1;
else
return 0;
}
int main()
{
char infx[500],postfix[500],ch,symb;
int i=0,j=0;
while(scanf("%s",infx)==1)
{
push('#');
printf("\n");
while ((ch = infx[i++]) != '\0')
{
if (ch == '(')
push(ch);
else if (check(ch))
postfix[j++] = ch;
else if (ch == ')')
{
while(stack[top] != '(')
postfix[j++] = pop();
symb = pop();
}
else
{
while (pr(stack[top]) >= pr(ch))
postfix[j++] = pop();
push(ch);
}
}
while (stack[top] != '#')
postfix[j++] = pop();
postfix[j] = '\0';
printf("%s\n",postfix);
printf("\n");
}
return 0;
}
727 - Equation
Moderator: Board moderators
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 727 - Equation
Try running your code on the sample input.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 11
- Joined: Sun Nov 09, 2014 6:46 pm
Re: 727 - Equation
Code: Select all
#include <iostream>
#include <string>
#include <stack>
#include <cstdio>
using namespace std;
typedef unsigned int uint;
int main()
{
uint t;
cin>>t;
int array[127] = { };
array[40]=1;
array[41]=1;
array[43]=2;
array[45]=2;
array[42]=3;
array[47]=3;
char c;
uint tt=t;
while(t)
{
string s,cc;stack<char> ss;
if(t==tt){getline(cin, cc);getline(cin, cc);}
getline(cin, cc);
c=cc[0];
while(cc.length() > 0)
{
if((int)c >=48 && int(c)<=57)
{s+=c;
}
else
{
if(ss.empty())
{ss.push(c);
}
else
{
if(array[(int)ss.top()] < array[c] || (array[c] == 1 && (int)c == 40)){ss.push(c);
}
else
{
while(!ss.empty())
{
if(array[(int)ss.top()] < array[c]){break;}
if(array[(int)ss.top()] > 1){s+=ss.top();}
if(array[(int)ss.top()] == 1){ss.pop();break;}
ss.pop();
}
if(array[c] > 1)ss.push(c);
}
}
}
getline(cin, cc);
c=cc[0];
}
cout<<s;while(!ss.empty()){cout<<ss.top();ss.pop();}
cout<<endl;
cout<<endl;
t--;
}
return 0;
}
Help Guys!!!
![:(](./images/smilies/icon_frown.gif)
![:(](./images/smilies/icon_frown.gif)
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 727 - Equation
Print a blank line between different expressions. Don't print an extra blank line at the end.
Check input and AC output for thousands of problems on uDebug!
-
- New poster
- Posts: 11
- Joined: Sun Nov 09, 2014 6:46 pm
Re: 727 - Equation
Accepted
thanks man!!
![:D](./images/smilies/icon_biggrin.gif)
thanks man!!
![:D](./images/smilies/icon_biggrin.gif)
![:D](./images/smilies/icon_biggrin.gif)
Re: 727 - Equation
Plz hlp.
Why RTE!
Why RTE!
Code: Select all
#include<stdio.h>
#include<string.h>
int i,j,cas;
char c, stk[100],p[100];
int main()
{
scanf("%d",&cas);
getchar();
getchar();
while( cas-- )
{
c=getchar();
i=1;
j=0;
stk[0]='0';
while( c!='\n' )
{
getchar();
if( c=='(' ){
stk[i++]=c;
}
else if( (c-'0')>=0 && (c-'0')<=9 ){
p[j++]=c;
}
else if( c=='+' || c=='-' ){
if( (stk[i-1]=='*' || stk[i-1]=='/' || stk[i-1]=='+' || stk[i-1]=='-') && (i-1)!=0 ){
while( stk[i-1]!='(' && (i-1)!=0 ){
i--;
p[j++]=stk[i];
}
stk[i]=c;
}
else{
stk[i]=c;
}
i++;
}
else if( c=='*' || c=='/' ){
if( (stk[i-1]=='*' || stk[i-1]=='/') && (i-1)!=0 ){
while( (stk[i-1]=='*' || stk[i-1]=='/') && (i-1)!=0 ){
i--;
p[j++]=stk[i];
}
stk[i]=c;
}
else{
stk[i]=c;
}
i++;
}
else{
i--;
while( stk[i]!='(' && (i-1)!=0 ){
p[j++]=stk[i--];
}
}
c=getchar();
}
i--;
while(i>0){
p[j++]=stk[i];
i--;
}
p[j]='\0';
puts(p);
if( cas!=0 ) printf("\n");
}
}
Re: 727 - Equation
Anyone please tell me why i am getting wrong !
my code pass all complex case i found here (like 1(2+3)9 ).. but still got wrong !
my code pass all complex case i found here (like 1(2+3)9 ).. but still got wrong !
Code: Select all
#include<bits/stdc++.h>
using namespace std;
int isInt(char b)
{
if(b>47 && b<58)
return 1;
else 0;
};
int main ()
{
// freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T,m=0,n=0;
cin >> T;
cin.ignore();
cin.ignore();
while(T--)
{
char a[100];
string f;
stack<char>p;
int len,i,j=0,k=0;
while(getline(cin, f) && f.size()>0)
{
a[k]=f[0];
k++;
}
a[k]='\0';
len=strlen(a);
char s[len];
for(i=0; i<len; i++)
{
if(isInt(a[i])==1)
{
s[j]=a[i];
j++;
}
else
{
if(a[i]==40)
{
p.push(a[i]);
}
else if(a[i]==41)
{
while(p.top()!=40)
{
s[j]=p.top();
j++;
p.pop();
}
p.pop();
}
else
{
if(a[i]==42)
{
while(!p.empty() && p.top()!=40)
{
if(p.top()==47)
{
s[j]=p.top();
j++;
p.pop();
}
else if(p.top()==42)
{
s[j]=p.top();
j++;
p.pop();
break;
}
else break;
}
p.push(a[i]);
}
if(a[i]==47)
{
while(!p.empty() && p.top()!=40)
{
if(p.top()==42)
{
s[j]=p.top();
j++;
p.pop();
}
else if(p.top()==47)
{
s[j]=p.top();
j++;
p.pop();
break;
}
else break;
}
p.push(a[i]);
}
else if(a[i]==43)
{
while(!p.empty() && p.top()!=40)
{
if(p.top()==42)
{
s[j]=p.top();
j++;
p.pop();
}
else if(p.top()==47)
{
s[j]=p.top();
j++;
p.pop();
}
else if(p.top()==45)
{
s[j]=p.top();
j++;
p.pop();
}
else
{
s[j]=p.top();
j++;
p.pop();
break;
}
}
p.push(a[i]);
}
else if(a[i]==45)
{
while(!p.empty() && p.top()!=40)
{
if(p.top()==42)
{
s[j]=p.top();
j++;
p.pop();
}
else if(p.top()==47)
{
s[j]=p.top();
j++;
p.pop();
}
else if(p.top()==43)
{
s[j]=p.top();
j++;
p.pop();
}
else
{
s[j]=p.top();
j++;
p.pop();
break;
}
}
p.push(a[i]);
}
}
}
}
while(!p.empty())
{
s[j]=p.top();
j++;
p.pop();
}
if(m==1)
cout<<endl;
else m=1;
for(i=0; i<j; i++)
cout<<s[i];
cout<<endl;
}
}