#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<fstream>
#include<conio.h>
using namespace std;
const int maxx = 4;
struct student {
int idnum;
char name[40];
char sex;
int age;
}stu[maxx];//最多添加4个学生
void choice();
void dell();
void xiugai();
void baocun();
void display()
{

cout << "-----------------------学生管理菜单----------------------\n";
cout << "1 添加学生" << " " << "2 删除学生 3 查询学生信息 4修改信息 5保存 6退出" << endl;
}
void init()
{
//初始化函数
for (int i = 0; i < maxx; i++)
{
stu[i].name[40] = '\0';
stu[i].sex = '\0';
stu[i].age = 0;
}
}
void add()
{
int k = 0, j;
for (int i = 0; i < maxx; i++)
{
if (stu[i].sex == '\0') k++;
}
cout << "剩余" << k << "个记录可以添加\n";
cout << "请输入你要添加的学生个数;\n";
cin >> j;
if (j > k) {
cout << "error number";
cout << endl;
display();
choice();
}
else
for (int i = 0; i < j; i++)
{
for (int h = 0; h < maxx; h++)
{
if (stu[h].sex == '\0')
{
cout << "请输入第" << i + 1 << "个学生学号 姓名 性别(用m表示男 w表示女)年龄" << endl;
cin >> stu[h].idnum >> stu[h].name >> stu[h].sex >> stu[h].age;
for (int g = 0; g < maxx; g++)
{
if (stu[g].idnum == stu[h].idnum && g != h)
{
cout << "该学号不能添加" << endl;
display();
}

}
if (stu[h].sex != 'w' && stu[h].sex != 'm')
{
stu[h].sex = '\0';
cout << "性别输入错误\n";
display();
}
if (stu[h].age < 1 || stu[h].age >100)
{
stu[h].sex = '\0';
cout << "年龄输入错误" << endl;
}
break;
}
}
cout << "\n学生添加成功\n";

}
display();
choice();
}
void dell()
{
//删除功能
int id,count=0;
cout << "您想删除的学生学号:\n";
cin >> id;
for (int i = 0; i < maxx; i++)
{
if (stu[i].idnum == id)
{
count++;
stu[i].idnum = -1;
stu[i].name[40] = '\0';
stu[i].sex = '\0';
stu[i].age = 0;
cout << "删除成功" << endl;
break;


}
}
if (count == 0)
{
cout << "该学号不存在\n";

}
display();
choice();
}


void inqui()
{
int id , count = 0;
cout << "您想查询的学生学号(输入0查看所有学生):\n";
cin >> id;
if (id == 0)
{
for (int i = 0; i < maxx; i++)
{
if (stu[i].sex != '\0')
cout << stu[i].idnum << " " << stu[i].name << " " << stu[i].sex << " " << stu[i].age << " \n";
}
}
else {
for (int i = 0; i < maxx; i++)
{
if (stu[i].idnum == id)
{
count++;
cout << stu[i].idnum << " " << stu[i].name << " " << stu[i].sex << " " << stu[i].age << " ";
}
}
if (count == 0)
cout << "不存在这个学号的学生!\n";
}
display();
choice();
}
void xiugai()
{
int count = 0;
cout << "您想修改的学生学号:\n";
int id;
cin >> id;
int i;
for (i = 0; i < maxx; i++)
{
if (stu[i].idnum == id)
{
count++;
cout << "您要修改的学生信息:";
cout << stu[i].idnum << " " << stu[i].name << " " << stu[i].sex << " " << stu[i].age << " \n";
cout << "请重新输入信息 按学号 姓名 性别 年龄输入;\n";
cin >> stu[i].idnum >> stu[i].name >> stu[i].sex >> stu[i].age;
cout << "已经修改";

break;
}
}
if (count == 0)
cout << "该学号不存在!\n"; \
display();
choice();
}
void baocun()
{

FILE* mysfile = NULL;
mysfile = fopen("ooo.txt", "r");
for (int i = 0; i < maxx; i++) {
fscanf(mysfile, "%d", &stu[i].idnum);
fscanf(mysfile, "%s",stu[i].name);
fscanf(mysfile, "%s", stu[i].sex);
fscanf(mysfile, "%d", stu[i].age);

}
cout << "信息已经保存\n";
fclose(mysfile);
display();
choice();
}

void exit()
{
system("cls");
exit(1);

}

void choice()
{
int a;
cout << "请选择功能:\n";
cin >> a;
if (a == 1 || a == 2 || a == 3 || a == 4)
{
switch (a)
{
case 1:add();
case 2: dell(); break;
case 3:inqui(); break;
case 4:xiugai(); break;
case 5:baocun(); break;
case 6:exit(); break;
default:
cout << "输入选择有错误!\n";
break;
}
}
}

int main()
{
init();
display();
choice();


}