#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
/*
Copyright Tracy Xiao
*/
struct student
{
char num[8];
char name[20];
float score;
};
void main()
{
FILE *fp;
fp = fopen("c:\\1.txt", "r");
int n=0;//行数
char buf[100];
while (fgets(buf, 100, fp) != NULL)
n++;
freopen("c:\\1.txt", "r", fp);
struct student *stu = new student[n];
int i = 0;
while (!feof(fp))
{
fscanf(fp, "%s %s %f\n", stu[i].num, stu[i].name, &(stu[i].score));
i++;
}
fclose(fp);
for (int i = 0; i < n;i++)
printf("%s,%s,%f,\n", stu[i].num, stu[i].name, stu[i].score);
printf("================================\n");
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - 1 - i; j++)
{
if (stu[j].score>stu[j + 1].score)
{
student tmp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = tmp;
}
}
}
for (int i = 0; i < n; i++)
printf("%s,%s,%f,\n", stu[i].num, stu[i].name, stu[i].score);
/*
写入新文件:2.txt
*/
FILE *p = fopen("c:\\2.txt", "a");
for (int i = 0; i < n; i++)
{
fprintf(p, "%s %s %f", stu[i].num, stu[i].name, stu[i].score);
fprintf(p, "\n");
}
system("pause");
}
一个北林的复试题,文件
最新推荐文章于 2024-02-02 10:03:01 发布