# Help on Detecting Errors in Mathematical Equations

• 09-28-2006
sangken
Help on Detecting Errors in Mathematical Equations
After several hours of brainstorming, it would seem i cannot solve this on my own, so i am humbly asking the experts here for some expert help (and if possible, some code snippets)

My problem is this:
1) User is asked to input a mathematical expression in INFIX form (the normal way).
2) My program then checks the expression for any syntax errors. (im stuck here for now).
3) It then converts it to postfix (or Reverse Polish Notation), then evaluates it.

examples of erroneous expressions are:

5 ( 5 + 5) 5 (no operation between parenthesis)
5 5 + 5 (5 5 is treated as two different numbers due to white space between)
5 + + 5 (double addition operation without operands)
+ 5 + 5 + (insufficient operands)
) 5 + 5 (invalid parenthesis placement)
( ( 5 + 5) (parenthesis mismatch)
...... other errors which you guys know but i havent figured out yet.

so far, i think i've solved the mismatched parenthesis problem. the rest seems too difficult for me.
i know how to do 3), but 2) seems to elude my wits. can anyone help me? this is what i've accomplished so far:

Code:

```#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <string.h> #include <ctype.h> #define MAX 20 typedef struct stackA {         char *token;   stackA *next; } stack; typedef *stack stack_ptr; int errorCheck(char *input); char *output(char *input); int evaluate(char *output); char *pop(stack_ptr S); void push(char *c, stack_ptr S); int isOperator(char c); int main(void) {         char input[MAX], *output, try_again;         int choice, error, answer;         do {                 clrscr();                 error=0;                 puts("Enter the infix notation:");                 fgets(input,sizeof(input),stdin); /* to prevent buffer overflow*/                 error=errorCheck(input);                 if(error != 0)                         puts("You have enterred an erroneous equation!");                 else {                         output=convert(input);                         printf("\nConverted to postfix: %s",output);                         answer=evaluate(output);                   printf("\nThe answer to the equation %s is: %d",input,answer);                 }                 puts("\nDo you wish to try again? \(type \"N\" to quit, any other to repeat\)");                 try_again=getchar();\         } while(!(try_again == 'N' || try_again == 'n'));         getch(); } int errorCheck(char *input) {         int i, len, found_error=0,Lparenthesis=0,Rparenthesis=0,digit=0,operator_start=0, operator_end=0;         len=strlen(input);         for(i=0;i<len && found_error==0;i++) {                 while(isspace(input[i]))                         i++;                 if(input[i] == '(')                         Lparenthesis++;                 else if(input[i] == ')') {                         if(Lparenthesis == 0)                                 found_error=1;                         else Rparenthesis++;                 }                 if(isdigit(input[i])) {                         digit=1;                         while(isdigit(input[i]))                                 i++;                         while(isspace(input[i]))                                 i++;                         if(isdigit(input[i]) || input[i] == '(' || input[i] == ')')                                 found_error=1;                         else digit=0;                 }         } }```
note) the program isnt finished yet, but if I separate the errorCheck function and convert it into a program, it compiles, but the expression (5 + 5) is reported as having errors. i tried to trace the sequence of the program but to no avail, i just couldnt see the problem.

• 09-28-2006
Prelude
Have you considered adjusting your order of operations? Error checking an infix expression is vastly simpler if you do it while converting to postfix rather than as a separate task.
• 09-28-2006
SlyMaelstrom
Quote:

examples of erroneous expressions are:

5 ( 5 + 5) 5 (no operation between parenthesis)
Where I come from, that's generally implied as multiplication, but I understand your examples.

Here's the facts... write your algorithm to convert to post-fix... somewhere in the middle of this your perfectly good algorithm is going to get stuck (It's going to look for a closing parenthesis and not find it, it's going to look for an operator and not find it, etc). This is where I would do error checking. At this point, you can simply output that there was an error in calculation and end it or you can delve deeper and try to figure out the error, it's up to you.

Now, before I finish, I want to make one thing clear. Just because here I say just do the algorithm and error check when an error occurs doesn't mean this is how all error checking should be handled in programming. It's worth noting the distinction between a logical error and a run-time error. A logical error you can identify as it happens and fix accordingly. A run-time error will crash your program before you get a chance to fix it. You'll want to do error checking on all potential run-time errors before the error has a chance to occur.

That's my opinion on that. There are plenty more experience programmers that may say different, so if they do, I'd be interested in hearing their logic, too.
• 09-28-2006
sangken
Hmmm, i'll consider it. i have the pseudocode for converting to postfix, and it's already too complicated for me. If i add the error detection in an otherwise already complicated code, im afraid i wont understand it anymore hehehe!!

but just out of curiousity, how would you do it (no code necessary)?? i will paste the pseudocode for converting to postfix later since i am running late for my class for today. thanks!
• 09-28-2006
SlyMaelstrom
Quote:

