按字母顺序对结构中的数据进行排序

时间:2014-04-19 11:25:26

标签: c sorting data-structures

我有点恼火。我找不到任何可以从结构中排序数据的解决方案。看,这是我的代码。我将解释发生了什么以及我正在尝试做什么:

(我必须将代码从我的母语翻译成英语,所以...如果你发现有任何奇怪的话请注意我。)

我正试图对这种国家结构进行排序。但我的错误是,我唯一要排序的是国家名称列表......所以数据会搞乱。你能帮我一个好的算法吗?

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

int n;

struct Countries{
    char country_code[20];
    char country_name[20];
    float country_area;
    int country_people;
    float global_income;
}*countries;

void menu();
void read();
void display();
void sort();
void exit();

int main(){
    printf("Enter no of countries: "); scanf("%d", &n);
    countries = malloc(n * sizeof(*countries));
    menu();
}

void menu(){
    int op;
    printf("[1] Read;\n[2] Display;\n[3] Sort;\n[4] Exit.\n");
    printf(">> "); scanf("%d", &op); fflush(stdin);
    switch(op){
        case 1:
            read();
            break;
        case 2:
            display();
            break;
        case 3:
            sort();
            break;
        case 4:
            exit();
            break;
        default:
            break;
    }
}

void read(){
    printf("\n\n\n______READ______n\n\n");
    int i;

    for(i=0; i<n; i++){
        printf("Country code #%d: ", i+1); gets(countries[i].country_code); fflush(stdin);
        printf("Country name: "); gets(countries[i].country_name); fflush(stdin);
        printf("Area: "); scanf("%f", &countries[i].country_area); fflush(stdin);
        printf("No of people: "); scanf("%d", &countries[i].country_people); fflush(stdin);
        printf("Global Income: "); scanf("%f", &countries[i].global_income); fflush(stdin);
        printf("\n\n\n");
    }
    menu();
}
void display(){
    printf("\n\n\n______DISPLAY______n\n\n");
    int i;

    for(i=0; i<n; i++){
        printf("Country code #%d: ", i+1); puts(countries[i].country_code);
        printf("Country name: "); puts(countries[i].country_name);
        printf("Area: "); printf("%.2f", countries[i].country_area);
        printf("\nNo of people: "); printf("%d", countries[i].country_people);
        printf("\nGlobal Income: "); printf("%.2f", countries[i].global_income);
        printf("\n\n\n");
    }
}

void sort(){
    int i, j;
    char temp[20];
    /* countries sorted in alphabetical order */
    for(i=0; i<n; i++){
        for(j=i+1; j<n; j++){
            if(strcmp(countries[i].country_name, countries[j].country_name)>0){
                strcpy(temp,countries[i].country_name);
                strcpy(countries[i].country_name, countries[j].country_name);
                strcpy(countries[j].country_name, temp);
            }
        }
    }

    for(i=0; i<n; i++){
        printf("\n\nCountry: "); puts(countries[i].country_name);

        printf("Population density: ");
        printf("%.2f", ((float)countries[i].country_people)/((float)countries[i].country_area));

        printf("\nIncome per people: ");
        printf("%.2f", ((float)countries[i].country_people)/((float)countries[i].global_income));
    }
}

void exit(){
    free(countries);
    getch();
}

1 个答案:

答案 0 :(得分:-1)

在sort函数中,复制所有结构而不仅仅是名称

void sort(){
    struct Countries temp;

            if(strcmp(countries[i].country_name, countries[j].country_name)>0){
                //strcpy(temp, countries[i]);
                //strcpy(countries[i], countries[j]);
                //strcpy(countries[j], temp);
                temp = countries[i];
                countries[i] = countries[j];
                countries[j] = temp;
            }