#include
#include
#include
#include
#define MAX_SIZE 100
#define OK 1
#define ERROR -1
struct Student
{
int num;
char name[MAX_SIZE];
int score;
} Student [MAX_SIZE];
struct Student_ReadFromFile
{
int num;
char name[MAX_SIZE];
int score;
} Student_ReadFromFile [MAX_SIZE];
int Write(int i);
void Input();
//void sort(int Size);
int main()
{
Input();
Read();
}
int Write(int i)
{
FILE * fp;
int j;
if ((fp=fopen("Data.txt"," w+ "))==NULL)
{
printf("ERROR !Can not open the file !\n");
exit(0);
}
for (j=0; j<i; j++)
{
fprintf(fp,"%10d ",Student[j].num);
fprintf(fp,"%s ",Student[j].name);
fprintf(fp,"%d\n",Student[j].score);
}
fclose (fp);
return OK;
}
void Input()
{
int i,flag=1,count=1,valid=1,check;
char *pstr;
// printf("请输入学生总数,n = ");
//scanf("%d",&n);
// printf("\n");
printf("请输入学生学号,姓名,成绩。\n");
do
{
do
{
printf("第 %d 位学生的学号(10位)是:\n",count);
scanf ("%10d",&Student [count-1] . num);
check=(Student [count-1] . num<=9999999999&&Student [count-1] . num>=1000000000);
}
while(!check);
do
{
printf("第 %d 位学生的成绩是(成绩在0-100内取值):\n",count);
scanf ("%d",&Student [count-1] . score);
}
while (Student [count-1] . score<0||Student [count-1] . score>100);
do
{
valid=1;
printf("第 %d 位学生的姓名是(仅含汉字不应包含数字字母等符号):\n",count);
scanf ("%s",Student [count-1] . name);
getchar();
pstr=&(Student [count-1] . name);
for (; *pstr!='\0'; pstr++)
{
// printf("%c\n",*pstr);
//if (!( *pstr >= 0x81 && *pstr <= 0xFE && *(pstr + 1) >= 0x40 && *(pstr + 1) <= 0xFE))
//if ((*pstr<='z'&&*pstr>='0'))||(*pstr<='Z'&&*pstr>='A')||(*pstr>='0'&&*pstr<='9'))
if (isalnum(*pstr))
valid=0;
}
}
while(!valid);
count ++;
printf("继续请输入1,结束输入请输入0:");
scanf("%d",&flag);
}
while (flag);
Write (count-1);
//Output(i);
printf("您录入的数据是:\n");
for (i=0; i<count-1; i++)
printf("%10d %s %d \n",Student [i] . num,Student [i] . name,Student [i] . score );
}
void Read()
{
FILE *fp;
struct Student_ReadFromFile *pt=NULL;
int i,count;
printf("自文件中读入数据为:\n");
if ((fp=fopen("Data.txt","r"))==NULL)
{
printf("打开失败\n");
exit(0);
}
for (i=0; !feof(fp); i++)
{
fscanf(fp,"%10d ",&Student_ReadFromFile [i] . num);
fscanf(fp,"%s ",&Student_ReadFromFile [i] . name);
fscanf(fp,"%d",&Student_ReadFromFile [i] . score);
}
fclose(fp);
count=i-1;
for (i=0; i<count; i++)
printf("%10d %s %d \n",Student_ReadFromFile [i] . num,Student_ReadFromFile [i] . name,Student_ReadFromFile [i] . score );
pt=Student_ReadFromFile;
// printf("%10d %s %d \n",pt->num,pt->name,pt->score );
sort(count,pt);
// printf("经过按照成绩降序排列后输出如下:\n");
//for (i=0; i<count; pt++)
// printf("%10d %s %d \n",pt->num,pt->name,pt->score );
}
void sort(int Size,struct Student_ReadFromFile *pt)
{
int i,j,k;
struct Student_ReadFromFile *Move=pt;
struct Student_ReadFromFile temp;
printf("经过按照成绩降序排列前输出如下:\n");
for (i=0; i<Size; i++, pt++)
printf("%10d %s %d \n",pt->num,pt->name,pt->score );
for(k=0; k<Size-1; k++)
{
printf("%d\n",pt->score);
printf("%d\n",pt->num);
printf("%s\n",pt->name);
for (j=i+1,Move=pt+1; j<Size; j++,Move++)
{
if ((pt->score)<(Move->score))
{
temp=*pt;
*pt=*Move;
*Move=temp;
}
}
pt++;
}
printf("经过按照成绩降序排列后输出如下:\n");
for (i=0; i<Size; i++)
printf("%10d %s %d\n",(pt+i)->num,(pt+i)->name,(pt+i)->score );
}