C语言学生信息管理系统(文件操作)

测试数据(文件)(data.txt)

 000000001      张三       100        99        98     99.00
 000000002      马七         0         1         0      0.33
 000000003      王五        50        60        60     56.67
 000000004      李四        90        90        90     90.00
 000000005      赵六        30        50        80     53.33
 000000006      孙八        64        87        78     76.33
 000000008    王明明        63        66        69     66.00
 000000009      钱九        86        58        79     74.33
 000000007    李琳琳        68        80        69     72.33

代码段(详细参考注释,及我的资源包)

#include<stdio.h>
#include<windows.h>
#include<stdlib.h> 
#include<conio.h>
#include<string.h>
#define N 9 //文件
#define password "xyq"

//带头结点
typedef struct student
{
	char number[10];    
	char name[15];  
	int C;//Chinese
	int M;//Math
	int E;//English
	float ave;//self_ave
	struct student *next;
}stu;

int login();//密码  
void menu();//菜单  
stu *creatNode();//创建带头结点链表  
void add_infor(stu *head);//学生信息添加 
void seek(stu *head);//学生信息查询 
void output(stu *head);//输出数据  
void fail(stu *head);//输出挂科学生 
stu *NoSort(stu *head);//按学号排序 
stu *AveSort(stu *head);//按平均分排序 
void AveScore(stu *head,float a[]);//计算各科平均分 
void freeNode(stu *head);//销链 
void save(stu *head); //保存数据 
void save_ave(float a[]);//保存各科平均分数据 
stu *del_book(stu *head);///删除指定数据 

int main()
{
		//system("title 图书管理系统");
		//system("color E0") ;
    stu *head=NULL,*h;
    float a[3]={0};
    int num,last=0;//last 用来区别保存的数据
    if(login()==0)return 0;
    head=creatNode();
    printf("File reading complete,Please press any key to continue...\n");
    getch();//waiting
    system("cls");  
    while(menu(),scanf("%d",&num)!=EOF)
    {
       switch(num)
       {
           case 0:printf("end......\n");Sleep(1000);exit(0);
           case 1:output(head);break;
           case 2:fail(head);break;
           case 3:h=NoSort(head);output(h);freeNode(h);break;
           case 4:h=AveSort(head);output(h);freeNode(h);break;
           case 5:AveScore(head,a);printf("\n计算完毕...");break;
           case 6:seek(head);break;
           case 7:add_infor(head);output(head);printf("\n\n添加完成...\n\n");break;
           case 8:h=del_book(head);
					 				if(h){  output(h);printf("\n\n删除完成...\n\n");  }
						 			else {  printf("\n\n未找到...\n\n");              }
						 			break;
						 			
           case 9:  if(last==5)         
                        save(head);
                    else
                    {
                        if(last==1||last==7)    
                            save(head);
                        else
                        {
                            save(h);
                            free(h);
                        }
                    }
                    printf("\n保存完成啦~\n");break;
           default :printf("(〃>目<)请输入 0 ~ 8 的数字...");break;
       }
        printf("\nback~\n");
        getch();
        system("cls");
        last=num;
    }
    free(head);//释放
    return 0;
}

int login()//密码
{
    char key[N];
    printf("请输入密码->__");
    while(1)
    {
        scanf("%s",&key);
        if((strcmp(password,key)==0))
            return 1;
        else
        printf("one more time-> ");
    }//重复输入密码直到正确
}

