大一数据结构与算法期末项目作业,C语言, 命令行窗口下的学生成绩管理系统,及数据生成程序,
学生成绩管理系统
-
页面效果如图:
-
代码如下:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> //行和列常量 #define ROW 20 #define COL 83 typedef struct student { char id[14]; char name[21]; double chinese; double math; double english; double sum; } student; int fileRead(student** ppheadStu); int fileWrite(student* pheadStu, int num); char getOper(); void printLine(student stu); char printPage(student* pcurrStu, int remain, int sum, int index); double getScore(char* pdescribe); char processWASD(char op, student** ppcurrStu, int* premain, int sum, int* pindex); void sort(student* pheadStu, int n, char sortAcc, char sortOp); void processSort(char* psortOp, char* psortAco); char processSearch(student* pheadStu, int sum, student** ppcurrStu, int* premain, int* pindex); char processNewBuildStu(student** ppheadStu, int* psum, int* premain, int* pindex, student** ppcurrStu); char processDeleteStu(student** ppheadStu, int* psum, int* premain, int* pindex, student** ppcurrStu); char processChangeStu(int* psum, int* premain, int* pindex, student** ppcurrStu); int main() { student* pheadStu = NULL; int sum = 0; if((sum = fileRead(&pheadStu))==-1){ perror("main,fileRead,读取文件失败"); return -1; } student* pcurrStu = pheadStu; int remain = sum,index = 1; char op = printPage(pcurrStu, remain, sum,index);//stu应该是值传递,不能被修改 while (op != 'q' && op != 'Q') { if (op == 'l' || op == 'L') { char sortOp, sortAcc; processSort(&sortAcc, &sortOp); sort(pheadStu, sum, sortAcc, sortOp); op = printPage(pcurrStu, remain, sum, index); } else if (op == 'd' || op == 'D' || op == 'a' || op == 'A' || op == 'w' || op == 'W' || op == 's' || op == 'S') { op = processWASD(op, &pcurrStu, &remain, sum, &index); } else if (op == 'k' || op=='K') { if (processSearch(pheadStu, sum, &pcurrStu, &remain, &index)) { op = printPage(pcurrStu, remain, sum, index); } else { } } else if (op == 'g' || op == 'G') { op = processNewBuildStu(&pheadStu, &sum,&remain,&index,&pcurrStu); } else if (op == 'j' || op == 'J') { op = processChangeStu(&sum, &remain, &index, &pcurrStu); } else if (op == 'h' || op == 'H') { op = processDeleteStu(&pheadStu, &sum, &remain, &index, &pcurrStu); } } if(fileWrite(pheadStu,sum)==-1){ perror("main,fileWrite,写入文件失败"); return -1; } printf("\033[1;40;31m已退出\033[0m"); return 0; } int fileRead(student** ppheadStu) { FILE* fp = fopen("./students.txt", "r+"); if (fp == NULL) { perror("fileRead,fopen,文件打开失败"); fclose(fp); return -1; } int num; if (fread(&num, sizeof(int), 1, fp) == 0) { num = 0; rewind(fp); fwrite(0, sizeof(int), 1, fp); fclose(fp); return 0; } (*ppheadStu) = (student*)malloc(sizeof(student) * num); if ((fread((*ppheadStu), sizeof(student), num, fp)) != num) { perror("fileRead,fread,读取学生信息失败"); fclose(fp); return -1; } fclose(fp); return num; } int fileWrite(student* pheadStu, int num) { FILE* fp = fopen("./students.txt", "w"); if (fp == NULL) { perror("fileWrite,fopen,打开文件失败"); fclose(fp); return -1; } if (fwrite(&num, sizeof(int), 1, fp) != 1) { perror("fileWrite,fwrite,写入学生人数失败"); fclose(fp); return -1; } if (fwrite(pheadStu, sizeof(student), num, fp) != num) { perror("fileWrite,fwrite,写入学生信息失败"); fclose(fp); return -1; } free(pheadStu); fclose(fp); return 0; } char printPage(student* pcurrStu, int remain, int sum, int index) {//0<= n <=50 char oper; //2 2 1 1 1 1 printf("\033[240A\r\033[0;47;30m |%-15s|%-20s|%-10s|%-10s|%-10s|%-10s\033[0m\n", "学号", "姓名", "语文", "数学", "英语", "总分"); for (int i = 0; i < (remain > ROW ? ROW : remain); i++) { if (i == (index - 1)) { printf("\033[0;40;37m%02d\033[0m", i + 1); } else { printf("\033[0;47;30m%02d\033[0m", i + 1); } printLine(pcurrStu[i]); } if (remain < ROW) { for (int i = 1; i <= ROW - remain; i++) { for (int j = 1; j <= COL; j++) { printf("\033[0;47;30m \033[0m"); } printf("\n"); } } //指令表宽度为54个字符,,printf("\033[0;47;30m\033[0m");返回值为14 int lenOfFooter = printf("\033[0;47;30m第 %d / %d 页\033[0m", (sum - remain) / ROW + 1, sum / ROW + (sum % ROW == 0 ? 0 : 1)); lenOfFooter -= 14; printf("\033[0;47;30m|\033[0m\033[1;47;31m上一页:a/A\033[0m"); printf("\033[0;47;30m|\033[0m\033[1;47;31m下一页:d/D\033[0m"); printf("\033[0;47;30m|\033[0m\033[1;47;31m上一个:w/W\033[0m"); printf("\033[0;47;30m|\033[0m\033[1;47;31m下一个:s/S\033[0m"); printf("\033[0;47;30m|\033[0m\033[1;47;31m退出:q/Q\033[0m\033[0;47;30m|\033[0m"); for (int i = 54 + lenOfFooter + 1; i <= COL; i++) { printf("\033[0;47;30m \033[0m"); } printf("\n"); for (int i = 1; i <= lenOfFooter; i++) { printf("\033[0;47;30m \033[0m"); } printf("\033[0;47;30m|\033[0m\033[1;47;31m新建:g/G \033[0m"); printf("\033[0;47;30m|\033[0m\033[1;47;31m删除:h/H \033[0m"); printf("\033[0;47;30m|\033[0m\033[1;47;31m修改:j/J \033[0m"); printf("\033[0;47;30m|\033[0m\033[1;47;31m查找:k/K \033[0m"); printf("\033[0;47;30m|\033[0m\033[1;47;31m排序:l/L\033[0m\033[0;47;30m|\033[0m"); for (int i = 54 + lenOfFooter + 1; i <= COL; i++) { printf("\033[0;47;30m \033[0m"); } printf("\n"); printf("\033[K输入操作指令:"); oper = getOper(); return oper; } void printLine(student stu) {//COL - 2个字符 printf("\033[0;47;30m|%-15s|%-20s|%-10.2lf|%-10.2lf|%-10.2lf|%-10.2lf\033[0m\n", stu.id, stu.name, stu.chinese, stu.math, stu.english, stu.sum); } double getScore(char* pdescribe) { double result; char score[7]; score[6] = '\0'; reset:; result = 0; int i = 0; int dot = 6, isDot = 0; while ((score[i] = getchar()) != '\n' && i <= 5) { if ((score[i] < '0' || score[i]>'9') && score[i] != '.') { printf("\033[1A\033[K输入错误,请重新输入%s成绩(000.00~100.00):", pdescribe); while (getchar() != '\n') { continue; } goto reset; } if (score[i] == '.') { if (isDot) { printf("\033[1A\033[K输入错误,请重新输入%s成绩(000.00~100.00):", pdescribe); while (getchar() != '\n') { continue; } goto reset; } else {//isDot == 0 isDot = 1; } dot = i; } i++; } if (score[0] == '\n') { printf("\033[1A\033[K输入错误,请重新输入%s成绩(000.00~100.00):", pdescribe); goto reset; } if (score[i] != '\n') { printf("\033[1A\033[K输入数据太长,请重新输入%s成绩(000.00~100.00):", pdescribe); while (getchar() != '\n') { continue; } goto reset; } else {//score[i]=='\n' if (dot == 6) { dot = i; } } score[i] = '\0'; for (int i = 0; i <= 5; i++) { if ((score[i] < '0' || score[i]>'9') && score[i] != '.') { break; } if (i < dot) { result += (score[i] - '0') * pow(10.0, (dot - i - 1.0)); } else if (i > dot) { result += (double)(score[i] - '0') / pow(10.0, (i - dot)); } } if (result < 0.0 || result>100.0) { printf("\033[1A\033[K输入数据超过范围,请重新输入%s成绩(000.00~100.00):", pdescribe); goto reset; } return result; } char getOper() { char c; c = getchar(); if (c != '\n') { while (getchar() != '\n') { continue; } } while (c != 'd' && c != 'D' && c != 'a' && c != 'A' && c != 'w' && c != 'W' && c != 's' && c != 'S'&& c != 'q' && c != 'Q' && c != 'g' && c != 'G' && c != 'h' && c != 'H' && c != 'j'&& c != 'J' && c != 'k' && c != 'K' && c != 'l' && c != 'L') { printf("\033[1A\033[K输入错误,请重新输入操作指令:"); c = getchar(); if (c != '\n') { while (getchar() != '\n') { continue; } } } return c; } char processWASD(char op, student** ppcurrStu, int* premain, int sum,int* pindex) { switch (op) { case 'd': case 'D': if ((*premain) <= ROW) { printf("\033[1A\033[K已经是最后一页了,重新输入操作指令:"); op = getOper(); while(op == 'd' || op == 'D') { printf("\033[1A\033[K已经是最后一页了,重新输入操作指令:"); op = getOper(); } return op; } (*ppcurrStu)+=ROW; (*premain) -=ROW; if ((*pindex) > (*premain)) { (*pindex) = (*premain); } op = printPage((*ppcurrStu), (*premain), sum,(*pindex)); return op; break; case 'a': case 'A': if ((*premain) == sum) { printf("\033[1A\033[K已经是第一页了,请重新输入操作指令:"); op = getOper(); while (op == 'a' || op == 'A') { printf("\033[1A\033[K已经是第一页了,请重新输入操作指令:"); op = getOper(); } return op; } (*ppcurrStu) -= ROW; (*premain) += ROW; op = printPage((*ppcurrStu), (*premain), sum,(*pindex)); return op; break; case 'w': case 'W': if ((*pindex) == 1) { printf("\033[1A\033[K已经是第一个了,请重新输入操作指令:"); op = getOper(); while (op == 'w' || op == 'W') { printf("\033[1A\033[K已经是第一个了,请重新输入操作指令:"); op = getOper(); } return op; } (*pindex)--; op = printPage((*ppcurrStu), (*premain), sum,(*pindex)); break; case 's': case 'S': if ((*pindex) == ROW || (*pindex)==(*premain) ) { while (op == 's' || op == 'S') { printf("\033[1A\033[K已经是最后一个了,请重新输入操作指令:"); op = getOper(); } return op; } (*pindex)++; op = printPage((*ppcurrStu), (*premain), sum, (*pindex)); return op; break; /*default: op = getOper(); return op; break;*/ } } void sort(student* pheadStu, int n, char sortAcc,char sortOp) {//op==1,由大到小 for (int i = 0; i < n; i++) { for (int j = i; j > 0; j--) { double current, next; switch (sortAcc) { case 'l': case 'L': current = pheadStu[j].sum; next = pheadStu[j - 1].sum; break; case 'h': case 'H': current = pheadStu[j].chinese; next = pheadStu[j - 1].chinese; break; case 'j': case 'J': current = pheadStu[j].math; next = pheadStu[j - 1].math; break; case 'k': case 'K': current = pheadStu[j].english; next = pheadStu[j - 1].english; break; case 'g': case 'G': { int compare = strcmp(pheadStu[j].id, pheadStu[j - 1].id); if (compare < 0) { current = 1.0; next = 2.0; } else if (compare > 0) { current = 2.0; next = 1.0; } else { current = 1.0; next = 1.0; } break; } } if (current > next) { if (sortOp =='w'|| sortOp =='W') { student temp = pheadStu[j]; pheadStu[j] = pheadStu[j - 1]; pheadStu[j - 1] = temp; } else { break; } } else if(current < next){ if (sortOp == 'w'|| sortOp =='W') { break; } else { student temp = pheadStu[j]; pheadStu[j] = pheadStu[j - 1]; pheadStu[j - 1] = temp; } } else if (current == next) { break; } } } } void processSort(char* psortAcc,char* psortOp) { printf("\033[1A\033[K输入排序依据(学号:g/G,语文:h/H,数学:j/J,英语:k/K,总分:l/L): "); (*psortAcc) = getchar(); if ((*psortAcc) != '\n') { while (getchar() != '\n') { continue; } } while ((*psortAcc) != 'g' && (*psortAcc) != 'G' && (*psortAcc) != 'h' && (*psortAcc) != 'H' && (*psortAcc) != 'j' && (*psortAcc) != 'J' &&(*psortAcc) != 'k' && (*psortAcc) != 'K' && (*psortAcc) != 'l' && (*psortAcc) != 'L') { printf("\033[1A\033[K输入错误,请重新输入排序依据(学号:g/G,语文:h/H,数学:j/J,英语:k/K,总分:l/L):"); (*psortAcc) = getchar(); if ((*psortAcc) != '\n') { while (getchar() != '\n') { continue; } } } printf("\033[1A\033[K输入排序方式(由小到大:s,由大到小:w):"); (*psortOp) = getchar(); if ((*psortOp != '\n')) { while (getchar() != '\n') { continue; } } while ((*psortOp) != 's' && (*psortOp) != 'S' && (*psortOp) != 'w' && (*psortOp) != 'W') { printf("\033[1A\033[K输入错误,请重新输入排序方式(由小到大:s,由大到小:w):"); (*psortOp) = getchar(); if ((*psortOp) != '\n') { while (getchar() != '\n') { continue; } } } } char processSearch(student* pheadStu, int sum, student** ppcurrStu, int* premain, int* pindex) { printf("\033[1A\033[K输入要查找学生的学号(13位数字):\033[0m"); char searId[14]; searId[13] = '\0'; reset:; for (int i = 0; i < 13; i++) { searId[i] = getchar(); if (searId[i] < '0' || searId[i]>'9') { if (searId[i] != '\n') { while (getchar() != '\n') { continue; } } printf("\033[1A\033[K输入错误,请重新开始输入要查找学生的学号:"); goto reset; } } while (getchar() != '\n') { while (getchar() != '\n') { continue; } printf("\033[1A\033[K输入学号过长,请重新开始输入要查找学生的学号:"); goto reset; continue; } for (int i = 0; i < sum; i++) { if (strcmp(searId, pheadStu[i].id)==0) { (*ppcurrStu) = pheadStu+(i-i%ROW);//i/ROW*ROW (*pindex) = i % ROW + 1; (*premain) = sum - (i - i % ROW); return printPage((*ppcurrStu), (*premain), sum, (*pindex)); } } printf("\033[1A\033[K此成绩单里没有该学生,请输入操作指令:"); return getOper(); } char processNewBuildStu(student** ppheadStu, int* psum,int *premain,int* pindex,student **ppcurrStu) { student tempStu; tempStu.chinese = 0; tempStu.math = 0; tempStu.english = 0; tempStu.id[13] = '\0'; printf("\033[1A\033[K请输入新建学生的学号(13位数字):"); resetId:; for (int i = 0; i < 13; i++) { tempStu.id[i] = getchar(); if (tempStu.id[i] < '0' || tempStu.id[i]>'9') { if (tempStu.id[i] != '\n') { while (getchar() != '\n') { continue; } } printf("\033[1A\033[K输入错误,请重新开始输入新建学生的学号(13位数字):"); goto resetId; } } if (getchar() != '\n') { printf("\033[1A\033[K输入学号过长,请重新开始新建学生的输入学号(13位数字):"); while (getchar() != '\n') { continue; } goto resetId; } resetName:; printf("\033[1A\033[K请输入新建学生的姓名(最大20个字符,一个汉字算两个字符):"); char avoidEnter; while ((avoidEnter = getchar()) == '\n') { printf("\033[1A\033[K输入错误,请重新输入新建学生的姓名:"); continue; } tempStu.name[0] = avoidEnter; int i = 1; while ((tempStu.name[i] = getchar()) != '\n' && i <= 19) { i++; continue; } if (tempStu.name[i] != '\n') { printf("\033[1A\033[K输入姓名过长,请重新输入新建学生的姓名(最大20个字符,一个汉字算两个字符):"); while (getchar() != '\n') { continue; } goto resetName; } tempStu.name[i] = '\0'; printf("\033[1A\033[K请输入新建学生的语文成绩(000.00~100.00):"); tempStu.chinese = getScore("新建学生的语文"); printf("\033[1A\033[K请输入新建学生的数学成绩(000.00~100.00):"); tempStu.math = getScore("新建学生的数学"); printf("\033[1A\033[K请输入新建学生的英语成绩(000.00~100.00):"); tempStu.english = getScore("新建学生的英语"); tempStu.sum = tempStu.chinese + tempStu.math + tempStu.english; //新建空间 (*psum)++; student* pnewHeadStu = (student*)malloc(sizeof(student) * (*psum)); for (int i = 0; i <= (*psum) - 2; i++) { pnewHeadStu[i] = (*ppheadStu)[i]; } pnewHeadStu[(*psum) - 1] = tempStu; free(*ppheadStu); (*ppheadStu) = pnewHeadStu; //修改页面显示到新建学生数据 (*ppcurrStu) = (*ppheadStu) + ((*psum) - 1) / ROW * ROW; (*premain) = (*psum) - ((*psum) - 1) / ROW * ROW; (*pindex) = ((*psum) - 1) % ROW + 1; char op = printPage((*ppcurrStu), *premain, *psum, *pindex); return op; } char processChangeStu(int* psum, int* premain, int* pindex, student** ppcurrStu) { student tempStu; char isSkip; printf("\033[1A\033[K输入要修改的学生所在行的序号(1~%d):", ROW); resetIndex:; int realNumber = 0; char charNumber; while ((charNumber = getchar()) != '\n') { if (charNumber < '0' || charNumber>'9') { printf("\033[1A\033[K输入错误,请重新输入要修改的学生所在行的序号(1~%d):", ROW); while (getchar() != '\n') { continue; } goto resetIndex; } realNumber *= 10; realNumber += charNumber - '0'; if (realNumber > 20) { printf("\033[1A\033[K输入序号超过范围,请请重新输入要修改的学生所在行的序号(1~%d):", ROW); while (getchar() != '\n') { continue; } goto resetIndex; } } if (realNumber <= 0) { printf("\033[1A\033[K输入错误,请重新输入要修改的学生所在行的序号(1~%d):", ROW); goto resetIndex; } (*pindex) = realNumber; tempStu = (*ppcurrStu)[(*pindex) - 1]; //Id======================================================== tempStu.id[13] = '\0'; printf("\033[1A\033[K是否修改学号(是:e/E,否:q/Q):"); while ((isSkip=getchar()) != 'q' && isSkip != 'Q' && isSkip != 'e' && isSkip != 'E') { if (isSkip != '\n') { while (getchar() != '\n') { continue; } } printf("\033[1A\033[K输入错误,请重新输入,是否修改学号(是:e/E,否:q/Q):"); } while (getchar() != '\n') { continue; } if (isSkip == 'e' || isSkip == 'E') { printf("\033[1A\033[K请输入修改后的学号(13位数字):"); resetId:; for (int i = 0; i < 13; i++) { tempStu.id[i] = getchar(); if (tempStu.id[i] < '0' || tempStu.id[i]>'9') { if (tempStu.id[i] != '\n') { while (getchar() != '\n') { continue; } } printf("\033[1A\033[K输入错误,请重新开始输入修改后的学号(13位数字):"); goto resetId; } } if (getchar() != '\n') { printf("\033[1A\033[K输入学号过长,请重新开始输入修改后的学号(13位数字):"); while (getchar() != '\n') { continue; } goto resetId; } tempStu.id[13] = '\0'; } //姓名======================================================== printf("\033[1A\033[K是否修改姓名(是:e/E,否:q/Q):"); while ((isSkip = getchar())!='q'&&isSkip!='Q'&&isSkip!='e'&&isSkip!='E') { if (isSkip != '\n') { while (getchar() != '\n') { continue; } } printf("\033[1A\033[K输入错误,请重新输入,是否修改姓名(是:e/E,否:q/Q):"); } while (getchar() != '\n') { continue; } if (isSkip == 'e' || isSkip == 'E') { printf("\033[1A\033[K请输入修改后的姓名(最大20个字符,一个汉字算两个字符):"); resetName:; int i = 0; while ((tempStu.name[i] = getchar()) != '\n' && i <= 19) { i++; } if (tempStu.name[i] != '\n') { printf("\033[1A\033[K输入姓名过长,请重新输入修改后的姓名(最大20个字符,一个汉字算两个字符):"); while (getchar() != '\n') { continue; } goto resetName; } tempStu.name[i] = '\0'; } //成绩============================================================== printf("\033[1A\033[K是否修改语文成绩(是:e/E,否:q/Q):"); while ((isSkip = getchar())!='q'&&isSkip!='Q'&&isSkip!='e'&&isSkip!='E') { if (isSkip != '\n') { while (getchar() != '\n') { continue; } } printf("\033[1A\033[K输入错误,请重新输入,是否修改语文成绩(是:e/E,否:q/Q):"); } while (getchar() != '\n') { continue; } if (isSkip == 'e' || isSkip == 'E') { printf("\033[1A\033[K请输入修改后的语文成绩(000.00~100.00):"); tempStu.chinese = getScore("修改后的语文"); } printf("\033[1A\033[K是否修改数学成绩(是:e/E,否:q/Q):"); while ((isSkip = getchar()) != 'q' && isSkip != 'Q' && isSkip != 'e' && isSkip != 'E') { if (isSkip != '\n') { while (getchar() != '\n') { continue; } } printf("\033[1A\033[K输入错误,请重新输入,是否修改数学成绩(是:e/E,否:q/Q):"); } while (getchar() != '\n') { continue; } if (isSkip == 'e' || isSkip == 'E') { printf("\033[1A\033[K请输入修改后的数学成绩(000.00~100.00):"); tempStu.math = getScore("修改后的数学"); } printf("\033[1A\033[K是否修改英语成绩(是:e/E,否:q/Q):"); while ((isSkip = getchar()) != 'q' && isSkip != 'Q' && isSkip != 'e' && isSkip != 'E') { if (isSkip != '\n') { while (getchar() != '\n') { continue; } } printf("\033[1A\033[K输入错误,请重新输入,是否修改英语成绩(是:e/E,否:q/Q):"); } while (getchar() != '\n') { continue; } if (isSkip == 'e' || isSkip == 'E') { printf("\033[1A\033[K请输入修改后的英语成绩(000.00~100.00):"); tempStu.english = getScore("修改后的英语"); } tempStu.sum = tempStu.chinese + tempStu.math + tempStu.english; (*ppcurrStu)[(*pindex) - 1] = tempStu; char op = printPage(*ppcurrStu, *premain, *psum, *pindex); return op; } char processDeleteStu(student** ppheadStu, int* psum, int* premain, int* pindex, student** ppcurrStu) { printf("\033[1A\033[K输入要删除的学生成绩所在行的序号(1~%d):", ROW); reset:; int realNumber = 0; char charNumber; while ((charNumber = getchar()) != '\n') { if (charNumber < '0' || charNumber>'9') { printf("\033[1A\033[K输入错误,请重新输入要删除的学生成绩所在行的序号(1~%d):", ROW); while (getchar() != '\n') { continue; } goto reset; } realNumber *= 10; realNumber += charNumber - '0'; if (realNumber > ROW) { printf("\033[1A\033[K输入序号超过范围,请重新输入要删除的学生成绩所在行的序号(1~%d):", ROW); while (getchar() != '\n') { continue; } goto reset; } } if (realNumber <= 0) { printf("\033[1A\033[K输入错误,请重新输入要删除的学生成绩所在行的序号(1~%d):", ROW); goto reset; } if ((*premain) < realNumber) { printf("\033[1A\033[K该序号所在行没有学生数据,请重新输入要删除的学生数据所在行的序号(1~%d):", ROW); goto reset; } for (int i = 0; i < (*premain) - realNumber; i++) { (*ppcurrStu)[realNumber - 1 + i] = (*ppcurrStu)[realNumber - 1 + i + 1]; } (*psum)--; (*premain)--; student* ptempHeadStu = (student*)malloc(sizeof(student) * (*psum)); if (ptempHeadStu == NULL) { perror("processDeleteStu,malloc,开辟空间错误"); return 'q'; } for (int i = 0; i < (*psum); i++) { ptempHeadStu[i] = (*ppheadStu)[i]; } free(*ppheadStu); (*ppheadStu) = ptempHeadStu; (*ppcurrStu) = (*ppheadStu) + ((*psum) - (*premain)); if ((*premain) == 0) { (*ppcurrStu) -= ROW; (*premain) = ROW; (*pindex) = ROW; } return printPage((*ppcurrStu), (*premain), (*psum), (*pindex)); }
数据生成程序,用于测试
#include <stdio.h> #include <stdlib.h> #include <time.h> typedef struct student { char id[14]; char name[21]; double chinese; double math; double english; double sum; } student; void studentCreate(student*); void read(FILE* fp,int num); int main() { srand((unsigned)time(NULL)); /*printf("%d", rand());*/ int num; FILE* fp; student* arr; //获取个数,创建数组 printf("输入要生成的学生数据个数:"); scanf("%d", &num); getchar(); arr = (student*)malloc(sizeof(student) * num); //打开文件,写入学生人数 fp = fopen("students.txt", "w+"); if (!fp) { perror("打开文件失败:"); exit(EXIT_FAILURE); } fwrite(&num, sizeof(int), 1, fp); //结构数组赋值 for (int i = 0; i < num; i++) { studentCreate(arr + i); arr[i].sum = arr[i].chinese + arr[i].math + arr[i].english; } fwrite(arr, sizeof(student), num, fp); /*for (int i = 0; i < num; i++) { fwrite(arr + i, sizeof(student), 1, fp); }*/ rewind(fp); read(fp,num); fclose(fp); free(arr); return 0; } void studentCreate(student* stu) { int ran; //id,id用的int型数据,输出时需要用格数转换说明,以在位数不够四位时补零 for (int i = 0; i < 13; i++) { ran = rand() % 10; stu->id[i] = '0' + ran; } stu->id[13] = '\0'; //name ran = rand() % 49 + 1; int i = 0; for (i; i < 10; i++) { ran = rand() % 26; if (ran % 2 == 0) { stu->name[i] = 'a' + ran; } else { stu->name[i] = 'A' + ran; } } stu->name[i] = '\0'; //score ran = rand() % 101; stu->chinese = ran; ran = rand() % 101; stu->math = ran; ran = rand() % 101; stu->english = ran; } void read(FILE* fp,int num) { fread(&num, sizeof(int), 1, fp); printf("%d\n", num); student temp; for (int i = 0; i < num;i++) { fread(&temp, sizeof(student), 1, fp); printf("%-13s\t%-20s\t%.2f\t%.2f\t%.2f\t%.2f\n", temp.id,temp.name,temp.chinese,temp.math,temp.english,temp.sum); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现