发布网友
共1个回答
热心网友
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct student
{
char name[20];
int age;
int score;
};
int main()
{
int n = 0;
struct student s[100];
struct temp;
FILE *fpIn,*fpOut;
fpIn = fopen("input.txt","r+");
if(fpIn == NULL)
{
printf("input file open error!\n");
return -1;
}
fpOut = fopen("output.txt","w+");
if(fpOut == NULL)
{
printf("output file open error!\n");
return -1;
}
n=0;
while (!feof(fpIn))
{
fscanf(fpIn,"%s%d%d\n",&s[n].name, &s[n].age, &s[n].score); /*文本输入*/
printf("n=%d\t name=%s\t age=%d\t score=%d\n",n, s[n].name, s[n].age, s[n].score);
n++;
}
fclose(fpIn);
for(int i = n-1; i >= 0; i--)
{
for(int j = 0; j < i; j++)
{
if((s[j].score < s[j+1].score) || (s[j].score == s[j+1].score && s[j].name < s[j+1].name)
|| (s[j].score == s[j+1].score && s[j].name == s[j+1].name && s[j].age < s[j+1].age))
{
student tmp = s[j];
s[j] = s[j+1];
s[j+1] = tmp;
}
}
}
for(int i=0; i < n; i++)
{
printf("n=%d\t name=%s\t age=%d\t score=%d\n", i, s[i].name, s[i].age, s[i].score);
}
for(int i=0; i < n; i++)
{
fprintf(fpOut,"n=%d\t name=%s\t age=%d\t score=%d\n",i, s[i].name, s[i].age, s[i].score);
}
fclose(fpOut);
return 0;
}
本地测试通过了的。
热心网友
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct student
{
char name[20];
int age;
int score;
};
int main()
{
int n = 0;
struct student s[100];
struct temp;
FILE *fpIn,*fpOut;
fpIn = fopen("input.txt","r+");
if(fpIn == NULL)
{
printf("input file open error!\n");
return -1;
}
fpOut = fopen("output.txt","w+");
if(fpOut == NULL)
{
printf("output file open error!\n");
return -1;
}
n=0;
while (!feof(fpIn))
{
fscanf(fpIn,"%s%d%d\n",&s[n].name, &s[n].age, &s[n].score); /*文本输入*/
printf("n=%d\t name=%s\t age=%d\t score=%d\n",n, s[n].name, s[n].age, s[n].score);
n++;
}
fclose(fpIn);
for(int i = n-1; i >= 0; i--)
{
for(int j = 0; j < i; j++)
{
if((s[j].score < s[j+1].score) || (s[j].score == s[j+1].score && s[j].name < s[j+1].name)
|| (s[j].score == s[j+1].score && s[j].name == s[j+1].name && s[j].age < s[j+1].age))
{
student tmp = s[j];
s[j] = s[j+1];
s[j+1] = tmp;
}
}
}
for(int i=0; i < n; i++)
{
printf("n=%d\t name=%s\t age=%d\t score=%d\n", i, s[i].name, s[i].age, s[i].score);
}
for(int i=0; i < n; i++)
{
fprintf(fpOut,"n=%d\t name=%s\t age=%d\t score=%d\n",i, s[i].name, s[i].age, s[i].score);
}
fclose(fpOut);
return 0;
}
本地测试通过了的。