文件版学生通讯系统

文件版学生通讯系统

此学生管理系统文件版的学生管理系统,输入或者修改的学生通讯信息将实时在文件中进行改变,并能够在文件中进行保存,在进行不适当操作的时候能够弹出窗口进行提,下面是我的全部代码

#include<iostream>
#include<graphics.h>
#include<fstream>
#include<string>
using namespace std;

typedef struct _T_data {
	char name[12];
	char sex;
	char tel[12];
}T_data;
typedef T_data* Pt_data;

typedef struct _T_students {
	T_data data;
	struct _T_students * pt_next;
}T_student;
typedef T_student* Pt_student;

char MyName[12];
char MySex;
char MyTel[12];

void AddStudnet(Pt_student &head);//增添学生
bool DelStudent(Pt_student &head);//删除学生
bool UpdateStudent(Pt_student &head);//修改学生
bool Querystudent(Pt_student &head);//查询学生
bool ClearListStudent(Pt_student &head);//清除链表学生
void ClearFileStudent();//清除文件中的学生信息
bool Query(Pt_student &head, int choice);//查询学生实现
bool DisplayStudent(Pt_student &head);//展示链表中的所有学生信息
void LoadStudent(Pt_student &head);//从文件中导入学生信息
void Input(int flag);//输入学生信息
void Output(Pt_student &head);//输出学生信息
void Menu();//程序界面主菜单

void Menu()
{
	cout << "****************************************************" << endl;
	cout << "\t\t欢迎来到学生通讯系统" << endl;
	cout << "****************************************************" << endl;
	cout << "\t\t1.添加联系人" << endl;
	cout << "\t\t2.删除联系人" << endl;
	cout << "\t\t3.修改联系人" << endl;
	cout << "\t\t4.查找联系人" << endl;
	cout << "\t\t5.清空通讯录" << endl;
	cout << "\t\t6.显示通讯录" << endl;
	cout << "\t\t7.退出通讯录" << endl;
	cout << "____________________________________________________" << endl;
}

void Input(int flag)
{
	switch (flag)
	{
	case 1:
			cout << "请输入学生姓名:";
			cin >> MyName;
			break;
	case 2:
		while (1)
		{
			cout << "请输入学生性别(F或M):";
			cin >> MySex;
			if (MySex != 'F' && MySex != 'M')
			{
				MessageBox(0, _T("输入错误,请重新输入"), _T("警告"), MB_OK|MB_ICONEXCLAMATION);
				continue;
			}
			break;
		}
		break;
	case 3:
		cout << "请输入学生电话:";
		cin >> MyTel;
		break;
	}
}

void AddStudnet(Pt_student &head)
{
	while (1)
	{
		cout << "****************************************************" << endl;
		Pt_student temp;
		temp = (Pt_student)malloc(sizeof(T_student));
		temp->pt_next = NULL;
		Input(1);
		strcpy(temp->data.name,MyName);
		Input(2);
		strcpy(&temp->data.sex, &MySex);
		Input(3);
		strcpy(temp->data.tel, MyTel);
		temp->pt_next = head->pt_next;
		head->pt_next = temp;
		cout << "\t添加成功" << endl;
		if (MessageBox(0, _T("是否继续添加联系人"), _T("请选择"), MB_YESNO|MB_ICONQUESTION) == IDNO)
			break;
	}
}

void Output(Pt_student &head)
{
	cout << "****************************************************" << endl;
	cout << "\tname" << "\t\tsex" << "\t\ttel" << endl;
	cout << "\t" << head->data.name << "\t\t" << head->data.sex << "\t\t" << head->data.tel << endl;
}

