所以我想要做的是找出如何按最小的數字排序文件。數字在結構中。該計劃是一種記分牌。你可以添加一個名字,並且包括得分(在這種情況下,它是關於跑步者和時間的)。C:如何按最小數字排序文件(使用結構)?
在添加名稱之後,程序按照最佳時間將得分和跑步者分類到文件中。
遇到問題搞清楚林如何:
- 您可以按照最低/最高。
- 在排序後保持跑步者姓名與他們的分數完好無損。
的代碼:
#include <stdio.h>
#include <string.h>
struct kilpailijat //The structure im using
{
char nimi[43+1];
float aika;
};
int main() {
int i, maara;
struct kilpailijat henkilo[50];
FILE *tulos;
size_t ln;
while (1 != 0) {
printf("Montako kilpailijaa haluat lisätä? (max 50): "); //How many runners
scanf_s("%d", &maara); // User input of how many runners
while (getchar() != '\n');
if (maara > 50) { //Checks that the ammount of runners is no more then 50
printf("Yritit lisätä liian monta kilpailijaa.\n");
} else if (maara < 0) { //And no less then 0
printf("Mielenkiintoista. Jos yrittäisit uudelleen.\n");
} else if (maara == 0) { // Zero Quits the program
return 0;
} else {
break;
}
}
for (i = 0; i < maara; i++){ // Start loop to add runners and their score.
printf("\nAnna kilpailijan nimi: ");
fgets(&henkilo[i].nimi[0], 43, stdin); //Add name of runner
ln = strlen(henkilo[i].nimi) - 1; //Remove newline off of fgets()
if (henkilo[i].nimi[ln] == '\n'){
henkilo[i].nimi[ln] = '\0';
}
printf("Kilpailijan aika: "); //Add score
scanf_s("%f", &henkilo[i].aika);
while (getchar() != '\n');
}
if ((tulos = fopen("tulos.txt", "w")) == NULL){ // Check if file opens
printf("Tiedoston avauksessa tapahtui virhe.\n");
}
for (i = 0; i < maara; i++){ //Writes Name and Score to file.
fprintf(tulos, "%s\t%.1f\n", henkilo[i].nimi, henkilo[i].aika);
}
fclose(tulos);
}
哇,我還沒有讀芬蘭幾十年 - 我現在頭疼:-) – paxdiablo
你有沒有看[C庫函數來執行排序(http://stackoverflow.com/q/1787996) –