2013-10-20 18 views
0
#include "int_set.h" 


    Set * int_set_new() 
    { 
    Set *result; 

result-> current_elements = 0; 
result-> max_elements = MIN_SIZE; 
result->array =malloc(sizeof(int) * MIN_SIZE); 
return result; 
    } 
    int int_set_lookup(Set*this, int item) 
    { 
    int i = 0; 
int result; 
int found = 0 ; 
for (int i = 0; i<this->max_elements && found ==0 ; i++){ 
    //if (result->array[i] == item) { 
    if(result->array[i]== item){ // error invalid type of argument'->' 

     found = 1; 
    } 
} 
    if (i>= this-> current_elements ){ 
     found*= -1 ; 
    } 
    return found ; 
} 

int int_set_add(Set*this, int item){ 


if (int_set_lookup(this, item) == 1){ 
    return 0; 
} 
if (this-> current_elements < this -> max_elements ){ 
    this -> array[this->current_elements] = item ; 
    this->current_elements++ ; 
    return 1 ; 
}else{ 
    int i; 
    int new_array[i]; 

    int [] new_array = malloc(sizeof(int)* 1.5*this->current_elements); // error 
    for (i=0; i<this->max_elements ; i++){ 
     new_array[i] = this->array[i]; 

    } 
    this-> max_elements = 1.5*this->current_elements; 

    new_array[this->current_elements] = item; 
    this-> current_elements ++; 

    free ((void *)this->array); 

    this->array = new_array ; 
    return 1; 
} 

} 

    int int_set_remove(Set * this, int item){ 
if (int_set_lookup(this, item) == 0){ 
    return 0; 
} 
int i = 0; 
int array[i]; 

for(i ; i< this-> current_elements && this->array != item; i++); 

for(i; i< this-> current_elements-1; i++){ 
    array[i] = array [i+1]; 
} 
this-> current_elements-- ; 

int freeSlots= this->max_elements-this->current_elements; 
int maxFree= 32> this->current_elements? 16 : this->current_elements/2 ; 
if(freeSlots > maxFree){ 

    int new_array[i]; 
    //int [] new_array = malloc(sizeof(int)* 1.5*this->current_elements); 
    int [] new_array = malloc(sizeof(int)* 1.5*this->current_elements); // error 
    for (i=0; i<this->current_elements ; i++){ 
     new_array[i] = this->array[i]; 

    } 
    this-> max_elements = 1.5*this->current_elements; 



    free ((void *)this->array); 

    this->array = new_array ; 
    } 

    return 1; 
    } 

帶有//錯誤的2行有和預期的標識符或' ['令牌錯誤 如何解決上述錯誤? 有問題的行是 if(result-> array [i] == item){//錯誤無效類型的參數' - >' 和 INT [] new_array = malloc的(的sizeof(int)的* 1.5 *這個 - > current_elements); //錯誤' - >'的無效類型參數和預期標識符或'(''''之前的'['令牌錯誤。

請幫助

+0

刪除'INT []'位,它是什麼意思? –

+3

'result'是一個'int' – krsteeve

回答

0
  1. result是一個int,不是一個指針,所以你不能使用->。不是說你可以使用int *,因爲它可以參考struct *
  2. 你已經宣佈new_array,所以不需要再做一次。與Java不同,Plus int [] new_array在C中的語法不正確。
+0

ive將結果更改爲\t int * result;它現在是一個指針,但它現在也給我一個錯誤。我的程序中究竟應該改變什麼? –

+0

不應該在'int_set_new()'中將'result'設置爲'Set *'? –

+0

這些是我已經改變的行\t \t int new_array [] = malloc(sizeof(int)* 1.5 * this-> current_elements);儘管如此, 仍然出現錯誤:S –

0

result是一個局部變量,所以在int_set_new()這是一個Set*int_set_lookup()它是一個int。如果您將其更改爲int*然後它指向一個int,而不是一個結構,所以你怎麼能是指爲int的場->

對於第二個錯誤,this是在C保留字++。也許你在錯誤的語言

BTW編譯你應該縮進你的代碼

相關問題