Docsity

C++ Programs for Stack, Queue, and Bracket Matching, Exercises of Programming Methodologies

Toggle sidebar
bg1
1 // Simple Stack Example
2 #include <iostream>
3 #include "console.h"
4 #include "stack.h"
5
6 using namespace std;
7 const char SPACE = ' ';
8
9 int main() {
10 string sentence = "hope is what defines humanity";
11 string word;
12 Stack<string> wordStack;
13
14 cout << "Original sentence: " << sentence << endl;
15
16 for (char c : sentence) {
17 if (c == SPACE and word != "") {
18 wordStack.push(word);
19 word = ""; // reset
20 }
21 else {
22 word += c;
23 }
24 }
25 if (word != "") {
26 wordStack.push(word);
27 }
28
29 cout << " New sentence: ";
30 while (!wordStack.isEmpty()) {
31 string word = wordStack.pop();
32 cout << word << SPACE;
33 }
34 cout << endl;
35 return 0;
36 }
37
38 // Queue mystery
39 #include <iostream>
40 #include "console.h"
41 #include "simpio.h"
42 #include "queue.h"
43
44 using namespace std;
45
46 int main() {
47 Queue<int> queue;
48 // produce: {1, 2, 3, 4, 5, 6}
49 for (int i = 1; i <= 6; i++) {
50 queue.enqueue(i);
51 }
52 // original mystery:
53 for (int i = 0; i < queue.size(); i++) {
54 cout << queue.dequeue() << " ";
55 }
56
57 // better idiom:
58 // while (!queue.isEmpty()) {
59 // cout << queue.dequeue() << " ";
60 // }
61
62 // okay idiom if you want to go through original queue elements once:
63 // int origQSize = queue.size();
64 // for (int i=0; i < origQSize; i++) {
65 // int value = queue.dequeue();
66 // cout << value << " ";
67 // // re-enqueue even values
68 // if (value % 2 == 0) {
69 // queue.enqueue(value);
70 // }
71 // }
72
73 cout << queue.toString() << " size " << queue.size() << endl;
74 return 0;
75 }
pf3
pf4

Partial preview of the text

Download C++ Programs for Stack, Queue, and Bracket Matching and more Exercises Programming Methodologies in PDF only on Docsity!

1 // Simple Stack Example 2 #include 3 #include "console.h" 4 #include "stack.h" 5 6 using namespace std; 7 const char SPACE = ' '; 8 9 int main() { 10 string sentence = "hope is what defines humanity"; 11 string word; 12 Stack wordStack; 13 14 cout << "Original sentence: " << sentence << endl; 15 16 for (char c : sentence) { 17 if (c == SPACE and word != "") { 18 wordStack.push(word); 19 word = ""; // reset 20 } 21 else { 22 word += c; 23 } 24 } 25 if (word != "") { 26 wordStack.push(word); 27 } 28 29 cout << " New sentence: "; 30 while (!wordStack.isEmpty()) { 31 string word = wordStack.pop(); 32 cout << word << SPACE; 33 } 34 cout << endl; 35 return 0 ; 36 } 37 38 // Queue mystery 39 #include 40 #include "console.h" 41 #include "simpio.h" 42 #include "queue.h" 43 44 using namespace std; 45 46 int main() { 47 Queue queue; 48 // produce: {1, 2, 3, 4, 5, 6} 49 for (int i = 1 ; i <= 6 ; i++) { 50 queue.enqueue(i); 51 } 52 // original mystery: 53 for (int i = 0 ; i < queue.size(); i++) { 54 cout << queue.dequeue() << " "; 55 } 56 57 // better idiom: 58 // while (!queue.isEmpty()) { 59 // cout << queue.dequeue() << " "; 60 // } 61 62 // okay idiom if you want to go through original queue elements once: 63 // int origQSize = queue.size(); 64 // for (int i=0; i < origQSize; i++) { 65 // int value = queue.dequeue(); 66 // cout << value << " "; 67 // // re-enqueue even values 68 // if (value % 2 == 0) { 69 // queue.enqueue(value); 70 // } 71 // } 72 73 cout << queue.toString() << " size " << queue.size() << endl; 74 return 0 ;

