/* Filename: list2.cpp Author: Br. David Carlson Date: July 5, 1999 Last Revised: September 19, 2022 This interactive program allows the user to insert integers into a list, sort them, find their average, find their maximum value, and print them. Tested with: Microsoft Visual C++ 2019 */ #include #include #include #include #include // to use the toupper function using namespace std; // Function prototypes: template void Print(list& L); char MenuChoice(void); int GetNum(void); void InsertBefore(list& L); void Average(list& L); int main(void) { list L; char Choice; int Num; Choice = MenuChoice(); while (Choice != 'H') { if (Choice == 'A') { Num = GetNum(); L.push_back(Num); } else if (Choice == 'B') { Num = GetNum(); L.push_front(Num); } else if (Choice == 'C') InsertBefore(L); else if (Choice == 'D') L.sort(); else if (Choice == 'E') Print(L); else if (Choice == 'F') Average(L); else if (Choice == 'G') { if (L.size() == 0) cout << "No maximum can be found since the list is empty" << endl; else cout << "Max value is " << *max_element(L.begin(), L.end()) << endl; } else if (Choice != 'H') cout << "Invalid choice" << endl; Choice = MenuChoice(); } return 0; } /* Given: Nothing. Task: To present the menu of available options and get the letter representing the user's menu choice. Return: This letter (translated to upper case) in the function name. */ char MenuChoice(void) { char Ch; cout << endl << "Menu of Available Options" << endl; cout << " A) Insert at the rear of the list" << endl; cout << " B) Insert at the front of the list" << endl; cout << " C) Insert before a specific item" << endl; cout << " D) Sort the list" << endl; cout << " E) Print the list" << endl; cout << " F) Find the average of the items in the list" << endl; cout << " G) Find the maximum item in the list" << endl; cout << " H) Quit" << endl; cout << "Enter the letter for your choice: "; cin >> Ch; cin.get(); // read in and discard the ENTER return toupper(Ch); } /* Given: Nothing. Task: To get an integer from the user. Return: This integer in the function name. */ int GetNum(void) { int Temp; cout << "Enter integer to be inserted: "; cin >> Temp; cin.get(); return Temp; } /* Given: L List of integers. Task: To prompt the user for a number to insert into L and for the number in L before which to insert the new number. Does the insertion (unless it cannot find the number before which to insert). Return: L Updated list of integers. */ void InsertBefore(list& L) { int Item, Target; list::iterator p; Item = GetNum(); cout << "Insert this number before what number? "; cin >> Target; cin.get(); p = find(L.begin(), L.end(), Target); if (p == L.end()) cout << "Sorry, could not find " << Target << " so " << Item << " not inserted" << endl; else L.insert(p, Item); } /* Given: L List of integers. Task: To find and print the average of the numbers in list L. Return: Nothing. */ void Average(list& L) { int Count; float Sum; list::iterator p; Count = L.size(); if (Count == 0) cout << "No average can be found since the list is empty" << endl; else { Sum = 0.0; for (p = L.begin(); p != L.end(); p++) Sum = Sum + *p; cout << "Average is " << Sum / Count << endl; } } /* Given: L A list containing items of type T. Task: To print out the contents of list L. Assumes: Items of type T can be printed using <<. Return: Nothing. */ template void Print(list& L) { cout << "Contents of list:" << endl; copy(L.cbegin(), L.cend(), ostream_iterator(cout, " ")); cout << endl << endl; }