Question
Use C to to implement a simple calculator, called evaluate, to evaluate simple arithmetic expression We will use s_exp to represent simple arithmetic expression, m_exp
Use C to to implement a simple calculator, called evaluate, to evaluate simple arithmetic expression
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.
s_exp m_exp
s_exp s_exp l_op m_exp
m_exp num
m_exp m_exp h_op num
l_op + |
h_op | /
first 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 floating numbers and we assume that the user will always enter valid numbers. Your program should handle non-valid input character for operators.
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 definition for simple expression in
// Input: sub_exp: the value of the sub s_expression to the left of oplocation 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) {
}
// 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) {
}
// 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() {
}
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