Question
PLEASE WRITE IN C The goal of the exercise is to implement a simple calculator, called evaluate, to evaluate simple arithmetic expression. (1) An arithmetic
PLEASE WRITE IN C
The goal of the exercise is to implement a simple calculator, called evaluate, to evaluate simple arithmetic expression. (1) An arithmetic expression is an expression that results in a numeric value. We consider numeric value to be real or oating point numbers. A simple arithmetic expression involves numeric values connected by arithmetic operators. In this exercise, numeric value will be real or oating point numbers and operators will be +, , , and /. For example, 4 + 35 is a simple arithmetic expression and 2.3 4 5 -7.8 + 9 / 3 is another simple arithmetic expression. In evaluating arithmetic expression, and / have higher precedence than + and . With operators of the same precedence, the order of evaluation is from left to right. We will use s exp to represent simple arithmetic expression, m exp to represent simple arithmetic expression in which all operators are or /, l op to represent operators + and , h op to represent operators and /, and num to represent numeric value. In the following, simple arithmetic expression is represented recursively, where | represent OR relationship. This recursive denition would be useful in designing your 1 solution for this exercise. s exp mexp
s exp s exp l op m exp
m exp num
m exp m exp h op num
l op + |
h op | /
(2) In your program, rst the user is asked to input a simple arithmetic expression. In the inputted simple arithmetic expression, there could be space characters before a number or an operator, The input numbers could be either integers or oating numbers and we assume that the user will always enter valid numbers. Your program should handle non-valid input character for operators. (3) After user input, the program will calculate and print the numeric value of the inputted simple arithmetic expression. The program does not read the whole expression before its calculation. The calculation proceeds while reading numbers and operators of the inputted simple arithmetic expression. In evaluating simple arithmetic expression, + and have the same precedence and the evaluation order is from left to right, and and / have the same precedence and the evaluation order is from left to right. We will use two recursive functions to perform the evaluation. The implementation of these recursive functions should follow the recursive denition for simple expression in (1). To guide you toward this goal, we provide a template function. We ask you to use this template and ll in the missing code. // Input: sub_exp: the value of the sub s_expression to the left of op
// location in stdin.
// op : an operator, + or -. op could also be
// indicating the end of the s_expression
// the rest of the expression will be read in from stdin
// Effect: the whole s_expression is evaluated using recursion:
// get next_num with m_exp() and then get next_op with get_op()
// use sub_exp op next_num and next_op to do recursive call
// Output: this function returns the value of the s_expression
float s_exp(float sub_exp, char op) { } 2 // Input: sub_exp: the value of the current sub m_expression
// to the left of op location in stdin.
// op : an operator, * or /. op could also be
// +, -, or indicating the end of the m_expression.
// "+, -, or should be pushed back to stdin.
// the rest of the m_expression will be read in from stdin
// Effect: the m_expression is evaluated using recursion:
// get next_num with get_num() and then get next_op with get_op()
// use sub_exp op next_num and next_op to do recursive call
// Output: this function returns the value of the current m_expression float m_exp(float sub_exp, char op) { } The following two functions should also be used to simplify the programming task. // Input: none, read from stdin
// Effect: get the next operator of the expression
// this operator can be +, -, *, /, or
// indicates the end of the expression input
// leading spaces should skipped
// Output: return the next operator of the expression. char get_op() { } // Input: none, read from stdin
// Effect: get the next numeric value of the expression
// Output: return the next numeric value of the expression. float get_num() { } To push back one character in ch to stdin, use ungetc(ch,stdin). ungetc() is a library function that can push back a character to a specied stream. When an error is detected, you can exit your program by a function call exit(EXIT FAILURE). To use this function, the required header is: #include < stdlib.h > 3 (4) Then the user is asked if she/he wants to continue. Two characters can be entered, each corresponding to a possible action. Y for continuing inputting a simple arithmetic expression N for quit (5) Evaluate and print values, or report input format errors, for the following simple arithmetic expressions with your program. 5 3 4 4 + 3 2.6 / 2 1.5 10 -2.0 2 + 1.5 10 100 3.5 -1.5 10 / 3.0 + 2 3 3.5 1.5 % 10 2.0
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started