zoukankan      html  css  js  c++  java
  • C语言项目(一):学生信息管理系统

    实现方式:链表

    结构定义

     1 typedef struct MyStu MyStudent;
     2 typedef struct node Node;
     3 typedef Node *pNode;
     4 
     5 struct MyStu {
     6     char name [20];
     7     int age;
     8     long ID;
     9     double score;
    10 };
    11 
    12 struct node {
    13     MyStudent student;
    14     pNode next;
    15 };
    16 
    17 static pNode head = NULL;

    基本功能

     1 void InputStudentInformation();
     2 
     3 void PrintStudentInformation();
     4 
     5 void SaveStudentInformation();
     6 
     7 void ReadStudentInformation();
     8 
     9 int CountStudentNumber();
    10 
    11 pNode FindStudent();
    12 
    13 pNode FinePrevious(pNode Cur);
    14 
    15 void ModifyStudentInformation();
    16 
    17 void DeleteStudentInformation();
    18 
    19 void Menu();
    20 
    21 void KeyDown();
    22 
    23 void SubMenu();

    界面

     1 void Menu() {
     2     printf("********************************************************
    ");
     3     printf("==============欢迎使用高校学生成绩管理系统==============
    ");
     4     printf("	=============请选择功能列表==============
    ");
     5     printf("	**************1.录入学生信息**************
    ");
     6     printf("	**************2.输出学生信息**************
    ");
     7     printf("	**************3.统计学生人数**************
    ");
     8     printf("	**************4.查找学生信息**************
    ");
     9     printf("	**************5.修改学生信息**************
    ");
    10     printf("	**************6.删除学生信息**************
    ");
    11     printf("	**************0.退 出 系 统**************
    ");
    12     printf("********************************************************
    ");
    13 }

    菜单实现

     1 void KeyDown() {
     2     int choice;
     3     printf("Function to chose (0~6):
    ");
     4     scanf("%d", &choice);
     5     switch (choice) {
     6         case 1:
     7             InputStudentInformation();
     8             SaveStudentInformation();
     9             break;
    10         case 2:
    11             PrintStudentInformation();
    12             break;
    13         case 3:
    14             printf("Total students is %d
    ", CountStudentNumber());
    15             break;
    16         case 4:
    17         {
    18             pNode Find = FindStudent();
    19             if (Find != NULL) {
    20                 printf("NUM:%ld	NAME:%s	AGE:%d	SCORE:%lf",
    21                        Find->student.ID,
    22                        Find->student.name,
    23                        Find->student.age,
    24                        Find->student.score);
    25             }
    26             break;
    27         }
    28         case 5:
    29             ModifyStudentInformation();
    30             SaveStudentInformation();
    31             break;
    32         case 6:
    33             DeleteStudentInformation();
    34             SaveStudentInformation();
    35             break;
    36         case 0:
    37             printf("Welcome to use again!");
    38             exit(0);
    39         default:
    40             printf("Invalid Input!");
    41             break;
    42     }
    43 }

    子菜单

     1 void SubMenu() {
     2     printf("********************************************************
    ");
     3     printf("==============欢迎使用高校学生成绩管理系统==============
    ");
     4     printf("	=============请选择功能列表==============
    ");
     5     printf("	**************a.修改姓名**************
    ");
     6     printf("	**************b.修改学号**************
    ");
     7     printf("	**************c.修改年龄**************
    ");
     8     printf("	**************d.修改成绩**************
    ");
     9     printf("	**************q.退出系统**************
    ");
    10     printf("********************************************************
    ");
    11 }

    打印功能

     1 void PrintStudentInformation() {
     2     system("cls");
     3     printf("********************************************************
    ");
     4     printf("==============欢迎使用高校学生成绩管理系统==============
    ");
     5     printf("********************************************************
    ");
     6     pNode tmp = head->next;
     7     while (tmp != NULL) {
     8         printf("NUM:%ld	NAME:%s	AGE:%d	SCORE:%lf
    ",
     9                tmp->student.ID,
    10                tmp->student.name,
    11                tmp->student.age,
    12                tmp->student.score);
    13         tmp = tmp->next;
    14 
    15     }
    16 }

    保存功能

     1 void SaveStudentInformation() {
     2     FILE *fp = fopen(PATH, "w+");
     3     if (!fp) {
     4         printf("FAIL");
     5         exit(0);
     6     }
     7     pNode tmp;
     8     tmp = head->next;
     9     while (tmp != NULL) {
    10         fwrite(&tmp->student, sizeof(Node), 1, fp);
    11         tmp = tmp->next;
    12     }
    13     fclose(fp);
    14     printf("SAVE DONE!");
    15 }

    读取功能

     1 void ReadStudentInformation() {
     2     FILE *read = fopen(PATH, "r");
     3     if (!read) {
     4         printf("FAIL");
     5         exit(0);
     6     }
     7     MyStudent stu;
     8     while (fread(&stu, sizeof(MyStudent), 1, read)) {
     9         pNode tmp = (pNode)malloc(sizeof(Node));
    10         memcpy(tmp, &stu, sizeof(MyStudent));
    11         if (head->next == NULL) {
    12             head->next = tmp;
    13             tmp->next = NULL;
    14         }
    15         else {
    16             pNode tmp_ = head->next;
    17             head->next = tmp;
    18             tmp->next = tmp_;
    19         }
    20     }
    21     fclose(read);
    22     printf("LOAD DONE!");
    23 }

    计数功能

     1 int CountStudentNumber() {
     2     int cnt = 0;
     3     pNode tmp;
     4     tmp = head->next;
     5     while (tmp != NULL) {
     6         cnt++;
     7         tmp = tmp->next;
     8     }
     9     return cnt;
    10 }

    增删改查

      1 void InputStudentInformation() {
      2     pNode NewNode = (pNode)malloc(sizeof(Node));
      3     if (head==NULL) {
      4         head = (pNode)malloc(sizeof(Node));
      5         head->next = NewNode;
      6         NewNode->next = NULL;
      7     }
      8     else {
      9         pNode tmp = head->next;
     10         head->next = NewNode;
     11         NewNode->next = tmp;
     12     }
     13     printf("Name:
    ");
     14     scanf("%s", NewNode->student.name);
     15     printf("NUM:
    ");
     16     scanf("%ld", &NewNode->student.ID);
     17     printf("AGE:
    ");
     18     scanf("%d", &NewNode->student.age);
     19     printf("SCORE:
    ");
     20     scanf("%lf", &NewNode->student.score);
     21     printf("DONE");
     22 }
     23 pNode FindStudent() {
     24     printf("Enter student ID:
    ");
     25     long id_f;
     26     scanf("%ld", &id_f);
     27     pNode tmp;
     28     tmp = head->next;
     29     while (tmp != NULL) {
     30         if (tmp->student.ID == id_f) {
     31             return tmp;
     32         }
     33         tmp = tmp->next;
     34     }
     35     printf("ERROR ID! 
    ");
     36     return NULL;
     37 }
     38 pNode FinePrevious(pNode Cur) {
     39     pNode tmp;
     40     tmp = head;
     41     while (tmp != NULL) {
     42         if (tmp->next == Cur) {
     43             return tmp;
     44         }
     45         tmp = tmp->next;
     46     }
     47     return NULL;
     48 }
     49 void ModifyStudentInformation() {
     50     pNode Find = FindStudent();
     51     if (Find == NULL) {
     52         printf("ID ERROR!");
     53         return;
     54     }
     55     SubMenu();
     56     char ch;
     57     scanf("%c", &ch);
     58     switch (ch) {
     59         case 'a':
     60             printf("Enter new name:
    ");
     61             scanf("%s", Find->student.name);
     62             printf("MODIFY DONE!
    ");
     63             break;
     64         case 'b':
     65             printf("Enter new ID:
    ");
     66             scanf("%ld", &Find->student.ID);
     67             printf("MODIFY DONE!
    ");
     68             break;
     69         case 'c':
     70             printf("Enter new age:
    ");
     71             scanf("%d", &Find->student.age);
     72             printf("MODIFY DONE!
    ");
     73             break;
     74         case 'd':
     75             printf("Enter new score:
    ");
     76             scanf("%lf", &Find->student.score);
     77             printf("MODIFY DONE!
    ");
     78             break;
     79         case 'q':
     80             printf("Welcome to using this system again!");
     81             break;
     82         default:
     83             printf("Invalid Input");
     84             break;
     85     }
     86 }
     87 void DeleteStudentInformation() {
     88     pNode Cur = FindStudent();
     89     if (Cur == NULL) {
     90         return;
     91     }
     92     pNode Prev = FinePrevious(Cur);
     93     if (Cur->next == NULL) {
     94         Prev->next = NULL;
     95         free(Cur);
     96     }
     97     else {
     98         pNode tmp = Cur->next;
     99         Cur->next = NULL;
    100         Prev->next = tmp;
    101         free(Cur);
    102     }
    103 
    104 }
  • 相关阅读:
    ubuntu 制做samba
    《Programming WPF》翻译 第4章 前言
    《Programming WPF》翻译 第4章 3.绑定到数据列表
    《Programming WPF》翻译 第4章 4.数据源
    《Programming WPF》翻译 第5章 6.触发器
    《Programming WPF》翻译 第4章 2.数据绑定
    《Programming WPF》翻译 第4章 1.不使用数据绑定
    《Programming WPF》翻译 第5章 7.控件模板
    《Programming WPF》翻译 第5章 8.我们进行到哪里了?
    《Programming WPF》翻译 第5章 5.数据模板和样式
  • 原文地址:https://www.cnblogs.com/oasisyang/p/13199431.html
Copyright © 2011-2022 走看看