void menu()//菜单
{
    printf("\n");
    printf("--------------------___学生信息管理系统___--------------------\n");
    printf("-----                                                   ------\n");
    printf("-----                        菜单                       ------\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("-----               [9]保存当前数据                     ------\n");
    printf("-----               [0]退出                             ------\n");
    printf("-----                                                   ------\n");
    printf("--------------------------------------------------------------\n");

}

stu *creatNode()//创建带头结点链表
{
    char number[10];    
		char name[15];  
		int C;//Chinese
		int M;//Math
		int E;//English
		float ave;//self_ave
    FILE *fp;
    if((fp=fopen("data.txt","r+"))==NULL)
    {
        printf("can not open this file\n");
        exit(0);
    }
    stu *h=NULL,*tail,*p;
    int i;
    h=(stu *)malloc(sizeof(stu));
    h->next=NULL;
    tail=h;
    for(i=0;i<N;i++)//可以用feof
    {
        p=(stu *)malloc(sizeof(stu));
        fscanf(fp,"%s%s%d%d%d%f",&number,&name,&C,&M,&E,&ave);
        strcpy(p->number,number);strcpy(p->name,name);//可以用fread
				p->C=C;p->M=M;p->E=E;p->ave=ave;
				p->next=NULL;
        tail->next=p;
        tail=p;
    }
    fclose(fp);
    return h;
}

void freeNode(stu *head)//销链
{
    stu *p=head;
    while(p)
    {
        head=head->next;
        free(p);
        p=head;
    }
}

void output(stu *head)//输出数据
{
	system("cls");
	stu *p=head->next;
	printf("\n\n--------------------------------------------------------------\n");
	printf("%10s%10s%10s%10s%10s%11s","学号","姓名","语文","数学","英语","平均分\n");
	while(p)
	{
		printf("%10s%10s%10d%10d%10d%10.2f\n",p->number,p->name,p->C,p->M,p->E,p->ave);
		p=p->next;
	}
}

void fail(stu *head)//输出挂科学生 
{
		system("cls");
    stu *p=head->next;
    printf("\n\n--------------------------------------------------------------\n");
    printf("%10s%10s%10s%10s%10s%11s","学号","姓名","语文","数学","英语","平均分\n");
    while(p)
    {
        if(p->C<60||p->E<60||p->M<60)
            printf("%10s%10s%10d%10d%10d%10.2f\n",p->number,p->name,p->C,p->M,p->E,p->ave);  
        p=p->next;
    }
}

stu *NoSort(stu *head)//按学号排序 
{
		system("cls");
    stu *p=head->next,*q,*h=NULL,*s;
    h=(stu *)malloc(sizeof(stu));
    h->next=NULL; 
    while(p)
    {
        q=h;
        s=(stu *)malloc(sizeof(stu));
        s->C=p->C;
				s->E=p->E;
				s->M=p->M;
				s->ave=p->ave;
				strcpy(s->name,p->name);
				strcpy(s->number,p->number);
				s->next=NULL;
        //插入排序
        if(h->next==NULL)
            h->next=s;
        else
        {
            while(q->next)
            {
                if(strcmp(q->next->number,s->number)>0) break;
                q=q->next;
            }
	            if(q->next==NULL)
	            q->next=s;
            else
            {
                s->next=q->next;
                q->next=s;
            }
        }
        p=p->next;
    }
    return h;
}

stu *AveSort(stu *head)//按平均分排序
{
		system("cls");
    stu *p=head->next,*q,*h=NULL,*s;
    h=(stu *)malloc(sizeof(stu));
    h->next=NULL;
    while(p)
    {
        q=h;
        s=(stu *)malloc(sizeof(stu));
        s->C=p->C;
				s->E=p->E;
				s->M=p->M;
				s->ave=p->ave;
				strcpy(s->name,p->name);
				strcpy(s->number,p->number);
				s->next=NULL;
        //插入排序
        if(h->next==NULL)
            h->next=s;
        else
        {
            while(q->next)
            {
                if(q->next->ave < s->ave) break;
                q=q->next;
            }
            if(q->next==NULL)
            q->next=s;
            else
            {
                s->next=q->next;
                q->next=s;
            }
        }
        p=p->next;
    }
    return h;
}

void AveScore(stu *head,float a[])//计算各科平均分 
{
		system("cls");
    stu *p=head->next;
    int i=0;
    printf("\n\n--------------------------------------------------------------\n");
    printf("%10s%10s%10s\n","语文","数学","英语");
		while(p)
		{
			i++;
			a[0]+=p->C ;
			a[1]+=p->M ;
			a[2]+=p->E ;
			p=p->next;
		}
		a[0]/=i;a[1]/=i;a[2]/=i;
		printf("%10.2f%10.2f%10.2f\n",a[0],a[1],a[2]);
}

void save(stu *head)//保存数据 
{
		system("cls");
    FILE *fp;
    stu *p=head->next;
    if((fp=fopen("data.txt","w+"))==NULL)
    {
        printf("can not save this file\n");
        exit(0);
    }
    while(p!=NULL)
    {
        fprintf(fp,"%10s%10s%10d%10d%10d%10.2f\n",p->number,p->name,p->C,p->M,p->E,p->ave);
				p=p->next;
    }
    fclose(fp);
}

void save_ave(float a[])//保存各科平均分信息 
{
	system("cls");
	FILE *fp;
	if((fp=fopen("data.txt","w+"))==NULL)
	{
		printf("打开文件失败\n");
		exit(0);
	}
	fprintf(fp,"%10.2f%10.2f%10.2f\n",a[0],a[1],a[2]);
	fclose(fp);
}

void seek(stu *head)//学生信息查询
{
    system("cls");
    stu *p;
    int n,flag;
    char ch[15];
    printf("--------------------------------------------------------------\n");
    printf("-----------------------想要按什么查询呢?---------------------\n");
    printf("-----------------------1 学号 or 2 姓名-----------------------\n");
    printf("-------------------------- 0 back ----------------------------\n");
    printf("--------------------------------------------------------------\n");
    printf("\n");
    while(scanf("%d",&n)!=EOF)
    {
    		flag=0; 
        p=head->next;
        switch(n)
        {
            case 1:
                printf("请输入要查询的学生的学号:\n");
                scanf("%s",&ch);
                while(p)
                {
                    if(strcmp(ch,p->number)==0)
                    {
                        flag=1;
                        printf("%10s%10s%10s%10s%10s%11s","学号","姓名","语文","数学","英语","平均分\n");
                        printf("%10s%10s%10d%10d%10d%10.2f\n",p->number,p->name,p->C,p->M,p->E,p->ave);
                        break;
                    }
                    p=p->next;
                }
                if(flag==0)
                    printf("未找到...");break;
            case 2:
                printf("请输入要查询的学生的姓名:\n");
                scanf("%s",&ch);
                while(p)
                {
                    if(strcmp(ch,p->name)==0)
                    {
                        flag=1;
                        printf("%10s%10s%10s%10s%10s%11s","学号","姓名","语文","数学","英语","平均分\n");
                        printf("%10s%10s%10d%10d%10d%10.2f\n",p->number,p->name,p->C,p->M,p->E,p->ave);
                        break;
                    }
                    p=p->next;
                }
                if(flag==0)
                    printf("未找到...");break;
            case 0:return ;
            default:printf("(〃>目<)请输入 0 ~ 2 的数字...");break;     
        }
        printf("\ncontinue~~\n");
    }
} 

void add_infor(stu *head)//学生信息添加 
{
		system("cls");
    char number[10];    
		char name[15];  
		int C;//Chinese
		int M;//Math
		int E;//English
		float ave;//self_ave
    int n;
    printf("想加入几个同学嘞~~\n");
    scanf("%d",&n);
    printf("%10s%10s%10s%10s%10s%11s\n","学号","姓名","语文","数学","英语","平均分");
    stu *p=head->next,*s;
    while(p->next)
        p=p->next;
    while(n--)
    {
        s=(stu *)malloc(sizeof(stu));
        scanf("%s%s%d%d%d%f",number,name,&C,&M,&E,&ave);
        strcpy(s->number,number);strcpy(s->name,name);
				s->C=C;s->M=M;s->E=E;s->ave=ave;
				s->next=NULL;
        p->next=s;
        p=s;
    }
}

stu *del_book(stu *head)//删除图书
{
    while(1)
    {
        char name[20];
        system("cls");
        stu *p=head->next,*pre=NULL;
        fflush(stdin);
        printf("请输入你要删除的学生的姓名:");
				scanf("%s",name);
        while(p&&strcmp(p->name,name)!=0)
        {
            pre=p;
            p=p->next;
        }
        if(p)
        {
            if(pre==NULL)
                head=p->next;
            else
            {
                pre->next=p->next;
            }
        }
        else  return NULL;
        return head;
    }
}

(细节太多,建议反复观看,后续再出,我懒)

和图书管理系统的区别就是这个系统的单个功能操作不会影响源文件,除非执行 9保存功能

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值