学生成绩管理系统

来源:互联网 发布:淘宝刷好评兼职流程 编辑:程序博客网 时间:2024/05/02 04:32

#include <stdio.h>

#include <stdlib.h>


typedef struct _Student{

char name[10];

int age;

int number;

int score;

}Student;

typedef struct _Node{

Student stu;//学生信息

struct _Node * next;//指向下一个学生

}Node;


Node * head=NULL;//定义第一个学生


//函数声明

void inputStudent();//录入学生信息

void printStudent();//打印学生信息

void saveStudent(); //保存学生信息

void readStudent(); //读取学生信息

int countStudent(); //统计所有学生人数

Node* findStudent();//查找学生信息

void modifyStudent(); //修改学生信息

void deleateStudent();//删除学生信息

int main(int argc,const char * argv[])

{

        printf("欢迎使用学生信息管理系统\n");

        printf("LG工作室2013作品V1.0\n");

        while (1) {

            printf("\n请选择功能列表:");

            printf("\n===============");

            printf("\n1.录入学生信息");

            printf("\n2.打印学生信息");

            printf("\n3.保存学生信息");

            printf("\n4.读取学生信息");

            printf("\n5.统计所有学生人数");

            printf("\n6.查找学生信息");

            printf("\n7.修改学生信息");

            printf("\n8.删除学生信息");

            printf("\n0.退出系统");

            printf("\n===============\n");

            

            char c;

            scanf(" %c",&c);

            switch (c) {

                case'1':

                    inputStudent();

                    break;

                case'2':

                    printStudent();

                    break;

                case'3':

                    saveStudent();

                    break;

                case'4':

                    readStudent();

                    break;

                case'5':

                    printf("学生的总人数为%d",countStudent());

                    break;

                case'6':

                {   Node *ptr;

                    ptr=findStudent();

                    if (ptr!=NULL) {

                        printf("\n学号%d,姓名%s,年龄%d,成绩%d",ptr->stu.number,ptr->stu.name,ptr->stu.age,ptr->stu.score);

                    }

                    break;

                }

                case'7':

                    modifyStudent();

                    break;

                case'8':

                    deleateStudent();

                    break;

                case'0':

                    return0;

                    break;

                default:

                    break;

            }

        }

        return0;

        }


        //1.录入学生信息

        void inputStudent(){

        printf("\n请输入学生信息,姓名学号 年龄成绩\n");

        Node * p;

        //p指向最后一个学生

        p=head;

        while (head!=NULL&&p->next!=NULL) {

            p=p->next;

        }

        //为新的学生分配一个空间

        Node * newnode=(Node *)malloc(sizeof(Node));

        newnode->next=NULL;

        if(head==NULL)

        {

            head=newnode;

            p=head;

        }

        else

        {

            p->next=newnode;//p的下一个节点为newnode

        }

        //输入新的学生数据

        scanf(" %s %d %d %d",newnode->stu.name,&newnode->stu.number,&newnode->stu.age,&newnode->stu.score);

        printf("\n数据添加成功........");

        }


        //2.打印学生信息

        void printStudent(){

        printf("\n打印所有学生信息");

        Node *p;

        p=head;//指向第一个学生

        while (p!=NULL) {//打印学生信息

            printf("\n学号%d,姓名%s,年龄%d,成绩%d",p->stu.number,p->stu.name,p->stu.age,p->stu.score);

            p=p->next;

        }


        }


        //3.保存学生信息

        void saveStudent(){

        printf("\n");

        FILE *fp;

        //打开文件

        fp=fopen("//Users//LGSC//Desktop//studentMMS.txt","w");

        if (fp==0) {

            printf("打开文件失败");

            return;

        }

        //写入数据

        Node *p;

        p=head;

        while (p!=NULL) {

            fprintf(fp,"%d %s %d %d \n",p->stu.number,p->stu.name,p->stu.age,p->stu.score);

            p=p->next;

        }

        printf("数据保存成功");

        //关闭文件

        fclose(fp);

        return;

        }


        //4.读取学生信息

        void readStudent(){

        //首先删除链表中的残余信息然后从文件读出数据

        Node *p,*p2;

        p=head;

        p2=p;

        while (p2!=NULL) {//逐个删除链表中的数据

            p=p->next;

            free(p2);

            p2=p;

        }

        head=NULL;

        FILE *fp;

        //打开文件

        fp=fopen("//Users//LGSC//Desktop//studentMMS.txt","r");

        if (fp==0) {

            printf("打开文件失败");

            return;

        }


        //读出数据

        while (!feof(fp)) {

            Node *temp=(Node *)malloc(sizeof(Node));//分配空间以存储数据

            fscanf(fp,"%d %s %d %d\n",&temp->stu.number,temp->stu.name,&temp->stu.age,&temp->stu.score);//从文件中读取数据

            printf("%d %s %d %d\n",temp->stu.number,temp->stu.name,temp->stu.age,temp->stu.score);

            

            if(head==NULL)//类似函数1创建链表

            {

                head=temp;

                p=head;

            }

            else

            {

                p->next=temp;//p的下一个节点为temp

                p=p->next;

                p->next=NULL;

            }

            

        }

        //关闭文件

        fclose(fp);


        return;


        }


        //5.统计所有学生人数

        int countStudent(){


        int number=0;

        readStudent();//先把文件中的数据读到链表中

        Node *p;

        p=head;


        while (p!=NULL) {

            number++;

            p=p->next;

        }

//            printf("%d",number);

        return number;


        }


        //6.查找学生信息

        Node* findStudent(){

            

        readStudent();//先把文件中的数据读到链表中

            

        int student_num;

        printf("\n请输入要查找学生的学号:\n");

        scanf("%d",&student_num);


        

        Node *p;

        p=head;//p指向链表

        while (p!=NULL) {

            if (p->stu.number==student_num) {

                return p;

            }

            p=p->next;

        }

        if(p==NULL){//遍历完链表也没找到信息

            printf("没有该学生信息");

        }


        returnNULL;

        }


        //7.修改某个学生的信息

        void modifyStudent()//通过链表来实现学生信息的修改,但是这种方法不太合适。

        {

        int student_num;

        printf("\n请输入要修改学生信息的学号:\n");

        scanf("%d",&student_num);


        readStudent();//先把文件中的数据读到链表中

        Node *p;

        p=head;

        while (p!=NULL) {

            if (p->stu.number==student_num) {

                printf("请输入要修改学生的信息姓名 学号年龄 成绩\n");

                scanf(" %s %d %d %d",p->stu.name,&p->stu.number,&p->stu.age,&p->stu.score);

                printf("修改成功!");

                break;

                

            }

            p=p->next;

        }


        if (p==NULL) {

            printf("没有该学生的信息!");

        }

        return ;

        }


        //8删除学生信息

        void deleateStudent(){


        readStudent();//先把文件中的数据读到链表中

            

        int student_num;

        printf("\n请输入要删除学生信息的学号:\n");

        scanf("%d",&student_num);

        


        Node *p,*p2;

        p=head;

        //判断是否是头结点

        if (head->stu.number==student_num) {

            p2=head;

            head=head->next;//头指针指向删除元素后的首元素

            free(p2);       //释放结点

            printf("删除成功");

            return;

        }

        //若不是头结点

        while (p->next!=NULL) {

            

            if (p->next->stu.number==student_num) {

                p2=p->next;

                p->next=p->next->next;//删除该结点指针跳过此节点

                free(p2);              //释放结点

                printf("删除成功");

                return;

            }

            p=p->next;//p指向下一个结点

            if(p->next==NULL){//判断是否到了链表的结尾

                break;

            }

        }

        if(p->next==NULL){ //输出提示信息

            printf("没有该学生信息");

        }

        return;


}

原创粉丝点击