⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainprog.cpp

📁 这是学习c++的一些编译过的示例,经典的,希望对你有用.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
***************************************************************
*
*	MainProg.cpp
*	
***************************************************************
*/


// standard library includes
#include <deque>
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <cctype>

// private includes
#include "project.h"

using std::cout;
using std::cin;
using std::endl;
using std::string;


// Function Prototypes
void display_record_menu();
void display_all(bool do_teachers, bool do_students);

void add_record();
void add_person(bool is_student);

void delete_record();
void delete_all_records();
void delete_surname(const string &surname);

void search_record();
void search_surname(const string &surname);

void save_records();
void load_records();

void record_report();

// Global variables
std::deque<Person*> persons;	// Person Base Class Container

Person *pPerson;	// Base class pointer


// Program code

/* 
******************************************************
*	Function:	main
*	return:		int
*	parameters:	none
*
*	Description:
*	Start of the program.  This function will display 
*	the Main Menu.
*
******************************************************
*/

int main() {
	char option;	// User input

	do{
		// Display the Main Menu
		cout << endl << endl;
		cout << endl << "MAIN MENU";
		cout << endl << "---------";
		cout << endl << "(A)dd a record";
		cout << endl << "(D)elete a record";
		cout << endl << "(C)lear all records";
		cout << endl << "(S)earch a record";
		cout << endl << "Displa(Y) records";
		cout << endl << "Sa(V)e records to disk";
		cout << endl << "(L)oad records from disk";
		cout << endl << "(R)eport list contents" << endl;

		cout << endl << "(Q)uit the Program" << endl << endl;
		cout << endl << "Enter Selection" << endl;
		
    cin >> option;
    option = static_cast<char>(std::toupper(option));

    switch (option) {
		case 'A': 
			// The user wants to add records
			add_record();
			break;
		case 'D': 
			// The user wants to delete record
			delete_record();
			break;
		case 'C': 
			// The user wants to delete all the records.
			delete_all_records();
			break;
		case 'S': 
			// The user wants to search a record
			search_record(); 
			break;
		case 'Y': 
			// The user wants to display records on the screen
			display_record_menu();
			break;
		case 'V': 
			// The user wants to save the records to disk
			save_records();
			break;
		case 'L': 
			// The user wants to load a record file
			load_records();
			break;
		case 'R': 
			// Report on what we have saved
			record_report();
			break;
		case 'Q':
			// Delete all records in Container 'persons' before exiting.
			persons.clear();
			break;
		default: 
			cout << endl << "Invalid choice.";
			break;
		}

	}while (option != 'Q'); // loop until user enters 'q' or 'Q'

	return 0;
}

/* 
******************************************************
*	Function:	display_record_menu
*	return:		void
*	parameters:	none
*
*	Description:
*	This function will display the menu which is used
*   to control record display
*
******************************************************
*/


void display_record_menu() {
	char option;

	do{
		// Display the display Record System menu
		cout << endl << endl;
		cout << endl << "display RECORD SYSTEM MENU";
		cout << endl << "------------------------";
		cout << endl << "display all (T)eachers";
		cout << endl << "display all (S)tudents";
		cout << endl << "display (A)ll Students and Teachers" << endl;

		cout << endl << "Press (Q)uit to return to the Main Menu" << endl;

		cout << endl << "Enter Selection" << endl;
		
    cin >> option;
    option = static_cast<char>(std::toupper(option));

		switch (option) {
		case 'T': 
			// display all teacher records
			display_all(true, false);
			break;
		case 'S': 
			// display all student records
			display_all(false, true);
			break;
		case 'A': 
			// display all teacher and student records
			display_all(true, true); 
			break;
		default: 
			cout << endl << "Invalid choice.";
			break;
		}

	}while (option != 'Q'); // loop until user enters 'q' or 'Q'

	cout << endl << "Returning to the Main Menu." << endl;
}


/* 
******************************************************
*	Function:	delete_Record
*	return:		void
*	parameters:	none
*
*	Description:
*	This function will prompt the user to enter the surname
*	of the record to delete.
*
******************************************************
*/


void delete_record() {
	string search_surname;
	char option;

	do	{
		cout << endl << "DELETE SURNAME. " << endl;
		cout << endl << "---------------" << endl;
		
		cout << endl <<"Enter the Surname of the record you want to delete, or '.' to exit" << endl;
		cin >> search_surname;
//		cin.get(); // retrieve the final "ENTER" and discard

		if (search_surname == ".")
			return;

		// Call the function vDelete_Surname to delete the record
		delete_surname(search_surname);

		// flush the standard input buffer
		//std::fflush(stdin);

		cout << endl << "Do you want to delete another record [Y/N]? " << endl;

    cin >> option;
    option = static_cast<char>(std::toupper(option));

	}while (option != 'N'); // Continue deleting until user enters 'N' or 'n'
}


/* 
******************************************************
*	Function:	delete_all_records
*	return:		void
*	parameters:	none
*
*	Description:
*	This function will clear the entire list of records, after
* first confirming the user's choice
*
******************************************************
*/


void delete_all_records() {
	char option;
	
	cout << endl << "Are you sure you want to delete all the records [Y/N]? " << endl;

	cin >> option;
  option = static_cast<char>(std::toupper(option));

	if (option == 'Y') {
		persons.clear();
		cout << "All records deleted" << endl;
	}
	else
		cout << "Deletion aborted" << endl;
}

/* 
******************************************************
*	Function:	delete_surname
*	return:		void
*	parameters:	const string &surname
*
*	Description:
*	This function will search the existing records 
*	to find which record matches the user-entered
*	surname, then delete the record.
*
******************************************************
*/

void delete_surname(const string &surname) {
	unsigned int i = 0;

	// Loop around all student and teacher records 
	// for a match on the surname.
	for (i = 0; i < persons.size(); i++) {
		pPerson = persons[i];
	
		// Compare the surname field of the record at 
		// index i with the user input record.  
		if ((surname.compare(pPerson->get_surname()) == 0)) {
			cout << endl << "Deleting the following Record." << endl;

			pPerson->display_info();
			pPerson->display_other_info();

			// Set the pointer to 0, then delete the record.
			pPerson = 0;
			persons.erase(persons.begin() + i);

			return;
		}
	}
	// If we get here then the record was not found.
	// Let the user know.
	cout << endl << "The Surname " << surname << " was not found." << endl;
}


/* 
******************************************************
*	Function:	add_record
*	return:		void
*	parameters:	none
*
*	Description:
*	This function will add a single record.  The
*	function will prompt the user to add either a
*	Student record or a Teacher record.
*
******************************************************
*/

void add_record() {
	char option = 0;

	cout << endl << "ADD RECORD MENU";
	cout << endl << "---------------";
	cout << endl << "Enter (T) to add a Teacher record.";
	cout << endl << "Enter (S) to add a Student record." << endl;

  cin >> option;
  option = static_cast<char>(std::toupper(option));

	try {
		switch(option) 	{
		case 'T': 
			// Add a single teacher record
			cout << endl << "Adding Teacher record." << endl;
			add_person(false);
			break;
		case 'S': 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -