Final Exam 1-2: Calculator (20%)
- Inputs:
- The expression to be evaluated.
- Each operand and operator is separated line by line.
- The operands are in
double
format. - The operators are in
std::string
format. - The expressiion is ended by
=
.
- The valid operators are
+
,-
,*
,/
- The
/
opeartor is only valid if the second operand is not zero.
- The
- The expression to be evaluated.
- Outputs:
- The list of operands and operators in the expression.
- The format is shown below.
- The operands are in defualt
double
format instd::cout
.
- The result of the expression.
- The calculation is done line by line.
- The result is in defualt
double
format.
- If the expression is invalid, the output is
Invalid Expression
and exit.- If the number of operands is not equal to the number of operators plus one.
- The invalid operator occurs.
- The list of operands and operators in the expression.
- File name:
final1_2_<student_id>.cpp
(e.g.final1_2_106062802.cpp
)
Notice:
- The program will not print any user prompts.
- The program should use functions to process the input and output.
- The program should be finished within 30 seconds. Any test cases will garuntee the program is finished within 30 second
Format
<operand_1>⏎
<operator_1>⏎
...
<operand_n>⏎
=⏎
Operand: <operand_1> ... <operand_n>
Operator: <operator_1> ... <operator_n-1>
Result: <result>
Example
$ ./a.out⏎
0.0⏎
=⏎
Operands: 0
Operators:
Result: 0
$ ./a.out⏎
0.0⏎
/⏎
1.0⏎
=⏎
Operands: 0 1
Operators: /
Result: 0
$ ./a.out⏎
1.0⏎
*⏎
2.0⏎
+⏎
3.0⏎
-⏎
4.0⏎
=⏎
Operands: 1 2 3 4
Operators: * + -
Result: 1
$ ./a.out⏎
-1.234e-5⏎
*⏎
-5.678e-6⏎
+⏎
9.876e-7⏎
-⏎
5.432e-8⏎
=⏎
Operands: -1.234e-05 -5.678e-06 9.876e-07 5.432e-08
Operators: * + -
Result: 9.3335e-07
$ ./a.out⏎
-1.234e-5⏎
*⏎
-5.678e-6⏎
+⏎
9.876e-7⏎
5.432e-8⏎
=⏎
Invalid Expression
$ ./a.out⏎
-1.234e-5⏎
*⏎
-5.678e-6⏎
+⏎
9.876e-7⏎
-⏎
=⏎
Invalid Expression
$ ./a.out⏎
-1.234e-5⏎
*⏎
-5.678e-6⏎
+⏎
9.876e-7⏎
%⏎
5.432e-8⏎
=⏎
Invalid Expression
$ ./a.out⏎
-1.234e-5⏎
*⏎
-5.678e-6⏎
+⏎
9.876e-7⏎
/⏎
0.0⏎
=⏎
Invalid Expression
Pseudo code
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// parse_expression
// Parse the expression from cin and store the operator and operand in
// operator_vector and operand_vector respectively.
// Hint: there is a function like atoi but it convert char[] to double.
// Hint 2: to convert string to char[] use .c_str() function.
void parse_expression(vector<string> &operator_vector,
vector<double> &operand_vector);
// check_expression
// Check if the expression is valid.
// Return true if the expression is valid, otherwise return false.
bool check_expression(const vector<string> &operator_vector,
const vector<double> &operand_vector);
// print_operator
// Print the operator in operator_vector.
void print_operator(const vector<string> &operator_vector);
// print_operand
// Print the operand in operand_vector.
// Use the default cout format for double.
void print_operand(const vector<double> &operand_vector);
// calculate_expression
// Calculate the expression store in operator_vector and operand_vector
// and return the result in double.
// User should check the expression is valid before calling this function.
double calculate_expression(const vector<string> &operator_vector,
const vector<double> &operand_vector);
int main(int argc, char *argv[])
{
vector<string> operator_vector;
vector<double> operand_vector;
parse_expression(operator_vector, operand_vector);
if (!check_expression(operator_vector, operand_vector))
{
cout << "Invalid Expression" << endl;
return EXIT_FAILURE;
}
print_operand(operand_vector);
print_operator(operator_vector);
cout << "Result: " << calculate_expression(operator_vector, operand_vector)
<< endl;
return EXIT_SUCCESS;
}
Reference Code:
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// parse_expression
// Parse the expression from cin and store the operator and operand in
// operator_vector and operand_vector respectively.
// Hint: there is a function like atoi but it convert char[] to double.
// Hint 2: to convert string to char[] use .c_str() function.
void parse_expression(vector<string> &operator_vector,
vector<double> &operand_vector);
// check_expression
// Check if the expression is valid.
// Return true if the expression is valid, otherwise return false.
bool check_expression(const vector<string> &operator_vector,
const vector<double> &operand_vector);
// print_operator
// Print the operator in operator_vector.
void print_operator(const vector<string> &operator_vector);
// print_operand
// Print the operand in operand_vector.
// Use the default cout format for double.
void print_operand(const vector<double> &operand_vector);
// calculate_expression
// Calculate the expression store in operator_vector and operand_vector
// and return the result in double.
// User should check the expression is valid before calling this function.
double calculate_expression(const vector<string> &operator_vector,
const vector<double> &operand_vector);
int main(int argc, char *argv[])
{
vector<string> operator_vector;
vector<double> operand_vector;
parse_expression(operator_vector, operand_vector);
if (!check_expression(operator_vector, operand_vector))
{
cout << "Invalid Expression" << endl;
return EXIT_FAILURE;
}
print_operand(operand_vector);
print_operator(operator_vector);
cout << "Result: " << calculate_expression(operator_vector, operand_vector)
<< endl;
return EXIT_SUCCESS;
}
void parse_expression(vector<string> &operator_vector,
vector<double> &operand_vector)
{
for (string temp; cin >> temp;)
{
if (temp == "=")
{
break;
}
else if(isnumber(temp[0]))
{
operand_vector.push_back(atof(temp.c_str()));
}
else
{
operator_vector.push_back(temp);
}
}
}
bool check_expression(const vector<string> &operator_vector,
const vector<double> &operand_vector)
{
for (unsigned i = 0; i < operator_vector.size(); i++)
{
if (operator_vector[i] == "/" && operand_vector[i + 1] == 0)
{
return false;
}
else if (operator_vector[i] != "+" && operator_vector[i] != "-" &&
operator_vector[i] != "*" && operator_vector[i] != "/")
{
return false;
}
}
return (operator_vector.size() == operand_vector.size() - 1);
}
void print_operator(const vector<string> &operator_vector)
{
cout << "Operators:";
for (int i = 0; i < operator_vector.size(); i++)
cout << ' ' << operator_vector[i];
cout << endl;
}
void print_operand(const vector<double> &operand_vector)
{
cout << "Operands:";
for (int i = 0; i < operand_vector.size(); i++)
cout << ' ' << operand_vector[i];
cout << endl;
}
double calculate_expression(const vector<string> &operator_vector,
const vector<double> &operand_vector)
{
double result = operand_vector[0];
for (unsigned i = 0; i < operator_vector.size(); i++)
{
if (operator_vector[i] == "+")
{
result += operand_vector[i + 1];
}
else if (operator_vector[i] == "-")
{
result -= operand_vector[i + 1];
}
else if (operator_vector[i] == "*")
{
result *= operand_vector[i + 1];
}
else if (operator_vector[i] == "/")
{
result /= operand_vector[i + 1];
}
}
return result;
}