727 - Equation
Moderator: Board moderators
Re: 727 - Equation
To jan vai,
Vhaia please help me. Where is the fault in my code.
sreejon
CUET
Vhaia please help me. Where is the fault in my code.

sreejon
CUET
Re: 727 - Equation
Try the case.
Input:
Output:
Hope it helps.
Input:
Code: Select all
1
0
*
8
-
4
*
6
+
4
/
7
*
2
Code: Select all
08*46*-47/2*+
Ami ekhono shopno dekhi...
HomePage
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?
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;
}
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:
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
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;
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
Hello,
Someone please tell how to take input for this problem.
Someone please tell how to take input for this problem.
-
- 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;
}
#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
i am confused about taking input 

Re: 727 - Equation

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.
-
- Experienced poster
- Posts: 136
- Joined: Sat Nov 29, 2008 8:01 am
- Location: narayangong,bangladesh.
- 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
For Hints: http://salimsazzad.wordpress.com
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com
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.
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
- Location: narayangong,bangladesh.
- 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
For Hints: http://salimsazzad.wordpress.com
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com
-
- New poster
- Posts: 3
- Joined: Sun Jun 13, 2010 9:37 pm
- Location: CSEDU,Dhaka,Bangladesh
- 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..
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
- Location: narayangong,bangladesh.
- Contact:
Re: 727 - Equation
think about it.
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+
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
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com