## 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

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

### Re: 727 - Equation

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

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

### Re: 727 - Equation

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
Contact:

### Re: 727 - Equation

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

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

### Re: 727 - Equation

I tried all test cases on the thread, but I still got WA.
What's wrong with my code?

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

### 727 - Equation Getting RUNTIME Error

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

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

### Re: 727 - Equation

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

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

i am confused about taking input

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

### Re: 727 - Equation

I can't understand , why my code get RE.
It's a very simple code.

Code: Select all

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

Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Contact:

### Re: 727 - Equation

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

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

### Re: 727 - Equation

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

Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Contact:

### Re: 727 - Equation

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

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

### Re: 727 - Equation

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).

Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Contact:

### Re: 727 - Equation

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

Try this:-

Code: Select all

``````input:
1

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

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