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

📄 5_5.cpp

📁 这是一个电话簿管理的小程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*第5题	电话簿管理--源代码及关键源代码注解如下:*/
//PhoneBook 1.0 By Mark Miller 
//compiler directives
#include<fstream.h>
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
#include<string.h>
#include<stdlib.h>
//global structures and variables
  struct friend_node  //结构体类型定义,包括:姓、名和电话号码
    {
     char last_name[20];
     char first_name[15];
     char phone_num[12];
     friend_node *next;
    };
  friend_node *head_ptr;    //全程变量,链头指针
  friend_node *current_ptr;  //全程变量,用于指明当前在链表中的位置
//function prototypes 
void handle_choice(int choice);  //--函数原形说明
void add_record();
void insert_node(friend_node *new_rec_ptr);
friend_node *position_insertion_point(char lastname[20]);
void make_node_new_head(friend_node *new_rec_ptr);
void add_node_to_end(friend_node *new_rec_ptr);
void move_current_to_end();
void display_list();
void delete_record();
void delete_head_of_list();
void delete_end_of_list(friend_node *previous_ptr);
void delete_from_middle_of_list(friend_node *previous_ptr);
int verify_delete();
void delete_node(friend_node *previous_ptr);
void delete_list();
void search_by_lastname();
void write_list_to_file();
void load_list_from_file();
void help_me();
char pause;
//main function
int main()
{
  cout << "Welcome to PhoneBook 1.0 for dos and freedos\n";
  cout << "Press Enter To Continue\n";
  cin.get(pause);
  system("cls"); //执行系统命令:cls-清屏
  int choice;
  head_ptr = NULL;       // Initialize head pointer to NULL.
  load_list_from_file(); // Load data from the disk file into linked list.
  do
   { // Display menu.--  主菜单显示
    cout << "1 - Add record\n";
    cout << "2 - Display all records\n";
    cout << "3 - Search for friend by last name\n";
    cout << "4 - Delete record\n";
	cout << "5 - Help\n";
	cout << "6 - Exit program\n";
    cout << "Enter choice: ";
    cin >> choice;
    handle_choice(choice); // Call function to direct flow based on choice.
   } while(choice != 6);  // Repeat menu until user chooses to exit.
   return 0;
} // end of main function
// Function to direct program flow based on user's choice.
void handle_choice(int choice) //	根据用户选择(choice)调用对应处函数
 {
  switch(choice) // choice is passed into the function by value.
   {
    case 1:  // If choice was to add a record to the database,
     add_record();  // call function to add a record to the linked list.
     break;
    case 2:  // If choice was to display all records in the database,
     display_list(); // call function to display all records in
     break;          // the linked list.
  	case 3:  // If choice was to search for a record in the database,
     search_by_lastname(); // call function to search for record by
     break;                // last name.
    case 4:  // If choice was to delete a record in the database,
     delete_record(); // call a function that searched for record
     break;	 // by last name and deletes it.
	case 5:  // If choice is for help
     help_me();
	 break;
	case 6:  // If choice was to exit,
     write_list_to_file();  // save database to a file and
     if(head_ptr != NULL)   // delete the list from memory.
      { 
       delete_list(); 
      }
     break;
	default : // If any other (invalid) choice was entered,
     cout <<"Invalid choice\n"; // display error message.
     break;
   }
 } // end of function handle_choice
// Function to add record to the linked list.
void add_record()    // 在链表中增加一个记录
 {
  friend_node *new_rec_ptr; // Declare temporary pointer for the new node.
  new_rec_ptr = new friend_node; // Allocate memory for a new node and
                         // initialize pointer to point to it.
  if(new_rec_ptr != NULL) // If no error allocating memory, get data
   {  system("cls");                    // and insert node.
    // Get name and phone number from the user.
    cin.ignore(20,'\n');
    cout << "First Name: ";
    cin.get(new_rec_ptr->first_name,15);
    cin.ignore(20,'\n');
	cout << "Last Name: ";
    cin.get(new_rec_ptr->last_name,20);
    cin.ignore(20,'\n');
    cout << "Phone Number: ";
    cin.get(new_rec_ptr->phone_num,15);
    cin.ignore(20,'\n');
    insert_node(new_rec_ptr);
   }	
  else  // If error occurred allocating memory, display warning
   {    // and do not create node.
    cout << "WARNING: Memory error. New record cannot be added.\n";
   }system("cls");
 } // end of function add_record
// Function to insert new node into correct position in list.
void insert_node(friend_node *new_rec_ptr) //将一个由new_rec_ptr 指向的新节点插入链表中
 {
system("cls");
  friend_node *before_ptr;
  friend_node *after_ptr;
  if(head_ptr == NULL)
   {                             // If no nodes exist, make the node
    new_rec_ptr->next = NULL;    // the head.
    head_ptr = new_rec_ptr;
   }
  else
   {
    if(strcmp(new_rec_ptr->last_name, head_ptr->last_name) < 0)
     {                                  // If new record comes before head,
      make_node_new_head(new_rec_ptr);  // make it the new head.
     }
    else                                // Else, determine where the new node
     {                                  // should be inserted.
      current_ptr = position_insertion_point(new_rec_ptr->last_name);
      before_ptr = current_ptr;      // Use pointers to keep track of nodes
      after_ptr = current_ptr->next; // on each side of the insertion point.
      if(after_ptr == NULL) // If after_ptr is NULL, the node needs to be
       {                    // added to the end of the list.
        add_node_to_end(new_rec_ptr);
       }
      else                  // Else add the node between the nodes pointed to
       {                    // by before_ptr and after_ptr.
        before_ptr->next = new_rec_ptr;
        new_rec_ptr->next = after_ptr;
       }
     }
    }
 } // end of function insert_node
// Function that positions current_ptr at the node before the position
// where the new node should be inserted.
friend_node *position_insertion_point(char lastname[20])
 {             // 根据姓氏,返回其在链表中的正确位置。新节点即将插入此点。
  char temp_name[20];
  friend_node *temp_ptr;
  int tempint;
  if(head_ptr->next != NULL) // If more than one node exists, search the
   {                         // list for the correct insertion point.
    current_ptr = head_ptr;
    temp_ptr = current_ptr->next;
    strcpy(temp_name, temp_ptr->last_name);
    // Loop until the proper insertion point is located.
    tempint = strcmp(lastname,temp_name);
    while((tempint > 0) && (current_ptr->next !=NULL))
     {
      current_ptr = temp_ptr;
	  // check to see if the current node is the last node
	  if(current_ptr->next != NULL)
	  {
       temp_ptr = current_ptr->next;
       strcpy(temp_name, temp_ptr->last_name);
       tempint = strcmp(lastname,temp_name);
	  }
     }
   }
  else  // If only one node exists in the list, current_ptr is the same
   {    // as head_ptr. New node will be added to the end of the list.
    current_ptr = head_ptr;
   }
  return(current_ptr);
 } // end of function position_insertion_point
// Function that makes the node pointed to by new_rec_ptr the new
// head of the linked list. It handles the special case of inserting at
// the front of the list.
void make_node_new_head(friend_node *new_rec_ptr)
 {
  friend_node *temp_ptr;  // temporary pointer to keep track of the head

  temp_ptr = head_ptr;  // Set temp_ptr to point at the current head.
  new_rec_ptr->next = temp_ptr; // Make new nodes next pointer point to
  head_ptr = new_rec_ptr;       // current head and make new node the head.
 } // end of function make_node_new_head
// Function that adds a node to the end of the linked list. It handles
// the special case of inserting at the end of the list.
void add_node_to_end(friend_node *new_rec_ptr)
 {
  new_rec_ptr->next = NULL;  // Set next node pointer of new node to NULL.
  move_current_to_end();       // Make sure current_ptr is at end of list.
  current_ptr->next = new_rec_ptr; // Place new node at the end of the list.
 } // end of function add_node_to_end
// Function that moves current_ptr to end of the linked list.
void move_current_to_end()
 {
  current_ptr = head_ptr;  // Move temp_ptr to head of the list.
  while(current_ptr->next != NULL)
   {                             // Traverse list until NULL is reached.
    current_ptr = current_ptr->next;
   }
 } // end of function move_current_to_end
// Function that displays entire linked list.
void display_list()
 {
  char fullname[36];  // used to combine names into one array

  current_ptr = head_ptr;   // Move current_ptr to head of list.
  if(current_ptr != NULL)
   {
    cout << endl;
    cout << "Name                                Phone Number\n";
    cout << "----------------------------------- ------------\n";
    do
     {
      strcpy(fullname,""); // Clear fullname.
      strcat(fullname, current_ptr->last_name);  // Put last name, then a
      strcat(fullname, ", ");                    // comma, then the
      strcat(fullname, current_ptr->first_name); // first name into fullname.
      cout.setf(ios::left);
      cout << setw(36) << fullname;
	 cout.unsetf(ios::left);
      cout.setf(ios::right);
      cout << setw(12) << current_ptr->phone_num << endl;
      current_ptr = current_ptr->next; // Set current_ptr to next node.
	  cout <<  endl;
     } while(current_ptr != NULL); // Loop until end of list.
	 cout << "Press Enter to continue \n";
	 cin.get(pause);
	 cin.ignore(1,pause);
  system("cls"); }
  else  // If list is empty, display message.
   {
    cout << "\nNO RECORDS TO DISPLAY\n";
   }
 } // end of function display_list
// Function that searches linked list for the first occurrence of a given
// last name and displays the record to the screen.
void search_by_lastname()
 {
system("cls");
  char search_string[20];  // Character array for last name to search for.
  current_ptr = head_ptr;  // Move current_ptr to head of list
                          // to begin search.
  cin.ignore(20,'\n');
  cout << "\nEnter the last name for which you want to search: ";
  cin.get(search_string,20);
  cin.ignore(20,'\n');
  // Loop until search_string is found or end of list is reached.
  while((current_ptr != NULL) && 
	   (strcmp(current_ptr->last_name, search_string) != 0))
   {

⌨️ 快捷键说明

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