You are not logged in. Please login at www.codechef.com to post your questions!

×

Wrong answer in ONP-Transform the expression

My code inspite of generating all the test cases is showing wrong answer

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct stack
{
  char a[400];
  int top;
}
stack;
stack s;
void push(char *,int);
int pop();

int main(void)
 {
  char x[400];
  int len,i,n,y;
  scanf("%d",&n);
  while(n>0)
  {
  scanf("%s",x);
  len=strlen(x);
  for(i=0;i<len;i++)
    {
      if((x[i])>=97&&x[i]<=122)
      printf("%c",x[i]);
      else
      if(x[i]=='/'||x[i]=='*'||x[i]=='+'||x[i]=='-'||x[i]=='^')
        {
        push(x,i);
        }
      else if(x[i]=='(')
      continue;
      else
       {
       y=pop();
       printf("%c",y);
       }
    }
    while (s.top !=0)
        {
          y = pop();
          printf("%c", y) ;
        }

    n--;
    }
    return 0;
   }
   void push(char *x,int i)
   {
     s.top++;
     s.a[s.top]=x[i];
   }
   int pop()
   {
     int temp;
     temp=s.a[s.top];
     s.top--;
     return temp;
   }

asked 24 Mar '15, 17:32

artist's gravatar image

1★artist
23214
accept rate: 0%


After each test case u need to move to a new line which ur code is not doing put a printf("\n") for each test case

link

answered 24 Mar '15, 17:40

sharru05's gravatar image

3★sharru05
5591723
accept rate: 14%

You need to print each output ( expressions in RPN form ) at a new line and it will be accepted.
In your code, add a new line after exhausting the stack contents. while (s.top !=0) { y = pop(); printf("%c", y) ; } printf("\n"); // new line after the stack elements are popped n--;

Check your accepted code with the above modification here.

link

answered 24 Mar '15, 17:40

mediocoder's gravatar image

3★mediocoder
1.1k313
accept rate: 36%

edited 24 Mar '15, 17:46

Yes now its working Thanks.)

(24 Mar '15, 17:52) artist1★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,490
×1,409
×173

question asked: 24 Mar '15, 17:32

question was seen: 1,084 times

last updated: 24 Mar '15, 17:52