Question

Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo t...

Use C++!

This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA.

Redo this program so that it uses the STL list and STL queue! Thank you!

HighestGPAData.txt*

3.4 Randy
3.2 Kathy
2.5 Colt
3.4 Tom
3.8 Ron
3.8 Mickey
3.6 Peter
3.5 Donald
3.8 Cindy
3.7 Dome
3.9 Andy
3.8 Fox
3.9 Minnie
2.7 Gilda
3.9 Vinay
3.4 Danny

highestGrade.cpp*

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

#include "myStack.h"

using namespace std;

int main()
{
   //Step 1
   double GPA;
   double highestGPA;
   string name;

   stackType<string> stack(100);

   ifstream infile;

   infile.open("HighestGPAData.txt"); //Step 2

   if (!infile) //Step 3
   {
       cout << "The input file does not "
           << "exist. Program terminates!"
           << endl;
       return 1;
   }

   cout << fixed << showpoint; //Step 4
   cout << setprecision(2); //Step 4

   infile >> GPA >> name; //Step 5

   highestGPA = GPA; //Step 6

   while (infile) //Step 7
   {
       if (GPA > highestGPA) //Step 7.1
       {
           stack.initializeStack(); //Step 7.1.1

           if (!stack.isFullStack()) //Step 7.1.2
               stack.push(name);

           highestGPA = GPA; //Step 7.1.3
       }
       else if (GPA == highestGPA) //Step 7.2
           if (!stack.isFullStack())
               stack.push(name);
           else
           {
               cout << "Stack overflows. "
                   << "Program terminates!"
                   << endl;
               return 1; //exit program
           }
       infile >> GPA >> name; //Step 7.3
   }

   cout << "Highest GPA = " << highestGPA
       << endl; //Step 8
   cout << "The students holding the "
       << "highest GPA are:" << endl;

   while (!stack.isEmptyStack()) //Step 9
   {
       cout << stack.top() << endl;
       stack.pop();
   }

   cout << endl;

   return 0;
}

myStack.h*

#pragma once
//Header file: myStack.h

#ifndef H_StackType
#define H_StackType

#include <iostream>
#include <cassert>

#include "stackADT.h"

using namespace std;

template <class Type>
class stackType : public stackADT<Type>
{
public:
   const stackType<Type>& operator=(const stackType<Type>&);
   //Overload the assignment operator.

   void initializeStack();
   //Function to initialize the stack to an empty state.
   //Postcondition: stackTop = 0

   bool isEmptyStack() const;
   //Function to determine whether the stack is empty.
   //Postcondition: Returns true if the stack is empty,
   // otherwise returns false.

   bool isFullStack() const;
   //Function to determine whether the stack is full.
   //Postcondition: Returns true if the stack is full,
   // otherwise returns false.

   void push(const Type& newItem);
   //Function to add newItem to the stack.
   //Precondition: The stack exists and is not full.
   //Postcondition: The stack is changed and newItem
   // is added to the top of the stack.

   Type top() const;
   //Function to return the top element of the stack.
   //Precondition: The stack exists and is not empty.
   //Postcondition: If the stack is empty, the program
   // terminates; otherwise, the top element
   // of the stack is returned.

   void pop();
   //Function to remove the top element of the stack.
   //Precondition: The stack exists and is not empty.
   //Postcondition: The stack is changed and the top
   // element is removed from the stack.

   stackType(int stackSize = 100);
   //constructor
   //Create an array of the size stackSize to hold
   //the stack elements. The default stack size is 100.
   //Postcondition: The variable list contains the base
   // address of the array, stackTop = 0, and
   // maxStackSize = stackSize.

   stackType(const stackType<Type>& otherStack);
   //copy constructor

   ~stackType();
   //destructor
   //Remove all the elements from the stack.
   //Postcondition: The array (list) holding the stack
   // elements is deleted.

private:
   int maxStackSize; //variable to store the maximum stack size
   int stackTop; //variable to point to the top of the stack
   Type *list; //pointer to the array that holds the
                   //stack elements

