学生管理系统plus初始化(可保存文件到桌面)

#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#include
typedef long long datestr;
using namespace std;
typedef struct student {//结构体创建
	char name[40];
	datestr id;
	char sex;
	char major[40];
	/*struct grade gradex;*/
	student* next;
}student, * stup;
struct grade {//成绩结构体创建
	int c_language;
	int date_structure;
	int computer_concept;
};
void UI();
void changeUI();
void print(student* fp);
void scan(student* fp);
void help();
void show(student* p);
student* add(student* list);
student* findx(student* findp, datestr idx);
student* deletelist(student* headx, datestr delid);
void amend(student* changep);
enum
{
	COMMAND_SHOW = 1,
	COMMAND_ADD,
	COMMAND_DELETE,
	COMMAND_CHANGE,
	COMMAND_SEARCH,
	OMMAND_HELP,
	COMMAND_QUIT
};
void UI()//下一步提示
{
	cout << "请选择下一步操作" << endl;
}
void changeUI()
{
	printf("<><><><><><><><><><><><><><>\n");
	printf("$$$$输入【1】修改学生学号$$$$\n");
	printf("%%%%%%%%输入【2】修改学生姓名%%%%%%%%\n");
	printf("@@@@输入【3】修改学生性别@@@@\n");
	printf("&&&&输入【4】修改学生专业&&&&\n");
	printf("####输入【5】修改完成----####\n");
	printf("<><><><><><><><><><><><><><>\n");
}
void print(student* fp)//打印学生信息函数
{
	/*cout << "name is " << p.name << " id is " << p.id << " sex is " << p.sex << " major is " << p.major << endl;/*| c-language:%4d
	| date-structure:%4d | computer-concept:%4d*/
	printf("name: %8s | id: %15lld | sex:%c | major:%16s \n",
		fp->name, fp->id, fp->sex, fp->major/*,fp->gradex.c_language,fp->gradex.date_structure,fp->gradex.computer_concept*/);
}
void scan(student* fp)//输入数据函数;
{
	printf("请输入学生的名字\n");
	scanf(" %s", &(fp->name));
	printf("请输入学生的学号\n");
	scanf("%lld", &(fp->id));
	printf("请输入学生的性别\n");
	scanf(" %c", &(fp->sex));
	printf("请输入学生的专业\n");
	scanf(" %s", &(fp->major));
}
void help()//菜单m
{
	printf("---------------------------------\n");
	printf("|---输入【1】显示所有学生信息---|\n");
	printf("|---输入【2】添加一个学生信息---|\n");
	printf("|---输入【3】删除一个学生信息---|\n");
	printf("|---输入【4】修改一个学生信息---|\n");
	printf("|---输入【5】查找一个学生信息---|\n");
	printf("|---输入【6】显示帮助*******----|\n");
	printf("|---输入【7】退出/保存文件**----|\n");
	cout << "_________________________________\n" << endl;

}
void show(student* p)//显示学生信息
{
	if (p == NULL)
	{
		cout << "没有学生,请先添加学生" << endl;
	}
	while (p != NULL)
	{
		print(p);
		p = p->next;
	}
}
student* findx(student* findp, datestr idx)//查找学生信息
{
	while (findp != NULL)
	{
		if (findp->id == idx)
		{
			return findp;
			break;
		}
		findp = findp->next;
	}
	if (findp == NULL)
	{
		return NULL;
	}
}
student* deletelist(student* headx, datestr delid)
{
	student* fdelp = headx;
	student* delp = fdelp->next;
	while (delp != NULL)
	{
		if (delp->id == delid)
		{
			return fdelp;
		}
		fdelp = fdelp->next;
		delp = delp->next;
	}
	//cout << "没有查找到此学生,请重新输入" << endl;
	return NULL;

}
void amend(student* changep)
{
	changeUI();
	int x, breakx = 0;
	while (breakx == 0)
	{
		scanf("%d", &x);
		switch (x)
		{
		case 1:
		{
			printf("请输入修改后的学号\n");
			scanf("%lld", &changep->id);
			printf("修改成功\n");
			break;

		}
		case 2:
		{
			cout << "请输入修改后的名字" << endl;
			scanf(" %s", &changep->name);
			printf("修改成功\n");
			break;
		}
		case 3:
		{
			cout << "请输入修改后的性别" << endl;
			scanf(" %c", &changep->sex);
			printf("修改成功\n");
			break;
		}
		case 4:
		{
			cout << "请输入修改后的专业" << endl;
			scanf(" %s", &changep->major);
			printf("修改成功\n");
			break;
		}
		case 5:
		{
			printf("已结束全部修改\n");
			print(changep);
			breakx = 1;
			break;
		}
		default: {
			printf("输入错误!!!\n请重新输入\n");
			break;
		}
		}
	}
}
student* add(student* endlist)
{
	student* nowlist;
	nowlist = (student*)malloc(sizeof(student));
	endlist->next = nowlist;
	scan(nowlist);
	nowlist->next = NULL;
	return nowlist;
}
int main()
{
	int scanfx = 0;//输入选择
	help();//显示菜单
	student head;
	head = { NULL,NULL,NULL,NULL };
	student* endlist;
	head.next = NULL;
	endlist = &head;
	while (scanfx != 7)
	{
		scanf("%d", &scanfx);
		switch (scanfx)
		{
		case COMMAND_SHOW://1显示学生信息
		{
			show((&head)->next);
			UI();
			break;
			system("pause");
		}//(1)完成
		case COMMAND_ADD://2添加学生信息
		{
			printf("请输入需要添加学生的信息\n");
			endlist = add(endlist);
			cout << "添加成功" << endl;
			UI();
			break;
		}//(2)完成
		case COMMAND_DELETE://3删除学生信息
		{
			cout << "请输入需要删除学生的信息" << endl;
			datestr delid;
			student* ffp;
			cin >> delid;
			ffp = deletelist(&head, delid);
			if (ffp != NULL)
			{
				ffp->next = ffp->next->next;
				//free(ffp->next);
				cout << "删除成功" << endl;
			}
			else cout << "没有查找到此学生,请重新输入" << endl;
			UI();
			break;
		}
		case COMMAND_CHANGE://4修改学生信息
		{
			int a = 0;
			while (a == 0)
			{
				printf("请输入需要修改学生的学号\n");
				datestr idx;
				scanf("%lld", &idx);
				student* changep;
				changep = findx(&head, idx);
				printf("请输入需要修改学生的信息\n");
				if (changep != NULL) {
					print(changep);
					amend(changep);
					a = 1;
				}
				else {
					printf("没有此学生,请重新输入\n");
					a = 0;
				}
			}

			UI();
			break;
		}//(4)完成
		case COMMAND_SEARCH://查找学生信息
		{
			datestr idx;
			cout << "请输入需要查找学生的学号" << endl;
			scanf("%lld", &idx);
			student* ffp;
			ffp = findx(&head, idx);
			if (ffp != NULL)
			{
				print(ffp);
				/*printf("name: % 6s | id : % 12lld | sex : % c |
				major : % 8s \n", ffp->name, ffp->id, ffp->sex, ffp->major);*/
			}
			else
			{
				printf("没有此学生,请重新输入\n");
			}
			UI();
			break;
		}//(5)完成
		case OMMAND_HELP://查看帮助
		{
			help();
			UI();
			break;
		}//(6)完成
		case COMMAND_QUIT://退出
		{
			char c;
			cout << "是否保存文件(y/n)" << endl;
			do
			{
				cin >> c;
				if (c == 'y'||c=='Y')
				{
					FILE* wp=fopen("E:\\Users\\Aeneas\\Desktop\\new_stu_info.txt", "a");
                    //文件地址填自己想保存的位置,否则无法运行
					student* p = (&head)->next;
					while(p!=NULL)
					{
						fprintf(wp, "name:%8s | id: %15lld | sex:%c | major:%16s \n",p->name,p->id,p->sex,p->major );
						p = p->next;
					}
					cout << "保存成功!" << endl;
					cout << "文件名:‘new_stu_info.txt’" << endl;
					cout << "地址为:E:\\Users\\Aeneas\\Desktop\\new_stu_info.txt" << endl;

				}
				else if (c == 'n'||c=='N')
				{
					cout << endl;
				}
				else
				{
					cout << "输入错误,请重新输入" << endl;
					c = '*';
				}
			} while (c=='*');
			break;
			system("pause");
		}//退出完成
		default:
		{
			printf("输入错误,请重新输入\n");
			UI();
		}//完成

		}
	}
	return 0;
}

注意:文件保存位置填自己想保存的位置否则无法运行

注:298行,文件位置

你可能感兴趣的:(学生管理系统)