727 - Equation

All about problems in Volume 7. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Ferdib_BSMRSTU
New poster
Posts: 1
Joined: Wed Sep 24, 2014 2:35 pm

Re: 727 - Equation

Post by Ferdib_BSMRSTU » Wed Sep 24, 2014 2:39 pm

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;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 727 - Equation

Post by brianfry713 » Thu Sep 25, 2014 4:00 am

Try running your code on the sample input.
Check input and AC output for thousands of problems on uDebug!

mohdali231993
New poster
Posts: 11
Joined: Sun Nov 09, 2014 6:46 pm

Re: 727 - Equation

Post by mohdali231993 » Sun Dec 21, 2014 10:37 pm

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;   
}

Tried all samples getting right output but still WA
Help Guys!!! :( :(

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 727 - Equation

Post by brianfry713 » Tue Dec 23, 2014 3:58 am

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!

mohdali231993
New poster
Posts: 11
Joined: Sun Nov 09, 2014 6:46 pm

Re: 727 - Equation

Post by mohdali231993 » Tue Dec 23, 2014 1:47 pm

Accepted
thanks man!! :D :D

hoimo
New poster
Posts: 18
Joined: Sun Sep 23, 2012 3:43 am

Re: 727 - Equation

Post by hoimo » Fri Apr 03, 2015 8:58 pm

Plz hlp.
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");
    }
}

nazmul.7
New poster
Posts: 1
Joined: Sun Nov 20, 2016 10:47 am

Re: 727 - Equation

Post by nazmul.7 » Sun Nov 20, 2016 11:56 am

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 !

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;

    }
}

Post Reply

Return to “Volume 7 (700-799)”