bool Query(Pt_student &head,int choice)
{   
	bool flag = false;
	Pt_student temp = NULL;
	if (1 == choice)
	{
		Input(1);
		for (temp = head; temp->pt_next != NULL; temp = temp->pt_next)
			if (!strcmp(temp->pt_next->data.name,MyName))
			{
				Output(temp->pt_next);
				flag = true;
			}			
	}
	else 
	{
		Input(3);
		for (temp = head; temp->pt_next != NULL; temp = temp->pt_next)
			if (!strcmp(temp->pt_next->data.tel,MyTel))
			{
				Output(temp->pt_next);
				flag = true;
			}
	}
	return flag;
}

bool Querystudent( Pt_student &head)
{
	bool flag = false;
	if (head->pt_next == NULL)
	{
		MessageBox(0, _T("当前通讯录为空,请先添加"), _T("警告"), MB_OK|MB_ICONEXCLAMATION);
		return false;
	}
	int choice = 1;
	cout << "****************************************************" << endl;
	cout << "\t1.学生姓名查询" << endl;
	cout << "\t2.学生手机号查询" << endl;
	cout << "----------------------------------------------------" << endl;
	while (1)
	{
		cout << "\t请输入你选择的查询方式(1或2):";
		cin >> choice;
		if (choice != 1 && choice != 2)
		{
			MessageBox(0, _T("输入错误,请重新输入"), _T("警告"), MB_OK|MB_ICONEXCLAMATION);
			continue;
		}		
		break;
	}
	if (!Query(head, choice))
		MessageBox(NULL, _T("查无此人,是否退出"), _T("提示"), MB_OK | MB_ICONQUESTION);
	else
		MessageBox(NULL, _T("查找成功,是否退出"), _T("提示"), MB_OK | MB_ICONQUESTION);
	return true;
}

bool DelStudent(Pt_student &head)
{
	if (head->pt_next == NULL)
	{
		MessageBox(0, _T("当前通讯录为空,请先添加"), _T("警告"), MB_OK|MB_ICONEXCLAMATION);
		return false;
	}	
	Pt_student pretemp,temp;
	cout << "----------------------------------------------------" << endl;
    cout << "\t请输入你要删除的学生信息:" << endl;
	Input(1);
	for (pretemp = head; pretemp->pt_next != NULL; pretemp = pretemp->pt_next)
		if (!strcmp(pretemp->pt_next->data.name , MyName))
		{
			cout << "要删除的学生的信息如下" << endl;
			Output(pretemp->pt_next);
			break;
		}
	if (pretemp->pt_next== NULL)
	{
		MessageBox(0, _T("查无此人,无法删除,是否退出"), _T("警告"), MB_OK | MB_ICONEXCLAMATION);
		return false;
	}
	temp = pretemp->pt_next;
	pretemp->pt_next = temp->pt_next;
	delete temp;
	MessageBox(NULL, _T("删除成功,是否退出"), _T("提示"), MB_OK | MB_ICONQUESTION);
	return true;
}

bool UpdateStudent(Pt_student &head)
{
	bool flag = false;
	if (head->pt_next == NULL)
	{
		MessageBox(0, _T("当前通讯录为空,请先添加"), _T("警告"), MB_OK|MB_ICONEXCLAMATION);
		return false;
	}
	Pt_student temp;
	cout << "----------------------------------------------------" << endl;
	cout << "\t请输入你要修改的学生信息:" << endl;
	Input(1);
	for (temp = head->pt_next; temp!= NULL; temp = temp->pt_next)
		if (!strcmp(temp->data.name , MyName))
		{
			cout << "\t修改之前的学生信息" << endl;
			Output(temp);
			break;
		}
	if (temp == NULL)
	{
		MessageBox(0, _T("查无此人,无法修改,是否退出"), _T("警告"), MB_OK | MB_ICONEXCLAMATION);
		return false;
	}
	cout << "\t请输入修改后的学生信息" << endl;
	Input(1);
	strcpy(temp->data.name, MyName);
	Input(2);
	strcpy(&temp->data.sex, &MySex);
	Input(3);
	strcpy(temp->data.tel, MyTel);
	cout << "\t修改之后的学生信息" << endl;
	Output(temp);
	MessageBox(NULL, _T("修改成功,是否退出"), _T("提示"), MB_OK | MB_ICONQUESTION);
	return true;
}

