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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
#include <iostream>
#include <string>
#include <queue>
#include <string>
#include <cstdlib>
#include <cmath>
using namespace std;
/********************************************************************************/
/*************************************************
*
*
* This is the start of Token.h
*
*
*/
#ifndef TOKEN_H
#define TOKEN_H
enum TokenType {opType, valType};
class Token
{
friend std::ostream& operator<<(std::ostream& o, Token t);
public:
Token(char c);
Token(double d);
bool isOp() {return type == opType;}
bool isVal() {return type == valType;}
char getOp() {return op;}
double getVal() {return val;}
int precedence();
protected:
private:
TokenType type;
char op; /// if type == op, this field is valid
double val; /// if type == number, this field is valid
};
#endif // TOKEN_H
/*
*
* This is the End of Token.h
*
*
*******************************************************/
/*****************************************************
*
*
*This is the start of Token.cpp
*
*
*/
Token::Token(char c)
{
op = c; // store the operator
type = opType; // set the type to be an operator
}
Token::Token(double d)
{
val = d; // store the value
type = valType; // set the type to be a value
}
// overloaded output operator
std::ostream& operator<<(std::ostream& o, Token t)
{
if (t.isOp())
{
std::cout << "Operator: " << t.getOp();
}
else if (t.isVal())
{
std::cout << "Number: " << t.getVal();
}
}
int Token::precedence()
{
switch (op)
{
case '!': return 8;
case '^': return 7;
case '*':
case '/': return 6;
case '+':
case '-': return 5;
//case gt,ge,lt,le: return 4;
//case eq, ne : return 3;
//case '&' : return 2;
//case '|' : return 1;
case '(': return 0;
}
}
/*
*
*
* This is the end of Token.cpp
*
***********************************************************/
/**************************************************************************/
/****************************************************************************/
/*
*
*
* This is the beginning of LinkedList.h
*
*
*/
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
struct Node
{
Node *next;
Token *data;
};
class LinkedList
{
friend ostream& operator<<(ostream &o, LinkedList ll);
public:
LinkedList();
virtual ~LinkedList();
void head_insert(Token s);
int count();
Token head_delete();
bool empty() {return !head;}
protected:
Node *head;
Node *tail;
private:
int count(Node *);
};
#endif // LINKEDLIST_H
/*
*
*
* This is the end of LinkedList.h
*
*
***************************************************************/
/****************************************************************************/
/*
*
*
* This is the beginning of LinkedList.cpp
*
*
*/
LinkedList::LinkedList()
{
head = tail = NULL;
}
LinkedList::~LinkedList()
{
//dtor
}
void LinkedList::head_insert(Token s)
{
Node * n = new Node;
if (!head) // empty list
{
head = tail = n;
head->data = &s;
head->next = NULL;
}
else // non-empty list
{
n->next = head;
head = n;
n->data = &s;
}
}
ostream& operator<<(ostream &o, LinkedList ll)
{
Node *ptr = ll.head;
while (ptr != NULL)
{
o << ptr->data << ", ";
ptr = ptr->next;
}
return o;
}
int LinkedList::count()
{
return count(head);
}
int LinkedList::count(Node *list)
{
if (!list) {
return 0;
}
else
{
return 1+count(list->next);
}
}
Token LinkedList::head_delete()
{
Token * retToken;
if (!head)
{
cerr << "Attempting to delete from an empty list" << endl;
exit(1);
}
if (head == tail) // one element
{
retToken = head->data;
delete head;
head = tail = NULL;
return *retToken;
}
retToken = head->data;
Node *deleteMe = head;
head = head->next;
delete deleteMe;
return *retToken;
}
/*
*
*
* This is the end of LinkedList.cpp
*
*
*******************************************************************/
/*****************************************************************************/
/************************************************************
*
*
* This is the beginning of Stack.h
*
*
*/
#ifndef STACK_H
#define STACK_H
class Stack : private LinkedList
{
public:
Stack();
void push(Token element) {head_insert(element);}
Token pop() {return head_delete();}
Token top() {return *(head->data);}
bool empty() {return head == NULL;}
protected:
private:
};
#endif //STACK_H
/*
*
*
* This is the end of stack.h
*
*
***************************************************************/
/****************************************************************************/
/**************************************************************
*
*
* This is the beginning of stack.cpp
*
*
*/
Stack::Stack()
{
//ctor
}
/*
*
*
* This is the end of stack.cpp
*
*
***************************************************************/
/************************************************************************/
/**************************************************************
*
*
* This is the beginning of main.cpp
*
*
*/
int main()
{
Stack s;
// creating 4 tokens, 2 char, 2 doubles
Token token1('+');
Token token2('-');
Token token3(90.21);
Token token4(13.78);
// pushing tokens onto the stack
s.push(token1);
s.push(token2);
s.push(token3);
s.push(token4);
// attempting to pop (deletes head node) and display top (should be 90.21)
s.pop();
std::cout << s.top() << std::endl;
return 0;
}
|