# Need Major Help On My Program!!

This is a discussion on Need Major Help On My Program!! within the C Programming forums, part of the General Programming Boards category; My program is to calculate the value of an expression involving arithmetic operators. The expression should be evaluated based on ...

1. ## Need Major Help On My Program!!

My program is to calculate the value of an expression involving arithmetic operators. The expression should be evaluated based on operator precedence. The program should prompt the user to input the expression and should indicate the end of the expression by inputting a character E. The expression should have only numeric values and operators to add, subtract, multiply, and divide.

-The program should prompt the user to input only integer values
-the program should be written for integer values in the expression
-Arrays should be used to store the input values of the user
-Output of the program should be an integer value
-Functions can be used if necessary
here is an example that a user might enter
34
*
2
+
9
-
3
E (terminates the program and then should print the answer)
here is what i have written:

#include <stdio.h>

#define MAX 100
int main()
{
int a[MAX];
char b[MAX];
int i = 0;
char c = 'e';
int total = 0;

printf("Welcome to my calculator program. My program\n");
printf("and division. So lets start the program\n");

printf("\nEnter an integer then an operator. To end the\n");
printf("program, enter the character E. To use the operators\n");
printf("use + for addition\nuse - for subtraction\nuse * for");
printf("multiplication\nand use / for division\n");

while (c != 'e')
{
scanf("%d", &a[i]);
while(i < MAX) {
scanf("%d", &b[i]);

if(b[i++]=='e') break;
} i++;
}

i = 0;
total = 0;
{
switch(b[i]){
case '*': total *=a[i+1]; break;
case '/': total /=a[i+1]; break;
case '+': total +=a[i+1]; break;
case '-': total =total - a[i+1]; break;
} i++;

}
while (b[i] != 'e');
printf("the total is: %d", total);

return 0;
}

2. Here's how I'd do it (or something close):

1) Break the command into seperate commands / entries.
2) Stick them all in a double linked list.
3) Process the list in order of precedence(sp)
Code:
```typedef struct node NODE;

NODE {
NODE *prev, *next;
int expression;
int value;
};

NODE *list;

enum { Invalid=-1, Number, Multiplication, Division, Addition, Subtraction };```
When you're tokenizing (ie, chopping up the list) you do something like this:
Code:
```// pesudo code, not all variables are declared
char chunk[10]={0};
expression = None;

for( x=0; x<10; x++ )
{
chunk[x] = wholeList[runningcount];
if( !isdigit( chunk[x] ) )
{
if( x != 0 )
{
// we've been reading a number, back up!
--x;
else
expression = getExpression( chunk[x] );
break;
}
// otherwise it's a number and we want to keep reading it
}
if( expression == Number )
{
value = atoi( chunk );
stick into a node as a 'value'
}
else
if( expression == Invalid )
{ we're done reading from our data, it appears to have ended }
else
{
stick into a node as a 'number'
}```
Basicly, what you do next is walk down the node looking for order of precedence.

Check for multiplication. If you find it, multiply the node->prev->value with the node->next->value and then remove both of those nodes from the list, and change this node so that it is now storing a value rather than a expression.

Pretty simple stuff.

Quzah.

3. You might also want to allow parenthesis.

Just add another field in the struct with the parenthesis recursion level. Then, parse in order of parenthesis level, and then in order of precedence.

*It would probably be a bit more interesting to let the user enter the data in one string, instead of making him/her separate the tokens for you.*

4. I agree. Actually, that's what I was assuming for my example, though it'd work either way.

Apparently (due to their spamming the board with this same post over and over!) their instructor says no pointers or recursion. Why? I guess that means no linked lists. Still, just apply my same exact method to an array, and you can do the same thing. You just have to 'fudge' the list when you 'remove' entries.

This could work. Just apply my logic to an array.

Quzah.