0

lastname 是记录中的一个字段,EmployeeRecord 在record.h 中定义,我正在尝试使用Qsort 对employee[emptotal].lastname 中的字符串进行排序。

#include "./record.h"

void externalSort(EmployeeRecord *lastname,int empcount,int emptotal)
{
empcount = 0;
emptotal = 5;
EmployeeRecord employee[emptotal];
int left=empcount,
right=emptotal;
EmployeeRecord pivot[].lastname = employee[(empcount+emptotal)/2].lastname;

  while (left < right) {
// find left candidate
  while (&employee[left].lastname  < &pivot[(empcount+emptotal)/2].lastname) left++; 
// find right candidate 
  while (&employee[right].lastname > &pivot[(empcount+emptotal)/2].lastname) right--; 
  if (left <= right) {
    EmployeeRecord &temp.lastname = &employee[left].lastname;
    &employee[left].lastname = &employee[right].lastname;
    &employee[right].lastname = &temp.lastname;
    left++;
right--;
  } 
} // while left < right
if (empcount < right) externalSort(char *lastname,empcount,right);
if (left < emptotal) externalSort(char *lastname,left,emptotal);

}

很抱歉有这么多不同的变化,我不知道前几行的正确方法,我的主要问题是声明行,EmployeeRecord pivot[].lastname = employee[(empcount +emptal)/2].姓氏;我相信,一旦我知道如何正确声明枢轴,我就会知道如何正确分配排序。

(也将有助于分配温度。)

(如果有帮助,请记录.h)

typedef char STR9[9+1];
typedef char STR7[7+1];
typedef char STR16[16+1];
typedef struct EmployeeRecord
{
   STR9 lastname;
   STR7 firstname;
   STR16 fullname;   
   float hours, 
         overhours,
         pay_rate, 
         defered, 
         gross, 
         fedtax, 
         statetax, 
         ssitax, 
         netpay;
} EmployeeRecord;
4

1 回答 1

0

首先,您的代码中有很多错误。

你肯定需要看看那些。

并且您似乎正在根据以下条件对记录进行排序lastname

你为什么不使用qsort

像这样的东西:

const int no_of_employee =100;

EmployeeRecord *ER[no_of_employee];

static int compare(const void *e1, const void *e2)
{
    EmployeeRecord *tmp1 =*(EmployeeRecord **)e1;
    EmployeeRecord *tmp2 =*(EmployeeRecord **)e2;
    return strcmp( tmp1->lastname,tmp2->lastname);
}


qsort (ER, no_of_employee, sizeof(EmployeeRecord *), compare);

另外,我建议使用 STL 使其更简单。

很难指出代码中的每个错误,分析与以下代码相关的错误(大约类似于您的代码)

void swap(EmployeeRecord *e1, EmployeeRecord *e2)
{
    EmployeeRecord tmp ;
    tmp = *e1;
    *e1 = *e2;
    *e2 = tmp;
}

void externalSort(EmployeeRecord employee[], int start, int end){
    if(start < end){
        int left=start+1, right=end;
        EmployeeRecord pivot = employee[start];
        while(left<right){
            if(strcmp(employee[left].lastname, pivot.lastname) < 0)
                left++;

            else if(strcmp(employee[right].lastname, pivot.lastname) > 0)
                right--;
            else
                swap(&employee[left],&employee[right]);
        }
        if(strcmp(employee[left].lastname, pivot.lastname) < 0){
            swap(&employee[left],&employee[start]);
            left--;
        }
        else{
            left--;
            swap(&employee[left],&employee[start]);
        }
        externalSort(employee, start, left);
        externalSort(employee, right, end);
    }
}

   // externalSort(employee, 0, emptotal);
于 2013-07-29T06:02:56.433 回答