Thread: C++ Program to evaluate expressions

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

    C++ Program to evaluate expressions

    Code:
    These are the program descriptions:
    
    Write a program that evaluates an expression:
    
    Enter an expression: 1+2.5*3
    Value of expression: 10.5
    
    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.
    
    
    I can enter any number of expressions, but the divide function does not work (the answer comes up as 0). Also, if I want to add two numbers, then multiply by another, it does not even exit. Is there a way that I don't have to enter 0 to terminate the program? If anyone can answer ASAP, it would be greatly appreciated.
    
    This is what I have so far.
    
    
    #include <stdio.h>
    
    int main(void)
    {
    char ch;
    float n, value = 0;
    printf("Enter an expression: ");
    
    scanf("%f%c%f", &n, &ch, &value);
    while (n != 0) {
    if (ch == '+')
    {
    value += n;
    }
    if (ch == '-')
    {
    value -= n;
    }
    if (ch == '*')
    {
    value *= n;
    }
    if (ch == '/')
    {
    value /= n;
    }
    scanf("%f", &n);
    }
    printf("The value of expression is: %.1f\n", value);
    
    return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You will probably need to read a value and operator at a time, and keep track of the value of the expression so far. Your scanf as you have it right now won't work.

    --
    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.

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Welcome to cBoard. If you haven't yet, read the Homework policy. (You're fine, so I suspect you might have.)

    First, remove the non-code stuff from between the [code] tags. It'll format your post a bit neater.
    Next, are you indenting your code? For example:
    Code:
    int main()
    {
    	printf("Hello world.\n");
    	for(int i = 0; i < 10; ++i)
    	{
    		printf("%d\n", i);
    	}
    }
    ...is much easier to read. (Once you've been coding for a while, the value of indentation will become clear.) I prefer 1 tab, others use 4 or more spaces. (It's a stylistic choice.)

    That looks like a good start. Inside your loop, you'll probably want to scan for more operators. Right now, it looks like you'll only pick up the first operator. Also, you scanf() n first, then value, but you do value /= n ... You probably want to scanf("...", &value, ..., &n);
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And bear in mind that scanf() is not really a C++ solution for reading data.

    --
    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.

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

    While loop

    Code:
    I am still trying to see how I can loop it without entering zero, because I am really not supposed to enter 0.
    
    #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;
    }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you've read any of the threads on i/o here, you've probably seen us advocate
    Code:
    while (cin >> variable)
    to keep reading until the read fails. In this case, you probably want to read the whole line in one go into a stringstream, and then read bits from that until you've processed it all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM