/*
* Ashish Balu
* East High School,
West Chester PA
* Stacks and Queues
* C++ Programming
* Intermediate 3
Contest 4
*/
#include <iostream>
#include <string.h>
using namespace std;
string pop(string,int,char);
string psh(string,string);
string dup(string,int);
string swp(string,int);
// No need of a fxn for swh(switch)
string crc(string,int,char);
string pin(string,string);
void prt(string,int,char);
int main()
{
string
list;
bool
printed = false; //for the input loop
int r = 0;
//for the entire program to run five times
string
command;
string
letter; //letter to push or pin
int
num; //number to use with functions
char
sorq; //stack or queue?
list =
"ABCDE";
for (r =
0;r < 5;r++){
printed =
false;
list =
"ABCDE";
cout
<< "Stack(S) or queue(Q)?" << endl;
cin
>> sorq;
while(!printed){
cout
<< "What command would you like to execute (capital letters
only)?" << endl;
cin
>> command;
if(command
== "POP"){
cout
<< "Enter the number." << endl;
cin
>> num;
list
= pop(list,num,sorq);
}
else
if(command == "PSH"){
cout
<<"Enter the letter to be pushed:" << endl;
cin
>> letter;
list
= psh(list,letter);
}
else
if(command == "DUP"){
cout
<< "Enter the number." << endl;
cin
>> num;
list
= dup(list,num);
}
else
if(command == "SWP"){
cout
<< "Enter the number." << endl;
cin
>> num;
list
= swp(list,num);
}
else
if(command == "SWH"){
if
(sorq == 'Q')
sorq
= 'S';
else
sorq
= 'Q';
}
else
if(command == "CRC"){
cout
<< "Enter the number." << endl;
cin
>> num;
list
= crc(list,num,sorq);
}
else
if(command == "PIN"){
cout
<<"Enter the letter to be pinned:" << endl;
cin
>> letter;
list
= pin(list,letter);
}
else{
cout
<< "Enter the number." << endl;
cin
>> num;
printed
= true;
}
cout
<< list << endl;
}
prt(list,num,sorq);
}
return 0;
}
string pop(string a,int b,char c)
{
if (c ==
'Q'){
a.erase(0,b);
}
else{
a.erase(a.length()
- b,b);
}
return a;
}
string psh(string a,string b)
{
a = a + b;
return a;
}
string dup(string a,int b)
{
string
temp;
temp = a;
temp =
temp.erase(b,a.length() - b);
a = a +
temp;
return a;
}
string swp(string a,int b)
{
string
temp1,temp2;
temp1 =
a.substr(0,b);
temp2 =
a.substr(a.length() - b,b);
a =
a.erase(0,b);
a =
a.erase(a.length() - b,b);
a = temp2 +
a + temp1;
return a;
}
string crc(string a,int b,char c)
{
string temp
= a;
if (c ==
'Q'){
temp.erase(b,
a.length() - b);
a
= a.erase(0,b);
a
= a + temp;
}
else{
temp.erase(0,
a.length() - b - 1);
a
= a.erase(a.length() - b,b);
a
= temp + a;
}
return a;
}
string pin(string a,string b)
{
a = b + a;
return a;
}
void prt(string a,int b,char c)
{
string
finaloutput;
if (c ==
'Q'){
finaloutput
= a.substr(0,b);
}
else{
finaloutput
= a.substr((a.length()-b),b);
}
cout
<< endl << endl << "Final Output:" <<
finaloutput << endl;
}