?? stu_info_7.8.c
字號:
***參數:char fname[100]
***功能:截取文件名
***返回值:文件名
*****************************************************************/
char *Getfilename(char fname[100],char file[100])
{
int i,t=0; //記錄最后一個"\"位置
for(i=strlen(fname)-1;i>=0;i--) //截取文件名
{
if(fname[i]=='\\')
{
t=i;
break;
}
}
strcpy(file,fname+t+1);
return file;
}
/*****************************************************************
***函數名:SaveData
***參數:無
***功能:實現數據保存到文件
***返回值:無
*****************************************************************/
void SaveData()
{
int len,i,x,y,len_file,len_file2,t,f=0; //T記錄文件名的后綴位置
char filename[100]={'0'},file[100]={'0'},file2[100]={'0'};
FILE *fp1,*fp2;
if(save_flag==0)
{
ClearMessage();
Gotoxy(0,19);
printf("Not any record !Pleased key to continue ...");
getch();
return;
}
do
{
Gotoxy(0,20);
printf("%*s",80," ");
Gotoxy(0,22);
printf("For example E:\\CN0804\\data.bat || data || data.bat");
ClearMessage();
Gotoxy(0,19);
printf("Please Input filename path:");
GetString(filename,50);
Left_Right(filename);
len=strlen(filename);
if(len==0)
{
Gotoxy(0,20);
printf("Error!filename NULL!");
getch();
Gotoxy(0,20);
printf("%*s",80," ");
continue;
}
//截取文件名
strcpy(file,Getfilename(filename,file));
len_file=strlen(file);
for(i=0;i<len_file;i++) //判斷文件名長度<=8
{
if(file[i]=='.')
{
t=i;
f=1;
break;
}
else if(file[i]!='.') //無后綴
{
f=0;
}
if(file[i]=='\\' || file[i]=='/' || file[i]==':' || file[i]=='*' || file[i]=='?'
|| file[i]=='\"' ||file[i]=='<' || file[i]=='>' || file[i]=='|') //判斷文件名合法
{
f=-1;
}
}
if(f==1)
{
strncpy(file2,file,t);
len_file2=strlen(file2);
file2[len_file2]='\0';
if(len_file2>8)
{
Gotoxy(0,20);
printf("Error!Input filename > 8.Please enter again!");
getch();
continue;
}
}
else if(f==0) //文件名長度>8
{
len=strlen(filename);
if(len>8)
{
Gotoxy(0,20);
printf("Error!Input filename > 8.Please enter again!");
getch();
continue;
}
}
else if(f==-1)//非法字符
{
Gotoxy(0,20);
printf("%*s",80," ");
Gotoxy(0,20);
printf("Error!Input filename illegal.Please enter again!");
getch();
continue;
}
for(i=0;i<len;i++) //創建多級目錄
{
if(filename[i]=='\\')
{
filename[i]='\0';
_mkdir(filename);
filename[i]='\\';
}
}
Gotoxy(0,20);
printf("System create files-ing...");
Sleep(600);
Gotoxy(0,20);
printf("%*s",80," ");
//判斷文件是否存在 0/-1
if(access(filename,0)==-1) //不存在
{
fp1=fopen(filename,"wb");
if((fp1==NULL))
{
ClearMessage();
Gotoxy(0,19);
printf("Error!can not save file!\n");
getch();
return;
}
fwrite(&stu[0],sizeof(STU),count,fp1);
if(fclose(fp1)==EOF)
{
ClearMessage();
Gotoxy(0,19);
printf("Error!Close file fail!\n");
getch();
return;
}
else
{
save_flag=0;
ClearMessage();
Gotoxy(0,19);
printf("Save Data success!Pleased key to continue ...");
getch();
return ;
}
}
else //存在
{
ClearMessage();
Gotoxy(0,19);
printf("The filename already exists!Do you overwrite?YES[Y/y] or [N/n] ");
Getxy(&x,&y);
do
{
Gotoxy(x,y);
ch=getche();
getch();
if(ch=='Y' || ch=='y')
{
fp2=fopen(filename,"wb");
if((fp2==NULL))
{
ClearMessage();
Gotoxy(0,19);
printf("can not save file!\n");
getch();
return;
}
fwrite(&stu[0],sizeof(STU),count,fp2);
if(fclose(fp2)==EOF)
{
ClearMessage();
Gotoxy(0,19);
printf("Close file fail!\n");
getch();
return;
}
else
{
save_flag=0;
ClearMessage();
Gotoxy(0,19);
printf("Save Data success!Pleased key to continue ...");
getch();
return ;
}
break;
}
else if(ch=='N' || ch=='n')
break;
else
InputError(x,y,ch);
}while(!(ch=='Y' || ch=='y' || ch=='N' || ch=='n'));
}
}while(ch=='N' || ch=='n');
}
/*****************************************************************
***函數名:LocalData
***參數:無
***功能:在本地機上讀取數據到系統
***返回值:無
*****************************************************************/
void LocalData()
{
int len;
FILE *fp;
char filename[100];
int i,x,y;
if(save_flag==1)
{
do
{
ClearMessage();
Gotoxy(0,19);
printf("Are you save Data?[Y]/[N]:");
Getxy(&x,&y);
ch=getche();
getch();
if(ch=='Y' || ch=='y')
{
SaveData();
return;
}
else if(ch=='N' || ch=='n')
{
break;
}
else
{
InputError(x,y,ch);
continue;
}
}while(1);
}
Gotoxy(0,22);
printf("For example E:\\CN0804\\data.bat || data || data.bat");
do
{
Gotoxy(0,20);
printf("%*s",80," ");
ClearMessage();
Gotoxy(0,19);
printf("Please Input filename path:");
GetString(filename,50);
Left_Right(filename);
len=strlen(filename);
if(len==0)
{
Gotoxy(0,20);
printf("Error!filename NULL!");
getch();
Gotoxy(0,20);
printf("%*s",80," ");
continue;
}
if(access(filename,0)!=0)//判斷文件是否存在
{
Gotoxy(0,20);
printf("Error!file does not exist!");
getch();
continue;
}
fp=fopen(filename,"rb");
if(fp==NULL)
{
ClearMessage();
Gotoxy(0,19);
printf("can not open file!\n");
getch();
return;
}
count=0;
while((fread(&stu[count],sizeof(STU),1,fp))==1)
count++;
for(i=0;i<count;i++)
{
sno[i]=i+1;
}
ClearMessage();
Gotoxy(0,19);
printf("open file success!Pleased key to continue ...");
getch();
break;
}while(1);
}
/*****************************************************************
***函數名:Stat
***參數:無
***功能:顯示各項數據統計
***返回值:無
*****************************************************************/
void Stat()
{
int s,i,x,y;
char *Sort[]=
{ "1.Chinese:",
"2.English:",
"3.Math:",
"4.Physics:",
"5.Chemistry:",
"6.Total:",
"0.return MenuList:"
};
system("cls");
Gotoxy(33,3);
printf(STATSCORE);
MakeMenuInfo();
printf("\n");
s=JuZhong(Sort,7); //菜單項--居中顯示
for(i=0;i<7;i++) //打印菜單項
printf("%*s%s\n",s," ",Sort[i]);
Gotoxy(0,0);
Gotoxy(26,15);
printf("Please Make a choices[0-6]:");
Getxy(&x,&y);
if(count==0)
{
ClearMessage();
Gotoxy(0,19);
printf("Error!Data is NULL.Pleased key to continue ...");
fflush(stdin);
getch();
return;
}
do
{
ch=getche();
getch();
switch(ch)
{
case '1':StatScore(0);Stat();break;
case '2':StatScore(1);Stat();break;
case '3':StatScore(2);Stat();break;
case '4':StatScore(3);Stat();break;
case '5':StatScore(4);Stat();break;
case '6':StatTotal();Stat();break;
case '0':return;break;
default:InputError(x,y,ch);break;
}
}while(!(ch>='0' && ch<='6'));
}
/*****************************************************************
***函數名:StatScore
***參數:float score[] ,int num (0-4-->5門成績 )
***功能:顯示成績數據統計
***返回值:無
*****************************************************************/
void StatScore(int num) //按大--》小
{
int i,j,k;
float top_1,top_2,top_3;
float total[100]={0.0};
double avg[100]={0.0};
STU temp;
Gotoxy(0,5);
for(i=5;i<17;i++) //清除5-17行
printf("%*s",80," ");
for(i=1;i<count;i++)
{
for(j=0;j<count-i;j++)
{
if(stu[j].score[num]<stu[j+1].score[num])
{
temp=stu[j];
stu[j]=stu[j+1];
stu[j+1]=temp;
}
}
}
for(i=0;i<5;i++) //各科總成績與平均成績
{
for(j=0;j<count;j++)
{
total[i]+=stu[j].score[i];
}
avg[i]=(double)total[i]/count;
}
Gotoxy(34,5);
printf("(Total avg)");
Gotoxy(19,6);
printf("%-9s%-9s%-6s%-9s%-11s","chinese","english","math","physics","chemistry");
Gotoxy(19,7);
printf(" %-9.1lf%-8.1lf%-8.1lf%-9.1lf%-8.1lf",avg[0],avg[1],avg[2],avg[3],avg[4]);
Gotoxy(34,9);
printf("(Stat Score)");
Gotoxy(19,10);
switch(num)
{
case 0: printf("top %-4s%-20s%-4s%-8s\n","sno","name","age","chinese");
Gotoxy(33,3);printf("Print Chinese");break;
case 1:printf("top %-4s%-20s%-4s%-8s\n","sno","name","age","english");
Gotoxy(33,3);printf("Print English");break;
case 2:printf("top %-4s%-20s%-4s%-8s\n","sno","name","age","math");
Gotoxy(33,3);printf("Print Math");break;
case 3:printf("top %-4s%-20s%-4s%-8s\n","sno","name","age","physics");
Gotoxy(33,3);printf("Print Physics");break;
case 4:printf("top %-4s%-20s%-4s%-8s\n","sno","name","age","chemistry");
Gotoxy(33,3);printf("Print Chemistry");break;
}
top_1=stu[0].score[num];
top_2=stu[1].score[num];
top_3=stu[2].score[num];
for(j=0;j<3;j++) //學科統計前三甲
{
Gotoxy(19,11+j);
printf("%-4d%-4s%-21s%-4d%-8.1f\n",j+1,stu[j].sno,stu[j].name,stu[j].age,stu[j].score[num]);
}
for(k=3;k<count;k++) //取并列
{
if(stu[k].score[num]==top_3)
{
Gotoxy(19,11+k);
printf("%-4d%-4s%-21s%-4d%-8.1f\n",3,stu[k].sno,stu[k].name,stu[k].age,stu[k].score[num]);
continue;
}
}
if(count<2)
{
Gotoxy(0,12);
printf("%*s",80," ");
Gotoxy(0,13);
printf("%*s",80," ");
}
if(count>=2 && count<3)
{
Gotoxy(0,13);
printf("%*s",80," ");
}
Gotoxy(0,19);
printf("Pleased key to continue ...");
fflush(stdin);
getch();
}
/*****************************************************************
***函數名:StatTotal
***參數:無
***功能:顯示總成績數據統計
***返回值:無
*****************************************************************/
void StatTotal()
{
int i,j,k;
float top_3;
float total[100],t;
STU temptotal;
Gotoxy(0,5);
for(i=5;i<17;i++) //清除5-17行
printf("%*s",80," ");
for(i=0;i<count;i++)
total[i]=stu[i].score[0]+stu[i].score[1]+stu[i].score[2]+stu[i].score[3]+stu[i].score[4];
//total從大到小
for(i=1;i<count;i++)
{
for(j=0;j<count-i;j++)
{
if(total[j]<total[j+1])
{
temptotal=stu[j];
stu[j]=stu[j+1];
stu[j+1]=temptotal;
t=total[j];
total[j]=total[j+1];
total[j+1]=t;
}
}
}
top_3=total[2];
Gotoxy(30,3);
printf("Print Total Score");
Gotoxy(0,5);
printf("%-4s%-4s%-21s%-4s%-8s%-8s%-5s%-8s%-10s%-6s\n",
"Top","sno","name","age","chinese","english","math","physics","chemistry","total");
for(j=0;j<3;j++) //打印前三甲
{
Gotoxy(0,6+j);
printf("%-4d%-4s%-21s%-5d%-8.1f%-7.1f%-7.1f%-8.1f%-8.1f%-6.1f\n",j+1,stu[j].sno,stu[j].name,stu[j].age,
stu[j].score[0],stu[j].score[1],stu[j].score[2],stu[j].score[3],stu[j].score[4],total[j]);
}
for(k=3;k<count;k++) //取并列情況
{
if(total[k]==top_3)
{
Gotoxy(6+k);
printf("%-4d%-4s%-21s%-5d%-8.1f%-7.1f%-7.1f%-8.1f%-8.1f%-6.1f\n",3,stu[k].sno,stu[k].name,stu[k].age,
stu[k].score[0],stu[k].score[1],stu[k].score[2],stu[k].score[3],stu[k].score[4],total[k]);
continue;
}
}
if(count==1)
{
for(i=0;i<=2;i++)
{
Gotoxy(0,7+i);
printf("%*s",80," ");
}
}
if(count==2)
{
for(i=0;i<2;i++)
{
Gotoxy(0,8+i);
printf("%*s",80," ");
}
}
Gotoxy(0,19);
printf("Pleased key to continue ...");
fflush(stdin);
getch();
}
/*****************************************************************
***函數名:main
***參數:無
***返回值:return 0
*****************************************************************/
int main()
{
SetColor(39,42);
system("cls");
Welcome();
for(;;)
MenuList();
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -