c语言实战项目:学生信息管理系统
一、题目:
根据要求编写一个学生管理系统
1、能够录入学生的姓名、年龄、性别、电话号码
2、能够实现删除功能
3、能够以输入姓名的方式查找学生
4、能够修改学生信息
5、能够打印学生信息
二、功能实现及其源代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<String.h>
#define _CRT_SECURE_NO_WARNINGS
struct student
{
char name[20];
int age;
char sex[5];
char tel[20];
};
struct Node
{
struct student data;
struct Node* next;
};
char n[20];//名字
char s[5];//性别
char t[5];//电话号码
int a;//年龄
//数据结构设计-----》》学生信息输出
//菜单设计
void menu()
{ //所有操作都同步到文件
printf(“[学生信息管理系统]\n");
printf("\t\t0.退出系统\n");
printf("\t\t1.录入信息\n");
printf("\t\t2.浏览信息\n");
printf("\t\t3.修改信息\n");
printf("\t\t4.删除信息\n");
printf("\t\t5.查找信息\n");
printf("******************\n”);
}
//用户的交互
//创建表头
struct Node* createList()
{
//结构体变量表示表头
//指针转变成变量 动态内存申请
struct Node* headNode=(struct Node*)malloc(sizeof(struct Node));
headNode->next=NULL;
return headNode;
}
//创建节点
struct Node* createNode(struct student data)
{
struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
newNode->data=data;
newNode->next=NULL;
return newNode;
}
//插入节点
void insertNodeByHead(struct Node* headNode,struct student data)
{
struct Node* newNode=createNode(data);//调用创建节点函数
//表头法
newNode->next=headNode->next;
headNode->next=newNode;
}
//指定位置删除
void deleteAppoinNode(struct Node* headNode,char* name)
{
struct Node* posNode=headNode->next;
struct Node* posFrontNode=headNode;
if (posNodeNULL)
{
printf(“数据为空,无法删除!\n”);
return;
}
while(strcmp(posNode->data.name,name))
{
posFrontNode=posNode;
posNode=posFrontNode->next;
if(posNodeNULL)
{
printf(“没有找到位置,无法删除!\n”);
}
}//退出循环,证明找到了指定点
posFrontNode->next=posNode->next;
free(posNode);
}
//查找功能
struct Node* searInfoByData(struct Node* headNode,char* name)
{
struct Node* pMove=headNode->next;
if(pMoveNULL)
return NULL;
while(strcmp(pMove->data.name,name))
{
pMove=pMove->next;
}
return pMove;
}
//文件读操作
void readInfoFromFile(struct Node* headNode,char *fileName)
{
//1.打开
FILE *fp;
struct student data;
fp=fopen(fileName,“r”);
if(fpNULL)
{
fopen(fileName,“W+”);
}
//2.读文件
while(fscanf(fp,"%s\t%d\t%s\t%s\n",data.age,&data.age,data.sex,data.tel)!=EOF)
{
insertNodeByHead(headNode,data);
}
//3.关闭文件
fclose(fp);
}
//文件写操作
void writeInfoToFile(struct Node* headNode,char fileName)
{
FILE fp;
fp=fopen(fileName,“w”);
if(fp==NULL)
{
printf(“打开文件失败!”);
return;
}
struct Node pMove=headNode->next;
while(pMove)
{
fprintf(fp,"%s\t%d\t%s\t%s\n",pMove->data.name,pMove->data.age,pMove->data.sex,pMove->data.tel);
pMove=pMove->next;
}
fclose(fp);
}
struct Node list=createList();
//修改信息
void changeList(struct Node* headNode,char* name)
{
int choice=0;
struct Node* pMove=searInfoByData(list,name);
if(pMove==NULL)
{
printf("没有查到该名同学,无法修改信息!");
system("pause");
}
else
{
printf("\t\t0.修改姓名\n");
printf("\t\t1.修改年龄\n");
printf("\t\t2.修改性别\n");
printf("\t\t3.修改电话号码\n");
scanf("%d",&choice);
struct Node* pmove=list->next;
switch(choice)
{
case 0:
printf("请输入要修改的姓名:\n");
scanf("%s",n);
strcpy(pmove->data.name,n);
printf("修改姓名成功!\n");
break;
case 1:
printf("请输入要修改的年龄:\n");
scanf("%d",a);
pmove->data.age=a;
printf("修改年龄成功!\n");
break;
case 2:
printf("请输入要修改的性别:\n");
scanf("%s",s);
strcpy(pmove->data.sex,s);
printf("修改性别成功!\n");
break;
case 3:
printf("请输入要修改的电话号码:\n");
scanf("%s",t);
strcpy(pmove->data.tel,t);
printf("修改电话号码成功!\n");
break;
default:
printf("********没有这项信息修改!************\n");
system("pause");
break;
}
}
}
//打印链表
void printList(struct Node* headNode)
{
struct Node* pMove=headNode->next;
printf(“姓名\t年龄\t性别\t电话\n”);
while(pMove)
{
printf("%s\t%d\t%s\t%s\n",pMove->data.name,pMove->data.age,pMove->data.sex,pMove->data.tel);
pMove=pMove->next;
}
printf("\n");
}
//根据所选的菜单,做相应的事情
void keyDown()
{
int choice = 0;
struct student data;
struct Node* pMove=NULL;
scanf("%d",&choice);
switch (choice)
{
case 0:
printf("*正常退出系统!\n");
system(“pause”);
exit(0);
break;
case 1:
printf("---------------录入信息------------\n");
//插入链表
printf(“请输入学生姓名,年龄,性别,电话:\n”);
fflush(stdin);//清空缓冲区
scanf("%s%d%s%s",data.name,&data.age,data.sex,data.tel);
insertNodeByHead(list,data);
break;
case 2:
printf("---------------浏览信息------------\n");
printList(list);
break;
case 3:
printf("---------------修改信息------------\n");
printf(“请输入要修改学生信息的姓名:\n”);
scanf("%s",data.name);
changeList(list,data.name);
break;
case 4:
printf("---------------删除信息------------\n");
printf(“请输入要删除学生的姓名\n”);
scanf("%s",data.name);
deleteAppoinNode(list,data.name);
break;
case 5:
printf("---------------查找信息------------\n");
printf(“请输入要查找学生的姓名:\n”);
scanf("%s",data.name);
pMove=searInfoByData(list,data.name);
if(pMove==NULL)
{
printf(“没有查到相关信息,无法删除!”);
system(“pause”);
}
else
{
printf(“姓名\t年龄\t性别\t电话\n”);
printf("%s\t%d\t%s\t%s\n",pMove->data.name,pMove->data.age,pMove->data.sex,pMove->data.tel);
}
break;
default:
printf(“没有这个功能选项,请重新录入信息!****\n”);
system(“pause”);
break;
}
writeInfoToFile(list,“1.txt”);
}
int main()
{
readInfoFromFile(list,“1.txt”);
while (1)
{
menu();
keyDown();
system(“pause”);//暂停,防止屏幕一闪而过
system(“cls”);//清屏
return 0;
}
}
**三、**运行结果如下