#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <conio.h>
#define HEAD_N 10 //Input_uint(FILE *fp)函数中限制输入的最大整数位数
#define Class_N 15 //最多科目门数
typedef struct Student
{
char name[20];
int num;
float score[Class_N];
float tot;
float arv;
struct Student *pnext;
}student;//学生信息记录结构体
typedef struct
{
int count,Class_M;//链表记录条数
student *pcurr;//链表当前记录指针
char subject[Class_N][20];//二维数组记录科目名称用
int flag; //是否正常退出标志
}mess;//运行参数设置用的结构体
mess info;//定义链表信息全局变量
int Input_uint(FILE *fp); //无符号整形输入函数
int Input_uint(FILE *fp,int digit );//无符号整形输入重载函数
void filebegin(FILE **fphead, double *start, char *argv[]);//程序开头建立同名文件并计时开始函数
void fileend(FILE **fphead,double *start,double *finish);//程序结束释放文件并计时结束函数
student *creat(FILE *fp);//建立链表函数
student *appen(FILE *fp,student *phead);//追加记录函数
student *delet(FILE *fp,student *phead, int index);//删除一条记录函数
student *insert(FILE *fp,student *phead);//在当前记录后插入一个记录函数
void find(FILE *fp,student *phead);//查找函数
void print(FILE *fp,student *phead);//屏幕显示函数
void file(FILE *fp,student *phead );//显示结果存储到文件函数
void sort(FILE *fp,student *phead );//排序记录函数
int main(int argc, char *argv[])
{
double start, finish;
FILE *fp, *fpini,*fpdb;//*fp 日志文件,*fpini 信息文件,*fpdb 数据文件
int choice=5,par;// choice 菜单选择项
student *phead,*tempstudent, *tempptail;
filebegin(&fp, &start, argv);
//filebegin(&fp, &start, &argv[0]);也一样
info.flag = 1;
if ( (fpini=fopen("student.ini", "rb+"))==NULL)
{
if ( (fpini=fopen("student.ini", "wb+"))==NULL)
{
printf("无法创建或打开\"student.ini\"文件\n");
exit(0);
}
else
{
info.count = 0;
info.pcurr = NULL;
printf("请输入科目的门数(最多不能超过%d门):",Class_N);
info.Class_M =0;
while(!(info.Class_M >=1 && info.Class_M <= Class_N))
{
info.Class_M = Input_uint(fp,2);
}
printf("请输入 %d 门科目名称:\n",info.Class_M);
for(int temp = 0 ;temp < info.Class_M ; temp++)
{
printf("请输入第 %d 门科目名称:",temp+1);
scanf("%8s",info.subject[temp]);
}
}
}
else
{
fread(&info,sizeof(mess),1,fpini);
int temp = info.flag;
info.flag=0;
rewind(fpini);
fwrite(&info,sizeof(mess),1,fpini);
fclose(fpini);
fpini=fopen("student.ini", "rb+");
info.flag = temp;
if (info.subject[0][0]==NULL)
{
printf("\"student.ini\"文件错误,请删除该文件,按任意键尝试自动删除!\n");
getchar();
fclose(fpini);
remove("student.ini");
if ( (fpdb=fopen("student.db", "rb+"))==NULL);
else
{
fclose(fpdb);
remove("student.db");
}
exit(0);
}
}
if ( (fpdb=fopen("student.db", "rb+"))==NULL)
{
if ( (fpdb=fopen("student.db", "wb+"))==NULL)
{
printf("无法创建或打开\"student.db\"文件\n");
exit(0);
}
else
{
phead = creat(fp);//建立链表
getchar();
}
}
else
{
tempptail = phead = (student*)malloc(sizeof(student));
fread(phead,sizeof(student),1,fpdb);
for(int temp = 1 ;temp < info.count ; temp++)
{
tempstudent= (student*)malloc(sizeof(student));
tempptail->pnext = tempstudent;
fread(tempstudent,sizeof(student),1,fpdb);
tempptail = tempstudent;
}
}
info.pcurr = phead;
if (0==info.flag)
{
printf("上一次程序非正常退出,请检查数据是否正确?\n");
printf("当前库文件共有记录 %d 条\n",info.count);
printf("当前库文件中记录的科目有:");
for(int temp = 0 ;temp < info.Class_M ; temp++)
{
printf("%8s",info.subject[temp]);
}
printf("\n按任意键继续!\n");
getchar();
}
while(1)
{
if(7==choice)
break;
system("cls");
printf("\n *******学生成绩管理系统****** ");
printf("\n ** 1.追加记录 ** ");
printf("\n ** 2.删除记录 ** ");
printf("\n ** 3.插入记录 ** ");
printf("\n ** 4.查找记录 ** ");
printf("\n ** 5.显示记录 ** ");
printf("\n ** 6.排序记录 ** ");
printf("\n ** 7.退出系统 ** ");
printf("\n ***************************** ");
if(choice>6 ||choice==0)
printf("\n *** 选择错误,请重新选择!***\n");
else
printf("\n *******请您选择******\n");
choice=Input_uint(fp,1);
switch(choice)
{
case 1 :phead = appen(fp,phead);
system("pause");
break;
case 2 :printf("删除第几个学生\n");
fprintf(fp,"删除第几个学生\n");
par=Input_uint(fp,1);
phead = delet(fp,phead,par);
system("pause");
break;
case 3 :phead = insert(fp,phead);
system("pause");
break;
case 4 :find(fp,phead);
system("pause");
break;
case 5 :print(fp,phead);
file(fp,phead);
system("pause");
break;
case 6 :sort(fp,phead);break;
case 7 :break;
default:break;
}
}
fileend(&fp, &start, &finish);
info.flag=1;
rewind(fpini);
fwrite(&info,sizeof(mess),1,fpini);
fclose(fpini);
if (phead != NULL)//判断是不是为空记录
{
rewind(fpdb);
for(int temp = 0 ;temp < info.count ; temp++)
{
fwrite(phead,sizeof(student),1,fpdb);
phead = phead->pnext;
}
fclose(fpdb);
}
else
{
fclose(fpdb);
remove("student.db");
}
return 0;
}
int Input_uint(FILE *fp)
{
int count_a=0,ratio=1,count_c=0,number=0,judge=1;
//count_a计数变量;ratio转换系数,1,10,100等;
// count_c计数变量;number函数返回值;
// judge输入判断变量,-1出错;
char str[HEAD_N];
for (count_a=0;count_a<HEAD_N;count_a++)
str[count_a]='0';
do
{
if (judge==1) ;
else
{
printf("\n输入错误。请输入一个大于等于零的整数:");
fprintf(fp,"\n输入错误。请输入一个大于等于零的整数:");
}
judge=1;
for(count_a=HEAD_N-1,count_c=HEAD_N-1;count_a>=0;count_a--,count_c--)
{
str[count_a]=getchar();
if(!((str[count_a]>='0' && str[count_a]<='9') || (str[count_a]==10)))
judge=-1;
if(str[count_a]==10 || count_a==0)
{
if(str[count_a]==10)
break;
if(str[count_a]!=10 && count_a==0)
{
for(;;)
{
count_a=getchar();
if(!((count_a>='0'&& count_a<='9') || (count_a==10)))
judge=-1;
if (count_a==10)
break;
}
}
count_a=0;
}
}
if (judge>0)
{
for(count_a=count_c+1;count_a<HEAD_N;count_a++)
{
if (str[count_a]>='0' && str[count_a]<='9')
{
number+=(str[count_a]-'0')*ratio;
ratio*=10;
}
else
break;
}
}
}while(judge<0);
return number;
}
int Input_uint(FILE *fp,int digit )
{
int count_a=0,ratio=1,count_c=0,number=0,judge=1;
//count_a计数变量;ratio转换系数,1,10,100等;
// count_c计数变量;number函数返回值;
// judge输入判断变量,-1出错;
char str[HEAD_N];
if (digit>HEAD_N)
digit=HEAD_N;
for (count_a=0;count_a<digit;count_a++)
jhanker
- 粉丝: 4
- 资源: 17
最新资源
- 部署 yolox 算法使用 deepstream.zip
- 基于webmagic、springboot和mybatis的MagicToe Java爬虫设计源码
- 通过实时流协议 (RTSP) 使用 Yolo、OpenCV 和 Python 进行深度学习的对象检测.zip
- 基于Python和HTML的tb商品列表查询分析设计源码
- 基于国民技术RT-THREAD的MULTInstrument多功能电子测量仪器设计源码
- 基于Java技术的网络报修平台后端设计源码
- 基于Python的美食杰中华菜系数据挖掘与分析设计源码
- 30.STM32_UART_RFID_读卡号_初始化钱包_语音.rar
- 基于Java开发的个人知识库记录系统设计源码
- 通过 LibTorch C++ API 部署 YOLOv5 进行实时对象检测.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