2017-02-25 57 views
0

我有以下代碼:鍵盤輸入,使功能無法正常工作 - ç

browser.h

#include <stdlib.h> 
#include <string.h> 

typedef struct webpage { 
    struct webpage *prev; 
    struct webpage *next; 
    char* url; 
} Webpage; 

Webpage *current = NULL; 

void free_mem(Webpage *p) { 
    if (p->next != NULL) { 
     free_mem(p->next); 
    } 
    free(p); 
} 

char * add_url(char *url) { 
    Webpage *p = malloc(sizeof(Webpage)); 
    p->url = url; 
    if (current != NULL) { 
     if (current->next != NULL) { 
      free_mem(current->next); 
     } 
     current->next = p; 
     p->prev = current; 

    } else { 
     p->prev = NULL; 
    } 
    p->next = NULL; 
    current = p; 
} 

void list_history() { 
    Webpage *temp = current; 
    printf("--- HISTORIAL ---\n"); 
    while(temp != NULL) { 
     printf("%s\n", temp->url); 
     temp = temp->prev; 
    } 
    printf("--- FIN ---\n"); 
} 

void nav_back() { 
    if (current->prev != NULL) { 
     current = current->prev; 
    } else { 
     printf("IGNORADO\n"); 
    } 
} 

void nav_forward() { 
    if (current->next != NULL) { 
     current = current->next; 
    } else { 
     printf("IGNORADO\n"); 
    } 
} 

的main.c

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

int main() { 
    add_url("www.google.com"); 
    add_url("www.something.com"); 
    add_url("www.a.com"); 
    add_url("www.b.com"); 
    add_url("www.c.com"); 
    add_url("www.d.com"); 
    add_url("www.e.com"); 
    list_history(); 
    nav_back(); 
    nav_back(); 
    list_history(); 
    nav_forward(); 
    list_history(); 
    add_url("www.new-url.com"); 
    list_history(); 
    system("pause"); // this line only works on Windows 
    return 0; 
} 

//int main (int argc, char *argv[]) { 
// add_url(argv[1]); 
// char *option = "Z"; 
// char input_text[2002]; 
// while (1) { 
//  printf("Ingrese comando(A,S,U,L,Q) (URL actual = %s): ", current->url); 
//  fgets(input_text, 2002, stdin); 
//  option = strtok(input_text, "\n "); 
//  if (strcmp(option, "A") == 0) { 
//   nav_back(); 
//  } else if (strcmp(option, "S") == 0) { 
//   nav_forward(); 
//  } else if (strcmp(option, "U") == 0) { 
//   char *url = strtok(NULL, "\n"); 
//   printf("La URL es: %s\n", url); 
//   add_url(url); 
//  } else if (strcmp(option, "L") == 0) { 
//   list_history(); 
//  } else if (strcmp(option, "Q") == 0) { 
//   break; 
//  } else { 
//   printf("OPCION NO VALIDA\n"); 
//  } 
// } 
//} 

第一main功能完美地工作,它做它所做的一切,但第二個main福nction不能正常工作(即我添加一些網址,它似乎很好地工作,但是當我嘗試list_history沒有出現)

我找不到爲什麼第二次main不工作的原因。如果你能幫助我將非常感激

樣品執行:

Ingrese comando(A,S,U,L,Q) (URL actual = (null)): U a.com 
La URL es: a.com 
Ingrese comando(A,S,U,L,Q) (URL actual = a.com): U b.com 
La URL es: b.com 
Ingrese comando(A,S,U,L,Q) (URL actual = b.com): U c.com 
La URL es: c.com 
Ingrese comando(A,S,U,L,Q) (URL actual = c.com): U d.com 
La URL es: d.com 
Ingrese comando(A,S,U,L,Q) (URL actual = d.com): U e.com 
La URL es: e.com 
Ingrese comando(A,S,U,L,Q) (URL actual = e.com): U f.com 
La URL es: f.com // works fine up to here 
Ingrese comando(A,S,U,L,Q) (URL actual = f.com): L // L should show a list of all URLS written above 
--- HISTORIAL --- 






--- FIN --- // You can see that history has the space for the URLs but none was printed 
Ingrese comando(A,S,U,L,Q) (URL actual =): A // now it doesn't show current URL "URL actual" any more. Notice before it was showing current after I added one. 
Ingrese comando(A,S,U,L,Q) (URL actual =): Q 
+0

請顯示一個示例運行並解釋輸出的錯誤。 –

+0

當你用調試器完成時會發生什麼,它會按照你的期望做什麼? – pm100

+0

@ pm100我已經嘗試過在CodeBlocks中使用調試器,但它在一段時間後崩潰 – angardi

回答

3

複製註釋到一個答案。

創建一個函數來打印給定起點的列表。用它來查看列表發生了什麼。

您不斷在循環中傳遞相同的緩衝區到add_url(),但它包含最近寫入的內容。您的工作代碼每次在不同的存儲器中傳遞不同的字符串。複製字符串,然後將其保存到列表中 - 查找strdup()