c语言 文件内容的重新排序

有一个奖牌榜,存储在medal.txt中之后要重新排序,先是按照国家首字母排序并显示,再按照总奖牌数排序并显示求程序,c或c++都行... 有一个奖牌榜,存储在medal.txt 中
之后要重新排序,先是按照国家首字母排序并显示,再按照总奖牌数排序并显示
求程序,c或c++都行
展开
 我来答
匿名用户
2012-05-10
展开全部
采用插入排序,通过排索引加速。C代码:

#include <stdio.h>
#define NATION_MAX 256
#define LINE_MAX 1000
#define HEADER_SIZE 256
typedef struct
{
int No;
char Nation[NATION_MAX];
int gold, silver, bronze, sum;
}Medal;
int loadMedal(Medal *medals, char *header, const char *fileName)
{
int n = 0;
FILE *fp = fopen(fileName, "r");
if(!fp) return 0;
fgets(header, HEADER_SIZE, fp);
while(fscanf(fp, "%d%s%d%d%d%d", &(medals[n].No), medals[n].Nation,
&(medals[n].gold), &(medals[n].silver), &(medals[n].bronze),
&(medals[n].sum)) == 6){
n ++;
if(n >= LINE_MAX) return n;
}
fclose(fp);
return n;
}
void showMedal(Medal *medals, char *header, int nMedal, int *sorted)
{
int i, index;
fputs(header, stdout);
for(i = 0; i < nMedal; i ++) {
index = sorted[i];
printf("%d\t%s\t%d\t%d\t%d\t%d\n", medals[index].No,
medals[index].Nation, medals[index].gold,
medals[index].silver, medals[index].bronze, medals[index].sum);
}
}

void sortByNation(Medal *medals, int nMedal, int *sorted)
{
int i, j;
int key;
int *a = sorted;
int index;
for(j = 1; j < nMedal; j ++){
key = medals[a[j]].Nation[0];
index = a[j];
for(i = j - 1; (medals[a[i]].Nation[0] > key) && (i >= 0); i--){
a[i+1] = a[i];
}
a[i+1] = index;
}
}
void sortBySum(Medal *medals, int nMedal, int *sorted)
{
int i, j;
int key;
int *a = sorted;
int index;
for(j = 1; j < nMedal; j ++){
key = medals[a[j]].sum;
index = a[j];
for(i = j - 1; (medals[a[i]].sum > key) && (i >= 0); i--){
a[i+1] = a[i];
}
a[i+1] = index;
}
}

void resetIndex(int *sorted, int nMedal)
{
int i;
if(nMedal > LINE_MAX) return ;
for(i = 0; i < nMedal; i++){
sorted[i] = i;
}
}
int main()
{
Medal medals[LINE_MAX];
int sorted[LINE_MAX];
char header[HEADER_SIZE];
int nMedal;
nMedal = loadMedal(medals, header, "medal.txt");
resetIndex(sorted, nMedal);
/* showMedal(medals, header, nMedal, sorted); */
/* 按国家首字母排序*/
sortByNation(medals, nMedal, sorted);
/* 显示 */
showMedal(medals, header, nMedal, sorted);
/* 按总奖牌数排序 */
sortBySum(medals, nMedal, sorted);
/* 显示 */
showMedal(medals, header, nMedal, sorted);
return 0;
}
沧海雄风2009
2012-05-10 · TA获得超过1.1万个赞
知道大有可为答主
回答量:8525
采纳率:79%
帮助的人:2874万
展开全部
C语言的

China 199 119 98 416
Korea 76 65 91 232
Japan 48 74 94 216
Iran 20 14 25 59
Kazakhstan 18 23 38 79
India 14 17 33 64
ChineseTaipei 13 16 38 67
Uzbekistan 11 22 23 56
Thailand 11 9 32 52
Malaysia 9 18 14 41
Sort By Name:
China 199 119 98 416
ChineseTaipei 13 16 38 67
India 14 17 33 64
Iran 20 14 25 59
Japan 48 74 94 216
Kazakhstan 18 23 38 79
Korea 76 65 91 232
Malaysia 9 18 14 41
Thailand 11 9 32 52
Uzbekistan 11 22 23 56
Sort By Sum:
Malaysia 9 18 14 41
Thailand 11 9 32 52
Uzbekistan 11 22 23 56
Iran 20 14 25 59
India 14 17 33 64
ChineseTaipei 13 16 38 67
Kazakhstan 18 23 38 79
Japan 48 74 94 216
Korea 76 65 91 232
China 199 119 98 416
Press any key to continue

#include <stdio.h>
#include <string.h>

#define NUM 10
#define C1 strcpy(line,gm[j].cNation);strcpy(gm[j].cNation, gm[j+1].cNation);strcpy(gm[j+1].cNation,line);
#define I1 tmp = gm[j].gold;gm[j].gold = gm[j+1].gold;gm[j+1].gold = tmp;
#define I2 tmp = gm[j].silver;gm[j].silver = gm[j+1].silver;gm[j+1].silver = tmp;
#define I3 tmp = gm[j].bronze;gm[j].bronze = gm[j+1].bronze;gm[j+1].bronze = tmp;
#define I4 tmp = gm[j].sum;gm[j].sum = gm[j+1].sum;gm[j+1].sum = tmp;

struct game
{
char cNation[20];
int gold;
int silver;
int bronze;
int sum;
}gm[NUM];

