## 327 - Evaluating Simple C Expressions

**Moderator:** Board moderators

### 327 - Evaluating Simple C Expressions

can any one give me a sample input why problem #327 is WA?

by

S M Shahed Nejhum

S M Shahed Nejhum

should yieldb++---c+d-c+b

z+d+c++-++b-a

++b-++c+--e-b+f++

--a-++b+c+a-c++

Expression: b++---c+d-c+b

value = 5

b = 3

c = 2

d = 4

Expression: z+d+c++-++b-a

value = 29

a = 1

b = 3

c = 4

d = 4

z = 26

Expression: ++b-++c+--e-b+f++

value = 6

b = 3

c = 4

e = 4

f = 7

Expression: --a-++b+c+a-c++

value = -3

a = 0

b = 3

c = 4

I have intentionally ignored those cases, since unary plus and minus are not mentioned in the problem description.MV wrote:Have you considered the following cases?

-a

+a

Anyway, test cases like a+b-a won't be in the input.

But you are right about Yarin's test cases - they do contain some lines with "undefined behaviour".

- a + b

b - z

a+b--+c++

c+f--+--a

f-- + c-- + d-++e

a--+b--+c--+d--+e--+f--+g--+h--+i++-j++-k++-l++-m++-n++-o++-p++-q++-r++-s+t-u+v

--z+a--

a

z

++ x+--y+z++

a++-c++-m+ + +o+k

- Expression: a + b

value = 3

a = 1

b = 2

Expression: b - z

value = -24

b = 2

z = 26

Expression: a+b--+c++

value = 6

a = 1

b = 1

c = 4

Expression: c+f--+--a

value = 9

a = 0

c = 3

f = 5

Expression: f-- + c-- + d-++e

value = 7

c = 2

d = 4

e = 6

f = 5

Expression:

value = 0

Expression: a--+b--+c--+d--+e--+f--+g--+h--+i++-j++-k++-l++-m++-n++-o++-p++-q++-r++-s+t-u+v

value = -79

a = 0

b = 1

c = 2

d = 3

e = 4

f = 5

g = 6

h = 7

i = 10

j = 11

k = 12

l = 13

m = 14

n = 15

o = 16

p = 17

q = 18

r = 19

s = 19

t = 20

u = 21

v = 22

Expression: --z+a--

value = 26

a = 0

z = 25

Expression: a

value = 1

a = 1

Expression: z

value = 26

z = 26

Expression: ++ x+--y+z++

value = 75

x = 25

y = 24

z = 27

Expression: a++-c++-m+ + +o+k

value = 11

a = 2

c = 4

k = 11

m = 14

o = 15

Anyway, my program could be wrong in some cases too, though AC.

### help

i have passed all of the test data above

but still WA

who can help me

thx in advance

but still WA

who can help me

thx in advance

### i got it

At last i got AC

there is no input like following

-a

a--b

++a + ++a + ++a

all of the input are very strict

But i don't why my code submited twice and got WA & AC

there is no input like following

-a

a--b

++a + ++a + ++a

all of the input are very strict

But i don't why my code submited twice and got WA & AC

### line by itself and will contain no more than 80 characters.

OK. Ive spent a lot of time trying to solve this problem.

After more than a week I decided to make the vector were I was reading the input bigger (5512 position, instead of 100 initialy). That made me get an accepted.

But in the problem specification it is really clear "Each of the expressions will appear on a line by itself and will contain no more than 80 characters.". He says "characters" not "valid characters", and, at least to me, space is a character also.

For those who are trying to solve this problem, just enlarge the input vector (at least it worked for me).

After more than a week I decided to make the vector were I was reading the input bigger (5512 position, instead of 100 initialy). That made me get an accepted.

But in the problem specification it is really clear "Each of the expressions will appear on a line by itself and will contain no more than 80 characters.". He says "characters" not "valid characters", and, at least to me, space is a character also.

For those who are trying to solve this problem, just enlarge the input vector (at least it worked for me).

It can't be input like

a + ++a or a+ --a ...and so on

I mean it can't be same letter used a few times

cause it was written

suppose it was such an input

--a + ++a

so according to problem description first calculate all values

so a = 1 then 0 then 1

and now calculate expression so it should be 1 + 1 so final value is 2

according to some of you guys it will be

--a + ++a so

(0) + (1) and value will be 1

that's why these input (b++---c+d-c+b) gave 2 different answers and is

for those who still get WA:

consider this a+++++b case

and remember to clear string from spaces

i.e. string[remove(string,string+strlen(string),' ') - string] = '\0';

Regards

a + ++a or a+ --a ...and so on

I mean it can't be same letter used a few times

cause it was written

so at first calculate expresisons like ++a and so onExecute the statements generated in step 1, then those generated in step 3, and finally the one generated in step 2, in that order.

suppose it was such an input

--a + ++a

so according to problem description first calculate all values

so a = 1 then 0 then 1

and now calculate expression so it should be 1 + 1 so final value is 2

according to some of you guys it will be

--a + ++a so

(0) + (1) and value will be 1

that's why these input (b++---c+d-c+b) gave 2 different answers and is

**invalid**so there**mustn't**be input like thisfor those who still get WA:

consider this a+++++b case

and remember to clear string from spaces

i.e. string[remove(string,string+strlen(string),' ') - string] = '\0';

Regards

keep it real!