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

Post Reply
User avatar
sreejond
New poster
Posts: 32
Joined: Fri May 23, 2008 6:16 pm
Contact:

Re: 727 - Equation

Post by sreejond » Fri May 23, 2008 6:27 pm

ac
Last edited by sreejond on Thu Jun 04, 2009 2:41 pm, edited 1 time in total.

User avatar
sreejond
New poster
Posts: 32
Joined: Fri May 23, 2008 6:16 pm
Contact:

Re: 727 - Equation

Post by sreejond » Sat May 24, 2008 4:53 pm

To jan vai,
Vhaia please help me. Where is the fault in my code. :(
sreejon
CUET

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Re: 727 - Equation

Post by Jan » Mon May 26, 2008 12:39 am

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.
Ami ekhono shopno dekhi...
HomePage

waliliadai
New poster
Posts: 1
Joined: Sun Jul 20, 2008 7:30 am

Re: 727 - Equation

Post by waliliadai » Sun Jul 20, 2008 8:03 am

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

assasin
New poster
Posts: 7
Joined: Wed Feb 13, 2008 3:59 pm
Location: Dhaka,Bangladesh

727 - Equation Getting RUNTIME Error

Post by assasin » Mon Jul 28, 2008 10:34 am

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

LMG
New poster
Posts: 2
Joined: Sun Aug 20, 2006 11:35 pm

Re: 727 - Equation

Post by LMG » Fri Nov 07, 2008 10:09 am

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;

lnr
Experienced poster
Posts: 141
Joined: Sat Jun 30, 2007 2:52 pm
Location: Dhaka,Bangladesh

Re: 727 - Equation

Post by lnr » Tue Jun 23, 2009 5:16 am

Hello,
Someone please tell how to take input for this problem.

aliahmed
New poster
Posts: 24
Joined: Fri Oct 24, 2008 8:37 pm
Location: CUET, Chittagong, Bangladesh
Contact:

Re: 727 - Equation

Post by aliahmed » Tue Sep 08, 2009 11:24 am

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

rupam016
New poster
Posts: 1
Joined: Thu Jun 03, 2010 10:51 am

Re: 727 - Equation

Post by rupam016 » Thu Jun 03, 2010 10:54 am

i am confused about taking input :(

nishith
New poster
Posts: 4
Joined: Wed Jul 07, 2010 11:17 pm

Re: 727 - Equation

Post by nishith » Sun Jul 11, 2010 8:06 am

:(
I can't understand , why my code get RE.
It's a very simple code.
please help me.

Code: Select all

// acc
Last edited by nishith on Mon Jul 12, 2010 5:44 pm, edited 1 time in total.

sazzadcsedu
Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.
Contact:

Re: 727 - Equation

Post by sazzadcsedu » Sun Jul 11, 2010 11:53 pm

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

}
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com

nishith
New poster
Posts: 4
Joined: Wed Jul 07, 2010 11:17 pm

Re: 727 - Equation

Post by nishith » Mon Jul 12, 2010 5:59 pm

Thank you Sazzad vai.
I have got it.
But i have a confusion.
What is relation between my input taking procedure and RE.

sazzadcsedu
Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.
Contact:

Re: 727 - Equation

Post by sazzadcsedu » Mon Jul 12, 2010 10:52 pm

your Code is no longer in the board,And i forgot which format u used.But your input taking procedure perhaps does not terminate.
Last edited by sazzadcsedu on Mon Sep 13, 2010 11:00 am, edited 2 times in total.
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com

iqbal csedu
New poster
Posts: 3
Joined: Sun Jun 13, 2010 9:37 pm
Location: CSEDU,Dhaka,Bangladesh
Contact:

Re: 727 - Equation

Post by iqbal csedu » Fri Sep 10, 2010 10:47 am

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
Last edited by iqbal csedu on Sun Sep 12, 2010 3:14 pm, edited 1 time in total.
I dream a dream...but my dream is not coming true(still now).

sazzadcsedu
Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.
Contact:

Re: 727 - Equation

Post by sazzadcsedu » Sat Sep 11, 2010 9:15 am


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+
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com

Post Reply

Return to “Volume 7 (700-799)”