943,891 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1956
  • C RSS
Sep 18th, 2009
0

Simple Left-To-Right Arithmetic Calculator

Expand Post »
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:
  1. #include <stdio.h>
  2. #define SIZE 32
  3.  
  4. int main(void){
  5. char val[SIZE];
  6. printf("Enter values to be calculated: ");
  7. scanf("%d", &val);
  8. }
Reputation Points: 10
Solved Threads: 0
Light Poster
Kombat is offline Offline
39 posts
since Sep 2009
Sep 18th, 2009
0

Re: Simple Left-To-Right Arithmetic Calculator

We don't do homework for you.
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Sep 18th, 2009
0

Re: Simple Left-To-Right Arithmetic Calculator

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
Reputation Points: 10
Solved Threads: 0
Light Poster
Kombat is offline Offline
39 posts
since Sep 2009
Sep 18th, 2009
0

Re: Simple Left-To-Right Arithmetic Calculator

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Need borland c++ for linux
Next Thread in C Forum Timeline: C Program Compiles in Dev-C++ but not GCC?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC