-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvariable.h
73 lines (68 loc) · 1.52 KB
/
variable.h
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
#ifndef VARIABLE_H
#define VARIABLE_H
#include<string>
#include<map>
#include<iostream>
using namespace std;
class variable{
public:
map<string,string> variables;
string errors="";
bool set_value(string &key,string &value);
string get_value(string key);
void show_errors();
};
bool variable::set_value(string &key,string &value){
int flag=0;
for(int i=0;i<key.length();i++){
if(!(isalpha(key[i]) || isdigit(key[i]) || key[i]=='_')){
flag=1;
break;
}
}
if((!isalpha(key[0])) || flag==1){
errors+=key+" is a illegal name of variable;";
}
map<string,string>::iterator it;
int p=0;
for(it=variables.begin();it!=variables.end();it++){
if(it->first==key){
p=1;
break;
}
}
if(p==1){
variables[key]=value;
}
else
variables.insert({key,value});
}
string variable::get_value(string key){
map<string,string>::iterator it;
int p=0;
for(it=variables.begin();it!=variables.end();it++){
if(it->first==key){
p=1;
break;
}
}
if(p==1){
if(variables.at(key)!="\0")
return variables.at(key);
else
return "\0";
}
else
return "\0";
}
void variable::show_errors(){
cout<<"\n\n";
for(int i=0;i<errors.length();i++){
if(errors[i]==';'){
cout<<endl;
continue;
}
cout<<errors[i];
}
}
#endif // VARIABLE_H