如何对struct-int和char类型的数组进行排序?

时间:2017-10-19 15:08:05

标签: c

查看代码中的函数sorteraF,我尝试对struct类型的数组进行排序。我想为name类型char和varunummer类型int执行此操作。但它不起作用。要对名称进行排序,我使用的sortAfterNames也不起作用。

             //lager program lab 
             #include <stdio.h>
             #include <stdlib.h>
             #include <string.h> 
             #define WORDLENGTH 30
             #define MAX 10

             struct varor{ //VAROR STANDS FOR GOODS----------
                 int varunummer;
                 char namn[WORDLENGTH];
                 int lagersaldo; 
             };
             typedef struct varor Vara;

             Vara createVara(int varunummer, char namn[], int lagersaldo){  //CREATE THE GOODS
                 Vara v;
                 v.varunummer=varunummer;
                 strcpy(v.namn, namn);
                 v.lagersaldo=lagersaldo;
                 return v;
             }
           //register
             void regVaror( Vara reg[], int *pNrOfGoods){   //REGISTER THE GOODS

                 char confirm;
                 char namn[WORDLENGTH],
                      tmp[WORDLENGTH];
                 int varunummer, lagersaldo;
                 printf("\nAre you sure you want to register new goods?\n1: YES - (continue)\n2: NO - (go back to menu)\n");
                 scanf(" %c%*c", &confirm);// choose
                 switch(confirm){
                 case '1': 
                 do{
                     printf("Enter varunummer:");//varunummer stands for identify number of the good
                     gets(tmp); 
                     varunummer=atoi(tmp);

                     printf("Enter name:");
                     gets(namn);

                     printf("Enter Availability:");
                     gets(tmp);
                     lagersaldo=atoi(tmp);

                     reg[*pNrOfGoods]=createVara(varunummer,namn,lagersaldo);
                     (*pNrOfGoods)++;
                     printf("\nRegister more goods?\n1: YES - (continue)\n2: NO - (go back to menu)\n");
                     scanf(" %c%*c", &confirm);
                 }while(confirm=='1');

                 case '2': break;

                 }
             }
             //PRINTING THE GOODS ---------
            void printVara(Vara reg[], int nrOfGoods){
                int x;
                printf("\nVarunummer \t Namn \t\t\t Lagersaldo\n");
                for(x=0;x<nrOfGoods;x++){
                    printf(" %d \t\t %s \t\t\t %d\n",reg[x].varunummer,reg[x].namn,reg[x].lagersaldo);
                }
            }
    //SORTING THE GOODS---------
             void sorteraF(Vara reg[], int length){
                 int i, j;
                     struct varor tmp;
                     for (i = 0; i < (length-1); i++){
                         for (j = 0; j < (length -1- i); j++){
                             if (reg[i].varunummer < reg[i + 1].varunummer){
                                 tmp = reg[j];
                                 reg[j] = reg[j + 1];
                                 reg[j + 1] = tmp;
                             } 
                         }
                     } 
             }
void sortAfterName(reg[]){
int i,j;
         char tmp[MAX];
         for(i=0;i<MAX;i++){
             for(j=i+1; j<MAX;j++){
                 if(strcmp(reg[i].namn, reg[i+1].namn) >0){
                     tmp=reg[i];
                     reg[i]=reg[i+1];
                     reg[i+1]=tmp[i];
                  }
              }

             int main(){
                 int run=1;
                 Vara vRegister[MAX];
                 int nrOfGoods=0;
                 while(run){
                    char choice;
                    printf("\n\t\tMeny - Lager Program\n\n\
                    (1) Regrister new varor\n\b\b\b\b\
                    (2) Print all varor\n\
                    (3) Sort varor\n\
                    (4) Avsluta programmet\n");
                    scanf(" %c%*c", &choice);

                    if(choice=='1') regVaror(vRegister, &nrOfGoods);
                    if(choice=='2') printVara(vRegister, nrOfGoods);
                    if(choice=='3') sorteraF(vRegister, MAX);
                    else if(choice=='4') run=0;

                    printf("\n\nNumber of varor: %d\n", nrOfGoods);


                 }
                 return 0;
             }

1 个答案:

答案 0 :(得分:3)

概述如何使用qsort()(没有理由在这里重新发明轮子):

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

#define WORDLENGTH 30

struct varor
{
    int varunummer;
    char namn[WORDLENGTH];
    int lagersaldo; 
};
typedef struct varor Vara;

// comparison function for qsort, just compare the names with strcmp:
static int compareByName(const void *v1, const void *v2)
{
    return strcmp(((const Vara *)v1)->namn, ((const Vara *)v2)->namn);
}

// the same for the numbers
static int compareByNumber(const void *v1, const void *v2)
{
    int n1 = ((const Vara *)v1)->varunummer;
    int n2 = ((const Vara *)v2)->varunummer;
    if (n1 < n2) return -1;
    if (n1 > n2) return 1;
    return 0;
}

int main(void)
{
    Vara v[100];

    // populate v with data

    // to sort by name:
    qsort(v, sizeof v / sizeof *v, sizeof *v, compareByName);

    // to sort by number:
    qsort(v, sizeof v / sizeof *v, sizeof *v, compareByNumber);
}

sizeof v / sizeof *v这里导致元素的整数计数(此处为100,在您的示例代码中可能为10) - 因此,如果您不填充整个数组,请传递数字使用元素。