78 // Postfix arithmetic, implementing +, -, , / 79 80 #include 81 #include "console.h" 82 #include "simpio.h" 83 #include "stack.h" 84 85 using namespace std; 86 87 const string OPERATORS = "+-x/"; 88 const string SEPARATOR = " "; 89 90 // function prototypes 91 double parsePostfix(string expression); 92 string getNextToken(string &expression); 93 void performCalculation(Stack &s, char op); 94 95 int main() { 96 string expression; 97 double answer; 98 do { 99 expression = getLine("Please enter a postfix expression (blank to quit): "); 100 answer = parsePostfix(expression); 101 cout << "The answer is: " << answer << endl << endl; 102 } while (expression != ""); 103 return 0 ; 104 } 105 106 double parsePostfix(string expression) { 107 Stack s; 108 string nextToken; 109 110 while (expression != "") { 111 // gets the next token and removes it from expression 112 nextToken = getNextToken(expression); 113 if (OPERATORS.find(nextToken) == string::npos) { 114 // we have a number 115 double operand = stringToDouble(nextToken); 116 s.push(operand); 117 } 118 else { 119 // we have an operator 120 char op = stringToChar(nextToken); 121 performCalculation(s,op); 122 } 123 } 124 return s.pop(); 125 } 126 127 void performCalculation(Stack &s, char op) { 128 double result; 129 double operand2 = s.pop(); // LIFO! 130 double operand1 = s.pop(); 131 switch(op) { 132 case '+': result = operand1 + operand2; 133 break; 134 case '-': result = operand1 - operand2; 135 break; 136 // allow "" or "x" for times 137 case '': 138 case 'x': result = operand1 * operand2; 139 break; 140 case '/': result = operand1 / operand2; 141 break; 142 } 143 s.push(result); 144 } 145 146 string getNextToken(string &expression) { 147 // pull out the substring up to the first space 148 // and return the token, removing it from the expression 149 string token; 150 int sepLoc = expression.find(SEPARATOR);

226 // if we made it through the characters and the stack is not empty, 227 // then we don't have a match :( 228 if (!s.isEmpty()) { 229 // report the top of the stack 230 unmatched = s.pop(); 231 return false; 232 } 233 // We made it! 234 return true; 235 } 236 237 bool closingBracketOkay(char c, Stack &s) { 238 char leftSymbol; 239 240 // look for closing brackets 241 if (closingBrackets.find(c) != string::npos) { 242 if (s.isEmpty()) { // uh-oh! 243 return false; 244 } 245 else { 246 leftSymbol = s.pop(); 247 // check to see if the symbols are the same type 248 if (openingBrackets.find(leftSymbol) != closingBrackets.find(c)) { 249 // nope! :( 250 return false; 251 } 252 } 253 } 254 return true; 255 } 256 257 void printInputWithLines(string input) { 258 // count the number of lines 259 int numLines = 1 ; 260 for (char c : input) { 261 if (c == '\n') { 262 numLines++; 263 } 264 } 265 // the log_10(numLines) is the maximum number of digits 266 int maxDigits = log(numLines) / log( 10 ); 267 268 int lineNum = 0 ; // the first line 269 int numDigits; 270 for (char c : input) { 271 if (lineNum == 0 || c == '\n') { 272 if (lineNum != 0 ) { 273 cout << endl; 274 } 275 lineNum++; 276 numDigits = log(lineNum) / log( 10 ); 277 printSpaces(maxDigits - numDigits); 278 cout << lineNum << " "; 279 if (lineNum == 1 ) { 280 cout << c; // first character is special 281 } 282 } 283 else { 284 cout << c; 285 } 286 } 287 cout << endl; 288 } 289 290 void printSpaces(int n) { 291 for (int i= 0 ; i < n; i++) { 292 cout << " "; 293 }