Page 7 of 11

Re: 727 - Equation

Posted: Fri May 23, 2008 6:27 pm
by sreejond
ac

Re: 727 - Equation

Posted: Sat May 24, 2008 4:53 pm
by sreejond
To jan vai,
Vhaia please help me. Where is the fault in my code. :(
sreejon
CUET

Re: 727 - Equation

Posted: Mon May 26, 2008 12:39 am
by Jan
Try the case.

Input:

Code: Select all

1

0
*
8
-
4
*
6
+
4
/
7
*
2
Output:

Code: Select all

08*46*-47/2*+
Hope it helps.

Re: 727 - Equation

Posted: Sun Jul 20, 2008 8:03 am
by waliliadai
I tried all test cases on the thread, but I still got WA.
What's wrong with my code? :cry:

Code: Select all

#include<iostream>
using namespace std;

int main(){
  char t, stack[50+1] = {'@'};
  int n, pri[255] = {}, top;
  pri['@'] = -1, pri['+'] = pri['-'] = 1, pri['*'] = pri['/'] = 2,
  pri['('] =  4, pri[')'] = 0;
  scanf("%d%*c%*c", &n);
  while(n--){
    top = 1;
	  while(t = getchar(), (t != '\n' && t != EOF)){
			getchar();
			if(t >= '0' && t <= '9'){
			  putchar(t);
	    }else{
			  while(pri[stack[top-1]] >= pri[t] && stack[top-1] != '('){
	        putchar(stack[--top]);
		  	}
			  (t != ')')?(stack[top++] = t):(top--);
		  }
		}
		while(--top){
		  putchar(stack[top]);						 
		}
		if(n){
  		printf("\n\n");
		}
	}
  //system("pause");
  return 0;
}

727 - Equation Getting RUNTIME Error

Posted: Mon Jul 28, 2008 10:34 am
by assasin
I ran my code in my pc , also for multiple inputs , but never got any runtime error
Can anyone help me to take input in the right way avoiding RunTime error

Here is the Code:

Code: Select all

#include <stdio.h>
#include <stack>
using namespace std;


int main(){
	
	stack<char> st;
	int cases,count = 0,newline = 0;
	char ch,ch1,str[3];
	
	int prec[200];
	prec['('] = 1 ;
	prec['+'] = 2 ; prec['-'] = 2 ;
	prec['*'] = 3 ;
	prec['\''] = 3;

	
	
	scanf("%d",&cases);
	scanf("%c",&ch);
	scanf("%c",&ch);

	while( count ++ < cases ){
		
			if(newline++)
				printf("\n");
		
		while(gets(str) != NULL && str[0] != '\0' ){
			
			ch = str[0];
			if( ch >= 48 && ch <= 57)
				printf("%c",ch);
			else if( ch == '(')
				st.push(ch);
			else if(ch == ')'){
			
				while( st.top() != '(' ){
				
					printf("%c",st.top());
					st.pop();
				}
				
				st.pop();	
			}
			
			else {//operator
			
				while(!st.empty() && prec[ch] <= prec[st.top()]){
				
					printf("%c",st.top());
					st.pop();
				}
				st.push(ch);	
			}
		
			
		}
		
		while(!st.empty()){
				printf("%c",st.top());
				st.pop();
			}
		
		printf("\n");
		
	}
}

Re: 727 - Equation

Posted: Fri Nov 07, 2008 10:09 am
by LMG
Hello,
I've passed all the test cases in this thread but still got WA. Would somebody give me any idea about my code?
Also appreciate critical test cases.

problem solved.

just be careful the line below.

cout << endl;
if (test_case != 1) //lacked this line, WA!!!!!!!
cout << endl;

Re: 727 - Equation

Posted: Tue Jun 23, 2009 5:16 am
by lnr
Hello,
Someone please tell how to take input for this problem.

Re: 727 - Equation

Posted: Tue Sep 08, 2009 11:24 am
by aliahmed
I'm tired of getting wa in 727...

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

int main()
{
char stack[100],p[100],c,str[100];
long i,j,n,l,top,f,count,count1,cc;


scanf("%ld",&n);
getchar();

while(n)
{
memset(0,sizeof(stack),0);
memset(0,sizeof(p),0);
memset(0,sizeof(str),0);
i=0;
count=0;
count1=0;
cc=0;
while(1)
{
scanf("%c",&c);
if(c=='\n')
{
cc++;
if(cc==2)
break;
continue;
}

str[i++]=c;
cc=0;
}
str=')';
str[i+1]='\0';
l=i;
top=0;
stack[top++]='(';

j=0;
f=0;

for(i=0; i<=l ; i++)
{
if(str>=48 && str<=57)
p[j++]=str;

else if(str=='(')
stack[top++]=str;

else if(str=='/' || str=='*' || str=='+' || str=='-')
{
if(((stack[top-1]=='*' || stack[top-1]=='/') || ((stack[top-1]=='+' || stack[top-1]=='-') && (str[i]=='+' || str[i]=='-'))) && top>=1)
{
p[j++]=stack[top-1];
stack[top-1]=str[i];
}
else
stack[top++]=str[i];
}
else if(str[i]==')')
{
while(1)
{
if(stack[top-1]=='+' || stack[top-1]=='-' || stack[top-1]=='*' || stack[top-1]=='/')
p[j++]=stack[top-1];
else if(stack[top-1]=='(')
break;
top--;
}
top--;
}

}

p[j]='\0';
puts(p);
if(n>1)
printf("\n");
n--;
}

return 0;
}

Re: 727 - Equation

Posted: Thu Jun 03, 2010 10:54 am
by rupam016
i am confused about taking input :(

Re: 727 - Equation

Posted: Sun Jul 11, 2010 8:06 am
by nishith
:(
I can't understand , why my code get RE.
It's a very simple code.
please help me.

Code: Select all

// acc

Re: 727 - Equation

Posted: Sun Jul 11, 2010 11:53 pm
by sazzadcsedu
You have problem with taking input. Use gets , it works nicely when input is taken in line by line fashion. Just modify your take_input function in following way and get Acc (yes , i tested).

Code: Select all

void take_input()
{
   char c1;
   k = 0;
   char str[10];
     

   while(gets(str))
   {
	 if(str[0]=='\0')
		   break;
	    
	    c1=str[0];
     
      input[k++] = c1;

         
   }
   
   input[k] = '\0';

}

Re: 727 - Equation

Posted: Mon Jul 12, 2010 5:59 pm
by nishith
Thank you Sazzad vai.
I have got it.
But i have a confusion.
What is relation between my input taking procedure and RE.

Re: 727 - Equation

Posted: Mon Jul 12, 2010 10:52 pm
by sazzadcsedu
your Code is no longer in the board,And i forgot which format u used.But your input taking procedure perhaps does not terminate.

Re: 727 - Equation

Posted: Fri Sep 10, 2010 10:47 am
by iqbal csedu
oh...I have tested almost all test data presented in this post...and all matched
but i can't understand why getting WA....it seems to me problem is in handling input..but cant solve..
is there any one to help me as soon as possible..

Code: Select all

removed after accepted

Re: 727 - Equation

Posted: Sat Sep 11, 2010 9:15 am
by sazzadcsedu

Operators at the same precedence level associate from left to right
. Parentheses act as grouping symbols that over-ride the operator priorities.
think about it.


Try this:-

Code: Select all

input:
1

(
3
*
5
/
2
*
6
/
(
3
+
2
)
*
5
)
+
4

Actual Output:
35*2/6*32+/5*4+

Your Output:
352/*632+/*5*4+