2015-09-06 88 views
-2

我有一個程序正在查找總數以及鏈接列表中的中間數字是什麼。我擁有的問題是爲什麼它不打印出值?C程序不會打印出值

下面是代碼:

int count(list values){ 
    if(values == NULL) 
     return 0; 
    else 
     return 1 + count(values->next); 
} 

void middle(struct node *head){ 
    int count = 0; 
    struct node *mid = head; 

    while (head != NULL){ 
     if(count & 1) 
      mid = mid->next; 

     count++; 
     head = head->next; 
    } 
} 

void traverse(list values){ 
    if(values->next) 
    printf("\n# of the values: %.1f% \nMiddle: %.1f%\n", count, middle); 
} 

int main(int argc, char *argv[]){ 
    FILE *input = stdin; 

    list values = readNumbers(input); 
    traverse(values); 
    return 0; 
} 
+0

向我們展示了整個代碼,請。 – Downvoter

+0

什麼是預期和實際產出? –

+0

列表readNumbers(FILE *文件){ \t列表值= NULL,第一= NULL; \t詮釋三; \t而((C = GETC(文件))= EOF!){ \t \t INT I = 0; \t \t \t \t列表新=( list)malloc(sizeof(struct node)); \t \t \t \t if(values == NULL)values = new; \t \t else { \t \t \t values-> next = new; \t \t \t values = new; \t \t} \t \t values = new; \t \t \t \t if(first == NULL)first = values; \t \t do { \t \t \t values-> value [i ++] = c; \t \t} while((c = getc(file))!='\ n'&& c!= EOF); \t \t values-> value [i] ='\ 0'; \t} \t首先返回; } – edge

回答

0

很難知道從哪裏開始。我無法確定你想要做什麼。

但是讓我們看看這個行:

printf("\n# of the values: %.1f% \nMiddle: %.1f%\n", count, middle); 

countmiddle的功能,但你是不是在這裏調用這些功能。你只是將這些函數的地址傳遞給printf(),它不知道這些函數是函數。您需要包括爲了調用這些函數的函數名(count(args)middle(args)後面的括號中。

+0

如果我通過計數(值)或中間值(值),它會工作嗎 – edge

+0

您沒有足夠的代碼來回答這個問題。但它是你的代碼。你是應該測試它的人。你試過了嗎?你有沒有編譯錯誤?什麼是錯誤? –

+1

我明白了這一點..我現在在中間工作..我想現在我遇到的問題是中間是空白..如果我需要更多的幫助,我會回來..謝謝 – edge

0
printf("\n# of the values: %.1f% \nMiddle: %.1f%\n", count, middle); 

count應接收的參數(list)。編譯器將把參數countmiddle作爲函數指針。

另請注意,middlevoid函數,所以您想要打印什麼?