void Print()
{
int i;
for (i=0;i<NUM;i++)
{
printf("%-15s\t%3d\t%3d\t%3d\t%3d\n",gm[i].cNation,gm[i].gold,gm[i].silver,gm[i].bronze,gm[i].sum);
}
}
main()
{
int i=0,j,tmp;
FILE *fp;
char line[100]={0};
fp=fopen("data.txt","r");
fgets(line,sizeof(line)-1.,fp);
while (fgets(line,sizeof(line)-1,fp))
{
sscanf(line,"%*d %[a-zA-Z] %d %d %d %d\n",gm[i].cNation,&gm[i].gold,&gm[i].silver,&gm[i].bronze,&gm[i].sum);
printf("%-15s\t%3d\t%3d\t%3d\t%3d\n",gm[i].cNation,gm[i].gold,gm[i].silver,gm[i].bronze,gm[i].sum);
i++;
}
for(i=0;i<NUM-1;i++)
{
for(j=0;j<NUM-i-1;j++)
{
if(strcmp(gm[j].cNation,gm[j+1].cNation)>0)
{
C1 I1 I2 I3 I4
}
}
}
printf("Sort By Name:\n");
Print();

for(i=0;i<NUM-1;i++)
{
for(j=0;j<NUM-i-1;j++)
{
if(gm[j].sum>gm[j+1].sum)
{
C1 I1 I2 I3 I4
}
}
}
printf("Sort By Sum:\n");
Print();
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友28b4182
2012-05-09 · TA获得超过7223个赞
知道大有可为答主
回答量:4847
采纳率:100%
帮助的人:1867万
展开全部
#include<stdio.h>
#include<algorithm>
using namespace std;
struct C
{
char name[100];
int gold,silver,bros,sum;
};
C a[100];
bool ByCountry(C a,C b)
{
return a.name[0]<b.name[0];
}
bool BySum(C a,C b)
{
return a.sum<b.sum;
}
void out(C a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%s\t%d\t%d\t%d\t%d\n",a[i].name,a[i].gold,a[i].silver,a[i].bros,a[i].sum);
}
}
int main()
{
int n=0;
freopen("C:\\a.txt","r",stdin);
gets(a[0].name);
while(scanf("%s",a[n].name)!=EOF)
{
scanf("%s%d%d%d%d",a[n].name,&a[n].gold,&a[n].silver,&a[n].bros,&a[n].sum);
n++;
}
sort(a,a+n,ByCountry);
out(a,n);
sort(a,a+n,BySum);
out(a,n);
return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
lyhdez1
2012-05-09 · TA获得超过1942个赞
知道小有建树答主
回答量:687
采纳率:100%
帮助的人:846万
展开全部
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<algorithm>
using namespace std;
struct medal
{
int No;
string Nation;
int nGold;
int nSilver;
int nBronze;
int sum;
}ob[10];
void print(medal &ob, int n, ofstream &file)
{
cout << setw(8) << n;
cout << setw(16) << ob.Nation;
cout << setw(8) << ob.nGold;
cout << setw(8) << ob.nSilver;
cout << setw(8) << ob.nBronze;
cout << setw(8) << ob.sum << endl;
file << setw(16) << n;
file << setw(16) << ob.Nation;
file << setw(16) << ob.nGold;
file << setw(16) << ob.nSilver;
file << setw(16) << ob.nBronze;
file << setw(16) << ob.sum << endl;
}

void main()
{
int me[10];
string firstc[10], txt;;
int i = 0, j;
ifstream in("medal.txt");
ofstream of1("Ca.txt");
ofstream of2("Me.txt");
if(!in && !of1 && of2)
{
cout << "Can't open the file!" << endl;
exit(0);
}
cout << setiosflags(ios::left);
cout << "The file is: " << endl;
while(in >> txt)
{
if(txt == "Nation")
cout << setw(16);
else
cout << setw(8);
cout << txt;
of1 << setw(16) << txt;
of2 << setw(16) << txt;
if(txt == "sum")
{
cout << endl;
of1 << endl;
of2 << endl;
break;
}
}

while(!in.eof())
{
in >> ob[i].No;
cout << setw(8) << ob[i].No;
in >> ob[i].Nation;
firstc[i] = ob[i].Nation;

if(ob[i].Nation == "Chinese")
{
in >> txt;
ob[i].Nation += " " + txt;
firstc[i] += " " + txt;
}

cout << setw(16) << ob[i].Nation;
in >> ob[i].nGold;
cout << setw(8) << ob[i].nGold;
in >> ob[i].nSilver;
cout << setw(8) << ob[i].nSilver;
in >> ob[i].nBronze;
cout << setw(8) << ob[i].nBronze;
in >> ob[i].sum;
me[i] = ob[i].sum;
cout << setw(8) << ob[i].sum << endl;
i++;
}
sort(me, me + 10);
sort(firstc, firstc + 10);

cout << "according the first character:\n" << endl;
for(i = 0; i < 10; i++)
{
for(j = 0; j < 10; j++)
{
if(ob[j].Nation == firstc[i])
break;
}
print(ob[j], i + 1, of1);
}

cout << "according the num of medals:\n " << endl;
for(i = 0; i < 10; i++)
{
for(j = 0; j < 10; j++)
{
if(ob[j].sum == me[i])
break;
}
print(ob[j], i + 1, of2);
}

in.close();
of1.close();
of2.close();
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式