写一个学生信息管理系统
要求包括开始进入系统加载存储的学生目录
目录结构体类型包含 成员学生的姓名,学号,年龄,电话,家庭住址
功能增删改查,插入,销毁,创建,保存。
逐步更新需要链表之类的,这个进阶的没有继续学习,目前在学习cpp以及一些硬件知识。
一、增:如果目录为空:count=0,就直接加在初始位置。如果不为空,就加在后续位置
二、查:按学号来查找,并返回索引(+1)
三、删:先查找是否有这个人,然后判断是否删除
四、改:查找是否有此人,再决定更改
五、插入:查找要插入的索引是否大于等于count,如果真,让他接到最后一个位置后面
小于的话让数据后移,空出插入的位置。
六、销毁:令count=0,无法访问该结构体数组
七、创建:就是从txt文件里加载所有内容
八、保存:将所有内容保存到txt文件内(以二进制形式)
本代码运行在Visual studio 2022没有错误,但是有38个警告。
哈哈,我也不去改了。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<Windows.h>
#include"apublic.h"
void downloadStu();
void showMenu();
void showStu();
void AddStu();
int FindStu();
void DestoryStu();
void InsertStu();
void DeleteStu();
void ModifStu();
void SaveStu();
int main()
{
downloadStu();
while (1)
{
showMenu();
int num;
printf("Please choose one target!\n");
scanf_s("%d", &num);
switch (num)
{
case 1: {showStu(); system("pause"); system("cls"); } break;
case 2: {AddStu(); system("pause"); system("cls"); } break;
case 3: {FindStu(); system("pause"); system("cls"); }break;
case 4: {ModifStu(); system("pause"); system("cls"); }break;
case 5: {DeleteStu(); system("pause"); system("cls"); }break;
case 6: {InsertStu(); system("pause"); system("cls"); }break;
case 7: {DestoryStu(); system("pause"); system("cls"); }break;
case 8: {SaveStu(); system("pause"); system("cls"); }break;
case 0:exit(0);
break;
default: {printf("ERROR! Please choose a number :0~8!\n"); system("pause"); system("cls");
}
break;
}
}
return 0;
}
void downloadStu()
{
FILE* fp;
int i, MaxSize = 100;
if (fopen_s(&fp, "student_Manage.txt", "rb") != NULL) //返回的是NULL就是创建失败
{
printf("cannot open file\n");
exit(0);
}
for (i = 0; i < MaxSize; i++)
{
fread(&stu[i], sizeof(struct Student), 1, fp);
if (stu[i].age == 0 )
{
break;
}
count++;
}
printf("READ OK!\n");
fclose(fp);
}
void showMenu()
{
printf("************************* This is a students's menu!********************\n");
printf("************************************************************************\n");
printf("** 1:显示学生目录 **\n");
printf("** 2:添加学生目录 **\n");
printf("** 3:查询学生目录 **\n");
printf("** 4:修改学生目录 **\n");
printf("** 5:删除学生目录 **\n");
printf("** 6:插入学生目录 **\n");
printf("** 7:销毁学生目录 **\n");
printf("** 8:保存学生目录 **\n");
printf("** 0:退出学生目录 **\n");
printf("************************************************************************\n");
}
void showStu()
{
if (0 == count)
{
printf("There is no anything!\n");
}
else {
for (int i = 0; i < count; i++)
{
printf("\r\nNO:%s name:%s age:%2d tel:%11s add:%s\r\n", stu[i].number, stu[i].name, stu[i].age, stu[i].telphone, stu[i].place);
}
}
}
void AddStu()
{
int choice;
while (1)
{
printf("Add another one student? y/n:(1/0)\n");
scanf_s("%d", &choice);
if (choice == 1)
{
count++;
printf("Please input NO:");
scanf_s("%s", stu[count - 1].number, sizeof(stu[count - 1].number));
printf("\nPlease input name:");
scanf_s("%s", stu[count - 1].name, sizeof(stu[count - 1].name));
printf("\nPlease input age:");
scanf_s("%d", &stu[count - 1].age);
printf("\nPlease input telphone:");
scanf_s("%s", stu[count - 1].telphone, sizeof(stu[count - 1].telphone));
printf("\nPlease input address:");
scanf_s("%s", stu[count - 1].place, sizeof(stu[count - 1].place));
printf("Add OK!\n");
}
else if (choice == 0)
{
break;
}
else
{
printf("ERROR!Please input 1/0!\n");
}
}
}
int FindStu()
{
int temp = -1;
char mNumber[30];
printf("Please input Number:\n");
scanf_s("%s", mNumber, sizeof(mNumber));
for (int i = 0; i < count; i++)
{
if (strcmp(mNumber, stu[i].number) == 0)
{
printf("Find! Index is %d\n", i + 1);
printf("\r\nNO:%s name:%s age:%2d tel:%11s add:%s\r\n", stu[i].number, stu[i].name, stu[i].age, stu[i].telphone, stu[i].place);
temp = i + 1;
}
}
if (temp == -1)
printf("None Find!\n");
return temp;
}
void ModifStu()
{ //按学号查找后修改,如果学号错了,查找就失效
int temp, choice;
temp = FindStu();
if (temp != -1)
{
printf("Confirm Modify? y/n(1/0)!\n");
scanf_s("%d", &choice);
if (choice == 1)
{
printf("Please input NO:");
scanf_s("%s", stu[temp - 1].number, sizeof(stu[temp - 1].number));
printf("\nPlease input name:");
scanf_s("%s", stu[temp - 1].name, sizeof(stu[temp - 1].name));
printf("\nPlease input age:");
scanf_s("%d", &stu[temp - 1].age);
printf("\nPlease input telphone:");
scanf_s("%s", stu[temp - 1].telphone, sizeof(stu[temp - 1].telphone));
printf("\nPlease input address:");
scanf_s("%s", stu[temp - 1].place, sizeof(stu[temp - 1].place));
printf("Modify OK!\n");
}
}
else
{
printf("Please input right number!\n");
}
}
void InsertStu()
{
int index;
printf("Please input insert index! index:1~500\n");
scanf_s("%d", &index);
if (index > count)
{
if (index <= 500)
{
count++;
printf("Please input NO:");
scanf_s("%s", stu[count - 1].number, sizeof(stu[count - 1].number));
printf("\nPlease input name:");
scanf_s("%s", stu[count - 1].name, sizeof(stu[count - 1].name));
printf("\nPlease input age:");
scanf_s("%d", &stu[count - 1].age);
printf("\nPlease input telphone:");
scanf_s("%s", stu[count - 1].telphone, sizeof(stu[count - 1].telphone));
printf("\nPlease input address:");
scanf_s("%s", stu[count - 1].place, sizeof(stu[count - 1].place));
printf("Insert OK!\n");
}
else
{
printf("Beyond Boundary!\n");
}
}
else
{
for (int i = count; i > (index - 1); i--)
{
stu[i] = stu[i - 1];
}
printf("Please input NO:");
scanf_s("%s", stu[index - 1].number, sizeof(stu[index - 1].number));
printf("\nPlease input name:");
scanf_s("%s", stu[index - 1].name, sizeof(stu[index - 1].name));
printf("\nPlease input age:");
scanf_s("%d", &stu[index - 1].age);
printf("\nPlease input telphone:");
scanf_s("%s", stu[index - 1].telphone, sizeof(stu[index - 1].telphone));
printf("\nPlease input address:");
scanf_s("%s", stu[index - 1].place, sizeof(stu[index - 1].place));
printf("Insert OK!\n");
count++;
}
}
void DeleteStu()
{
char number[30];
int temp = -1, choice;
printf("Please input number!\n");
scanf_s("%s", number, sizeof(number));
for (int i = 0; i < count; i++)
{
if (strcmp(number, stu[i].number) == 0)
{
printf("Find! Index is %d\n", i + 1);
printf("\r\nNO:%s name:%s age:%2d tel:%11s add:%s\r\n", stu[i].number, stu[i].name, stu[i].age, stu[i].telphone, stu[i].place);
temp = i + 1;
printf("Confirm Delete? y/n(1/0)!\n");
scanf_s("%d", &choice);
if (choice == 1)
{
if (temp == count)
{
stu[temp - 1] = stu[temp];
}
else
{
for (int i = temp - 1; i < count; i++)
{
stu[i] = stu[i + 1];
}
}
count--;
}
else
{
break;
}
}
}
if (temp == -1)
printf("None Find!\n");
}
void DestoryStu()
{
int choice;
while (1)
{
printf("Cofirm Destory? y/n(1/0)\n");
scanf_s("%d", &choice);
if (choice == 1)
{
count = 0;
printf("Destory OK!\n");
break;
}
else if (choice == 0)
{
break;
}
else {
printf("Please input 1/0 !\n");
}
}
}
void SaveStu()
{
FILE* fp;
int i, MaxSize = 100;
if (fopen_s(&fp,"student_Manage.txt", "wb") != NULL)
{
printf("cannot open file\n");
exit(0);
}
for (i = 0; i < count; i++)
{
if (fwrite(&stu[i], sizeof(struct Student), 1, fp) != 1)
printf("file write error\n");
printf("Write OK count:%d", i + 1);
}
fclose(fp);
}
#pragma once
#ifndef __APUBULIC_H
#define __APUBULIC_H
struct Student
{
int age;
char name[30];
char number[30];
char telphone[30];
char place[30];
}stu[100];
int count = 0;
#endif // !__APUBULIC_H