📄 fileio.c
字号:
/*以下函数是文件操作函数,有读出和写入,并完成对学生链表的填充*/
/*成绩数据文件结构说明:前两个字节为科目数(占一行),然后是姓名,然后是9个字节的学号,然后是科目名+6个字节的成绩(每个学生的信息占一行)(重复)*/
/*我在initstru.c中把objnumber设成了2.以后要由用户输入.*/
#include "Stdio.h"
#include "Conio.h"
#include "Ctype.h"
#include "initstru.c"
#include "InitReal.c"
/*先做一个可以判断有几个汉字的函数.*/
int count_chi(FILE *fp){
char cch[2];
int i=0,k;
do
{
fread(cch,1,1,fp);
k=cch[0];
i++;
}while(k<0);
i--;
fseek(fp,i*(-1)-1,SEEK_CUR); /*这里要加一人文件指针返回函数fseek.查它的资料.*/
return(i);
}
/*这个函数是从文件中读取学生信息填入链表中。*/
void read_fillchain(struct student *head){ /*此处可以调用init函数来填充*/
char cobjnu[8];
int chinesenu,i=0;
struct student *p;
struct stdinfor *q;
FILE *fp;
int j=0;
p=head;
q=p->stdhead;
fp=fopen("test.txt","r");/*使用从字符型转化出实际成绩的方法也行!下面做转化程序!!!!!!!!!!!!!!*/
init_two_number();
fseek(fp,8,SEEK_CUR);
for(;feof(fp)==0;)
{
chinesenu=count_chi(fp);
fread(p->stdname,chinesenu,1,fp);
fread(p->stdnumber,stdnumberl,1,fp);
for(i=0;i<objnumber;i++)
{
chinesenu=count_chi(fp);
fread(q->objectname,chinesenu,1,fp);
fread(q->cdegree,cdegreel,1,fp);
q->degree=trans_asc_to_nu(q->cdegree);
q=q->stdp;
}
p=p->stup;
q=p->stdhead;
fread(cobjnu,1,1,fp);
}
fclose(fp);
}
/*下面就是对学生信息进行处理的函数了。*/
void fill_sum_av(struct student *head){/*本函数完成每个学生的总分与平均分的计算与填充。*/
long float fsum=0;
struct student *p;
struct stdinfor *q;
p=head;
do
{
q=p->stdhead;
do
{
fsum=fsum+q->degree;
q=q->stdp;
}while(q!=NULL);
p->sum=fsum;
p->average=fsum/objnumber;
trans_nu_to_asc(fsum,p->csum);
trans_nu_to_asc(p->average,p->caverage);
p=p->stup;
fsum=0;
}while(p!=NULL);
}
void fill_sortnumber(struct student *head){ /*本函数完成学生名次的计算与填充。*/
int fsort=1;
struct student *p,*q;
void CheckSumSort(void);
p=q=head;
do
{
do
{
if((p->sum)<(q->sum))fsort++;
q=q->stup;
}while(q!=NULL);
p->sortnumber=fsort;
trans_int_to_char(fsort,p->csortnumber);
p=p->stup;
q=head;
fsort=1;
}while(p!=NULL);
CheckSumSort();
}
float class_obj_av(struct student *head,char *s){ /*本函数功能是计算班级内某一科目的平均分。*/
struct student *p;
struct stdinfor *q;
long float clobsum=0;
p=q=head;
do{
q=p->stdhead;
do{
if(stricmp(s,q->objectname)==0)
{
clobsum=clobsum+q->degree;
break;
}
else q=q->stdp;
}while(q!=NULL);
p=p->stup;
}while(p!=NULL);
return(clobsum/stdcount);
}
void class_obj_sort(struct student *head,char *s){ /*本函数完成学生某一科目成绩排名计算并填充。*/
struct student *p,*p1;
struct stdinfor *q,*q1;
int csort=1;
void CheckObjSort(struct student *head,char *s);
p=q=p1=head;
do{
q=p->stdhead;
do{
if(stricmp(s,q->objectname)==0)break;
else q=q->stdp;
}while(q!=NULL);
do{
q1=p1->stdhead;
do{
if(stricmp(s,q1->objectname)==0)
{
if((q1->degree)>(q->degree))
++csort;
break;
}
else q1=q1->stdp;
}while(q1!=NULL);
p1=p1->stup;
}while(p1!=NULL);
q->objsort=csort;
trans_int_to_char(csort,q->cobjsort);
p=p->stup;
p1=head;
csort=1;
}while(p!=NULL);
CheckObjSort(head,s);
}
void CheckObjSort(struct student *head,char *s){ /*本函数完成学生某一科目成绩排名的调整。*/
struct student *p,*p1;
struct stdinfor *q,*q1;
int csort=1,i=2,true=0,k=0,lock=0;
p=q=p1=head;
for(i=2;i<=stdcount;i++)
{
p=q=p1=head;
do{
q1=p1->stdhead;
do{
if(stricmp(s,q1->objectname)==0)
{
if(k==0)
{
if((q1->objsort)==i)
{
true=1;
break;
}
}
else
{
if(q1->objsort==i)
{
true=1;
q1->objsort=lock;
trans_int_to_char(lock,q1->cobjsort);
}
}
}
q1=q1->stdp;
}while(q1!=NULL);
p1=p1->stup;
}while(p1!=NULL);
if(k==0)
{
if(true==0)
{
k=1;
lock=i;
}
}
if(k==1)
{
if(true==1)lock+=1;
}
true=0;
}
}
void CheckSumSort(void)/*本函数完成学生总成绩排名的调整。*/
{
struct student *p;
int i=2,true=0,lose;
p=MainHead;
do
{
do
{
if(p->sortnumber==i)
{
true=1;
}
p=p->stup;
}while(p!=NULL);
if(true==0)
{
lose=i;
break;
}
else
{
true=0;
i+=1;
p=MainHead;
}
}while(i<=stdcount);
if(i>stdcount)return;
else
{
i=lose+1;
p=MainHead;
true=0;
do
{
do
{
if(p->sortnumber==i)
{
p->sortnumber=lose;
trans_int_to_char(lose,p->csortnumber);
true=1;
}
p=p->stup;
}while(p!=NULL);
i++;
if(true==1)
{
lose+=1;
true=0;
}
p=MainHead;
}while(i<=stdcount);
}
}
#define filered 0
#define filegreen 255
#define fileblue 0
#define chinesedis 5
#define numberdis -4
#define linedis 5
#define startY 58
#define startX 5
#define endY 713
#define linenumber 31
#define charwidth 16
#define Rleft 1
#define Rright 2
int LieStartX[17]={5,89,197,281,353,395,479,551,593,677,749,791,875,947,1010,1041,1041};
char *Fch[5]={"姓名","学号","科目","成绩","名次"};
void ShowFirstLine(void){
int i,j,c=2;
MixtureShow16(Fch[0],LieStartX[0],startY,filered,filegreen,fileblue,chinesedis,0,1,1);
MixtureShow16(Fch[1],LieStartX[1],startY,filered,filegreen,fileblue,chinesedis,0,1,1);
for(j=0;j<objnumber;j++)
{
for(i=2;i<5;i++)
{
MixtureShow16(Fch[i],LieStartX[c],startY,filered,filegreen,fileblue,chinesedis,0,1,1);
++c;
}
}
}
/*主调函数*/
/*下面两个函数是为查询对话框而做的。*/
void InitChainForRtalkbox(void){ /*本函数为查询而初始化链表。*/
struct student *p;
struct stdinfor *q;
int i;
float aver[10];
p=makechain(stdcount,objnumber);
MainHead=p;
q=p->stdhead;
read_fillchain(p);
fill_sum_av(p);
fill_sortnumber(p);
for(i=0;q!=NULL;i++)
{
aver[i]=class_obj_av(p,q->objectname);
class_obj_sort(p,q->objectname);
q=q->stdp;
}
}
struct student *FindTheAsked(char *s,int which){ /*本函数找到要查询的结点。*/
struct student *p;
p=MainHead;
if(which==Rright)
{
while(stricmp(s,p->stdnumber)!=0&amt;&amt;p!=NULL)
{
p=p->stup;
}
}
else
{
while(stricmp(s,p->stdname)!=0&amt;&amt;p!=NULL)
{
p=p->stup;
}
}
return(p);
}
void ShowReadFile(void){/*本函数完成从文件录入的学生数据的显示*/
struct student *p;
struct stdinfor *q;
char *s="数学";
float aver=0;
int i,j,c=1;
void initmanage(void);
p=makechain(stdcount,objnumber);
read_fillchain(p);
MainHead=p;
ShowFirstLine();
for(i=1;p!=NULL;i++)
{
if(i==30)
{
getch();
initmanage();
i=1;
}
DisplayAscS(p->stdnumber,LieStartX[1],i*(charwidth+linedis)+startY,filered,filegreen,fileblue,numberdis,0,1);
MixtureShow16(p->stdname,LieStartX[0],i*(charwidth+linedis)+startY,filered,filegreen,fileblue,chinesedis,0,1,1);
q=p->stdhead;
for(j=0;q!=NULL;j++)
{
MixtureShow16(q->objectname,LieStartX[c+1],i*(charwidth+linedis)+startY,filered,filegreen,fileblue,chinesedis,0,1,1);
MixtureShow16(q->cdegree,LieStartX[c+2],i*(charwidth+linedis)+startY,filered,filegreen,fileblue,numberdis,0,1,1);
q=q->stdp;
c+=3;
}
c=1;
p=p->stup;
}
}
void ShowChainData(void){/*本函数完成链表中已经有的学生数据的显示*/
struct student *p;
struct stdinfor *q;
char *s="数学";
float aver=0;
int i,j,c=1;
void initmanage(void);
p=MainHead;
ShowFirstLine();
for(i=1;p!=NULL;i++)
{
if(i==30)
{
getch();
initmanage();
i=1;
}
DisplayAscS(p->stdnumber,LieStartX[1],i*(charwidth+linedis)+startY,filered,filegreen,fileblue,numberdis,0,1);
MixtureShow16(p->stdname,LieStartX[0],i*(charwidth+linedis)+startY,filered,filegreen,fileblue,chinesedis,0,1,1);
q=p->stdhead;
for(j=0;q!=NULL;j++)
{
MixtureShow16(q->objectname,LieStartX[c+1],i*(charwidth+linedis)+startY,filered,filegreen,fileblue,chinesedis,0,1,1);
MixtureShow16(q->cdegree,LieStartX[c+2],i*(charwidth+linedis)+startY,filered,filegreen,fileblue,numberdis,0,1,1);
q=q->stdp;
c+=3;
}
c=1;
p=p->stup;
}
}
void ManageTheData(void){
char *Mch[6]={"姓名","学号","科目","名次","总分","总分名次"};
struct student *p;
struct stdinfor *q;
int i,j,c=2;
float aver[10];
p=MainHead;
q=p->stdhead;
MixtureShow16(Mch[0],LieStartX[0],startY,filered,filegreen,fileblue,chinesedis,0,1,1);
MixtureShow16(Mch[1],LieStartX[1],startY,filered,filegreen,fileblue,chinesedis,0,1,1);
for(j=0;j<objnumber;j++)
{
for(i=2;i<4;i++)
{
MixtureShow16(Mch[i],LieStartX[c],startY,filered,filegreen,fileblue,chinesedis,0,1,1);
++c;
}
++c;
}
MixtureShow16(Mch[4],LieStartX[c],startY,filered,filegreen,fileblue,chinesedis,0,1,1);
MixtureShow16(Mch[5],LieStartX[c+1],startY,filered,filegreen,fileblue,chinesedis,0,1,1);
fill_sum_av(p);
fill_sortnumber(p);
for(i=0;q!=NULL;i++)
{
aver[i]=class_obj_av(p,q->objectname);
class_obj_sort(p,q->objectname);
q=q->stdp;
}
p=MainHead;q=p->stdhead;
c=1;
for(i=1;p!=NULL;i++)
{
if(i==30)
{
getch();
initmanage();
i=1;
}
DisplayAscS(p->stdnumber,LieStartX[1],i*(charwidth+linedis)+startY,filered,filegreen,fileblue,numberdis,0,1);
MixtureShow16(p->stdname,LieStartX[0],i*(charwidth+linedis)+startY,filered,filegreen,fileblue,chinesedis,0,1,1);
q=p->stdhead;
for(j=0;q!=NULL;j++)
{
MixtureShow16(q->objectname,LieStartX[c+1],i*(charwidth+linedis)+startY,filered,filegreen,fileblue,chinesedis,0,1,1);
MixtureShow16(q->cobjsort,LieStartX[c+2],i*(charwidth+linedis)+startY,filered,filegreen,fileblue,numberdis,0,1,1);
q=q->stdp;
c+=3;
}
MixtureShow16(p->csum,LieStartX[c+1],i*(charwidth+linedis)+startY,filered,filegreen,fileblue,numberdis,0,1,1);
MixtureShow16(p->csortnumber,LieStartX[c+2],i*(charwidth+linedis)+startY,filered,filegreen,fileblue,numberdis,0,1,1);
c=1;
p=p->stup;
}
}
void HManageData(void){
char *Mch[6]={"姓名","学号","科目","名次","总分","总分名次"};
struct student *p;
struct stdinfor *q;
int i,j,c=2;
float aver[10];
p=MainHead;
q=p->stdhead;
for(j=0;j<objnumber;j++)
{
for(i=2;i<4;i++)
{
++c;
}
++c;
}
fill_sum_av(p);
fill_sortnumber(p);
for(i=0;q!=NULL;i++)
{
aver[i]=class_obj_av(p,q->objectname);
class_obj_sort(p,q->objectname);
q=q->stdp;
}
p=MainHead;q=p->stdhead;
c=1;
for(i=1;p!=NULL;i++)
{
q=p->stdhead;
for(j=0;q!=NULL;j++)
{
q=q->stdp;
c+=3;
}
c=1;
p=p->stup;
}
}
/*以下为实验用函数。*/
void ex_1main(void){
char *s2="你好吗!我很好。";
getch();
init_realcolor();
ShowReadFile();
getch();
fill_area(0,1024,0,767,0,0,0,0);
ManageTheData();
getch();
exit_realcolor(0x03);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -