班级管理系统
废话不多,看代码。原创。有疑问提问
代码
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#define MAX 20
#define Size 10
typedef struct student{
char score[MAX]; //学号
char name[MAX];
char age[MAX];
char phone[MAX];
char located[MAX]; //地址
struct student *next;
}stu;
stu *head;
//输入学生信息
void Insert(){
FILE *fp;
if((fp=fopen("studata","w"))==NULL){
printf("打开失败!\n");
exit(0);
}
stu *pNew;
char ch;
int n;
printf("请输入要插入的条数:\n");
scanf("%d",&n);
for(int i=1;i<=n;i++){
if(i==1){
head=(stu*)malloc(sizeof(stu));
printf("请输入学生学号,姓名,年龄,手机号,家庭住址:\n");
scanf("%s %s %s %s %s",head->score,head->name,head->age,head->phone,head->located);
fprintf(fp,"%s %s %s %s %s\n",head->score,head->name,head->age,head->phone,head->located);
head->next=NULL;
}else{
pNew=(stu*)malloc(sizeof(stu));
printf("请输入学生学号,姓名,年龄,手机号,家庭住址:\n");
scanf("%s %s %s %s %s",pNew->score,pNew->name,pNew->age,pNew->phone,pNew->located);
fprintf(fp,"%s %s %s %s %s\n",pNew->score,pNew->name,pNew->age,pNew->phone,pNew->located);
pNew->next=head->next;//pNew的后与head->next的前相连
head->next=pNew; //head的厚与pNew的前相连
//头插法
}
}
fclose(fp);
}
//遍历
void Out(){
FILE *fp;
if((fp=fopen("studata","r"))==NULL){
printf("无法打开文件!\n");
exit(0);
}
stu *p=head;
printf("学号 姓名 年龄 手机号 家庭住址");
while(p){
printf("\n");
fscanf(fp,"%s %s %s %s %s",p->score,p->name,p->age,p->phone,p->located); //从studata文件中把信息读出来
fprintf(stdout,"%s%7s%10s%10s%11s\n",p->score,p->name,p->age,p->phone,p->located);// 把读出来的信息输出到显示器上
p=p->next;
}
fclose(fp);
}
//删除 e
void dele(){
FILE *fp;
if((fp=fopen("studata","w"))==NULL){
printf("无法打开文件!\n");
exit(0);
}
stu *p=head,*temp=NULL,*q=NULL;
char ch;
char score1[MAX];
printf("\n请输入要删除的学生的学号:\n");
getchar();
scanf("%s",score1);
while(p){
if(!strcmp(p->score,score1)){
break;
}else{
temp=p; // temp是p的前驱结点
p=p->next;
}
}
if(!p){
printf("\n不存在该学生的信息.\n");
exit(0);
}
printf("该学生的信息为:\n");
printf("%5s %5s %5s %5s %5s\n",p->score,p->name,p->age,p->phone,p->located);
//
if(p==head){
q=head;
head=head->next;
free(q);
}else{
q=p; //要删除的p接点用p换下来
temp->next=p->next; //把p的前驱temp与它的后继p->next连上
free(q); //删除该结点
}
printf("\n已经删除该学生的信息.");
p=head;
while(p){
fprintf(fp,"%s %s %s %s %s\n",p->score,p->name,p->age,p->phone,p->located);//删除后要让修改后的数据重新存入文件
p=p->next;
}
fclose(fp);
}
int main(){
Insert();
Out();
dele();
return 0;
}
//输入:101 ma 123 1244 qq
// 102 xing 1234 434 ww
// 103 ru 234 44 ee
// 再删除一个即可