-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintCommand.cpp
More file actions
44 lines (41 loc) · 1.26 KB
/
PrintCommand.cpp
File metadata and controls
44 lines (41 loc) · 1.26 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
//
// Created by duni on 22/12/2019.
//
#include <iostream>
#include "PrintCommand.h"
#include "globalVariables.h"
#include "ExpressionCalculate.h"
#include <algorithm>
int PrintCommand::execute(vector<string> params) {
// Check if the parameter in the print command is an expression or a string
if (doesDelimExist(params[1], '"')) {
cout << params[1].substr(1, params[1].length() - 2) << endl;
} else {
// If it is an expression - then calculate
Interpreter *i = new Interpreter();
Expression *answer = nullptr;
// Remove spaces
string::iterator end_pos = remove(params[1].begin(), params[1].end(), ' ');
params[1].erase(end_pos, params[1].end());
// Remove tabs
end_pos = remove(params[1].begin(), params[1].end(), '\t');
params[1].erase(end_pos, params[1].end());
// Calculate
answer = i->interpret(params[1]);
double answerResult = answer->calculate();
cout << answerResult << endl;
}
return 2;
}
// This function checks if the print command parameter is a string - surrounded by brackets
bool PrintCommand::doesDelimExist(string line, char delim) {
char currChar;
int i;
for (i = 0; i < line.length(); i++) {
currChar = line[i];
if (currChar == delim) {
return true;
}
}
return false;
}