-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.cpp
180 lines (167 loc) · 3 KB
/
main.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include<iostream>
#include<string>
#include "Sales_data.h"
class Token
{
friend std::ostream & operator <<(std::ostream & os, const Token &t);
public:
Token() : tok(INT), ival{0} { }
Token(const Token &t) : tok(t.tok) { copyUnion(t); }
Token & operator=(const Token &);
~Token();
Token & operator=(const std::string &);
Token & operator=(const Sales_data &);
Token & operator=(char);
Token & operator=(int);
Token & operator=(double);
private:
enum {INT, CHAR, DBL, STR, SALE} tok;
union {
char cval;
int ival;
double dval;
std::string sval;
Sales_data saval;
};
void copyUnion(const Token &);
};
Token::~Token()
{
if(tok == STR)
sval.~basic_string();
if(tok == SALE)
saval.~Sales_data();
}
Token & Token::operator=(int i)
{
if(tok == STR)
sval.~basic_string();
if(tok == SALE)
saval.~Sales_data();
ival = i;
tok = INT;
return *this;
}
Token & Token::operator=(const std::string & s)
{
if(tok == SALE)
saval.~Sales_data();
if(tok == STR)
sval = s;
else
new (&sval) std::string(s);
tok = STR;
return *this;
}
Token & Token::operator=(const Sales_data & s)
{
if(tok == STR)
sval.~basic_string();
if(tok == SALE)
saval = s;
else
new (&saval) Sales_data(s);
tok = SALE;
return *this;
}
Token & Token::operator=(char c)
{
if(tok == CHAR)
sval.~basic_string();
if(tok == SALE)
saval.~Sales_data();
cval = c;
tok = CHAR;
return *this;
}
Token & Token::operator=(double d)
{
if(tok == DBL)
sval.~basic_string();
if(tok == SALE)
saval.~Sales_data();
dval = d;
tok = DBL;
return *this;
}
void Token::copyUnion(const Token & t)
{
switch(t.tok)
{
case INT:
ival = t.ival;
break;
case CHAR:
cval = t.cval;
break;
case DBL:
dval = t.dval;
break;
case STR:
new (&sval) std::string(t.sval);
break;
case SALE:
new (&saval) Sales_data(t.saval);
}
}
Token & Token::operator=(const Token &t)
{
if(tok == STR && t.tok != STR)
sval.~basic_string();
if(tok == SALE && t.tok != SALE)
saval.~Sales_data();
if(tok == STR && t.tok == STR)
sval = t.sval;
else if(tok == SALE && t.tok == SALE)
saval = t.saval;
else
copyUnion(t);
tok = t.tok;
return *this;
}
std::ostream & operator<<(std::ostream & os, const Token &t)
{
switch(t.tok)
{
case Token::INT:
std::cout << "INT " << t.ival;
break;
case Token::CHAR:
std::cout << "CHAR " << t.cval;
break;
case Token::DBL:
std::cout << "DBL " << t.dval;
break;
case Token::STR:
std::cout << "STR " << t.sval;
break;
case Token::SALE:
std::cout << "SALE " << t.saval;
break;
}
return os;
}
int main()
{
Sales_data a(std::string("qweasd"), 1, 2.0);
Token t, t1;
std::cout << t << std::endl;
t = 3;
std::cout << t << std::endl;
t = 'q';
std::cout << t << std::endl;
t = 3.14;
std::cout << t << std::endl;
t = std::string("qwerty");
std::cout << t << std::endl;
t = std::string("qaz");
std::cout << t << std::endl;
t = a;
std::cout << t << std::endl;
t = t;
std::cout << t << std::endl;
t1 = 'q';
t = t1;
std::cout << t << std::endl;
return 0;
}