#include<stdio.h>
#include<stdlib.h>
#define SIZE 5
struct student/*结构体*/
{char name[10];
int num,xuefen;
int score[3];
float ave;
}stud[SIZE],work;
void main(){void sortA(void);/*按平均分升序排列并存档*/
void sortB(void);/*按平均分降序排列并存档*/
void sortC(void);/*按学号升序排列并存档*/
void sortD(void);/*按学号降序排列并存档*/
void save(void);/*存档*/
int i,xuefen;
float sum[SIZE];
FILE *fp1;
for(i=0;i<SIZE;i++)
{scanf("%d %s %d %d %d",&stud[i].num,stud[i].name,&stud[i].score[0],&stud[i].score[1],&stud[i].score[2]);
sum[i]=stud[i].score[0]+stud[i].score[1]+stud[i].score[2];
stud[i].ave=sum[i]/3;
if(stud[i].score[0]>=60) stud[i].xuefen+=1;
if(stud[i].score[1]>=60) stud[i].xuefen+=1;
if(stud[i].score[2]>=60) stud[i].xuefen+=1;
}
save();
printf("please me number 0-3! 0:up by ave! 1:down by ave! 2up by NO! down NO!");
scanf("%d",&i);
switch(i)
{ case0:sortA();break;
case1:sortB();break;
case2:sortC();break;
case3:sortD();break;
}
fp1=fopen("scort.txt","rb");
printf("\n NO name score1 score2 score3 ave xuefen\n");
printf("-----------------------------------------------------------\n");
for(i=0;i<SIZE;i++)
{fread(&stud[i],sizeof(struct student),1,fp1);
printf("%3d %-10s %7d %7d %7d %8.2f %d\n",stud[i].num,stud[i].name,stud[i].score[0],stud[i].score[1],stud[i].score[2],stud[i].ave,stud[i].xuefen);}fclose(fp1);
}=================================/*以下是SOVE SORTA B C D的定义*/================void save(void)
{FILE *fp;
int i;
if((fp=fopen("student.txt","wb"))==NULL)
{printf("The file can't open\n");
return;
}
for(i=0;i<SIZE;i++)
if(fwrite(&stud[i],sizeof(struct student),1,fp)!=1)
{printf("file write error\n");
return;
}
fclose(fp);
}void sortA(void)
{FILE *fp1,*fp2;
int i,j;
if((fp1=fopen("student.txt","rb"))==NULL)
{printf("the file cant open\n\n");
exit(0);
}
if((fp2=fopen("score.txt","wb"))==NULL)
{printf("the file cant open\n");
exit(0);
}
for(i=0;i<SIZE;i++)
if(fread(&stud[i],sizeof(struct student),1,fp1)!=1)
{printf("file read error\n");
exit(0);
}
for(i=0;i<SIZE;i++)
{for(j=i+1;j<SIZE;j++)
if(stud[i].ave<stud[j].ave)
{work=stud[i];
stud[i]=stud[j];
stud[j]=work;
}
fwrite(&stud[i],sizeof(struct student),1,fp2);
}
fclose(fp1);
fclose(fp2);
}
void sortB(void)
{FILE *fp1,*fp2;
int i,j;
if((fp1=fopen("student.txt","rb"))==NULL)
{printf("the file can't open\n");
exit(0);
}
if((fp2=fopen("scort.txt","wb"))==NULL)
{printf("the file can't open\n");
exit(0);
}
for(i=0;i<SIZE;i++)
if(fread(&stud[i],sizeof(struct student),1,fp1)!=1)
{printf("file read error\n");
exit(0);
}
for(i=0;i<SIZE;i++)
{for(j=i+1;j<SIZE;j++)
if(stud[i].ave>stud[i].ave)
{work=stud[i];
stud[i]=stud[j];
stud[j]=work;
}
fwrite(&stud[i],sizeof(struct student),1,fp2);
}
fclose(fp1);
fclose(fp2);}
void sortC(void)
{FILE *fp1,*fp2;
int i,j;
if((fp1=fopen("student.txt","rb"))==NULL)
{printf("the file can't open\n");
exit(0);
}
if((fp2=fopen("scort.txt","wb"))==NULL)
{printf("the file can't open\n");
exit(0);
}
for(i=0;i<SIZE;i++)
if(fread(&stud[i],sizeof(struct student),1,fp1)!=1)
{printf("the file is error\n");
exit(0);
}
for(i=0;i<SIZE;i++)
{for(j=i+1;j<SIZE;j++)
if(stud[i].num<stud[j].num)
{work=stud[i];
stud[i]=stud[j];
stud[j]=work;
}
fwrite(&stud[i],sizeof(struct student),1,fp2);
}
fclose(fp1);
fclose(fp2);
}
void sortD(void)
{FILE *fp1,*fp2;
int i,j;
if((fp1=fopen("student.txt","rb"))==NULL)
{printf("the file can't open\n");
exit(0);
}
if((fp2=fopen("scort.txt","wb"))==NULL)
{printf("the file can't open\n");
exit(0);
}
for(i=0;i<SIZE;i++)
if(fread(&stud[i],sizeof(struct student),1,fp1)!=1)
{printf("the file read error\n");
exit(0);
}
for(i=0;i>SIZE;i++)
{for(j=i+1;j<SIZE;j++)
if(stud[i].num>stud[j].num)
{work=stud[i];
stud[i]=stud[j];
stud[j]=work;
}
fwrite(&stud[i],sizeof(struct student),1,fp2);
}
fclose(fp1);
fclose(fp2);
}
#include<stdlib.h>
#define SIZE 5
struct student/*结构体*/
{char name[10];
int num,xuefen;
int score[3];
float ave;
}stud[SIZE],work;
void main(){void sortA(void);/*按平均分升序排列并存档*/
void sortB(void);/*按平均分降序排列并存档*/
void sortC(void);/*按学号升序排列并存档*/
void sortD(void);/*按学号降序排列并存档*/
void save(void);/*存档*/
int i,xuefen;
float sum[SIZE];
FILE *fp1;
for(i=0;i<SIZE;i++)
{scanf("%d %s %d %d %d",&stud[i].num,stud[i].name,&stud[i].score[0],&stud[i].score[1],&stud[i].score[2]);
sum[i]=stud[i].score[0]+stud[i].score[1]+stud[i].score[2];
stud[i].ave=sum[i]/3;
if(stud[i].score[0]>=60) stud[i].xuefen+=1;
if(stud[i].score[1]>=60) stud[i].xuefen+=1;
if(stud[i].score[2]>=60) stud[i].xuefen+=1;
}
save();
printf("please me number 0-3! 0:up by ave! 1:down by ave! 2up by NO! down NO!");
scanf("%d",&i);
switch(i)
{ case0:sortA();break;
case1:sortB();break;
case2:sortC();break;
case3:sortD();break;
}
fp1=fopen("scort.txt","rb");
printf("\n NO name score1 score2 score3 ave xuefen\n");
printf("-----------------------------------------------------------\n");
for(i=0;i<SIZE;i++)
{fread(&stud[i],sizeof(struct student),1,fp1);
printf("%3d %-10s %7d %7d %7d %8.2f %d\n",stud[i].num,stud[i].name,stud[i].score[0],stud[i].score[1],stud[i].score[2],stud[i].ave,stud[i].xuefen);}fclose(fp1);
}=================================/*以下是SOVE SORTA B C D的定义*/================void save(void)
{FILE *fp;
int i;
if((fp=fopen("student.txt","wb"))==NULL)
{printf("The file can't open\n");
return;
}
for(i=0;i<SIZE;i++)
if(fwrite(&stud[i],sizeof(struct student),1,fp)!=1)
{printf("file write error\n");
return;
}
fclose(fp);
}void sortA(void)
{FILE *fp1,*fp2;
int i,j;
if((fp1=fopen("student.txt","rb"))==NULL)
{printf("the file cant open\n\n");
exit(0);
}
if((fp2=fopen("score.txt","wb"))==NULL)
{printf("the file cant open\n");
exit(0);
}
for(i=0;i<SIZE;i++)
if(fread(&stud[i],sizeof(struct student),1,fp1)!=1)
{printf("file read error\n");
exit(0);
}
for(i=0;i<SIZE;i++)
{for(j=i+1;j<SIZE;j++)
if(stud[i].ave<stud[j].ave)
{work=stud[i];
stud[i]=stud[j];
stud[j]=work;
}
fwrite(&stud[i],sizeof(struct student),1,fp2);
}
fclose(fp1);
fclose(fp2);
}
void sortB(void)
{FILE *fp1,*fp2;
int i,j;
if((fp1=fopen("student.txt","rb"))==NULL)
{printf("the file can't open\n");
exit(0);
}
if((fp2=fopen("scort.txt","wb"))==NULL)
{printf("the file can't open\n");
exit(0);
}
for(i=0;i<SIZE;i++)
if(fread(&stud[i],sizeof(struct student),1,fp1)!=1)
{printf("file read error\n");
exit(0);
}
for(i=0;i<SIZE;i++)
{for(j=i+1;j<SIZE;j++)
if(stud[i].ave>stud[i].ave)
{work=stud[i];
stud[i]=stud[j];
stud[j]=work;
}
fwrite(&stud[i],sizeof(struct student),1,fp2);
}
fclose(fp1);
fclose(fp2);}
void sortC(void)
{FILE *fp1,*fp2;
int i,j;
if((fp1=fopen("student.txt","rb"))==NULL)
{printf("the file can't open\n");
exit(0);
}
if((fp2=fopen("scort.txt","wb"))==NULL)
{printf("the file can't open\n");
exit(0);
}
for(i=0;i<SIZE;i++)
if(fread(&stud[i],sizeof(struct student),1,fp1)!=1)
{printf("the file is error\n");
exit(0);
}
for(i=0;i<SIZE;i++)
{for(j=i+1;j<SIZE;j++)
if(stud[i].num<stud[j].num)
{work=stud[i];
stud[i]=stud[j];
stud[j]=work;
}
fwrite(&stud[i],sizeof(struct student),1,fp2);
}
fclose(fp1);
fclose(fp2);
}
void sortD(void)
{FILE *fp1,*fp2;
int i,j;
if((fp1=fopen("student.txt","rb"))==NULL)
{printf("the file can't open\n");
exit(0);
}
if((fp2=fopen("scort.txt","wb"))==NULL)
{printf("the file can't open\n");
exit(0);
}
for(i=0;i<SIZE;i++)
if(fread(&stud[i],sizeof(struct student),1,fp1)!=1)
{printf("the file read error\n");
exit(0);
}
for(i=0;i>SIZE;i++)
{for(j=i+1;j<SIZE;j++)
if(stud[i].num>stud[j].num)
{work=stud[i];
stud[i]=stud[j];
stud[j]=work;
}
fwrite(&stud[i],sizeof(struct student),1,fp2);
}
fclose(fp1);
fclose(fp2);
}
解决方案 »
- C#模拟登陆的POST难题急等高手帮忙
- 求助字符串转换
- 问个LINQ比如我表A中有DATE字段。我想按日分组,怎么写? group by的问题
- 保存xml已占用的问题!急
- 无法定位序数801于msjet40.dll是什么错?
- 如何让一个Label充满整个Form ?
- 在C#中, 基于WinForm, 怎么遍历整个画面上的控件.
- c# winform如何像webform 的gridview那样自定义格式?
- 关于将其他控件加入datagrid的问题,一个问题五十分
- 关于Marshal.GetFunctionPointerForDelegate 函数的问题
- 请高手帮忙!!如何进行字体设置
- 初学的体会,不知有没有同感.希望过来人讲讲体会。
在工程中添加头文件比如func.h
#ifndef FUNC_H_
#define FUNC_H_
void sortA(void);
void sortB(void);
void sortC(void);
#endif添加对应的cpp或c文件,添加上面函数的实现:
#include "func.h"void sortA(void)
{
...
}
void sortB(void)
{
...}
void sortC(void)...