110 lines
No EOL
4 KiB
C++
110 lines
No EOL
4 KiB
C++
//Corey Williams
|
|
//COP3530 01Z
|
|
//Project 7
|
|
|
|
/*
|
|
Write a C / C++ or Java application that creates a Stack data structure.
|
|
The Stack must be built using a Linked List(i.e., refer to Linked List Implementation, Figure 4.5 pp. 137 - 138).
|
|
The application also creates a "DisplayStackElement" routine.
|
|
The application must be menu driven(with an option to terminate the application) and provide the following features.
|
|
- Allow insertion of a "Circle" object / structure in the Stack data structures.The Circle contains a "radius" data member.
|
|
The Circle also uses functions / methods "setRadius", "getRadius" and calculateArea(returns a double data type).
|
|
- Allow display of an element from Stack data structure by Invoking a method / function "DisplayStackElement" (uses the "Pop" method)
|
|
- Allow for deletion of the Stack
|
|
*/
|
|
|
|
using namespace std;
|
|
|
|
#include <iostream>
|
|
#include "genListStack.h"
|
|
#include "circle3.h"
|
|
#include <string>
|
|
|
|
void displayStackElements() {
|
|
|
|
}
|
|
|
|
void enterStackElements(LLStack<Circle>& s) { //stack is passed in by reference in order to remain persistent between main and function calls
|
|
system("cls");
|
|
string r = "0"; //Takes string so that user input can be processed regardless of validity
|
|
|
|
//Greeting/instruction text
|
|
cout << "1. Enter Stack Elements" << endl;
|
|
cout << "=======================" << endl;
|
|
cout << "Input a positive number to add a circle with that radius to the stack"
|
|
<< endl << "or enter q to exit back to the main menu" << endl << endl;
|
|
|
|
//Loops until exit condition is input by user
|
|
while (r != "q") {
|
|
cout << "Input: ";
|
|
cin >> r;
|
|
|
|
if (r == "q" or r == "Q") { return; }
|
|
else {
|
|
try { //try block encapsulates attempt to cast string to double
|
|
Circle tmp(stod(r)); //instantiates circle with user-input radius
|
|
s.push(tmp); //adds circle to stack
|
|
cout << "Circle of radius " << r << " pushed to stack." << endl;
|
|
}
|
|
catch (...) { //catches when user input is not able to be used as a numeric input
|
|
cout << "Not a valid entry, please try again. ";
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void displayStackElement(LLStack<Circle>& s) { //pops the top element and prints to screen
|
|
cout << endl << "Elements:" << endl;
|
|
if (s.isEmpty()) { cout << endl << "There are no elements to display. "; system("pause"); }
|
|
string control = "y";
|
|
while (!s.isEmpty() and control!="n" and control !="N") {
|
|
Circle tmp = s.pop();
|
|
cout << endl << "Radius: " << tmp.getRadius();
|
|
cout << endl << "Area: " << tmp.calculateArea() << endl << endl;
|
|
|
|
cout << "Display another element? y/n: ";
|
|
cin >> control;
|
|
|
|
if (s.isEmpty()) { cout << endl << "There are no more elements. "; system("pause"); }
|
|
}
|
|
}
|
|
|
|
void deleteCurrentStack(LLStack<Circle>& s) { //receives reference to main function's queue and deletes all elements
|
|
s.clear();
|
|
cout << endl << endl;
|
|
cout << "The stack has been cleared. ";
|
|
system("pause");
|
|
}
|
|
|
|
int main()
|
|
{
|
|
LLStack<Circle> cirStack;
|
|
string menuChoice = "0";
|
|
|
|
//Main Menu - reads string from user and parses for menu selection
|
|
//Loops until exit condition is input by user
|
|
while (menuChoice != "4") {
|
|
menuChoice = "0";
|
|
system("cls");
|
|
|
|
cout << "Main Menu" << endl
|
|
<< "=========" << endl
|
|
<< "1. Enter Stack Elements" << endl
|
|
<< "2. Display Top Element" << endl
|
|
<< "3. Delete Current Stack" << endl
|
|
<< "4. Exit Program" << endl
|
|
<< endl << "Choose an option (1-4): ";
|
|
cin >> menuChoice;
|
|
|
|
if (menuChoice == "1") { enterStackElements(cirStack); }
|
|
else if (menuChoice == "2") { displayStackElement(cirStack); }
|
|
else if (menuChoice == "3") { deleteCurrentStack(cirStack); }
|
|
else if (menuChoice == "4") { return 0; }
|
|
else {
|
|
cout << endl << "Not a valid entry, please try again. ";
|
|
system("pause");
|
|
menuChoice = "0";
|
|
}
|
|
}
|
|
} |