This is begining.

BALANCE PARANTHESIS and show the result of ((9-4)/(44*34)) etc ???
what to do?

int main()
{
char arr[30];
for(int i=0;i<30;i++){
if(c=='(')
c++;
else if(d==')')
d++;}
if(c==d)
printf("parenthesis are balance");
else
printf("are not balance");
}
further cant understand.

Recommended Answers

All 6 Replies

Looks good to me.

Now start from the beginning of the string, find the innermost set of parenthesis, and perform the indicated operation.

I mean, reduce each part of the expression, by one level.

Then reduce it by another level, and you're done.

cant understand :( sorry can u help me by code plz

No, I have to run errands and get off the computer.

It sounds stupid, but the computer is very stupid with stuff like this. If you know how to make stacks, you can use them, here. See Wikipedia for postfix and RPN.

If not, "walk" in from left to right, until you hit the first RIGHT ), Then back up until you hit the first (. The expression in between should be "balanced" and just walk to the right again, and do the indicated operation (add, subtract, whatever).

Then continue on, and do the same for the next factor in the expression.

Sorry to talk "math speak", but believe me, I'm not a math person. My math teachers will swear to that! ;)

I can only say hints:
1. Use

char* token=strtok(string,"()")

to read tokens from expression.
2. Use

sscanf(token,"%d%c%d",&arg1,&operator,&arg2)

to read int's and operator character from token.
3. Write

switch(operator) {/*case by operator type*/}{

and perform arithmetic actions on these arg1, arg2 variables.
4. Construct new string composed of old one but with your computed result instead of old () expression.
5. Repeat everything from 1, until no parentheses left.

not understand this question completely need full help and program

OX69's token approach is nice. Mine was more basic:

1) you test it for balanced parenthesis

2) start at the left side, "walk" char by char, until you reach the first right ).

Now walk back to the first left (. Everything in between is a balanced factor, so perform the indicated operation

((9-4)/(44*34))

9-4
5
And remove both of the inner parenthesis, so (5)/((44*34)) is left

now go right until you reach an operator (*/+-). When you reach it, you keep going into a new factor, ((44*34)), and repeat the same process you used for the first one.

You will wind up with a simple fraction (5)/(1496)

3) Again, walk through the factors, and test if any factor has more than one number in it. If it has no operators, then it has only one number.

So remove the parenthesis from both factors, and perform the division, as indicated: 5/1496

If your math expressions become more complex than this algorithm will handle, then switch to postfix notation or RPN. Those are quite robust.

When I say "walk" through the expression, I mean use a for loop and go through the string until you reach the end of string char: '\0', or other stopping point, in the algorithm.

for(i = 0; i < strlen(myMathStr); i++)  {
  if(myMathStr[i] == ')')
     //etc.

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.