该学生管理系统的实现是通过分文件的方式来写的,infor.h
文件为头文件,源文件infor.c实现源文件test.c中封装函数,建议
读者在做较大的系统是分文件来实现,可以提高代码的运行效率。
(该学生管理系统由b站鹏哥c语言通讯录视频改进而来)
infor.h
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <windows.h>
#define MAX_NAME 20
#define MAX_SEX 10
#define MAX_TELE 15
#define MAX_ADDR 30
#define begin 3
#define add 2
typedef struct Student
{
char name[MAX_NAME];
char sex[MAX_SEX];
int age;
char tele[MAX_TELE];
char addr[MAX_ADDR];
}Student;
typedef struct student
{
Student*data;
int sz;
int capacity;
}student;
void Initstudent(student*pc);
void ADDstudent(student* pc);
void PRINTstudent(student* pc);
void SEARCHstudent(student* pc);
void MODIFYstudent(student* pc);
void DELETEstudent(student* pc);
void DESETORYstudent(student* pc);
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"infor.h"
void menu()
{
system("color fc");
printf("\n");
printf("\n");
printf("\n");
printf(" ***********郑州轻工业大学学生信息管理系统************\n");
printf(" *-------0.退出 1.增加学生信息-------*\n");
printf(" *-------2.浏览学生信息 3.查找学生信息-------*\n");
printf(" *-------4.修改学生信息 5.删除学生信息-------*\n");
printf(" *****************************************************\n");
printf("\n");
printf("\n");
printf("\n");
}
enum option
{
EXIT,
ADD,
PRINT,
SEARCH,
MODIFY,
Delete
};
int main()
{
int input = 0;
student stu;
Initstudent(&stu);
do
{
menu();
printf("请选择:>\n");
scanf("%d", &input);
switch(input)
{
case EXIT:
DESETORYstudent(&stu);
printf("退出系统\n");
break;
case ADD:
ADDstudent(&stu);
break;
case PRINT:
PRINTstudent(&stu);
break;
case SEARCH:
SEARCHstudent(&stu);
break;
case MODIFY:
MODIFYstudent(&stu);
break;
case Delete:
DELETEstudent(&stu);
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
return 0;
}
infor.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"infor.h"
void Initstudent(student* pc)
{
pc->data = (Student*)calloc(begin,sizeof(Student));
pc->sz = 0;
pc->capacity = begin;
}
void ADDstudent(student* pc)
{
if (pc->sz == pc->capacity)
{
Student* ptr = (Student*)realloc(pc->data, (pc->capacity + add)*sizeof(Student));
if (ptr != NULL)
{
pc->data = ptr;
pc->capacity += add;
printf("~~~增容成功~~~\n");
}
else
{
perror("ADDstudent");
printf("增加联系人失败\n");
return;
}
}
printf("请输入姓名:\n");
scanf("%s", pc->data[pc->sz].name);
printf("请输入性别:\n");
scanf("%s", pc->data[pc->sz].sex);
printf("请输入年龄:\n");
scanf("%d", &(pc->data[pc->sz].age));
printf("请输入电话:\n");
scanf("%s", pc->data[pc->sz].tele);
printf("请输入地址:\n");
scanf("%s", pc->data[pc->sz].addr);
pc->sz++;
printf("增加成功\n");
}
void DESETORYstudent(student* pc)
{
free(pc->data);
pc->data = NULL;
pc->capacity = 0;
pc->sz = 0;
}
void PRINTstudent(student* pc)
{
int i = 0;
printf("%-10s%-10s%-10s%-15s%-30s\n", "姓名", "性别", "年龄", "电话", "地址");
for (i = 0; i < pc->sz; i++)
{
printf("%-10s%-10s%-10d%-15s%-30s\n",
pc->data[i].name,
pc->data[i].sex,
pc->data[i].age,
pc->data[i].tele,
pc->data[i].addr
);
}
}
static int Findbyname(student* pc, char name[])
{
int i = 0;
for (i = 0; i < pc->sz; i++)
{
if (strcmp(pc->data[i].name, name) == 0)
{
return i;
}
}
return -1;
}
void SEARCHstudent(student* pc)
{
char name[MAX_NAME] = { 0 };
printf("请输入要查找学生姓名:\n");
scanf("%s", name);
int end = Findbyname(pc, name);
if (end == -1)
{
printf("你所要查找的人不存在");
}
else
{
printf("%-10s%-10s%-10s%-15s%-30s\n", "姓名", "性别", "年龄", "电话", "地址");
printf("%-10s%-10s%-10d%-15s%-30s\n",
pc->data[end].name,
pc->data[end].sex,
pc->data[end].age,
pc->data[end].tele,
pc->data[end].addr
);
}
}
void MODIFYstudent(student* pc)
{
char name[MAX_NAME] = { 0 };
printf("请输入要修改学生信息的学生姓名:\n");
scanf("%s", name);
int end = Findbyname(pc, name);
if (end == -1)
{
printf("你所要修改学生信息的人不存在");
}
else
{
printf("请输入性别:\n");
scanf("%s", pc->data[end].sex);
printf("请输入年龄:\n");
scanf("%d", &(pc->data[end].age));
printf("请输入电话:\n");
scanf("%s", pc->data[end].tele);
printf("请输入地址:\n");
scanf("%s", pc->data[end].addr);
printf("修改成功\n");
}
}
void DELETEstudent(student* pc)
{
char name[MAX_NAME] = { 0 };
printf("请输入要删除学生信息的学生姓名:\n");
scanf("%s", name);
int end = Findbyname(pc, name);
if (end == -1)
{
printf("你所要删除学生信息的人不存在");
}
int i = 0;
for (i = end; i < pc->sz - 1; i++)
{
pc->data[i] = pc->data[i + 1];
}
pc->sz--;
printf("删除成功");
}
|