一、实验目的
通过用C ++编写一个学生通讯录管理系统,强化面向对象程序设计思想,使学生能够将C ++程序设计中的面向对象、重载、模板、文件等各种概念,灵活的运用到实际的程序设计中去。
二、环境
Windows 10,VS2017。
三、正文
(一)分析
根据需求,该系统应具备以下功能:
1.对联系人基本信息的录入
2.显示所有联系人的基本信息
3.按照姓名查找联系人信息
4.删除联系人信息
5.保存联系人信息至文件
6.从文件中读取联系人的信息
7.添加新联系人信息
(二)设计
开始
进入主函数
结束
退出
添加联系人信息(tianjia)
从文件中读取联系人信息(duqu0
保存联系人信息至文件(baocun)
删除联系人信息(delete)
查找联系人信息(chayue)
显示联系人信息(showlist)
输入联系人信息(Creatlise)
输入指令(h)
(三)代码实现
#include<iostream>
#include<string.h>
#include <fstream>
#include <stdlib.h>
#include<conio.h>
using namespace std;
struct Address //说明结构
{
char name[20]; //结点data域
char Tel[20];
char Email[20];
char Relation[20];
Address *next; //结点next域
};
void Createlist(Address *&head) //建立链表
{
Address *s, *p;
s = new Address;
int a = 1;
while (a == 1)
{
cout << "请输入姓名:";
cin >> s->name;
cout << "请输入电话号:";
cin >> s->Tel;
cout << "请输入Email:";
cin >> s->Email;
cout << "请输入与您关系:";
cin >> s->Relation;
{
if (head == NULL)
head = s;
else
p->next = s;
}
p = s;
s = new Address;
cout << "是否继续输入,输入按1不输入按0 :"; //判断是否继续输入
cin >> a;
}
p->next = NULL;
delete s;
return;
}
void showlist(Address *&head) //显示链表
{
cout << "您的通信录为: " << endl;
cout << "姓名" << '\t' << "电话" << '\t' << "电子邮箱" << '\t' << "关系" << endl;
while (head)
{
cout << head->name << '\t';
cout << head->Tel << '\t';
cout << head->Email << '\t';
cout << head->Relation << endl;;
head = head->next;
}
cout << endl;
system("pause");
}
void Chayuelist(Address *head) //查阅链表
{
char a[10];
cout << "请输入你所要查阅的姓名:";
cin >> a;
while (head)
{
if (strcmp(head->name, a) == 0)
{
cout << "姓名" << '\t' << "电话" << '\t' << "电子邮箱" << '\t' << "关系" << endl;
cout << head->name << '\t';
cout << head->Tel << '\t';
cout << head->Email << '\t';
cout << head->Relation << endl;
system("pause");
return;
}
head = head->next;
}
cout << "查不到联系人信息!" << endl;
system("pause");
}
void Delete(Address *head) //删除链表数据
{
char a[10];
Address *s;
cout << "请输入你所要删除的姓名:";
cin >> a;
for (Address *q = head; q != NULL; q = q->next)
{
if (strcmp(q->next->name, a) == 0)
{
s = q->next;
q->next = s->next;
delete s;
}
cout << "删除成功!" << endl;
system("pause");
}
}
void baocun(Address *head)//保存
{
ofstream outstuf;
outstuf.open("c:\\save.txt", ios::out);
if (!outstuf)
{
cerr << "File could not be open." << endl;
abort();
}
while (head)
{
outstuf << head->name << ' '
<< head->Tel << ' '
<< head->Email << ' '
<< head->Relation << '\n';
head = head->next;
}
system("cls;");
cout << "保存成功!";
}
void duqu(Address *&head) //读取保存文件
{
Address *next, *s;
s = new Address;
head = new Address; //头插法建立单链表
head->next = new Address;
s = head->next;
ifstream infile;
infile.open("c:\\save.txt"); //打开外存文件,看是否有数据存在
if (!infile)
cout << "电话系统中没有任何号码" << endl;
else
{
infile.close();
infile >> s->name >> s->Tel >> s->Email >> s->Relation;
cout << "读取电话号码系统成功!" << endl;
system("pause");
}
}
void tianjia(Address *&head) //添加
{
Address *s;
s = new Address;//生成新结点
cout << "\n\t\t请输入姓名:";
cin >> s->name;
cout << "\n\t\t请输入电话号:";
cin >> s->Tel;
cout << "\n\t\t请输入Email:";
cin >> s->Email;
cout << "\n\t\t请输入与您关系:";
cin >> s->Relation;
cout << "\n\t\t你已经添加成功";
s->next = head;
head = s;
return;
}
void tuichu()
{
system("cls");
cout << "您已成功推出本系统,欢迎您下次使用!" << endl;
system("pause");
}
void main() //主函数
{
system("cls");
int h;
Address *head = NULL;
cout << "--------------------本程序为通讯录管理系统-----------------" << endl;
while (h <= 7)
{
cout << '\n';
cout << '\t' << '\t' << "请选择你所要实现的功能的编号" << endl;
cout << '\t' << '\t' << "*********1.输入记录***********" << endl;
cout << '\t' << '\t' << "*********2.显示记录*********" << endl;
cout << '\t' << '\t' << "*********3.查阅记录*********" << endl;
cout << '\t' << '\t' << "*********4.删除记录*********" << endl;
cout << '\t' << '\t' << "*********5.保存记录*********" << endl;
cout << '\t' << '\t' << "*********6.读取记录*********" << endl;
cout << '\t' << '\t' << "*********7.添加记录*********" << endl;
cout << '\t' << '\t' << "*********8.退出*************" << endl;
cin >> h;
switch (h)
{
case 1:Createlist(head); break;
case 2:showlist(head); break;
case 3:Chayuelist(head); break;
case 4:Delete(head); break;
case 5:baocun(head); break;
case 6:duqu(head); break;
case 7:tianjia(head); break;
default:tuichu(); break;
}
system("cls");
}
}
(四)结果
1、主界面
2、输入信息
3、显示记录
4、删除记录
5、查阅记录
如果没有联系人的话
6、保存记录
7、读取记录
8、添加记录
四、总结
课程设计是培养我们综合运用所学知识,发现,提出,分析和解决实际问题,锻炼实践能力的重要环节,是对我们实际工作能力的具体训练和考察过程。回顾此次数据结构课程的学习,至今我仍感慨颇多,此次课程设计不仅可以巩固了以前所学的知识,而且学到了很多在书本上所没有学到过的知识。我坚信此次的课程设计能够在以后的学习与实践结合方面对我提供很大的帮助!