bool ClearListStudent(Pt_student &head)
{
	bool flag = false;
	if (head->pt_next == NULL)
	{
		MessageBox(0, _T("当前通讯录为空,请先添加"), _T("警告"), MB_OK|MB_ICONEXCLAMATION);
		return false;
	}
	Pt_student temp,tempnext;
	for (temp = head->pt_next; temp != NULL; temp = tempnext)
	{
		tempnext = temp->pt_next;
		delete temp;
	}
	head->pt_next = NULL;
	cout << "\t\t清空完毕" << endl;
	return true;
}

void ClearFileStudent()
{
	ofstream fout("students.txt", ios::trunc);
	fout.close();
}

bool DisplayStudent(Pt_student &head)
{
	
	bool flag = false;
	if (head->pt_next == NULL)
	{
		MessageBox(0, _T("当前通讯录为空,请先添加"), _T("警告"), MB_OK|MB_ICONEXCLAMATION);
		return false;
	}
	Pt_student temp = head->pt_next;
	for (; temp != NULL; temp = temp->pt_next)
		Output(temp);
	MessageBox(NULL, _T("全部信息以显示,是否退出"), _T("提示"), MB_OK | MB_ICONEXCLAMATION);
	return true;
}

//保存记录
void SaveStudent(Pt_student &head)
{
	Pt_student temp = head->pt_next;
	ofstream fout("students.txt");
	while (temp)
	{
		fout << temp->data.name << "\t" << temp->data.sex << "\t" << temp->data.tel <<"\n";
		temp = temp->pt_next;
	}
	fout.close();
	cout << "\t数据保存完毕!\n";
}
//读取记录
void LoadStudent(Pt_student &head)
{
	ifstream fin("students.txt");
	int i = 0;
	while (fin >>MyName>>MySex>>MyTel)
	{
		Pt_student temp = (Pt_student)malloc(sizeof(T_student));
		strcpy(temp->data.name, MyName);
		strcpy(&temp->data.sex, &MySex);
		strcpy(temp->data.tel, MyTel);
		temp->pt_next = head->pt_next;
		head->pt_next = temp;
		i++;
	}
	if (i == 0)
	{
		cout << "加载失败,目前文档内没有数据!" << endl;
		fin.close();
	}
	else
	{
		fin.close();
		cout << "已成功加载" << i << "行数据" << endl;
	}

}

int main()
{
	int choice;
	//构建一个带头结点的单链表
	Pt_student head;
	head = (Pt_student)malloc(sizeof(T_student));
	head->pt_next = NULL;

	LoadStudent(head);	//导入文件中的通信信息
	while (1)
	{
		system("cls");
		Menu();
		cout << "\t请输入你的选择(1-7):";
		cin >> choice;
		if (!(0==choice || 1==choice ||2==choice||3==choice||4==choice||5==choice||6==choice))
		{
			MessageBox(0, _T("输入错误,请重新输入"), _T("警告"), MB_OK|MB_ICONEXCLAMATION);
			continue;
		}	
		switch (choice)
		{
		case 1:
			system("cls");
			AddStudnet(head);
			ClearFileStudent();
			SaveStudent(head);
			break;
		case 2:
			system("cls");
			DelStudent(head);
			ClearFileStudent();
			SaveStudent(head);
			break;
		case 3:
			system("cls");
			UpdateStudent(head);
			ClearFileStudent();
			SaveStudent(head);
			break;
		case 4:
			system("cls");
			Querystudent(head);
			break;
		case 5:
			system("cls");
			ClearFileStudent();
			ClearListStudent(head);
			break;
		case 6:
			system("cls");
			DisplayStudent(head);
			break;
		case 7:
			system("cls");
			exit(0);
		default:
			break;
		}
	}
	return 0;
}

下面是运行的截图
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值