-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalcTokens.cpp
More file actions
136 lines (108 loc) · 3.1 KB
/
Copy pathcalcTokens.cpp
File metadata and controls
136 lines (108 loc) · 3.1 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <string.h>
#include <cctype>
#include <algorithm>
#include "calc.h"
TokenInfo getToken(const char *tokenString)
{
TokenInfo ourToken;
//empty string is not correct option
if (tokenString[0] == '\0')
{
ourToken.token_type = OTHER;
return ourToken;
}
ourToken.token_value = tokenString;
if (strlen(tokenString) == 1 && (tokenString[0] == '+' || tokenString[0] == '-' || tokenString[0] == '*' || tokenString[0] == '/'))
{
ourToken.token_type = OPERATOR;
return ourToken;
}
bool dot_used = false;
for (int i = 0; i < strlen(tokenString); ++i)
{
if ((!isdigit(tokenString[i]) && tokenString[i] != '.')
|| (tokenString[i] == '.' && (dot_used || i == 0 || i + 1 == strlen(tokenString))))
{
ourToken.token_type = OTHER;
return ourToken;
}
else if (tokenString[i] == '.')
dot_used = true;
}
if (dot_used)
ourToken.token_type = DOUBLE;
else
ourToken.token_type = INTEGER;
return ourToken;
}
void parseArgs(int argc, char **argv, std::vector<std::string> &arguments)
{
if (argc != 2)
{
std::cout << "Wrong arguments, you can use -help or -h. All input must be in quotes, because of asterix interpretation by shell." << std::endl;
exit(EXIT_FAILURE);
}
else if (argc == 2 && ((strcmp(argv[1], "-help") == 0) || strcmp(argv[1], "-h") == 0))
{
std::cout << "This is help for you:\nWrite what you want to calculate. With style:\nNumber Operation Number\nBrackets have not been implemented yet.\n\n";
exit(EXIT_SUCCESS);
}
std::string input = argv[1];
//remove all whitespaces
input.erase(std::remove_if(input.begin(), input.end(), ::isspace), input.end());
int pos = 0;
bool operatorMode = false;
for (int i = 0; i < input.length(); ++i)
{
if (isdigit(input[i]) || (input[i] == '.'))
{
if (operatorMode)
{
arguments.push_back(input.substr(pos, i - pos));
pos = i;
operatorMode = false;
}
}
else
{
if (!operatorMode)
{
//string going from start to this position was number
arguments.push_back(input.substr(pos, i - pos));
pos = i;
operatorMode = true;
}
}
}
//input was ended with operator
if (operatorMode)
{
std::cout << "Expresion was not ended with number" << std::endl;
exit(EXIT_FAILURE);
}
arguments.push_back(input.substr(pos));
//check those arguments that have been isolated
TokenInfo testToken;
for (int i = 0; i < arguments.size(); ++i)
{
if ((i % 2) == 0 || i == 0)
{
testToken = getToken(arguments.at(i).c_str());
if (testToken.token_type != DOUBLE && testToken.token_type != INTEGER)
{
std::cout << "First argument is wrong, you have to put number here." << std::endl;
exit(EXIT_FAILURE);
}
}
else
{
testToken = getToken(arguments.at(i).c_str());
if (testToken.token_type != OPERATOR)
{
std::cout << "Second argument is wrong, you have to put number here." << std::endl;
exit(EXIT_FAILURE);
}
}
}
return;
}