2017-02-11 33 views
0

我试图调用快速排序使用结构“学生” 数组我比较它的属性:比较中的qsort没有要求[错误:比较之前预期的表达]

typedef struct 
{ 
    int ID;      // 4 bytes    = 164 [+ int] 
    char firstname[NAME_LENGTH]; // 1 bytes * length (80) = 160 [2 * NAME_LENGTH] 
    char lastname[NAME_LENGTH]; // 1 bytes * length (80) 
} Student; 

我的代码试图从函数中调用qsort 3次:按ID,然后名字,然后是姓氏对它进行排序。主函数处理将其他函数调用为读写操作。找到一个错误应该使我能够将它应用于其他功能,对吧?然而涉及排序功能是:

#ifdef TEST_SORTID 
void StudentSortbyID(Student * stu, int numelem) 
{ 
    qsort(&(stu-> ID), numelem, sizeof(stu), compareInts); 
} 
#endif 

#ifdef TEST_SORTFIRSTNAME 
void StudentSortbyFirstName(Student * stu, int numelem) 
{ 
    qsort(&(stu-> firstname), numelem, sizeof(stu), compareStrings); 
} 
#endif 

#ifdef TEST_SORTLASTNAME 
void StudentSortbyLastName(Student * stu, int numelem) 
{ 
    qsort(&(stu-> lastname), numelem, sizeof(stu), compareStrings); 
} 
#endif 

#ifdef TEST_COMPAREINTS 
int compareInts(const void * argu1, const void * argu2) 
{ 
    const int * iptr1 = (const int *) argu1; //convert void to integer pointer 
    const int * iptr2 = (const int *) argu2; 
    int ival1 = * iptr1;      //convert pointer to value 
    int ival2 = * iptr2; 
    if(ival1 < ival2)  { return -1; } //return -1 if first value is less 
    if(ival1 > ival2)  { return 1; } //return 1 if previous value is greater 
    if(ival1 == ival2)  { return 0; } //return 0 if the adjacent values are equal 
} 
#endif 

#ifdef TEST_COMPARESTRINGS 
int compareStrings(const void * argu1, const void * argu1) 
{ 
    //String is an array of characters (string = char*) -> pointing to string 
    const char * const * sptr1 = (const char * *) argu1; //converting empty pointers to strings which point to characters [**] 
    const char * const * sptr2 = (const char * *) argu2; 
    const char * string1 = * sptr1;      // a string is a character pointer 
    const char * string2 = * sptr2; 
    return strcmp(string1,string2); 
} 
#endif 

当运行GCC是我得到的错误:

student.c:120: error: too few arguments to function ‘compareInts’ 

我想快速排序的比较不带参数?当我尝试放入数组的前两个元素时,它也会出错。有任何想法吗?

+0

而且我们不应该理清错误信息可能涉及哪些行?对不起,我们不是一个debugginmg服务。 – Olaf

+1

在调用qsort之前是否声明了比较函数? – dromtrund

+0

对不起,错误发生在第4行@Olaf –

回答

0

我建议一个接一个地解决一件事,也就是说从开始按ID排序,提供一个特定的“sortStudentById” - 比较函数,并且以一个没有任何代码的代码开始(这些通常会使调试和理解编译器错误更复杂)。

我想一个原因是你的比较函数在qsort之前没有被声明。

一旦你解决了这个问题,你肯定会遇到下一个使用qsort的问题。如果你想排序学生对象,你的比较函数需要指向学生对象(而qsort将重复交换学生对象作为一个整体,而不是指向它们)。将一个指向成员的指针传递给qsort(如&(stu-firstname))将使qsort以某种肯定无意的方式进行交换。

尝试下面的代码作为起点,使adaptions根据需要:

int compareStudentsByID(const void* s1, const void *s2) { 
    return ((Student *)s1)->ID - ((Student*)s2)->ID; 
} 

void sortByID(Student *s) { 

    qsort(s,100,sizeof(Student),compareStudentsByID); 
} 

int main() { 

    Student students[100]; 
    for (int i=0; i<100; i++) { 
     students[i].ID = rand()%1000; 
    } 
    sortByID (students); 

    return 0; 
} 
+0

的头文件中。不幸的是,我不能删除ifdef语句,因为它们用于部分信用评分。 该函数在头文件中声明为: int compareInts(const void * argu1,const void * argu2); int compareStrings(const void * argu1,const void * argu2); 但是我正在做qsort调整,我会让你知道它是如何工作的! 谢谢! –

+0

我在排序函数和修改后的qsort之前移动了函数,我又遇到了另一个熟悉的错误。未定义的参考 –

+0

你可以发布你的代码吗? –

0
// Do not remove #ifdef ... #endif before and after each function. 
// 
// They are used to test different functions in your program and give 
// partial credits, in case your program does not work completely. 

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

void StudentPrint(Student * stu, int num) 
{ 
    printf("There are %d students\n", num); 
    for (int ind = 0; ind < num; ind ++) 
    { 
     printf("ID = %d, First Name = %s, Last Name = %s\n", 
     stu[ind].ID, stu[ind].firstname, stu[ind].lastname); 
    } 
} 

#ifdef TEST_READ 
// return false if anything is wrong 
// return true if reading the file successfully and the data is save in 
// the allocated memory 
// input: filename, the name of the input file 
// output: address of the allocated memory 
// output: number of students 
bool StudentRead(char * filename, Student * * stu, int * numelem) 
{ 
    int id; 
    char first[NAME_LENGTH]; 
    char last[NAME_LENGTH]; 
    // open the file to read 
    FILE * inputPtr = fopen(filename, "r"); 

    // if fopen fails, return false 
    // do not use fclose since fopen already fails 
    if (inputPtr == NULL) 
    { 
     fprintf(stderr, "File Opening failed; Evaluate read function\n"); 
     return false; 
    }  

    // count the number of lines to determine the number of students 
    while(0 != fscanf(inputPtr, "\n")) { numelem++; } 


    // return to the beginning of the file 
    // you can use fseek or 
    // fclose followed by fopen 
    // You need to check whether fseek or fopen fails 
    // Do not use rewind because it does not report whether it fails 
    fclose(inputPtr); 
    inputPtr = fopen(filename, "r");   // reading starts from 0 

    // allocate memory for the data 
    stu = malloc(((sizeof(*(stu))) * (*numelem))); 

    // check whether memory allocation fails 
    if (stu == NULL) 
    { 
     fprintf(stderr, "Memory allocation failed; evaluate malloc\n"); 
     return false; 
    } 

    // read the data from the file and store the data in the allocated memory 
    for (int count = 0; count < *numelem; count++) 
    { 
     fscanf(inputPtr, "%i %s %s\n", &id, &first[NAME_LENGTH], &last[NAME_LENGTH]); 

     stu[count] -> ID = id; 
     stu[count] -> firstname[NAME_LENGTH] = first[NAME_LENGTH]; 
     stu[count] -> lastname[NAME_LENGTH] = last[NAME_LENGTH]; 
    } 
     if (stu[0] == NULL) 
    { 
     fprintf(stderr, "Inputting file values to variable failed; evluate read function\n"); 
     return false; 
    } 
    // close the file 
fclose(inputPtr); 

    return true; 
} 
#endif 

#ifdef TEST_WRITE 
// return false if anything is wrong 
// return true if writing the file successfully 
// input: filename, the name of the output file 
// input: address of the student array 
// input: number of students 

bool StudentWrite(char * filename, Student * stu, int numelem) 
{ 

    // open the file to write 
    FILE * outputFile = fopen(filename, "w"); 

    // if fopen fails, return false 
    // do not use fclose since fopen already fails 
    if (outputFile == NULL) 
    { 
     fprintf(stderr, "Opening file failed; evaluate Write function\n"); 
     return false; 
    } 
    // write the students to the output file 
    for (int count = 0; count < numelem; count++) 
    { 
     fprintf(outputFile, "%i %s %s\n", (stu[count]).ID, (stu[count]).firstname, (stu[count]).lastname); 
    } 
    return true; 
} 
#endif 

#ifdef TEST_SORTID 
void StudentSortbyID(Student * stu, int numelem) 
{ 
    qsort(stu, numelem, sizeof(Student), compareInts); 
} 
#endif 

#ifdef TEST_SORTFIRSTNAME 
void StudentSortbyFirstName(Student * stu, int numelem) 
{ 
    qsort(stu -> firstname, numelem, sizeof(stu), compareStrings); 
} 
#endif 

#ifdef TEST_SORTLASTNAME 
void StudentSortbyLastName(Student * stu, int numelem) 
{ 
    qsort(stu -> lastname, numelem, sizeof(stu), compareStrings); 
} 
#endif 

int compareInts(const void * argu1, const void * argu2) 
{ 
    const Student * iptr1 = (const Student *) argu1; //convert void to integer pointer 
    const Student * iptr2 = (const Student *) argu2; 
    return iptr1 ->ID - iptr2 -> ID; 
} 

int compareStrings(const void * argu1, const void * argu2) 
{ 
    //String is an array of characters (string = char*) -> pointing to string 
    const Student * sptr1 = (const char *) argu1; //converting empty pointers to strings which point to characters [**] 
    const Student * sptr2 = (const char *) argu2; 
    const char string1 = sptr1 -> firstname;       // a string is a character pointer 
    const char string2 = sptr2 -> firstname; 
    return strcmp(string1,string2); 

} 

这是这些功能的全部代码。另一个.c包含主要的

+0

有了这个,我遇到了比较字符串函数的错误。初始化来自不兼容的指针类型等等。 我正在尝试修复它们现在 –

+0

代码现在编译。在制作我的makefile时遇到困难,但那又是另一个话题 –