键盘输入使功能无法正常工作 - C
Keyboard input makes function not work properly - C
我有以下代码:
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
函数无法正常工作(即我添加了一些 url,它似乎工作正常,但是当我尝试 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
正在将评论复制到答案中。
创建一个函数来打印给定起点的列表。用它来查看列表发生了什么。
您在循环中不断将相同的缓冲区传递给 add_url()
,但它包含最近写入的内容。您的工作代码每次都在不同的存储中传递不同的字符串。在将字符串保存到列表之前复制它 — 查找 strdup()
.
我有以下代码:
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
函数无法正常工作(即我添加了一些 url,它似乎工作正常,但是当我尝试 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
正在将评论复制到答案中。
创建一个函数来打印给定起点的列表。用它来查看列表发生了什么。
您在循环中不断将相同的缓冲区传递给 add_url()
,但它包含最近写入的内容。您的工作代码每次都在不同的存储中传递不同的字符串。在将字符串保存到列表之前复制它 — 查找 strdup()
.