一.功能
二.源代碼
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NUM 100
typedef struct
{char no[30];char name[10];char sex[10];char phone[20];float cyuyan;float computer;float datastruct;
} *student, student1;typedef struct
{student stu[MAX_NUM];int number;
} *studentDB;void read(studentDB temp)
{FILE *infile;infile = fopen("./數據.txt", "r");if (!infile){printf("文件打開失敗!");exit(0);}while (!feof(infile)){temp->stu[temp->number] = malloc(sizeof(student1));fscanf(infile, "%s %s %s %s %f %f %f", temp->stu[temp->number]->no, temp->stu[temp->number]->name, temp->stu[temp->number]->sex, temp->stu[temp->number]->phone, &(temp->stu[temp->number]->cyuyan), &(temp->stu[temp->number]->computer), &(temp->stu[temp->number]->datastruct));temp->number++;}fclose(infile);
}void write(studentDB temp)
{FILE *outfile;outfile = fopen("./數據.txt", "w");if (!outfile){printf("文件打開失敗!");exit(1);}if (temp && temp->number > 0){int i;for (i = 0; i < temp->number; i++){if (i == temp->number - 1){fprintf(outfile, "%s %s %s %s %.2f %.2f %.2f", temp->stu[i]->no, temp->stu[i]->name, temp->stu[i]->sex, temp->stu[i]->phone, temp->stu[i]->cyuyan, temp->stu[i]->computer, temp->stu[i]->datastruct);}else{fprintf(outfile, "%s %s %s %s %.2f %.2f %.2f\n", temp->stu[i]->no, temp->stu[i]->name, temp->stu[i]->sex, temp->stu[i]->phone, temp->stu[i]->cyuyan, temp->stu[i]->computer, temp->stu[i]->datastruct);}}}fclose(outfile);printf("保存成功");read(temp);
}void display(studentDB temp)
{int i;printf("| 學號 |姓名|性別| 手機號 |c語言|英語|高等數學|\n");for (i = 0; i < temp->number; i++){printf("%s %s %s %s %.2f %.2f %.2f\n", temp->stu[i]->no, temp->stu[i]->name, temp->stu[i]->sex, temp->stu[i]->phone, temp->stu[i]->cyuyan, temp->stu[i]->computer, temp->stu[i]->datastruct);}
}void menu()
{printf("\n\n\t****************************簡單學生信息管理系統*****************************\n");printf("\t* 1.顯示學生信息 *|\n");printf("\t* 2.增加學生信息 *|\n");printf("\t* 3.刪除學生信息 *|\n");printf("\t* 4.修改學生信息 *|\n");printf("\t* 5.查詢學生信息 *|\n");printf("\t* 6.排序學生成績 *|\n");printf("\t* 7.計算學生平均成績 *|\n");printf("\t* 8.保存學生信息 *|\n");printf("\t* 9.統計全部課程及格人數 *|\n");printf("\t* 10.輸出總成績最高的學生信息 *|\n");printf("\t* 0.退出系統 *|\n");printf("\t***************************************************************************\n");printf("請選擇你的操作并將序號輸入:");
}int countDigits(long long n)
{int count = 0;while (n > 0){n /= 10;count++;}return count;
}void insert(studentDB temp)
{char no[30];printf("請輸入要添加學生的學號:");scanf("%s", no);int n;long long num;sscanf(no, "%lld", &num);n = countDigits(num);if (n != 11){printf("輸入的學號位數有誤,請重新輸入!");return;}else{student1 stu;FILE *outfile;outfile = fopen("./數據.txt", "a");if (!outfile){printf("文件打開失敗!");exit(0);}strcpy(stu.no, no);printf("請輸入姓名:");scanf("%s", stu.name);printf("請輸入性別:");scanf("%s", stu.sex);printf("請輸入手機號:");scanf("%s", stu.phone);printf("請輸入c語言成績(帶小數點):");scanf("%f", &stu.cyuyan);printf("請輸入英語成績(帶小數點):");scanf("%f", &stu.computer);printf("請輸入高等數學成績(帶小數點):");scanf("%f", &stu.datastruct);n = fprintf(outfile, "\n%s %s %s %s %f %f %f", stu.no, stu.name, stu.sex, stu.phone, stu.cyuyan, stu.computer, stu.datastruct);printf("%s %s %s %s %.2f %.2f %.2f", stu.no, stu.name, stu.sex, stu.phone, stu.cyuyan, stu.computer, stu.datastruct);printf("學生信息已成功插入!信息長度:%d\n", n);fclose(outfile);read(temp);return;}
}void delete(studentDB temp)
{printf("請輸入要刪除的學生信息學號:");char no[15];int i, k;scanf("%s", no);printf("%s", no);for (i = 0; i < temp->number; i++){if (strcmp(temp->stu[i]->no, no) == 0){for (k = i; k < temp->number; k++){temp->stu[k] = temp->stu[k + 1];}free(temp->stu[temp->number]);temp->number--;printf("學生信息已成功刪除!\n");return;}}printf("學生學號輸入錯誤!");return;
}void modify(studentDB temp)
{printf("請輸入要修改的學生信息學號:");char no[15];int i, flag = 0;long num;scanf("%s", no);sscanf(no, "%ld", &num);for (i = 0; i < temp->number; i++){if (strcmp(no, temp->stu[i]->no) == 0){printf("\n學號:%s 姓名:%s 性別:%s 手機號:%s c語言:%.2f 英語:%.2f 高等數學:%.2f\n\n\n", temp->stu[i]->no, temp->stu[i]->name, temp->stu[i]->sex, temp->stu[i]->phone, temp->stu[i]->cyuyan, temp->stu[i]->computer, temp->stu[i]->datastruct);printf("|1.學號|2.姓名|3.性別|4.手機號|5.c語言成績|6.英語成績|7.高等數學成績|8.不改動并返回菜單|9.依次修改全部數據\n\n\n請輸入要改動的數據項:");scanf("%d", &flag);switch (flag){case 1:printf("請輸入要修改的學號:");scanf("%s", temp->stu[i]->no);break;case 2:printf("請輸入姓名:");scanf("%s", temp->stu[i]->name);break;case 3:printf("請輸入性別:");scanf("%s", temp->stu[i]->sex);break;case 4:printf("請輸入手機號:");scanf("%s", temp->stu[i]->phone);break;case 5:printf("請輸入c語言成績(帶小數點):");scanf("%f", &(temp->stu[i]->cyuyan));break;case 6:printf("請輸入英語成績(帶小數點):");scanf("%f", &(temp->stu[i]->computer));break;case 7:printf("請輸入高等數學成績(帶小數點):");scanf("%f", &(temp->stu[i]->datastruct));break;case 8:return;case 9:printf("請輸入姓名:");scanf("%s", temp->stu[i]->name);printf("請輸入性別:");scanf("%s", temp->stu[i]->sex);printf("請輸入手機號:");scanf("%s", temp->stu[i]->phone);printf("請輸入c語言成績(帶小數點):");scanf("%f", &(temp->stu[i]->cyuyan));printf("請輸入英語成績(帶小數點):");scanf("%f", &(temp->stu[i]->computer));printf("請輸入高等數學成績(帶小數點):");scanf("%f", &(temp->stu[i]->datastruct));break;default:printf("請重新選擇!");break;}return;}}
}void search(studentDB temp)
{printf("請輸入要查詢的學生信息學號:");char no[30];int i, n;long long num;scanf("%s", no);sscanf(no, "%lld", &num);n = countDigits(num);if (n != 11){printf("輸入的學號位數有誤,請重新輸入!");return;}else{for (i = 0; i < temp->number; i++){if (strcmp(temp->stu[i]->no, no) == 0){printf("\n學號:%s 姓名:%s 性別:%s 手機號:%s c語言:%.2f 英語:%.2f 高等數學:%.2f\n\n\n", temp->stu[i]->no, temp->stu[i]->name, temp->stu[i]->sex, temp->stu[i]->phone, temp->stu[i]->cyuyan, temp->stu[i]->computer, temp->stu[i]->datastruct);return;}}printf("學生學號輸入錯誤!");return;}
}void sort(studentDB temp)
{int i, j, n;float t;for (i = 0; i < temp->number; i++){for (j = i + 1; j < temp->number; j++){if ((temp->stu[i]->cyuyan + temp->stu[i]->computer + temp->stu[i]->datastruct) < (temp->stu[j]->cyuyan + temp->stu[j]->computer + temp->stu[j]->datastruct)){student1 *tempstu = temp->stu[i];temp->stu[i] = temp->stu[j];temp->stu[j] = tempstu;}}}printf("已按成績排序完成!\n");
}void average(studentDB temp)
{int i;float ctotal = 0, comtotal = 0, dstotal = 0;for (i = 0; i < temp->number; i++){ctotal += temp->stu[i]->cyuyan;comtotal += temp->stu[i]->computer;dstotal += temp->stu[i]->datastruct;}printf("C語言平均成績:%.2f 英語平均成績:%.2f 高等數學平均成績:%.2f\n", ctotal / temp->number, comtotal / temp->number, dstotal / temp->number);
}void passing(studentDB temp)
{int i, cpass = 0, compass = 0, dpass = 0;for (i = 0; i < temp->number; i++){if (temp->stu[i]->cyuyan >= 60){cpass++;}if (temp->stu[i]->computer >= 60){compass++;}if (temp->stu[i]->datastruct >= 60){dpass++;}}printf("C語言及格人數:%d 英語及格人數:%d 高等數學及格人數:%d\n", cpass, compass, dpass);
}void max(studentDB temp)
{int i, index = 0;float maxScore = 0;for (i = 0; i < temp->number; i++){float totalScore = temp->stu[i]->cyuyan + temp->stu[i]->computer + temp->stu[i]->datastruct;if (totalScore > maxScore){maxScore = totalScore;index = i;}}printf("學號:%s 姓名:%s 性別:%s 手機號:%s c語言:%.2f 英語:%.2f 高等數學:%.2f 總成績:%.2f\n", temp->stu[index]->no, temp->stu[index]->name, temp->stu[index]->sex, temp->stu[index]->phone, temp->stu[index]->cyuyan, temp->stu[index]->computer, temp->stu[index]->datastruct, maxScore);
}int main()
{int select;studentDB stu = malloc(sizeof(studentDB));stu->number = 0;read(stu);do{menu();scanf("%d", &select);switch (select){case 1:display(stu);break;case 2:insert(stu);break;case 3:delete (stu);break;case 4:modify(stu);break;case 5:search(stu);break;case 6:sort(stu);break;case 7:average(stu);break;case 8:write(stu);break;case 9:passing(stu);break;case 10:max(stu);break;case 0:exit(1);}} while (1);return 0;
}
三.注意事項
(1)事先準備:?數據.txt?文件(可以含有學生信息,可以空白,但是必須有)
(2)學號為11位
(3)該代碼不含有C99或者C11