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

CDiMa
Experienced poster
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

Re: #727 Runtime Error(SISIGIV)

Post by CDiMa » Thu Jan 29, 2004 11:58 am

Zhao Le wrote:I know and I also hate to post my code here.
BUT I don't know how to get rid of Runtime ERROR(SISIGIV)
It looks like you're not handling multiple inputs. This may break your input handling and let it misbehave...

Ciao!!!

Claudio

Zhao Le
Learning poster
Posts: 80
Joined: Mon May 05, 2003 4:09 am
Location: Shanghai,China

Post by Zhao Le » Thu Jan 29, 2004 2:12 pm

It looks like you're not handling multiple inputs.
how should I understand this sentense?
The input file contains only one infix expression
AC makes me feels good,
But WA makes me thinks hard.

Dominik Michniewski
Guru
Posts: 834
Joined: Wed May 29, 2002 4:11 pm
Location: Wroclaw, Poland
Contact:

Post by Dominik Michniewski » Thu Jan 29, 2004 2:26 pm

Blue checkmark means multiple input. If judge don't change description and remove blue (assigning red) checkmark, problem description says about one block of input.

Best regards
DM
If you really want to get Accepted, try to think about possible, and after that - about impossible ... and you'll get, what you want ....
Born from ashes - restarting counter of problems (800+ solved problems)

Zhao Le
Learning poster
Posts: 80
Joined: Mon May 05, 2003 4:09 am
Location: Shanghai,China

Post by Zhao Le » Thu Jan 29, 2004 4:16 pm

Thanks all who replies.
I really have not noticed that.
AC makes me feels good,
But WA makes me thinks hard.

souchiwang
New poster
Posts: 3
Joined: Mon May 17, 2004 3:33 pm
Contact:

727 Runtime Error (SIGSEGV) help

Post by souchiwang » Mon May 17, 2004 3:36 pm

#include <stdio.h>
#include <iostream.h>

int getToken(char c);
char setToken(int n);

void main()
{
/**
isp == in-stack precedence
icp == incoming precedence

lparen,rparen,plus,minus,times,divide,mod,eos
*/

static int isp[] = { 0,19,12,12,13,13,13,0};
static int icp[] = {20,19,12,12,13,13,13,0};


int N;
cin>>N;
int i;
char c;
scanf("%c",&c);
for(i=0;i<N;i++)
{

int token;
int n = 0 ;
int top = 0;
int stack[10] ;
stack[0] = 7;

while (true)
{
scanf("%c",&c);
if (c=='\n' || c == NULL)
{
break;
}
token = getToken(c);
if (token == 8)
{
printf("%c",c);;
}else if(token == 1)
{
while(stack[top] != 0)
{
printf("%c",setToken(stack[top--]));
}
--top;
}else
{
while(isp[stack[top]] >= icp[token] )
{
printf("%c",setToken(stack[top--]));
}
stack[++top] = token;
}
scanf("%c",&c);
}

while((token=stack[top--])!=7)
{
printf("%c",setToken(token));
}
printf("\n");
if (i != N-1)
{
printf("\n");
}
}
}

char setToken(int n)
{
if (n == 0)
{
return '(';
}else if(n==1)
{
return ')';
}else if(n==2)
{
return '+';
}else if(n==3)
{
return '-';
}else if(n==4)
{
return '/';
}else if(n==5)
{
return '*';
}else if(n==6)
{
return '%';
}else
{
return ' ';
}
}

int getToken(char c)
{
if (c=='(')
{
return 0;
}else if(c==')')
{
return 1;
}else if(c=='+')
{
return 2;
}else if(c=='-')
{
return 3;
}else if(c=='/')
{
return 4;
}else if(c=='*')
{
return 5;
}else if(c=='%')
{
return 6;
}else if(c==' ')
{
return 7;
}else
{
return 8;
}
}

[/cpp]
where is my problem...
someone help me..
is the error is about blue input..

souchiwang
New poster
Posts: 3
Joined: Mon May 17, 2004 3:33 pm
Contact:

727 runtime error ...how to solve the multiple input..

Post by souchiwang » Sat May 22, 2004 4:19 pm

