You are required to write a C program to carry out a strict-left-to-right evaluation of an
arithmetic expression consisting of integer constants and the operators +, −, *, and /. Here, the
operator / denotes integer division; that is, the remainder is discarded. In a strict-left-to-right
evaluation, there is no notion of precedence. For example, the value of the expression 6+4*3 when
evaluated in a strict-left-to-right fashion is 30. (Under usual precedence rules, where multiplication
has higher precedence than addition, the value of the above expression would be 18.) Similarly, the
value of the expression 6 + 4 * 3/7 − 9 when evaluated in a strict-left-to-right fashion is −5.
Here is some additional information about the input.
1. You may assume that each input expression is terminated by the newline (’\n’) character.
2. There may be zero or more blanks (spaces) between successive non-blank characters of an
expression.
3. For an expression to be valid, all of the following four conditions must be satisfied.
(a) The expression consists only of integer constants, the operators +, −, *, / and blanks.
In particular, valid expressions don’t contain parentheses.
(b) Each integer constant in the expression consists of only one decimal digit.
(c) The expression begins with an integer constant, without any preceding sign.
(d) In the expression, integer constants and operators alternate.
Note that an expression consisting of a single digit integer constant, without any operators,
is a valid expression.
Your program must detect the following errors.
(a) The expression is not valid; that is, it does not satisfy one or more of the conditions mentioned
above.
(b) During the evaluation, division by zero is encountered.
2
When an error is encountered, your program must print a suitable error message and stop. Your
program may stop as soon as the first error is detected.
The outline of your C program is as follows.
1. Prompt the user for an expression.
2. Read the expression character by character and carry out a strict-left-to-right evaluation of
the expression.
3. During the evaluation process, if the expression is found to be invalid, print a short error
message (e.g. “Invalid expression.”) and stop. Likewise, if during the evaluation, a division
by zero is encountered, print an error message (e.g. “Division by zero attempted.”)
and stop.
4. If no errors occur, print the value of the expression and stop.
Thus, each time your program for Part (b) is executed, it should handle just one expression. As
in part (a), bear in mind that your program reads its input from stdin and writes its output to
stdout.

Code So Far:

#include <stdio.h>
#define SIZE 32

int main(void){
    char val[SIZE];
    printf("Enter values to be calculated:  ");
    scanf("%d", &val);
}

Recommended Answers

All 3 Replies

We don't do homework for you.

I understand. I do not want the result but can someone put me in the right direction. I think I need switch statements but idk how to initialize them

http://www.daniweb.com/forums/thread223648.html
The amount of effort it took just to reverse a string makes this near impossible for you.

You need some more intermediate steps if you actually want to make a contribution for yourself rather than waiting for us to write it all a few lines at a time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.