Thread: Need Major Help On My Program!!

  1. #1
    Registered User
    Join Date
    Oct 2001

    Exclamation 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
    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("will calculate addition, subtraction, multiplication\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;
    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. #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)
    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:
    // 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!
             expression = getExpression( chunk[x] );
       // 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'
       link the node
    if( expression == Invalid )
       { we're done reading from our data, it appears to have ended }
       stick into a node as a 'number'
       link the node
    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.


  3. #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. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    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.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM