-
Notifications
You must be signed in to change notification settings - Fork 0
/
CalculatorTester.cpp
71 lines (60 loc) · 1.53 KB
/
CalculatorTester.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Assignment 10X. Calculator
*
* An interactive calculator for arithmetic expressions with numbers
* and the + - * / operators and parenthesized subexpressions.
*
* Extra credit #1: Allow leading + and -
* Extra credit #2: Allow exponentiation with the ^ operator
*
* Author: Hamsini Sankaran
* Department of Computer Engineering
* San Jose State University
*/
#include <iostream>
#include <stdlib.h>
#include "Calculator.h"
using namespace std;
void evaluate_expression(Calculator& calculator);
/**
* The main.
*/
int main()
{
Calculator calculator;
char ch;
// Prompt for and evaluate arithmetic expressions.
do
{
cout << endl << "Expression? ";
cin >> ws;
ch = cin.peek();
// Evaluate the expression unless it's the end sentinel.
if (ch != '.') evaluate_expression(calculator);
} while (ch != '.');
cout << endl << "Done!" << endl;
return 0;
}
/**
* Evaluate an arithmetic expression using a calculator.
* @param calculator the calculator to use.
*/
void evaluate_expression(Calculator& calculator)
{
try
{
// Evaluate the expression and print its value.
cout << calculator.evaluate() << endl;
// An = sign must follow the expression.
char ch;
cin >> ch;
if (ch != '=') throw string("Unexpected ") + ch;
}
catch(string& msg)
{
cout << "***** " << msg << endl;
}
// Skip the rest of the input line.
string rest_of_line;
getline(cin, rest_of_line);
}