Simple Left-To-Right Arithmetic Calculator

Reply

Join Date: Sep 2009
Posts: 17
Reputation: Kombat is an unknown quantity at this point 
Solved Threads: 0
Kombat Kombat is offline Offline
Newbie Poster

Simple Left-To-Right Arithmetic Calculator

 
0
  #1
Sep 18th, 2009
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. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Simple Left-To-Right Arithmetic Calculator

 
0
  #2
Sep 18th, 2009
We don't do homework for you.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 17
Reputation: Kombat is an unknown quantity at this point 
Solved Threads: 0
Kombat Kombat is offline Offline
Newbie Poster

Re: Simple Left-To-Right Arithmetic Calculator

 
0
  #3
Sep 18th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Simple Left-To-Right Arithmetic Calculator

 
0
  #4
Sep 18th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC