119 lines
3.2 KiB
C++
119 lines
3.2 KiB
C++
/*
|
|
Written by: Iurii Tatishchev
|
|
=============================
|
|
CIS 22C
|
|
Project: Stack ADT
|
|
*/
|
|
|
|
#include <fstream>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <cstdlib>
|
|
|
|
#include "StackADT.h"
|
|
|
|
using namespace std;
|
|
|
|
void printInfo();
|
|
void processNumbers(const string&, Stack<int> &);
|
|
void printStack(Stack<int> &);
|
|
|
|
int main() {
|
|
printInfo();
|
|
|
|
cout << "Enter input file name: ";
|
|
string filename;
|
|
getline(cin, filename); // assume valid
|
|
cout << endl;
|
|
|
|
// Create a stack
|
|
Stack<int> s;
|
|
|
|
// Process the numbers in the file
|
|
processNumbers(filename, s);
|
|
|
|
// Print remaining stack elements
|
|
printStack(s);
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
/*
|
|
This function displays the project's title
|
|
*/
|
|
void printInfo() {
|
|
cout << " ~*~ Project: Stack ADT ~*~ " << endl;
|
|
}
|
|
|
|
void processNumbers(const string& filename, Stack<int> &s) {
|
|
ifstream inFile(filename);
|
|
// Check if the file was opened successfully
|
|
if (!inFile.is_open()) {
|
|
cout << "There was an error opening \"" << filename << "\". Exiting.\n";
|
|
exit(1);
|
|
}
|
|
cout << "\nInput File: " << filename << "\n";
|
|
|
|
int number;
|
|
Stack<int> maxStack;
|
|
inFile >> number;
|
|
while(inFile.good()) {
|
|
switch (number) {
|
|
// Each time we read 0, display the number of elements in the stack.
|
|
case 0:
|
|
cout << "Count: " << s.getLength() << "\n";
|
|
break;
|
|
// Each time we read 1, display the top element of the stack and the maximum element in the stack.
|
|
case 1:
|
|
// If there are no elements in the stack display "Top: Empty".
|
|
if (s.isEmpty()) {
|
|
cout << "Top: Empty\n";
|
|
break;
|
|
}
|
|
cout << "Top: " << s.peek() << "\n";
|
|
cout << "Max: " << maxStack.peek() << "\n";
|
|
break;
|
|
default:
|
|
// Each time we read a number greater than 1, we push it onto the stack.
|
|
if (number > 1) {
|
|
s.push(number);
|
|
maxStack.push(max(maxStack.isEmpty()? 0 : maxStack.peek(), number));
|
|
}
|
|
// Each time we read a negative number,
|
|
// we pop and print the number removed from the stack and the largest value in the stack.
|
|
// If there are no numbers in the stack, display "Pop: Empty".
|
|
// If there's only one number in the stack, after removing it, print it.
|
|
else {
|
|
if (s.isEmpty()) {
|
|
cout << "Pop: Empty\n";
|
|
} else {
|
|
cout << "Pop: " << s.pop() << "\n";
|
|
maxStack.pop();
|
|
if (!maxStack.isEmpty()) cout << "Max: " << maxStack.peek() << "\n";
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
inFile >> number;
|
|
}
|
|
// Don't forget to close the file
|
|
inFile.close();
|
|
}
|
|
|
|
/*
|
|
This function pops and prints all elements in the stack.
|
|
*/
|
|
void printStack(Stack<int> &s) {
|
|
cout << "Stack: ";
|
|
if (s.isEmpty()) {
|
|
cout << "Empty\n";
|
|
return;
|
|
}
|
|
|
|
while (s.getLength() != 1) {
|
|
cout << s.pop() << ", ";
|
|
}
|
|
cout << s.pop() << ".\n";
|
|
}
|