Originally Posted by sangken
but just out of curiousity, how would you do it (no code necessary)??

It involved using a stack, normally... Check this link out.
http://www.qiksearch.com/articles/cs/infix-postfix/
• 09-28-2006
sangken
Quote:

Originally Posted by SlyMaelstrom
Where I come from, that's generally implied as multiplication, but I understand your examples.

it's the same where i come from too. but if i allow that, im afraid i do not know how to handle such exceptions (in programming), yet. so i generalized that between operands, there should be operators, and '(' or ')' are not considered as operands in this program only.
• 09-28-2006
Prelude
>If i add the error detection in an otherwise already complicated code, im afraid i wont
>understand it anymore hehehe!!
The conversion algorithm is straightforward and (to me) extremely simple. If you found it somewhere, chances are good that it lacks any error checking at all, which is a very bad thing. By adding the error checking, you're killing two birds with one stone: completing another task in your list and making your code more robust.
• 09-28-2006
sangken
Quote:

Originally Posted by SlyMaelstrom
It involved using a stack, normally... Check this link out.
http://www.qiksearch.com/articles/cs/infix-postfix/

what i meant was how would you guys do error checking while converting at the same time. :D
• 09-28-2006
sangken
My professor gave us this project without requiring error checking at all, and only using single digits for operands. But i want to do error checking because good programs should be able to handle user input errors right? and what kind of calculator only accepts single digits? LOL. but darn, it's not as easy as i thought it would be.
• 09-28-2006
SlyMaelstrom
Well, that's all up to you... personally, I would just keep trucking along until my algorithm can't figure out what to you based on it's logic... basically the "default" case on a switch statement or the "else" on an if statement. Of course, the first few times this happened, I would track the error to make sure it was in fact a logical error on the input. As for reporting with errors when they happen... basically, if your conversion logic is correct, when the error is encountered it should be pretty obvious what is missing (You'll know what precedence you're looking for, or what kind of symbol is missing). You just have to look at what you got and assess the problem with one of many predefined error messages. That's how I'd do it.
• 09-28-2006
Prelude
>but darn, it's not as easy as i thought it would be.
Programming is hard. Welcome to reality. :)
• 09-28-2006
sangken
Quote:

Originally Posted by Prelude
>but darn, it's not as easy as i thought it would be.
Programming is hard. Welcome to reality. :)

yeah, thanks for opening my eyes... hmm, tis not yet too late to "shift" to an "easier" course, like, say, NURSING. LOL...

but anyways, i'll give the adding-the-error-checking-during-the-conversion idea a try. if i cant handle it, i'll ask you guys again for some "concrete" help. and if i still cant do it, then darn, i guess i'll just die another day. :D
• 09-28-2006
Prelude
>tis not yet too late to "shift" to an "easier" course, like, say, NURSING.
Nursing is hard too. Maybe something more like this? ;)

>if i cant handle it, i'll ask you guys again for some "concrete" help.
:)

>and if i still cant do it,
Then I would be very surprised. This forum has very sharp minds, very talented programmers, and excellent teachers. Sometimes all three rolled up into one.
• 09-28-2006
sangken
Just one last question though, in my above code snippet, specifically in the errorCheck() function, why does the equation (5+5) register as having errors? i traced the program cleanly and according to my analysis, it shouldnt. i know im missing something here but i just dont know what. :(

here's the function, i converted it into a single program for testing purposes:

Code:

```#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <string.h> #include <ctype.h> int main(void) {         int i, len, found_error=0,Lparenthesis=0,Rparenthesis=0,digit=0,operator_start=0, operator_end=0;         char input[20];         clrscr();         puts("Enter:");         gets(input);         len=strlen(input);         for(i=0;i<len && found_error==0;i++) {                 while(isspace(input[i]))                         i++;                 if(input[i] == '(')                         Lparenthesis++;                 else if(input[i] == ')') {                         if(Lparenthesis == 0)                                 found_error=1;                         else Rparenthesis++;                 }                 if(isdigit(input[i])) {                         digit=1;                         while(isdigit(input[i]))                                 i++;                         while(isspace(input[i]))                                 i++;                         if((isdigit(input[i]) || input[i] == '(') && digit==1)                                 found_error=1;                         else digit=0;                 }         }         if((Lparenthesis-Rparenthesis) != 0)                 found_error=1;         if(found_error==1)                 puts("Error found");         else puts("No errors!");         getch(); }```
• 09-28-2006
Prelude
You aren't recognizing the closing paren. When doing a lot of index work inside of the loop, you shouldn't include an increment step at the end as it usually causes problems:
Code:

```for ( i = 0; i < len && found_error == 0; ) {   /* Make all changes to i in the body */ }```