| | |
Simple Left-To-Right Arithmetic Calculator
![]() |
•
•
Join Date: Sep 2009
Posts: 17
Reputation:
Solved Threads: 0
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:
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:
C Syntax (Toggle Plain Text)
#include <stdio.h> #define SIZE 32 int main(void){ char val[SIZE]; printf("Enter values to be calculated: "); scanf("%d", &val); }
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.
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.
![]() |
Similar Threads
- Need help with a simple python Area Calculator??? (Python)
- Could use some help with a simple program (C#)
- help me please I try to make a arithmetic calculator but i have 3 errors (C++)
- Square root function in calculator (Java)
- Algebraic question (C++)
- Special calculator help (C++)
- Do I need Breadcrumbs? (Site Layout and Usability)
- Code Snippet: Simple Windows GUI Calculator (Python) (Python)
- I just need a push! (C)
Other Threads in the C Forum
- Previous Thread: Need borland c++ for linux
- Next Thread: problem with conversion of units like km to m etc...
| Thread Tools | Search this Thread |
#include * adobe ansi api array asterisks binarysearch centimeter changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork function getlasterror getlogicaldrivestrin givemetehcodez global grade gtkgcurlcompiling gtkwinlinux hacking hardware highest histogram ide include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opendocumentformat opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing segmentationfault sequential single socket socketprograming standard string systemcall threads turboc unix user voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