   void copyStack(const stackType<Type>& otherStack);
   //Function to make a copy of otherStack.
   //Postcondition: A copy of otherStack is created and
   // assigned to this stack.
};


template <class Type>
void stackType<Type>::initializeStack()
{
   stackTop = 0;
}//end initializeStack

template <class Type>
bool stackType<Type>::isEmptyStack() const
{
   return(stackTop == 0);
}//end isEmptyStack

template <class Type>
bool stackType<Type>::isFullStack() const
{
   return(stackTop == maxStackSize);
} //end isFullStack

template <class Type>
void stackType<Type>::push(const Type& newItem)
{
   if (!isFullStack())
   {
       list[stackTop] = newItem; //add newItem to the
                                   //top of the stack
       stackTop++; //increment stackTop
   }
   else
       cout << "Cannot add to a full stack." << endl;
}//end push

template <class Type>
Type stackType<Type>::top() const
{
   assert(stackTop != 0); //if stack is empty,
                                   //terminate the program
   return list[stackTop - 1]; //return the element of the
                                   //stack indicated by
                                   //stackTop - 1
}//end top

template <class Type>
void stackType<Type>::pop()
{
   if (!isEmptyStack())
       stackTop--; //decrement stackTop
   else
       cout << "Cannot remove from an empty stack." << endl;
}//end pop

template <class Type>
stackType<Type>::stackType(int stackSize)
{
   if (stackSize <= 0)
   {
       cout << "Size of the array to hold the stack must "
           << "be positive." << endl;
       cout << "Creating an array of size 100." << endl;

       maxStackSize = 100;
   }
   else
       maxStackSize = stackSize; //set the stack size to
                                   //the value specified by
                                   //the parameter stackSize

   stackTop = 0; //set stackTop to 0
   list = new Type[maxStackSize]; //create the array to
                                   //hold the stack elements
}//end constructor

template <class Type>
stackType<Type>::~stackType() //destructor
{
   delete[] list; //deallocate the memory occupied
                   //by the array
}//end destructor

template <class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack)
{
   delete[] list;
   maxStackSize = otherStack.maxStackSize;
   stackTop = otherStack.stackTop;

   list = new Type[maxStackSize];

   //copy otherStack into this stack
   for (int j = 0; j < stackTop; j++)
       list[j] = otherStack.list[j];
} //end copyStack


template <class Type>
stackType<Type>::stackType(const stackType<Type>& otherStack)
{
   list = nullptr;

   copyStack(otherStack);
}//end copy constructor

template <class Type>
const stackType<Type>& stackType<Type>::operator=
(const stackType<Type>& otherStack)
{
   if (this != &otherStack) //avoid self-copy
       copyStack(otherStack);

   return *this;
} //end operator=   

#endif

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <list>        //include the list STL
using namespace std;

int main()
{
   //Step 1
double GPA;
double highestGPA;
string name;
list<string> gradelist;   //create a list of strings to store the names of students with highest GPA

   ifstream infile;
   infile.open("HighestGPAData.txt"); //Step 2
   if (!infile) //Step 3
{
cout << "The input file does not " << "exist. Program terminates!" << endl;
return 1;
}

   cout << fixed << showpoint; //Step 4
   cout << setprecision(2); //Step 4
   infile >> GPA >> name; //Step 5 read the first GPA and name from the input file
   highestGPA = GPA; //Step 6 assign the GPA as highestGPA

   while (infile) //Step 7 when file is not empty
{
if (GPA > highestGPA) //Step 7.1
{
        gradelist.clear();           //clear the previous list
        gradelist.push_front(name);   //push the name at the front of the list
        highestGPA = GPA;           //assign the GPA as highestGPA
   }
   else if (GPA == highestGPA) //Step 7.2
   {
          gradelist.push_front(name);   //push the name at the front of the list
   }
       infile >> GPA >> name; //Step 7.3 read the next GPA and name from the input file
   }

       cout << "Highest GPA = " << highestGPA << endl; //Step 8 display the highest GPA
       cout << "The students holding the " << "highest GPA are:" << endl;
       while (!gradelist.empty())
       {
   cout << gradelist.front() << endl;   //display name of students with highest GPA
   gradelist.pop_front();
   }
   cout << endl;
   return 0;
}

