-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathval.h
More file actions
47 lines (38 loc) · 1.13 KB
/
Copy pathval.h
File metadata and controls
47 lines (38 loc) · 1.13 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
#ifndef VALUE_H
#define VALUE_H
#include <iostream>
#include <string>
using namespace std;
enum ValType { VINT, VSTR, VERR };
class Value {
ValType T;
int I;
string S;
public:
Value(): T(VERR), I(0) {}
Value(int vi): T(VINT), I(vi) {}
Value(string vs): T(VSTR), I(0), S(vs) {}
ValType GetType() const { return T; }
bool isErr() const { return T == VERR; }
bool isInt() const { return T == VINT; }
bool isStr() const { return T == VSTR; }
int getInt() const {if (isInt()) { return I; } throw string("RUNTIME ERROR: Value not an integer");}
string getStr() const {if (isStr()) { return S; } throw string("RUNTIME ERROR: Value not a string");}
Value operator+(const Value& op) const;
Value operator-(const Value& op) const;
Value operator*(const Value& op) const;
Value operator/(const Value& op) const;
friend ostream& operator<<(ostream& out, const Value& op) {
if (op.isInt()) {
out << op.I;
}
else if (op.isStr()) {
out << op.S;
}
else {
out << "ERROR";
}
return out;
}
};
#endif