-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.cpp
More file actions
104 lines (95 loc) · 2.77 KB
/
calculator.cpp
File metadata and controls
104 lines (95 loc) · 2.77 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// main.cpp
// commandLab
//
// Created by shen on 16/11/7.
// Copyright © 2016年 shen. All rights reserved.
//
#include <iostream>
#include <vector>
#include "composite.h"
#include "menu.h"
#include "command.h"
#include <stdlib.h>
#include <cstring>
using namespace std;
bool isInteger(const string& s)
{
if(s.empty()) {
return false ;
}
char * p ;
strtol(s.c_str(), &p, 10) ;
return (*p == 0) ;
}
int main() {
Menu* menu = new Menu();
string input;
while(input != "exit") {
cout << "Choose a Menu Command: ";
getline(cin, input);
//cout << isInteger(input.substr(2)) << endl;
//cout << input.substr(0,1) << "======" << endl;
//string a =input.substr(0,1);
if(input == "exit") {
break;
}
else if(input == "redo") {
cout << "Redoing Command" << endl;
menu->redo();
cout << "Result: ";
menu->execute();
}
else if(input == "undo") {
cout << "Undoing Command" << endl;
menu->undo();
cout << "Result: ";
menu->execute();
}
else if(isInteger(input)) {
if(!menu->initialized()) {
menu->add_command(new OpCommand(atoi(input.c_str())));
cout << "Result: ";
menu->execute();
}
}
//
else if(input.substr(0,1) == "+") {
if(menu->initialized()) {
if(isInteger(input.substr(2))) {
menu->add_command(new AddCommand(menu->get_command(), atoi((input.substr(2)).c_str())));
cout << "Result: ";
menu->execute();
}
}
}
else if(input.substr(0,1) == "-") {
if(menu->initialized()) {
if(isInteger(input.substr(2))) {
menu->add_command(new SubCommand(menu->get_command(), atoi((input.substr(2)).c_str())));
cout << "Result: ";
menu->execute();
}
}
}
else if(input.substr(0,1) == "*") {
if(menu->initialized()) {
if(isInteger(input.substr(2))) {
menu->add_command(new MultCommand(menu->get_command(), atoi((input.substr(2)).c_str())));
cout << "Result: ";
menu->execute();
}
}
}
else if(input == "^") {
if(menu->initialized()) {
menu->add_command(new SqrCommand(menu->get_command()));
cout << "Result: ";
menu->execute();
}
}
else {
cout << "Invalid Command" << endl;
}
}
};