学生信息管理系统

来源:互联网 发布:如何提高淘宝卖家信誉 编辑:程序博客网 时间:2024/06/05 14:12

#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(){
    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("%d %s %d %d",&newnode->stu.number,newnode->stu.name,&newnode->stu.age,&newnode->stu.score);
    printf("\n数据添加成功........");
}

void printStudent(){
    printf("\n这里是打印学生信息\n");
    Node *p;
    p=head;//指向第一个学生
     while (p!=NULL) {
        printf("\n学号%d,姓名%s,年龄%d,成绩%d\n",p->stu.number,p->stu.name,p->stu.age,p->stu.score);
        p=p->next;
    }
    printf("\n数据打印成功........");
}

void saveStudent(){
       printf("\n这里是保存学生信息\n");
  FILE *fp;
  fp=fopen("C:\\Documents and Settings\\Administrator\\桌面\\作业02.txt","w");
   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;
    }
    fclose(fp);
    printf("\n数据保存成功");

}

int readStudent(){
    printf("\n这里是读取学生信息\n");
    FILE *fp;


    if((fp=fopen("C:\\Documents and Settings\\Administrator\\桌面\\作业02.txt","r"))==NULL)
    {
        printf("文件打开失败");
        return 0;
    }
    char ch;
    ch=fgetc(fp);//判断是否为空文件
    if(ch==EOF)
    {
        printf("空文件");
        fclose(fp);
        return 0;
    }
    else
    {
        Node *p;
        head=(Node *)malloc(sizeof(Node));
        p=head;
        rewind(fp);//指针返回文件之首
        while(!feof(fp))
        {


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

           fscanf(fp,"%d %s %d %d\n",&newnode->stu.number,newnode->stu.name,&newnode->stu.age,&newnode->stu.score);
           printf("%d %s %d %d\n",newnode->stu.number,newnode->stu.name,newnode->stu.age,newnode->stu.score);
           p=newnode;
           newnode->next=NULL;
           p=p->next;
        }
    }
    fclose(fp);
    printf("\n数据读取成功........");
    return 0;
}

 

void countStudent(){
    printf("\n这里是统计学生人数\n");
      Node *p;
      p=head;
      int count=0;
      while(p!=NULL)
      {
          count++;
          p=p->next;
      }
      printf("\n总共有%d位学生\n",count);

}

Student * findStudent(){
    printf("\n这里是查找学生信息\n");
    int xuehao;
    printf("\n请输入您要查学生的学号:");
    scanf("%d",&xuehao);
    Node *p;
    p=head;
    while(p!=NULL)
    {
       if(p->stu.number==xuehao)
       {
           printf("\n要查学生的姓名:%s,学号:%d,年龄:%d,成绩:%d",p->stu.name,p->stu.number,p->stu.age,p->stu.score);
           break;
       }
       else
       {
           p=p->next;
       }
    }
    if(p==NULL)
    {
        printf("\n查无此人\n");
    }
    return 0;
}


void modifyStudent(){
    printf("\n这里是修改学生信息\n");
    int xuehao;
    printf("\n请输入您要修改学生的学号:");
    scanf("%d",&xuehao);
    Node *p;
    p=head;
    while(p!=NULL)
    {
       if(p->stu.number==xuehao)
       {
           printf("\n要修改学生的姓名:%s,学号:%d,年龄:%d,成绩:%d",p->stu.name,p->stu.number,p->stu.age,p->stu.score);
           printf("\n请输入该学生的新信息\n");
           printf("姓名: ");
           scanf("%s",p->stu.name);
           printf("年龄: ");
           scanf("%d",&p->stu.age);
           printf("成绩: ");
           scanf("%d",&p->stu.score);
           break;
       }
       else
       {
           p=p->next;
       }
    }
    if(p==NULL)
    {
        printf("\n查无此人\n");
    }

}
void insertStudent()
{
     printf("\n这里是添加学生信息\n");

}
void deleteStudent()//删除学习信息
{
    printf("\n这里是删除学生信息\n");
 Node *p,*q;
 p = head;

 if(head==NULL)
    {
        printf("\n空信息\n");
        return ;
    }
 int num;
 printf("\n请输入您要删除的学生的学号:");
 scanf("%d",&num);
   if(head->stu.number==num)
    {
        if(head->next!=NULL)
        {
            q=head;
            head=head->next;
            free(q);
            return;
        }
        else
        {
            free(head);
            return;
        }

    }
    while(p->next!=NULL)
    {
        if(p->next->stu.number==num)
        {
            q=p->next;
            p->next=q->next;
            free (q);
            return;
        }
        else
        {
            if(p->next!=NULL)
             p=p->next;
        }
    }

}
int main(int argc, const char * argv[])
{
    printf("\n\n\t\t\t欢迎使用学生信息管理系统\n");
    printf("\t\t\t天津市中北镇工作室2013作品\n\t\t\t最新版本:V1.0\n");
    while (1) {
        printf("\n\n请输入管理功能表 1 ~ 0:\n");
        printf("\n 1.输入学生信息:");
        printf("\n 2.打印学生信息:");
        printf("\n 3.保存学生信息:");
        printf("\n 4.读取学生信息:");
        printf("\n 5.统计学生人数:");
        printf("\n 6.查找学生信息");
        printf("\n 7.修改学生信息");
        printf("\n 8.添加学生信息");
        printf("\n 9.删除学生信息");
        printf("\n 0.退出:\n");
        printf("\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':
                countStudent();
                break;
            case '6':
                findStudent();
                break;
            case '7':
                modifyStudent();
                break;
            case '8':
                insertStudent();
                break;
            case '9':
                deleteStudent();
                break;

            case '0':
                return 0;
                break;
            default:
                break;
        }
    }
    Node *p;
    p=head;//指向第一个学生
     while (p!=NULL) {
        free(p);
        p=p->next;
    }
    return 0;
}