-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calc.h
executable file
·107 lines (92 loc) · 3 KB
/
Calc.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
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
#ifndef CALC_H
#define CALC_H
/* DO NOT CHANGE: This file is used in evaluation */
#include <iostream>
#include <string>
#include "Stack.h"
#include "SymTab.h"
using namespace std;
#define OPERATOR 0
#define VALUE 1
#define VARIABLE 2
class Operator {
friend class Calculator;
friend class Word;
friend ostream & operator << (ostream &, const Operator &);
char ascii; /* ascii character of operator */
long index; /* index in parallel arrays */
long priority; /* priority of operator */
Operator (char);
};
class Variable {
friend class Calculator;
friend class Word;
friend Variable * assign (Variable *, long);
friend ostream & operator << (ostream &, const Variable &);
char name[80]; /* name of variable */
long value; /* value of interest */
Variable (char * nm, long val = 0) : value (val) {
memset (name, '\0', sizeof (name));
strcpy (name, nm);
}
public:
Variable (void) : value (0) {
memset (name, '\0', sizeof (name));
}
operator const char * (void) const {
return name;
}
Variable (const Variable & variable) {
memset (name, '\0', sizeof (name));
strcpy (name, variable.name);
value = variable.value;
}
long operator == (const Variable & vvv) const {
return ! strcmp (name, vvv.name);
}
long operator < (const Variable & vvv) const {
return (strcmp (name, vvv.name) < 0) ? 1 : 0;
}
};
/* declare the word to place on calulator stacks */
class Word {
friend class Calculator;
friend ostream & operator << (ostream &, const Word &);
union {
Operator * op;
long value;
Variable * var;
};
long type;
char isoperator (void) const {
return (type == OPERATOR);
}
char isvalue (void) const {
return (type == VALUE);
}
char isvariable (void) const {
return (type == VARIABLE);
}
Word (char character) : op (new Operator(character)), type (OPERATOR) {}
Word (long val) : value (val), type (VALUE) {}
Word (char * name, long val = 0) :
var (new Variable (name, val)), type (VARIABLE) {}
public:
~Word (void) {
if (type == OPERATOR)
delete op;
else if (type == VARIABLE)
delete var;
}
};
class Calculator {
friend ostream & operator << (ostream &, const Calculator &);
SymTab<Variable> symtab;
Stack<Word> stack1, stack2;
public:
Calculator (const char * datafile) : symtab (datafile) {}
long Eval (void);
long InToPost (void);
ostream & Write_Postfix (ostream &) const;
};
#endif