Final Exam 1: Command Line Floating Point Calculator

Final Exam 1-1: Display Operators & Operands (35%)

  • Inputs:
    1. The expression to be evaluated.
      1. Each operand and operator is separated line by line.
      2. The operands are in double format.
      3. The operators are in std::string format.
      4. The expressiion is ended by =.
    2. The valid operators are +, -, *, /
      1. The / opeartor is only valid if the second operand is not zero.
  • Outputs:
    1. The list of operands and operators in the expression.
      1. The format is shown below.
      2. The operands are in defualt double format in std::cout.
    2. If the expression is invalid, the output is Invalid Expression and exit.
      1. If the number of operands is not equal to the number of operators plus one.
      2. The invalid operator occurs.
  • File name: final1_1_<student_id>.cpp (e.g. final1_1_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>

Example

$ ./a.out⏎
0.0⏎
=⏎
Operands: 0
Operators:
$ ./a.out⏎
0.0⏎
/⏎
1.0⏎
=⏎
Operands: 0 1
Operators: /
$ ./a.out⏎
1.0⏎
*⏎
2.0⏎
+⏎
3.0⏎
-⏎
4.0⏎
=⏎
Operands: 1 2 3 4
Operators: * + -
$ ./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: * + -
$ ./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);

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);

    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);

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);

    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;
}