Thread: error in do-while loop correctly handling ( ) in arithmetic expressions

  1. #1
    Unregistered
    Guest

    error in do-while loop correctly handling ( ) in arithmetic expressions

    If anyone is up all bright and early, I'm working on an assignment (a glorified calculator it seems).

    For part 1 of the assignment I had to write the main program that called my eval_expr function. Got it working great.

    Part 2 of my homework modifies the eval function to handle arithmetic expressions within ( ). Spec says to modify while loop into a do-while giving the following example:



    do
    {
    if (scanf("%lf", &num) != 1)
    { /*if num*/
    if (scanf(" %c", &op) == 1 && op =='(' ) /* if '(' */
    num = eval_expr(); /* call expr using recursion */
    }
    ...


    } while (op && op != ';' && op != ')' );



    with the assumption that we figure out what goes in the ... section.


    The following is my code from part 1:



    #include <stdio.h>

    #define True 1
    #define False 0

    double eval_expr(void); /*function that will evaluate user's expression*/

    int
    main (void)
    {
    double value;

    printf("Enter an arithmetic expression: ");
    value = eval_expr();
    printf("The value of the expression is: %lG ", value);

    return 0;
    }


    double eval_expr(void) /*evaluate user's expression*/
    {
    double num, term, exp= 0.0; /*temporary operands*/
    char op, aop = 0, mop= 0; /*temporary operators*/


    while (scanf("%lf", &num) ==1) /*get next operand*/
    {
    if (mop == '*') /*process multi op*/
    term *= num;
    else if (mop == '/')
    term /= num;
    else
    term = num;
    mop = 0; /*reset mult op*/

    if (scanf(" %c", &op) != 1) /*get next operator*/
    op = 0;
    if (op == '*' || op == '/') /*set next multi op*/
    {
    mop = op;
    continue;
    }

    if (aop == '+') /*process add op*/
    exp += term;

    else if (aop == '-')
    exp -= term;
    else
    exp = term;
    aop = 0; /*reset add op*/
    if (op == '+' || op == '-') /*set next add op*/
    {
    aop = op;
    continue;
    }
    if (op == 0 || op == ';')
    break; /*end at EOF or semicolon*/
    }
    return exp;
    }




    I modifyed my code above to add:

    do loop before the above original while loop, and it's companion while after the orig while loop but before return exp. (as the spec example shows)

    it compiles/builds correctly, but running the program and entering the following expression:
    (1+2)*3;
    it returns a value of 3

    I'm thinking that it is now only doing the arithmetic within( ) and ending the do-while loop (thus the inner while loop).

    I've tried making several types of changes and neither myself or classmates can figure what we need to do. Either I get incorrect math results, or program will hang after user input.

    help!

    Can't move on to the next part (call the function in another .c file) until I get this corrected.

    --Mel, thanks in advance

  2. #2
    Unregistered
    Guest
    What I'd do, is handle this recursivly. Basicly, to parse parenthesis, do this:

    scan until you find a (
    call the recursive function from that point, to return the final value of what is in the parenthesis.

    You do this by extracting everything from the matching closing )
    Every time you encounter another (, you call the recursive function. Every time you encounter a ), you apply your math to the contents of the ( ), and return it.

    Thus:

    recurse( "4+(4*5)-(4/(1*2))" )
    {
    .... 4 + recurse( "4*5" ) - recurse( 4 / recurse( 1*2 ) )
    }

    That's basicly what it turns out to be.

    Quzah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  2. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  3. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  4. Ex. 2-4 KnR (2nd ed.) loop && array handling
    By olbas in forum C Programming
    Replies: 2
    Last Post: 03-23-2004, 11:07 PM
  5. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM