OpenStudy (anonymous):

input a string "(1+2)*3" then output an integer value of 9. using c compiler..pls help.

5 years ago
OpenStudy (anonymous):

the output should be an integer value of 9..

5 years ago
OpenStudy (espex):

So you need to parse that statement or you are taking in 1, 2, 3 as strings and cast them to int?

5 years ago
OpenStudy (anonymous):

yes sir..but im still a beginner in c language..i don't have that much knowledge about it..can you please help me??..please..

5 years ago
OpenStudy (espex):

Is this for a class? Usually there is a toolset given and you need to employ that method for the lesson. Otherwise I would suggest that you look at doing something a little easier as a first run.

5 years ago
OpenStudy (anonymous):

Is this the only input string?

5 years ago
OpenStudy (anonymous):

Otherwise specify the input restriction.

5 years ago
OpenStudy (anonymous):

Hehe... just printf("9");

5 years ago
OpenStudy (anonymous):

Given a string: (1+2)*3 The output should be equal to an integer number 9.

5 years ago
OpenStudy (anonymous):

If you know it's going to be in the format of (a + b)*c then you know to split it at asterisks and plus signs. If it can be any expression, then the approach will change.

5 years ago
OpenStudy (espex):

If the lesson is to simply pull those numbers out of the string, I would use a for loop and check each char to match your 1,2,3 and then use atoi to make them integers, at that point you can do what you wish with them.

5 years ago
OpenStudy (anonymous):

For the given problem: int main(){ char str[10]; scanf("%s",str); puts("9"); return 0; }

5 years ago
OpenStudy (anonymous):

if it is of the form (a+b)*c then just use this scanf format: scanf("%*c%d+%d%*c%*c%d",&a,&b,&c);

5 years ago
OpenStudy (anonymous):

I haven't praticed C for quite a while so I am not sure if printf("(%d+%d)*%d", .. ) would work or not.

5 years ago
OpenStudy (anonymous):

Given an expression which is a string: (1+2)*3 The output should be equal to an integer value of 9.

5 years ago
OpenStudy (espex):

Something like this is not the optimum way but given the parameters of the question... int main() { char equ[7]; int i, a, b, c; equ = "(1+2)*3"; for (i=0; i<7; i++){ if(equ[i] == '1') a = 1; if(equ[i] == '2') b = 2; if(equ[i] == '3') c = 3; } printf("(%d + %d) * %d = %d", a, b, c, (a+b)*c); }

5 years ago
OpenStudy (espex):

I counted wrong.. :( int main() { char equ[8]="(1+2)*3"; int i, a, b, c; for (i=0; i<8; i++){ if(equ[i] == '1') a = 1; if(equ[i] == '2') b = 2; if(equ[i] == '3') c = 3; } printf("(%d + %d) * %d = %d\n", a, b, c, (a+b)*c); }

5 years ago
OpenStudy (anonymous):

wow!..thank you so much Sir eSpeX!..it works!

5 years ago
OpenStudy (espex):

You're welcome. Fool started it so thanks for that too. :)

5 years ago
OpenStudy (anonymous):

The original question is ill-defined; presumably this should work with more general cases. Otherwise bluepig148's first answer is the best one. The given code would not work on "(2+6)*2" etc. (I think most of the answerers agree on that already :-) And if you got it to work with different values for a, b and c, it would then not work on "3 * (1 +2)". Even just putting spaces in unexpected places would cause problems. And so on. Whoever posed this question needs to specify the assumptions more (or maybe he/she did, but they weren't relayed to us in the original question. There are many many things wrong with eSpex' final answer (and he was the first to admit that). And while the itch is strong for me to point some of them out, I'll just leave it at that.

5 years ago