Question

In: Computer Science

(C++ program) ***User Interface Write a program that offers an easy way to add items, remove...

(C++ program)

***User Interface

Write a program that offers an easy way to add items, remove the last item, look at the last item put in the list. You will write all of the code for processing the stack - do not use any predefined objects in C++.  You decide how the interface will look. A menu driven program is always a good choice.

***Rules for program***

  • NEVER use break, exit, return, pass, continue or anything to leave a loop (or iteration), function, or other construct prematurely, unless it is part of the structure as in a case statement.
  • NEVER use global variables. However, you may use global constants if it is appropriate and they are used properly.
  • Should only have only one return statement in a function. (Exception – Multiple return statements may be necessary in a recursive function.)

create a class “Item” with the following private data attributes:

  • Item name (string)
  • Color (string)

For this lab have separate files in a project. Put your class definitions in header files and the implementations of the methods in a .cpp file.

You will have the following public methods:

  • Accessors and mutators for each attribute
  • Constructor that initializes the attributes to nulls (empty string)

Create another class to implement a stack of "Items" using an array-based implementation.

Your stack class should include the following operations:

  1. isEmpty – tell whether there are any entries in the stack
  2. push – add another item
  3. pop – remove the top item
  4. peek – look at the item on top
  5. display - displays the items in the stack indicating where the top is (this one just gives and easy way for me to see what is in the stack as I test your program)

Solutions

Expert Solution

// stack.h

#ifndef STACK_H
#define STACK_H

#include <string>
using namespace std;

class Item
{
private:
string name;
string color;
public:
Item();
void setName(const string& name);
void setColor(const string& color);
string getName() const;
string getColor() const;
};

class Stack
{
private:
static const int MAX = 100; // large so that stack is never full
Item items[MAX];
int top;
public:
Stack();
bool isEmpty() const;
void push(const Item& item);
void pop();
Item peek() const;
void display() const;
};

#endif // STACK_H

//end of stack.h

// stack.cpp

#include "stack.h"
#include <iostream>

// default constructor
Item::Item() : name(""), color("")
{}

// set the name of the item
void Item:: setName(const string& name)
{
this->name = name;
}

// set the color of the item
void Item:: setColor(const string& color)
{
this->color = color;
}

// return the name of the item
string Item:: getName() const
{
return name;
}

// return the color of the item
string Item:: getColor() const
{
return color;
}

// default constructor to create an empty stack of maximum size 100
Stack::Stack(): top(-1)
{}

// returns true if stack is empty else false
bool Stack:: isEmpty() const
{
return top == -1;
}

// insert item at top of stack
void Stack:: push(const Item& item)
{
top++;
items[top] = item;
}

// remove the top element of stack
void Stack:: pop()
{
if(!isEmpty()) // stack is not empty
top--;
}

// return the top element of stack
Item Stack:: peek() const
{
Item item;
// non-empty stack
if(!isEmpty())
item = items[top]; // get the top item
return item;
}

// display all the items of the stack
void Stack:: display() const
{
cout<<"Top: "<<top<<endl; // display top index
// display the items from top to bottom
cout<<"Stack Items(top to bottom): "<<endl;
for(int i=top;i>=0;i--)
cout<<"Name: "<<items[i].getName()<<" Color: "<<items[i].getColor()<<endl;
}

//end of stack.cpp

// driver.cpp : C++ program to test the stack class
#include <iostream>
#include "stack.h"

using namespace std;

int main()
{
Stack stack;
int choice;
string name, color;
Item item;
// loop that continues until the user exits
do
{
// display menu
cout<<"1) Add an item"<<endl;
cout<<"2) Remove the top item"<<endl;
cout<<"3) Is the stack empty"<<endl;
cout<<"4) Get top item"<<endl;
cout<<"5) Display the stack"<<endl;
cout<<"6) Exit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;

// based on menu choice perform the operation
if(choice == 1)
{
cout<<"Name: ";
cin.ignore();
getline(cin, name);
cout<<"Color: ";
getline(cin, color);
item.setName(name);
item.setColor(color);
stack.push(item);
}
else if(choice == 2)
{
stack.pop();
}
else if(choice == 3)
{
if(stack.isEmpty())
cout<<"Stack is empty"<<endl;
else
cout<<"Stack is not empty"<<endl;
}
else if(choice == 4)
{
if(!stack.isEmpty()){
item = stack.peek();
cout<<"Name: "<<item.getName()<<" Color: "<<item.getColor()<<endl;
}else
cout<<"Stack is empty"<<endl;
}
else if(choice == 5)
stack.display();
else if(choice != 6)
cout<<"Invalid choice"<<endl;
cout<<endl;

}while(choice != 6);


return 0;
}

//end of driver.cpp

Output:


Related Solutions

write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
Write a Java Program that can:​ Remove a particular element from an array.​ Add a new...
Write a Java Program that can:​ Remove a particular element from an array.​ Add a new element to an array.​ Change an element with the new one.​ Search for a particular element in the array.​ ​The code must have four separate methods for each task stated above.​ Do not use any pre-defined Java functions.​ You are free to use int or String data-type for the array.​
Write a C++ program that prompts the user for the radius of a circle and then...
Write a C++ program that prompts the user for the radius of a circle and then calls inline function circleArea to calculate the area of that circle. It should do it repeatedly until the user enters -1. Use the constant value 3.14159 for π Sample: Enter the radius of your circle (-1 to end): 1 Area of circle with radius 1 is 3.14159 Enter the radius of your circle (-1 to end): 2 Area of circle with radius 2 is...
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
Tail of a File, C++ Program. write a program that asks the user for the name...
Tail of a File, C++ Program. write a program that asks the user for the name of a text file. The program should display the last 10 lines, or all lines if less than 10. The program should do this using seekg Here is what I have so far. #include<iostream> #include<fstream> #include<string> using namespace std; class File { private:    fstream file;    string name; public:    int countlines();    void printlines(); }; int File::countlines() {    int total =...
Build a C program project about Personal diary which will have daily note add, remove ,...
Build a C program project about Personal diary which will have daily note add, remove , edit , search, and view notes system. Write the appropriate comment in your code. Comment writing is mandatory. describe your logic, purpose of a function, purpose of the variable etc need to be described in the comment.
C++ OOP Provide a user-friendly way to enter expressions to be evaluated. Please add a comment...
C++ OOP Provide a user-friendly way to enter expressions to be evaluated. Please add a comment explaining each step of your program. Make a program to evaluate infix arithmetic expressions containing integer operands and the operators + (addition), - (subtraction), * (multiplication), / (division) and pairs of parentheses, properly nested. Use the following two-stack algorithm (by E. W. Dijkstra): If the next token in the expression is an integer, push the integer onto the value stack. If the next token...
C++ Program: Write a program that prompts the user for two numbers and stores them in...
C++ Program: Write a program that prompts the user for two numbers and stores them in signed integers. The program should then add those two numbers together and store the result in a signed integer and display the result. Your program should then multiply them by each other and store the result in another integer and display the result. Then do the same but with dividing the first number by the second. Display an error message to the screen if...
C++ Question: write a program that prompts the user for the length and width of a...
C++ Question: write a program that prompts the user for the length and width of a rectangle in inches.  The program then uses functions to compute the perimeter and area of the rectangle and to convert those to meters and square meters respectively. Sample output from one instance of the program is shown below: ```html Welcome to the Foot-To-Meter Rectangle Calculator ================================================= Enter the rectangle length in feet: 2 Enter the rectangle width in feet: 3 The rectangle dimensions are: 0.61...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT