Skip to content

Instantly share code, notes, and snippets.

@dbechrd
Created February 2, 2022 04:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbechrd/fb8b39b91c91b3d0790d08367ab0bb7c to your computer and use it in GitHub Desktop.
Save dbechrd/fb8b39b91c91b3d0790d08367ab0bb7c to your computer and use it in GitHub Desktop.
/*
Author: Dan Bechard
Date: November 2, 2010
Project: Stack class
Professor: Bo Kim
Course: CS217-A
*/
#include <iostream>
#include "MyStack.h"
using namespace std;
//Function declarations
char Menu();
void CollectGarbage();
void Dec2Bin(int dec);
//Garbage Collector
char garbcol;
int main(int argc, char *argv[]) {
//My stack
MyStack stack;
//Display Menu (first time)
char choice = Menu();
garbcol = getchar();
while(choice != 'Q') {
//Dec2Bin
if(choice == 'D') {
int element;
//Validate element
CollectGarbage();
while(true) {
cout << "Decimal value to convert: ";
cin >> element;
if(cin.fail()) {
cin.clear();
cout << "Invalid input.\n\n";
garbcol = getchar();
CollectGarbage();
} else {
garbcol = getchar();
CollectGarbage();
break;
}
}
//Convert and display
Dec2Bin(element);
//Push
} else if(choice == 'P') {
int element;
//Validate element
CollectGarbage();
while(true) {
cout << "Value to push: ";
cin >> element;
if(cin.fail()) {
cin.clear();
cout << "Invalid input.\n\n";
garbcol = getchar();
CollectGarbage();
} else {
garbcol = getchar();
CollectGarbage();
break;
}
}
//Push element onto stack
if(stack.Push(element)) {
cout << "Pushed " << element << " onto stack.";
} else {
cout << "Stack is full.";
}
//Pop
} else if(choice == 'O') {
if(stack.Pop()) {
cout << "Popped top element off stack.";
} else {
cout << "Nothing to pop. Stack is empty.";
}
//Top
} else if(choice == 'T') {
if(!stack.Empty()) {
cout << "Top element: " << stack.Top();
} else {
cout << "No top element. Stack is empty.";
}
//Size
} else if(choice == 'S') {
cout << "Size of stack: " << stack.Size();
//Empty
} else if(choice == 'E') {
if(stack.Empty()) {
cout << "Stack is empty.";
} else {
cout << "Stack is not empty.";
}
//Full
} else if(choice == 'F') {
if(stack.Full()) {
cout << "Stack is full.";
} else {
cout << "Stack is not full.";
}
//Invalid command
} else {
cout << "Command '" << choice << "' not found.";
}
CollectGarbage();
//Display menu
choice = Menu();
garbcol = getchar();
}
//Return on success
return 0;
}
//Function: Prints menu and gets command
//Pre: None
//Post: Return user input as uppercase char
char Menu() {
cout << "\n\n";
cout << "===================\n";
cout << " Stack Menu\n";
cout << "===================\n"; //Number of arguments
cout << " (D)ec2Bin\n"; //1
cout << " (P)ush\n"; //1
cout << " P(o)p\n"; //0
cout << " (T)op\n"; //0
cout << " (S)ize\n"; //0
cout << " (E)mpty\n"; //0
cout << " (F)ull\n"; //0
cout << " (Q)uit\n"; //0
cout << "===================\n";
cout << "> ";
return (char)toupper(getchar());
}
//Function: stdin garbage collector
//Pre: stdin holds unwanted data
//Post: Clears everything in the stdin stream up to the first newline
void CollectGarbage() {
while(garbcol != '\n') {
garbcol = getchar();
}
}
//Function: Decimal to binary convertre
//Pre: Pass valid integer
//Post: Prints base-2 equivalent of given base-10 number
void Dec2Bin(int dec) {
MyStack bin;
int remainder = 0;
cout << "Binary equivalent: ";
//Push binary onto stack
while(dec > 1) {
bin.Push(dec % 2);
dec /= 2;
}
bin.Push(1);
//Print
while(!bin.Empty()) {
cout << bin.Top();
bin.Pop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment