/*第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(frie
C++课程设计(10个经典例子)
5星 · 超过95%的资源 需积分: 50 92 浏览量
2011-11-13
17:00:41
上传
评论 19
收藏 491KB RAR 举报
「已注销」
- 粉丝: 0
- 资源: 6
最新资源
- DSP开发实战教程-国产DSP替代进口TI DSP的使用技巧 进芯DSP替换文件
- 植被恢复能力估算python代码(KNDVI代码).zip
- 基于java打造的深度学习框架,帮助你快速搭建神经网络,实现模型推理与训练,引擎支持自动求导,多线程与GPU运算
- 界线与不动产测绘智能计算经纬度及标注软件
- CANOPEN使用方法与教程
- 极影毁片圆 · 电脑字体设置.zip
- 同态加密部分算法实现Homomorphic-Encryption-main.zip
- helib同态加密socket通信helibsocket-master.zip
- pll_inst.vhd
- 快速入门同态加密homomorphic-encryption-master.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