Question

In: Computer Science

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

C++ Code

***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.)

****PLEASE READ**** Additional information for program below.

Write a program to keep up with a collection of "Item" objects. ( Please put comments on all functions other than main to describe purpose)

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

Program to implement stack functions using array implementation:

// In this array has a pair two string value called item_name and color,

//peek - method to display top element;

//isempty - to check stack is empty;

//push - to add element in the stack class;

//pop - to remove last inserted data;

// display - to show all the data in the stack class;

#include <iostream>

using namespace std;

class Item{ // Item class is created here

public:

    pair<string,string> stack[100]; // Here is the pair of item_name and color and size of array is 100

    int n=100; // n here is used to know the size of array

    int top=-1; // top here is used to check the top element index and also used to check empty stack and full stack

    void push(string item_name,string color) { // push method takes two argument item_name and color

    if(top>=n-1)  // if loop is to check the stack is full aur not

     cout<<"Stack is full"<<endl;

    else {

      stack[top]=make_pair(item_name,color); // if not full we push or add the pair of item_name and color

      top++; // and increment the value of top to add another data in next index

   }

}

void isEmpty(){  // isempty is used to check stack is empty or not

     if(top<=-1){ // if the top value is -1 or less than -1 then the stack is empty

        cout<<"Stack is empty"<<endl;

    }

}

void peek(){  // peek is used to display top element of stack

    if(top<=-1){ // check whether the stack is empty or not

        cout<<"Stack is empty"<<endl;

    }

    else{

        cout<<stack[top].first<<" "<<stack[top].second<<endl; // if not empty then display the current position of top in stack array

    }

}

void pop() { // here pop method is used to delete element which is last inserted

   if(top<=-1)

   cout<<"Stack is empty"<<endl;

   else {

      top--; // in this we decrement the value of top so that access to last inserted data is not possible

   }

}

void display() { // display is used to show all the elements in the stack array

   if(top>=0) { // show only when stack is not empty

      for(int i=top; i>=0; i--) // for loop to iterate over the stack array

      cout<<stack[i].first<<" "<<stack[i].second<<endl;

   } else

   cout<<"Stack is empty";

}

};

int main() {

    Item t; // create a object of Item class

    t.push("Apple","red"); // Push element in the Item Stack class

    t.push("Hair","black");

    t.push("sky","blue");

    t.push("heart","pink");

    t.push("chocolate","brown");

    t.pop(); // pop last inserted element in the Item Stack Class

    t.display();  

}


Related Solutions

(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...
Please code in c# (C-Sharp) Write a program that will ask the user for their name....
Please code in c# (C-Sharp) Write a program that will ask the user for their name. If the user does not input anything, display a warning before continuing. The program will then ask the user whether they want to have an addition, subtraction, multiplication, or division problem. Once the user indicates their choice, the program will display 2 randomly generated numbers from 1 to 9 in a math problem matching the user’s choice. Example: user selects addition, the equation presented...
*Please write code in C++* Write a program to verify the validity of the user entered...
*Please write code in C++* Write a program to verify the validity of the user entered email address.   if email is valid : output the stating that given email is valid. ex: "The email sarahwinchester@gmall.com is valid" else : output the statement that the email is invalid and list all the violations ex:  "The email sarahwinchester.com is invalid" * @ symbol * Missing Domain name The program should keep validating emails until user enter 'q' Upload your source code. ex: main.cpp
Simple code please thats easy to follow. C++ Write a program that can be used to...
Simple code please thats easy to follow. C++ Write a program that can be used to compare Insertion Sort, Merge Sort and Quick Sort. Program must: Read an array size from the user, dynamically an array of that size, and fill the array with random numbers Sort the array with the Insertion Sort, MergeSort and QuickSort algorithms studied in class, doing a time-stamp on each sort. Use your program to measure and record the time needed to sort random arrays...
In C Programming, Thanks Many user-created passwords are simple and easy to guess. Write a program...
In C Programming, Thanks Many user-created passwords are simple and easy to guess. Write a program that takes a simple password and makes it stronger by replacing characters using the key below, and by appending "q*s" to the end of the input string. You may assume that the string does not contain spaces and will always contain less than 50 characters. i becomes ! a becomes @ m becomes M B becomes 8 o becomes . Ex: If the input...
Write a C program/code that prompts the user for a minimum min and a maximum max....
Write a C program/code that prompts the user for a minimum min and a maximum max. Then use these values to print the squares of all even numbers between the min and max variables. For example if the user enters 6 as the minimum and 200 as the maximum, the program/code should print the following. Enter limit on minimum square: 6 Enter limit on maximum square: 200 36 64 100 144 196
Please code C# 8. Write a program that prompts the user to enter an integer. The...
Please code C# 8. Write a program that prompts the user to enter an integer. The program then determines and displays the following: Whether the integer is divisible by 5 and 6 Whether the integer is divisible by 5 or 6
This code needs to be in C++, please. Step 1: Add code to prompt the user...
This code needs to be in C++, please. Step 1: Add code to prompt the user to enter the name of the room that they are entering information for. Validate the input of the name of the room so that an error is shown if the user does not enter a name for the room. The user must be given an unlimited amount of attempts to enter a name for the room. Step 2: Add Input Validation to the code...
Write the program in Java (with a graphical user interface) and have it calculate and display...
Write the program in Java (with a graphical user interface) and have it calculate and display the mortgage payment amount from user input of the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage. Allow the user to loop back and enter new data or quit. You need to include Calculate, Reset, and Exit buttons on your GUI. Please insert comments in the program to document the program. Allow the user to enter...
All Code should be written in C: 1. Write a C program which prompts the user...
All Code should be written in C: 1. Write a C program which prompts the user to enter two integer values. Your program should then print out all numbers between 1 and 1000 that are divisible by both of those numbers. 2. Modify your program from question 1 such that the first 1000 numbers that are divisible by both numbers are printed out, instead of numbers up to 1000. 3. Using dynamic memory, allocate memory for an array of 100...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT