2017-02-11 56 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時遇到困難,但那又是另一個話題 –