(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);
}
}
(C语言)我的第一个项目:命令行窗口下的学生成绩管理系统,及数据生成程序的更多相关文章
- 在windows命令行窗口下执行:查看所有的端口占用情况
开始--运行--cmd 进入命令提示符 输入netstat -ano 即可看到所有连接的PID 之后在任务管理器中找到这个PID所对应的程序如果任务管理器中没有PID这一项,可以在任务管理器中选&qu ...
- 干掉命令行窗口下MySql乱码
晚上重温dos窗口操作mysql的时候,遇到了一个巨蛋疼的问题------>中文验证码 -->_-->,所以找了找资料弄懂了怎么解决乱码问题,,小记一下. 新建一个表 create ...
- windows环境隐藏命令行窗口运行Flask项目
Linux下可以使用nohub来使Flask项目在后台运行,而windows环境下没有nohub命令,如何让Flask项目在windows中在后台运行而不显示命令行窗口呢? 1.写一个.bat脚本来启 ...
- Vue Create 创建一个新项目 命令行创建和视图创建
Vue Create 创建一个新项目 命令行创建和视图创建 开始之前 你可以先 >>:cd desktop[将安装目录切换到桌面] >>:vue -V :Vue CLI 3.0 ...
- C语言项目:学生成绩管理系统
C语言项目:学生成绩管理系统 1.数据结构:学生信息:学号.姓名.年龄.性别.3课成绩 2.功能: (1)增加学生记录 (2) 删除学生记录 (3) 查找学生信息(学号 ...
- 核心系统命令实战 第一章Linux命令行简介
第一章Linux命令行简介 1.1 Linux命令行概述 1.1.1 Linux 命令行的开启和退出 开启:登陆账号密码进入系统 退出:exit/logout 快捷键:Ctrl+d 1.1.2 Li ...
- [项目记录] 用c语言完成的一个学生成绩管理系统
一.要求: 学生成绩管理系统 某班有最多不超过30人(具体人数由键盘输入)参加期末考试,最多不超过6门(具体门数由键盘输入).使用链表编程实现如下菜单驱动的学生成绩管理系统. 从文件读入每个学生个人信 ...
- 【学生成绩管理系统】 大二c语言作业
几年前写的了,只能在命令行窗口运行,虽然比较挫,还是有一定参考价值... #include <cstdio> #include <conio.h> #include <i ...
- 命令行窗口中用telnet测试HTTP协议
1. 命令行窗口中用telnet测试HTTP协议 HTTP消息是由普通ASCII文本组成.消息包括消息头和数据体部分.消息头以行为单位,每行以CRLF(回车和换行)结束,消息头结束后,额外增加一个CR ...
- C语言实现---学生成绩管理系统
C语言实现了学生成绩管理系统,可以进行学生成绩的增加,删除,更新,查询,计算和展示. 完整代码如下: #include<stdio.h> #include<stdlib.h> ...
随机推荐
- 广州|阿里云 Serverless 技术实战营邀你来玩!
活动简介 "Serverless 技术实战与创新沙龙 " 是一场以 Serverless 为主题的开发者活动,活动受众以关注Serverless 技术的开发者.企业决策人.云原生领 ...
- 从青铜到王者,揭秘 Serverless 自动化函数最佳配置
[福利活动]1分钟Serverless部署PHP商城实验班上线啦! 带你体验如何使用 Serverless 应用引擎 SAE 快速部署一个PHP商城,并体验 SAE 带来的弹性伸缩.应用监控等强大能力 ...
- Vue3 Diff算法之最长递增子序列,学不会来砍我!
专栏分享:vue2源码专栏,vue3源码专栏,vue router源码专栏,玩具项目专栏,硬核推荐 欢迎各位ITer关注点赞收藏 Vue2 Diff算法可以参考[Vue2.x源码系列08]Diff算法 ...
- sipp3.6多方案压测脚本
概述 SIP压测工具sipp,免费,开源,功能足够强大,配置灵活,优点多. 有时候我们需要模拟现网的生产环境来压测,就需要同时有多个sipp脚本运行,并且需要不断的调整呼叫并发. 通过python脚本 ...
- DOCKER本地仓库
概述 随着docker的应用越来越多,安装部署越来越方便,批量自动化的镜像生成和发布都需要docker仓库的本地化应用. 试用了docker的本地仓库功能,简单易上手,记录下来以备后用. 环境 cen ...
- freeswitch的distributor模块
概述 freeswitch 是一款简单好用的VOIP开源软交换平台. 当呼叫是同一个入中继,但是有多条出中继时,需要对出中继做负载均衡,mod_distributor模块可以完成对应的配置和路由. m ...
- iview 表单有值却校验失败
转载请注明出处: iview 表单校验数值的时候,表单有值,却在提交的时候,提示表单校验失败: 解决方案: 1. IviewUI的文档里查到了rules规则里面有个校验类型的属性字段type rule ...
- 13-Verilog for Design
Verilog for Design 设计人员知道写的RTL可以综合成么样的电路 设计人员对于硬件系统进行描述 验证人员搭建验证环境对设计人员描述的硬件系统进行验证 对Standcell,模拟/定制I ...
- Linux-服务管理-service-checkconfig
- [转帖]解Bug之路-记一次JVM堆外内存泄露Bug的查找
https://zhuanlan.zhihu.com/p/245401095 解Bug之路-记一次JVM堆外内存泄露Bug的查找 前言 JVM的堆外内存泄露的定位一直是个比较棘手的问题.此次的Bug查 ...