/
Input.cpp
81 lines (74 loc) · 1.42 KB
/
Input.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
72
73
74
75
76
77
78
79
80
81
/*
**
** Input.cpp
** Copyright by Jan Poeschko
**
*/
#include "StdAfx.h"
#include "Input.h"
#include <iostream>
using std::cin;
Input::Input(): text("")
{
position = text.c_str();
}
Input::~Input()
{
}
void Input::ReadInput()
{
getline(cin, text, cin.widen('\n'));
position = text.c_str();
}
bool Input::StartsWith(char start, string &following)
{
if(text.length() > 0 && text.at(0) == start)
{
// Return substring starting at second character
following = text.substr(1);
return true;
}
else
{
following = "";
return false;
}
}
string Input::GetIdentifier()
{
string result("");
while(position && (
*position >= 'a' && *position <= 'z' ||
*position >= 'A' && *position <= 'Z' ||
*position >= '0' && *position <= '9'))
{
result += *position;
position++;
}
return result;
}
string Input::GetOperator()
{
static const int Operator_Characters_Count = 22;
static const char Operator_Characters[Operator_Characters_Count] = {
'+', '-', '*', '^', '/', '=', '@', '&', '|', '<', '>', '!', '?', '~', '.', ':', '%',
';', '(', '{', '[', '#'
};
string result("");
while(position)
{
bool isValid(false);
for(int i=0; i<Operator_Characters_Count; i++)
if(*position == Operator_Characters[i])
{
isValid = true;
break;
}
if(isValid)
result += *position;
else
break;
position++;
}
return result;
}