highestgrade.cpp 1 #include <iostream» #include <10man!p> 3 #include <fstream> <string> #include //include the list STL 5 #in

highestgrade.cpp 23 cout << fixed << showpoint; //Step 4 cout << setprecision(2); //step 4 infile > GPA > name; //Step 5 read

10 infile >GPA >name; //Step 7.3 read the next GPA and name from the input file cout<Highest GPA -<< highestGPA << endl; //

Output

D:cppprog Basic c programs Vhighestgrade.exe Highest GPA 3.90 The students holding the highest GPA are inay Minnie Andy Proce

Add a comment
Know the answer?
Add Answer to:
Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo t...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (C++) Two stacks of the same type are the same if they have the same number...

    (C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...

  • C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise...

    C++ Please ensure code is fully working, will rate Question to be answered:    Repeat Exercise 1 for the class linkedStackType question one:        Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same, false otherwise. Also, write the definition...

  • Given an array-based stack of integers, sort it largest to smallest using recursion. Do NOT use...

    Given an array-based stack of integers, sort it largest to smallest using recursion. Do NOT use any loop constructs such as while, for and so on. Only use the following stack ADT functions in the recursion: IsEmpty Push Pop Top (note: doesn’t remove anything from the stack). Your program should read 10 integers into a stack from a file named input.txt (outputting them to the screen first) then implement the recursions. Use stacks only, no queues. The program should then...

  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

  • My Output s1 (size 0): s1 is empty Testing push() s1 (size 1): 17 s1 is...

    My Output s1 (size 0): s1 is empty Testing push() s1 (size 1): 17 s1 is not empty s1 (size 4): 4 6 2 17 s1 is not empty Testing copy constructor s1 (size 4): 4 6 2 17 s2 (size 4): 4 6 2 17 Testing clear() s1 (size 0): s2 (size 4): 0 1477251200 1477251168 1477251136 s3 (size 4): 28 75 41 36 Testing assignment operator s3 (size 4): 28 75 41 36 s4 (size 4): 28 75...

  • // thanks for helping // C++ homework // The homework is to complete below in the...

    // thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...

  • in c++ please include all of the following " template class, template function, singly linked list,...

    in c++ please include all of the following " template class, template function, singly linked list, the ADT stack, copy constructor, operator overloading, "try catch"and pointers Modify the code named "Stack using a Singly Linked List" to make the ADT Stack that is a template class has the code of the destructor in which each node is directly deleted without using any member function. As each node is deleted, the destructor displays the address of the node that is being...

  • This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and...

    This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and unorderedArrayList templates that are attached. Create a header file for your unorderedSet template and add it to the project. An implementation file will not be needed since the the new class will be a template. Override the definitions of insertAt, insertEnd, and replaceAt in the unorderedSet template definition. Implement the template member functions so that all they do is verify that the...

  • 1. Here are codes to define a stack class based on dynamic array, please complete the...

    1. Here are codes to define a stack class based on dynamic array, please complete the copy constructor //--- Definition of Stack copy constructor Stack::Stack(const Stack & original) : myCapacity(original.myCapacity), myTop(original.myTop) { //--- Get new array for copy myArray = new(nothrow) StackElement[myCapacity]; if (myArray != 0) // check if memory available                         // copy original's array member into this new array {              // Please complete the function here        } else {          cerr << "*Inadequate memory to allocate...

  • The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function...

    The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function so that it is written as a value returning function, returning the required item. If the location of the item to be returned is out of range, use the assert function to terminate the program. note: please give all code in c++ below is the class and implementation(a test program would be helpful in determining how to use it): class arrayListType { public:    ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT