大一c++练习作业黑框程序,由链表实现,所有功能都测试通过。应该没有太大的问题,仅供参考。
#include<iostream>
#include<string>
using namespace std;
struct Student {//学生信息结构体
int s_number;//学号
string s_name;//姓名
string s_sex;//性别
string s_phone;//电话
string s_school;//所属学院
Student* next;
};
void myprint() {// 显示通讯录主菜单函数定义
cout << "************************" << endl;
cout << "***** 1.创建通讯录 *****" << endl;
cout << "***** 2.显示通讯录 *****" << endl;
cout << "***** 3.查询通讯录 *****" << endl;
cout << "***** 4.添加通讯录 *****" << endl;
cout << "***** 5.修改通讯录 *****" << endl;
cout << "***** 6.删除通讯录 *****" << endl;
cout << "***** 7.排序通讯录 *****" << endl;
cout << "***** 0.退出通讯录 *****" << endl;
cout << "************************" << endl;
}
Student* mycreat() {// 创建通讯录信息函数定义
Student* head, * p1, * p2;
int n = 0;
head = NULL;
p1 = p2 = new Student;
cout << "请依次输入学生的学号,姓名,性别,手机号码,所属学院(最后一组数据全输入0退出)" << endl;
cin >> p1->s_number >> p1->s_name >> p1->s_sex >> p1->s_phone >> p1->s_school;
while (p1->s_number != 0) {
n = n + 1;
if (n == 1)head = p1;
else p2->next = p1;
p2 = p1;
p2->next = p1;
p1 = new Student;
cin >> p1->s_number >> p1->s_name >> p1->s_sex >> p1->s_phone >> p1->s_school;
}
p2->next = NULL;
cout << "已成功创建一个学生通讯录" << endl;
system("pause");
system("cls");
return head;
}
void mydisplay(Student *head) {// 显示通讯录信息函数定义
Student* p1;
p1 = head;
while (p1 != NULL) {
cout << p1->s_number <<"\t" << p1->s_name << "\t" <<p1->s_sex << "\t" << p1->s_phone << "\t" << p1->s_school << endl;
p1 = p1->next;
}
system("pause");
system("cls");
}
void mysearch(Student* head) {// 查询通讯录信息函数定义
int ans;
cout << "请选择根据何种方式查询(1.学号 2.姓名 3.性别 4.电话 5.学院)" << endl;
cin >> ans;
void search1(Student *head);
void search2(Student * head);
void search3(Student * head);
void search4(Student * head);
void search5(Student * head);
switch (ans) {
case 1:search1(head); break;
case 2:search2(head); break;
case 3:search3(head); break;
case 4:search4(head); break;
case 5:search5(head); break;
}
}
void search1(Student* head) {//学号查找
int num;
Student* p;
p = head;
cout << "************************" << endl;
cout << "请输入需要查找学生的学号" << endl;
cin >> num;
bool finded = false;
while (p != NULL) {
if (p->s_number == num) {
finded = true;
cout << p->s_number << "\t" << p->s_name << "\t" << p->s_sex << "\t" << p->s_phone << "\t" << p->s_school << endl;
}
p = p->next;
}
if (!finded) { cout << "没有查询到该同学的信息" << endl; }
system("pause");
system("cls");
}
void search2(Student* head) {//姓名查找
string name;
Student* p;
p = head;
cout << "************************" << endl;
cout << "请输入需要查找学生的姓名" << endl;
cin >> name;
bool finded = false;
while (p != NULL) {
if (p->s_name == name) {
finded = true;
cout << p->s_number << "\t" << p->s_name << "\t" << p->s_sex << "\t" << p->s_phone << "\t" << p->s_school << endl;
}
p = p->next;
}
if (!finded) { cout << "没有查询到该同学的信息" << endl; }
system("pause");
system("cls");
}
void search3(Student* head) {//性别查找
string sex;
Student* p;
p = head;
cout << "************************" << endl;
cout << "请输入需要查找学生的性别" << endl;
cin >> sex;
bool finded = false;
while (p != NULL) {
if (p->s_sex == sex) {
finded = true;
cout << p->s_number << "\t" << p->s_name << "\t" << p->s_sex << "\t" << p->s_phone << "\t" << p->s_school << endl;
}
p = p->next;
}
if (!finded) { cout << "没有查询到该同学的信息" << endl; }
system("pause");
system("cls");
}
void search4(Student* head) {//电话查找
string phone;
Student* p;
p = head;
cout << "************************" << endl;
cout << "请输入需要查找学生的电话" << endl;
cin >> phone;
bool finded = false;
while (p != NULL) {
if (p->s_phone == phone) {
finded = true;
cout << p->s_number << "\t" << p->s_name << "\t" << p->s_sex << "\t" << p->s_phone << "\t" << p->s_school << endl;
}
p = p->next;
}
if (!finded) { cout << "没有查询到该同学的信息" << endl; }
system("cls");
}
void search5(Student* head) {//学院查找
string school;
Student* p;
p = head;
cout << "************************" << endl;
cout << "请输入需要查找学生的学院" << endl;
cin >> school;
bool finded = false;
while (p != NULL) {
if (p->s_school == school) {
finded = true;
cout << p->s_number << "\t" << p->s_name << "\t" << p->s_sex << "\t" << p->s_phone << "\t" << p->s_school << endl;
}
p = p->next;
}
if (!finded) { cout << "没有查询到该同学的信息" << endl; }
system("pause");
system("cls");
}
Student *myadd(Student *head,int number,string name,string sex,string phone,string school) {// 添加通讯录信息函数定义
Student* p0, * p1, * p2;
p1 = head;
p2 = new Student;
p0 = new Student; // 插入节点
p0->s_number = number;// 插入数据
p0->s_name = name;
p0->s_sex = sex;
p0->s_phone =phone;
p0->s_school = school;
if (head == NULL) {
head = p0; p0->next = NULL;
}
else
while (p0->s_number > p1->s_number && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;// p0,p1和p2位置: p2->p1->p0
}
if (p0->s_number <= p1->s_number)
{
if (p1 == head)
{// 头部前段插入 p0和p1位置: p0->p1->...
head = p0;
p0->next = p1;
}
else
{// 插入中间节点 p0,p1和p2位置: p2-> p0 -> p1
p2->next = p0;
p0->next = p1;
}
}
else
{ // 尾部插入节点 p0,p1和p2位置: p2->p1->p0->NULL
p1->next = p0;
p0->next = NULL;
}
cout << "已成功添加该学生的信息" << endl;
system("pause");
system("cls");
return head;
}
void mymodify(Student *head) {// 修改通讯录信息函数定义
int ans;
cout << "请先查询需要修改信息的同学(1.姓名 2.学号)" << endl;
cin >> ans;
if (ans == 1) {
string name,sex,phone,school;
int ans2,number;
Student* p;
p = head;
cout << "************************" << endl;
cout << "请输入需要查找学生的姓名" << endl;
cin >> name;
bool finded = false;
while (p != NULL) {
if (p->s_name == name) {
finded = true;
cout << p->s_number << "\t" << p->s_name << "\t" << p->s_sex << "\t" << p->s_phone << "\t" << p->s_school << endl;
cout << "请输入需要修改的信息(1.学号 2.姓名 3.性别 4.电话 5.学院)" << endl;
cin >> ans2;
switch (ans2) {
case 1:cout << "请输入修改后的学号" << endl; cin >> number; p->s_number = number; break;
case 2:cout << "请输入修改后的姓名" << endl; cin >> name; p->s_name = name; break;
case 3:cout << "请输入修改后的性别" << endl; cin >> sex; p->s_sex = sex; break;
case 4:cout << "请输入修改后的电话" << endl; cin >> phone; p->s_phone = phone; break;
case 5:cout << "请输入修改后的学院" << endl; cin >> school; p->s_school = school; break;
}
break;
}
p = p->next;
}
cout << "已成功修改该学生的信息" << endl;
if (!finded) { cout << "没有查询到该同学的信息" << endl;
}
}
else if (ans == 2) {
string name, sex, phone, school;
int ans2, number;
Student* p;
p = head;
cout << "************************" << endl;
cout << "请输入需要查找学生的学号" << endl;
cin >> number;
bool finded = false;
while (p != NULL) {
if (p->s_number == number) {
finded = true;
cout << p->s_number << "\t" << p->s_name << "\t" << p->s_sex << "\t" << p->s_phone << "\t" << p->s_school << endl;
cout << "请输入需要修改的信息(1.学号 2.姓名 3.性别 4.电话 5.学院)" << endl;
cin >> ans2;
switch (ans2) {
case 1:cout << "请输入修改后的学号" << endl; cin >> number; p->s_number = number; break;
case 2:cout << "请输入修改后的姓名" << endl; cin >> name; p->s_name = name; break;
case 3:cout << "请输入修改后的性别" << endl; cin >> sex; p->s_sex = sex; break;
case 4:cout << "请输入修改后的电话" << endl; cin >> phone; p->s_phone = phone; break;
case 5:cout << "请输入修改后的学院" << endl; cin >> school; p->s_school = school; break;
}
break;
}
p = p->next;
}
cout << "已成功修改该学生的信息" << endl;
if (!finded) { cout << "没有查询到该同学的信息" << endl;
}
}
system("pause");
system("cls");
}
Student *mydelete(Student *head) {// 删除通讯录信息函数定义
int ans;
char c;
cout << "请选择根据何种方式确定需要删除学生(1.学号 2.姓名)" << endl;
cin >> ans;
if (ans == 1) {
int number;
cout << "请输入需要删除的学生学号" << endl;
cin >> number;
struct Student* p1, * p2 = NULL;
if (head == NULL)
{
cout << "通讯录为空\n";
}
p1 = head;
while (number!= p1->s_number && p1->next != NULL)
{
p2 = p1; p1 = p1->next;
}
if (number == p1->s_number)
{
cout << p1->s_number << "\t" << p1->s_name << "\t" << p1->s_sex << "\t" << p1->s_phone << "\t" << p1->s_school << endl;
cout << "确定要删除该同学的信息吗?(是:y,否:n)" << endl;
cin >> c;
if (c == 'n') return head;
else {
if (number == head->s_number)head = p1->next;
else p2->next = p1->next;
}
cout << "已成功删除该同学的信息" << endl;
}
else cout << "通讯录中没有录入该同学的信息\n";
system("pause");
system("cls");
return head;
}
else if(ans==2)
{
string name;
char c;
cout << "请输入需要删除的学生姓名" << endl;
cin >> name;
struct Student* p1, * p2 = NULL;
if (head == NULL)
{
cout << "通讯录为空\n";
}
p1 = head;
while (name != p1->s_name && p1->next != NULL)
{
p2 = p1; p1 = p1->next;
}
if (name == p1->s_name)
{
cout << p1->s_number << "\t" << p1->s_name << "\t" << p1->s_sex << "\t" << p1->s_phone << "\t" << p1->s_school << endl;
cout << "确定要删除该同学的信息吗?(是:y,否:n)" << endl;
cin >> c;
if (c == 'n') return head;
else {
if (name == head->s_name)head = p1->next;
else p2->next = p1->next;
}
cout << "已成功删除该学生的信息" << endl;
}
else cout << "通讯录中没有录入该同学的信息\n";
system("pause");
system("cls");
return head;
}
}
void mysort(Student *head) {// 通讯录信息排序函数定义
Student* p;
int temp, temp3;
string temp1, temp2;
int flag;
while (true) {
flag = 0;
for (p = head; p->next != NULL; p = p->next) {
if (p->s_number > p->next->s_number) {
temp = p->s_number;
p->s_number = p->next->s_number;
p->next->s_number = temp;
string temp2 = p->s_name;
p->s_name = p->next->s_name;
p->next->s_name = temp2;
string temp3 = p->s_sex;
p->s_sex = p->next->s_sex;
p->next->s_sex = temp3;
string temp4 = p->s_phone;
p->s_phone = p->next->s_phone;
p->next->s_phone = temp4;
string temp5 = p->s_school;
p->s_school = p->next->s_school;
p->next->s_school = temp5;
flag = 1;
}
}
if (flag == 0)break;
}
cout << "已完成按照学号(从小到大)排序" << endl;
system("pause");
system("cls");
}
int main() {
cout << "欢迎使用学生通讯录管理系统^_^" << endl;
int select = 0;//创建用户选择的变量
Student* head = NULL;
int number;
string name, sex, phone, school;
while (true) {
myprint();//菜单的调用
cin >> select;
switch (select) {
case 1://1.创建通讯录
head=mycreat();
break;
case 2://2.显示通讯录
mydisplay(head);
break;
case 3://3.查询通讯录
mysearch(head);
break;
case 4://4.添加通讯录
cout << "请输入需要添加的学生信息(学号,姓名,性别,电话,学院)" << endl;
cin >> number >> name >> sex >> phone >> school;
head = myadd(head, number, name, sex, phone, school);
break;
case 5://5.修改通讯录
mymodify(head);
break;
case 6://6.删除通讯录
head=mydelete(head);
break;
case 7://7.排序通讯录
mysort(head);
break;
case 0://0.退出通讯录
cout << "欢迎下次使用!^_^" << endl;
system("pause");
return 0;
break;
default:
break;
}
}
system("pause");
return 0;
}