Thread: Problems about this eval() function

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    12

    Problems about this eval() function

    This function is to calculate expression and output results.
    for example, input 3+5 will output 8.0000
    and input 3^2 will output 9.0000

    I cannot understand it very well on some coding,
    my problems are highlighted as comments in the code.


    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <math.h>

    typedef enum Ops
    {
    None,
    Plus,
    Minus,
    Multiply,
    Divide,
    Power
    } OPS;

    long signif[] = /* what is it mean?? */
    { /* why it is needed ?? */
    0,
    10,
    10,
    11,
    11,
    12
    };


    long double Evaluate(long start, long stop, char* expr);

    int main()
    {
    char buffer[256];
    int c;
    do
    {
    printf("Enter expression to evaluate: ");
    scanf("%s", buffer);
    printf("Result: %f \n\n", (float)Evaluate(0, strlen(buffer)-1, buffer));

    printf("Again? (1/0)");
    scanf("%d", &c);
    } while(c);

    return 0;
    }

    long double Evaluate(long start, long stop, char* expr)
    {
    OPS op_found;
    OPS op_last = None;
    unsigned long op_cursign = 0xFFFFFFFF; /* what is it mean? */
    long op_pos;
    long parnest, parpair;
    long curpos;
    int nonwhite;

    //Remove enclosing whitespaces paranthesis
    while(1) /* why use 1 , is it a endless loop ??? */
    {


    //Remove enclosing parathesis
    parnest = 0;
    parpair = 0;
    for(curpos = start; curpos <= stop; curpos++)
    {
    if(expr[curpos] == '(')
    {
    parnest++;
    } else if(expr[curpos] == ')')
    {
    parnest--;
    if(parnest == 0) parpair++;
    }
    }

    if(expr[start] == '(' && expr[stop] == ')' && parpair == 1)
    {
    start++;
    stop--;
    } else break; //Break if there are no more paranthesis
    }

    //Search for operators
    nonwhite = 0;
    parnest = 0;
    op_cursign = 0xFFFFFFFF; /* what is it mean */
    op_last = None;
    for(curpos = start; curpos <= stop; curpos++)
    {
    if(parnest == 0) // Are we in paranthesis
    {
    op_found = None;
    if(nonwhite) /* what it means? nonwhite =0 is false */
    { /* if statement willl not run ?? */
    switch(expr[curpos])
    {
    case '+':
    op_found = Plus;
    break;
    case '-':
    op_found = Minus;
    break;
    case '*':
    op_found = Multiply;
    break;
    case '/':
    op_found = Divide;
    break;
    case '^':
    op_found = Power;
    break;
    }
    } else /* why use this else statements ?? */
    {
    switch(expr[curpos])
    {
    //Interpret the operators as white spaces
    case '+':
    case '-':
    case '*':
    case '/':
    case '^':

    case ' ':
    case '\t':
    case '\n':
    case '\r':
    break; //Ignore white spaces
    default:
    nonwhite = 1; /* assignment it for what use ? */
    }
    }

    if(op_found != None)
    {
    nonwhite = 0;
    if(signif[op_found] <= op_cursign)
    { /* what is this mean ?? */
    op_cursign = signif[op_found]; )
    op_last = op_found;
    op_pos = curpos;
    }
    }
    }

    if(expr[ curpos] == '(')
    {
    parnest++;
    } else if(expr[curpos] == ')')
    {
    parnest--;
    }
    }

    if(parnest != 0)
    {
    printf("\nError: Parenthesis nesting error!\n");
    return 0;
    }

    //Did we find any operators?
    if(op_last != None)
    {
    long double a, b;

    a = Evaluate(start, op_pos - 1, expr); /*why use this */
    b = Evaluate(op_pos + 1, stop, expr); /* why use this */

    switch(op_last)
    {
    case Plus:
    return a+b;

    case Minus:
    return a-b;

    case Multiply:
    return a*b;

    case Divide:
    return a/b;

    case Power:
    return pow(a, b);
    }
    return 1; /* what is this mean */
    }



    return atof(&expr[start]); /* what is this mean */
    }


    My problems are a little bit more, I'm happy if you can answer me.
    As my c program is not good. ^^

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You will not learn anything unless you start small, and write your own routine!

    I noticed you pasted someone elses code here!

    Why don't you try a simpler program and work your way up to something more difficult?

    It is as if you were starting to learn architecture and then plan to build a castle for your first project!!

    Rediculous!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Problems with str.replace function
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 03:35 AM