void main()
{
int n;
cin>>n; //n test case
int i;
scanf("%c",&c); ///blank line
for(int i=0;i<n;i++)
{
while (true)
{
scanf("%c",&c); //input character
if (c=='\n' || c == NULL) //EOF
{
break;
}
//**....algorithm....**//
scanf("%c",&c); // '\n'
}
}

where 's my problem...
why runtime error
the 727 input is .
2

2
+
1

3
*
2
/
4(EOF)
thanks...

User avatar
UFP2161
A great helper
Posts: 277
Joined: Mon Jul 21, 2003 7:49 pm
Contact:

Post by UFP2161 » Sat May 22, 2004 4:41 pm

scanf ("%c", &c) doesn't set c to NULL when it reaches EOF.

It'd probably by faster and easier to use gets (char[]) or getline (cin, string s) than to parse the file character by character.

souchiwang
New poster
Posts: 3
Joined: Mon May 17, 2004 3:33 pm
Contact:

Post by souchiwang » Sat May 22, 2004 5:18 pm

i change my code..but still get runtime error..
.....where 's my problem...i can run in VC...

void main()
{
int n;
cin>>n; //n test case
int i;
char c[10];
cin.getline(c,10,'\n'); //blank line
for(int i=0;i<n;i++)
{
while (true)
{
cin.getline(c,10,'\n'); //input character
if (c[0]=='\n' || c == NULL) //EOF
{
break;
}
//**....algorithm....**//
}
}

abc
New poster
Posts: 15
Joined: Sun Dec 15, 2002 2:51 pm

727

Post by abc » Thu Jul 15, 2004 10:17 pm

Anyone have trick cases? My solution works for all the inputs on the site. Is this problem fixed or illegal inputs?

jackie
New poster
Posts: 47
Joined: Tue May 04, 2004 4:24 am

Post by jackie » Sat Jul 31, 2004 5:07 am

I got AC for the first time i submit the code.

Just use simple stack algorithm.

[cpp] case '(':
//push
case '-':
case '+':
//pop "+-*/" to the standard output if it's any of them on top of the stack then push
case '/':
case '*':
//pop "*/" if any on the top of the stack then push
case ')':
//pop until find "(" and pop "("
default:
//for digit just output it
//then pop all the item in the stack[/cpp]
input for debug
9

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

(
3
+
2
)
*
5

1
+
2
+
3
+
4
+
5

1
+
1

1
*
2

1
*
2
*
3
*
4
*
5

(
1
+
2
)
3

1
(
7
*
8
)
6

(
1
*
2
*
3
*
4
*
5
)
6
+
2
-
5
*
9
+
1
corresponding output (i don't know whether there is such input like the last or last - 1 one in judge's test data set and it's obviously not valid input)
41263/*+5--

32+5*

12+3+4+5+

11+

12*

12*3*4*5*

12+3

178*6

12*3*4*5*62+59*-1+
hope it will help

Good Luck

eshika
New poster
Posts: 11
Joined: Sat Oct 02, 2004 12:02 pm
Location: Bangladesh

727

Post by eshika » Thu Oct 14, 2004 11:01 am

Why my program is getting run time error? Is it for stack overflow? How can I handle multiple input? Can anybody help?

sumaira
New poster
Posts: 2
Joined: Thu Nov 04, 2004 12:30 am
Location: Islamabad
Contact:

727 Correct sol NOT Subbmitted In Correct Subbmitted WA?

Post by sumaira » Thu Nov 04, 2004 8:20 am

The input sequence is not there in ACM as mentioned in problem
plz tell me the input and output sequence


Plzz Answer MY NOT SUBBMITTED CODE BEC I LIKE TO SUBBMIT MINE NOT THE SOLUTION

:oops: Subbmitted and not subbmitted code are the following

NOT SUBBMITTED CODE ->1
SUBBMITTED CODE -->2
----->1

**********************************



---->>>>>>>>>2
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <conio.h>

#define MAX 1000000
char stack[MAX];
int top;

void push(char ch) {
stack[top++] = ch;
assert(top < MAX);
}

char pop() {
assert(top > 0);
return stack[--top];
}

char peek() {
assert(top > 0);
return stack[top - 1];
}

int empty() {
return top == 0;
}

int pre(char ch, char ch2) {
if (ch == '*' || ch == '/') {
if (ch2 == '+' || ch2 == '-')
return 1;
}
else {
if (ch2 == '*' || ch2 == '/')
return -1;
}
return 0;
}

int gets_line(char s[]) {
int i = 0; char ch;
s[0] = '\0';
if (1 != scanf("%c", &ch)) return 0;
while (ch != '\n') {
if (ch != '\r') s[i++] = ch;
if (1 != scanf("%c", &ch)) break;
}
assert(i < 50);
s = '\0';
return i;
}

void add_ch(char ch) {
if (ch >= '0' && ch <= '9')
printf("%c", ch);
else {
if (ch == ')') {
ch = pop();
while (ch != '(') {
printf("%c", ch);
ch = pop();
}
}
else if (empty() || peek() == '(') push(ch);
else if (ch == '(') push(ch);
else if (pre(ch, peek()) > 0)
push(ch);
else if (pre(ch, peek()) == 0) {
printf("%c", pop());
add_ch(ch);
}
else if (pre(ch, peek()) < 0) {
printf("%c", pop());
add_ch(ch);
}
}
}

int main() {
int i, pn;
char line[1000], token[100];
gets_line(line);
sscanf(line, "%d", &pn);
gets_line(line);

for (i = 1; i <= pn; i++) {
top = 0;
if (i > 1) printf("\n");
while (gets_line(line)) {
if (1 != sscanf(line, "%s", token)) break;
assert(strlen(token) == 1);
add_ch(token[0]);
}
while (!empty()) printf("%c", pop());
printf("\n");
}
getch();
return 0;
}

********************************
sddsdd

sumaira
New poster
Posts: 2
Joined: Thu Nov 04, 2004 12:30 am
Location: Islamabad
Contact:

727 Why WA?Check Input Seq

Post by sumaira » Fri Nov 05, 2004 6:09 am

My Email is attarctiveperson@hotmail.com
plzzz tel me why WA?
Or Check My INPUT SEQ





//#include "stack.h"

#include <stdio.h>

#ifndef stack_h
#define stack_h
#include <string>
#include <iostream>
#include <cctype>
using namespace std;
template<class T>
class stack
{
private:
T *p;
int size;
int top;
public:
stack(int a)
{
size=a;
p=new T [size];
top=-1;
}
T topvalue()
{
return p[top];
}
void push(T a)
{
if(top==size-1)
{
// cout<<"stack if full"<<endl;

}
else
{
p[++top]=a;
}
}
T pop()
{
if(top==-1)
{
// cout<<"stack is empty"<<endl;
return 0;
}
else
{
// cout<<"The pop value is "<<p[top]<<endl;
return p[top--];
}
}
bool isempty()
{return top==-1;
}

};
#endif

int main()
{
//string s;
//cout<<"ENTER THE POST FIX NUMBER TERMINATED BY #"<<endl;
//getline(cin,s,'#');
char s[50];
int length;
int in=0;
char inn;
int ii=0;
while(in<2)
{
scanf("%c", &inn);

if(inn=='\n')
{
scanf("%c", &inn);
if(inn=='\n')
{
in=2;
length=ii;
}
else
{
s[ii++] = inn;
}
}
else
{
if (inn != '\r')
s[ii++] = inn;
}
}
/*for(int p=0;p<length;p++)
{
cout<<s[p]<<" ";
}
cout<<endl;*/
stack <char> ch(length);
for(int i=0;i<length;i++)
{
char a=s;
if((a=='+') || (a=='-') || (a=='*') || (a=='/') || (a=='(') || (a==')'))
{
if((a=='+') ||(a=='-'))
{
if(!(ch.isempty()))
{
while((ch.topvalue()=='*')||(ch.topvalue()=='/')||(ch.topvalue()=='+')||(ch.topvalue()=='-'))
{
cout<<ch.pop();
}
}

ch.push(a);
}
if((a=='*') ||(a=='/'))
{
if(!(ch.isempty()))
{
while(((ch.topvalue()=='*')||(ch.topvalue()=='/')))
{
cout<<ch.pop();
}
}
ch.push(a);
}
if(a=='(')
{
ch.push(a);
}
if(a==')')
{
char f=ch.pop();
while(f!='(')
{
cout<<f;
f=ch.pop();
}

}
}
else
{
if(a==')' || a=='(')
{}
else cout<<a;
}
}
while (!(ch.isempty()))
{
cout<<ch.pop();
}
//cout<<endl;
return 0;
}
sddsdd

akhter900
New poster
Posts: 7
Joined: Fri Mar 19, 2004 3:27 pm

Post by akhter900 » Sat Nov 27, 2004 8:32 am

Runtime Error may be for MULTIPLE INPUT.

THIS IS A BLUTIC PROBLEM
To take multiple input:

SCAN AN INT VALUE 'n'
AND DO THE PROGRAM INSIDE A LOOP FROM
1 TO 'n'.

Ok,
Try again.
AkHtEr

User avatar
Sedefcho
A great helper
Posts: 374
Joined: Sun Jan 16, 2005 10:18 pm
Location: Bulgaria

Post by Sedefcho » Tue May 10, 2005 11:19 am

I know the messages from akhter900 and eshika are
a bit old now ( several months ).

Well... I have two questions here.

1) Are you talking about problem 727 ?

2) If yes then... Is problem 727 a multiple input problem ?!

The problem statement clearly says that the input file will
contain only only expression which we have to convert to
postfix notation.

Am I right that we have to expect just one expression in
the input file ?!

Post Reply

Return to “Volume 7 (700-799)”