如何将排序结构与字符串合并? [英] How to merge sort struct with strings?

查看:90
本文介绍了如何将排序结构与字符串合并?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修改常见的mergesort,以对字符串结构进行排序.但是我不知道怎么了.使用strcpy()&的每一行都会显示一条警告. strcmp()告诉:

I'm trying to modify a common mergesort, for sorting a struct of strings. But I can't figure out what's wrong. A warning appears on every line with strcpy() & strcmp() that tells:

warning: passing argument 1 of 'strcpy' makes pointer from integer without a cast [-Wint-conversion]|
C:\Program Files (x86)\CodeBlocks\MinGW\include\string.h|45|note: expected 'char *' but argument is of type 'char'|

它建议我将*放在char L [n1]和R [n2]

And it recommends me to put * on char L[n1] and R[n2]

But even if i declare 
char *L[n1] 
char *R[n2]

它总是在strcpy()或什至在strcmp()处停止. . .我不明白为什么.有人有任何想法如何strcpy和strcmp这两个吗?

It always stops at strcpy() or maybe even at strcmp() . . . i can't understand why. Does anybody have any ideas how to strcpy and strcmp these two?

struct kegiatan{

     char nama[50];
};

void merge(struct kegiatan *keg, int low, int mid, int high){

    int i, j, k;

    int n1 = mid - low + 1;
    int n2 = high - mid;
    char L[n1];
    char R[n2];

    for(i = 0; i < n1; i++){
        strcpy(L[i], keg[low + i].nama);        // the program always stops here.
    }
    printf("L = %s", L[i]);
    printf("keg = %s", keg[low + i].nama);
    system("pause");
    for(j = 0; j < n2; j++){
        strcpy(R[j], keg[mid + 1 + j].nama);
    }

    i = 0;
    j = 0;
    k = low;

    while(i < n1 && j < n2){
        if(strcmp(L[i], R[j]) < 0){
            strcpy(keg[k].nama, L[i]);
            i++;
        } else{
            strcpy(keg[k].nama, R[j]);
            j++;
        }
        k++;
    }
     while (i < n1) { 
        strcpy(keg[k].nama, L[I]); 
        i++; 
        k++; 
     }

    while(j < n2) {
        strcpy(keg[k].nama, R[j]);
        j++;
        k++;
    }

}

void mergesort(struct kegiatan *keg, int low, int high){

    if(low < high){
        int mid = (low + high) / 2;

        mergesort(keg, low, mid);
        mergesort(keg, mid + 1, high);
        merge(keg, low, mid, high);
    }

}

推荐答案

您误用了char字符串.在这里strcpy(L[i], keg[low + i].nama);您没有将一个字符串复制到另一个字符串.您正在将一个字符串复制到数组L的一个元素

you are mistaking string with char .here strcpy(L[i], keg[low + i].nama); you are not copying one string to another .you are copying one string to one element of array L.

还请注意,在printf中,您不应将%s用于L[i],也不要在strcpy中使用R[j](这是一个字符).

also note in printf you shouldn't use %s for L[i] and don't use R[j] in strcpy(it's a char).

请注意,您在此处比较两个字符if(strcmp(L[i], R[j]),所以请不要使用strcmp.

note you are comparing two char here if(strcmp(L[i], R[j]) , so don't use strcmp.

简而言之,当您声明char string[num]时,它是一个字符数组,但是此string[i]不是数组,它只是array的一个元素.因此,请勿将其视为数组.

in short when you declare char string[num], it's an array of characters ,but this string[i] is not an array , it's just one element of array.so don't treat it like an array.

在所有strcpy中注意,您已经发送了一个字符串和一个字符串元素作为参数.

note in all of your strcpy you have send a string and one element of string as arguments.

这篇关于如何将排序结构与字符串合并?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