Thread: Expressions

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    3

    Expressions

    Code:
    l have to create a C program in which you can enter expressions to be evaluated. 
    
    Here are the instructions: 
    
    The operands in the expression are floating-point numbers; the operators are +, -, *, and /. The expression is evaluated from left to right (no operator takes precedence over any other operator). 
    
    Specifications:
    a)	You can assume that the expression has the format: an operand followed by an operator, then by an operand, an operator, …, ended with an operand.
    b)	Use float type for the operands and the value of expressions.
    c)	If there is a dividing by zero in the expression, display “Cannot divide by zero” error message and abort the program.
    d)	Skip blank characters in an expression.
    
    Is there another way I can create the loop without requesting input of 0? 
    
    This is the expected output:
    
    Enter an expression: 1+2.5*3
    Value of expression: 10.5
    
    This is what I get.
    
    Enter an expression: 1+2.5*3
    0
    The value of expression is: 10.5
    
    Can anyone help me to figure this out?
    
    Here is what I have.
    
    
    #include <stdio.h>
    int main(void)
    {
    char ch;
    float n, value = 0;
    printf("Enter an expression: ");
    
    scanf("%f%c%f", &value, &ch, &n);
    if (n == 0)
    {
    printf ("ERROR!! Cannot divide by zero.\n");
    return 0;
    }
     
    while (n != 0) {
    if (ch == '+')  
    {
    value += n;
    }
    if (ch == '-')
    {
    value -= n;
    }
    if (ch == '*')
    {
    value *= n;
    }
    if (ch == '/')
    {
    value /= n;
    }
     
    scanf("%c%f", &ch, &n);
    }
    printf("The value of expression is: %.1f\n", value);
    return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If your while loop is (n!=0) then yeah you're going to have to input a zero. The answer is, don't do that. Make your while loop look at your scanf (since that's what's reading the input) -- even better just read in one line, then use sscanf to read from that string until the string is exhausted.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You did get some replies in the previous thread you started. If you decided that it should be C rather than C++, then it would have been better to post in your existing thread that you want it moved - the moderators are able to do that.

    C++ Program to evaluate expressions

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack that evaluates postfix expressions
    By killmequick in forum C Programming
    Replies: 7
    Last Post: 10-01-2008, 06:23 PM
  2. Evaluationof expressions
    By plan7 in forum C Programming
    Replies: 2
    Last Post: 10-28-2007, 07:18 AM
  3. Regular expressions [Boost]
    By Desolation in forum C++ Programming
    Replies: 8
    Last Post: 12-30-2006, 10:10 PM
  4. Help please: regular expressions in C++
    By reivaj7999 in forum C++ Programming
    Replies: 3
    Last Post: 08-24-2005, 01:11 AM
  5. Prefix Expressions
    By mannyginda2g in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2004, 01:30 AM