gpt4 book ai didi

c - 没有 Goto 的更好方法?

转载 作者:行者123 更新时间:2023-11-30 18:53:37 26 4
gpt4 key购买 nike

我有一个程序,我的代码使用 goto 语句,我想以一种很好的方式摆脱它,但我似乎找不到解决方案。如果 goto 是最好的方法,请告诉我。以下是代码摘要:

//Counts how many times every word appears in a file
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NUMWORDS 1000
#define WORDLEN 50

typedef struct
{
char word[WORDLEN + 1];
int num;
} Appearance;

int main(int argc, char *argv[]) {
FILE *readfile;
Appearance *appearlist[NUMWORDS] = {NULL};
char word[WORDLEN + 1];
int i;
//Get a valid filename and open the file, store pointer into readfile
...

char c;

while (c != EOF) {
skip: //Annoying label
//Get a word from readfile, store into word
...

if (word[0] != '\0') {
for (i = 0; i < NUMWORDS && appearlist[i]; i++) {
if (strcmp(appearlist[i] -> word, word) == 0) {
appearlist[i] -> num++;
goto skip; //Annoying goto
}
}
appearlist[i] = (Appearance *) malloc(sizeof(Appearance));
appearlist[i] -> num = 1;
strcpy(appearlist[i] -> word, word);
}
}

//Display results, free memory
...

return 0;
}

问题是,我想跳过我想跳过的循环之外的代码。我不想创建另一个专门为此设计的变量。如果您想要完整的代码,请单击“显示代码片段”。

//Counts how many times every word appears in a file
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NUMWORDS 1000
#define WORDLEN 50
#define FILENAMELEN 50

typedef struct
{
char word[WORDLEN + 1];
int num;
} Appearance;

int main(int argc, char *argv[])
{
char filename[FILENAMELEN];
FILE *readfile;
Appearance *appearlist[NUMWORDS] = {NULL};
char word[WORDLEN + 1];
size_t ln;
int i;

if (argc == 2)
strncpy(filename, argv[1], sizeof(filename));
else {
printf("Enter a filename to count appearances from, or just press enter to quit: ");
fgets(filename, FILENAMELEN, stdin);
ln = strlen(filename) - 1;
if (filename[ln] == '\n')
filename[ln] = '\0';
}

while((readfile = fopen(filename, "r")) == NULL) {
if (filename[0] == '\0')
return 0;
printf("Invalid file! Please enter another filename, or just press enter to quit: ");
fgets(filename, FILENAMELEN, stdin);
ln = strlen(filename) - 1;
if (filename[ln] == '\n') filename[ln] = '\0';
}

char c;

while (c != EOF) {
skip:
for (i = 0; (c = getc(readfile)) != EOF && (isalnum(c) || c == '\''); i++) {
if (i >= WORDLEN) {
word[i] = '\0';
printf("\nWarning: word too long (over %d characters), trimming to: %s\n", WORDLEN, word);
while ((c = getc(readfile)) != EOF && (isalnum(c) || c == '\'')) ;
} else {
word[i] = tolower(c);
}
}
word[i] = '\0';

if (word[0] != '\0') {
for (i = 0; i < NUMWORDS && appearlist[i]; i++) {
if (strcmp(appearlist[i] -> word, word) == 0) {
appearlist[i] -> num++;
goto skip;
}
}
appearlist[i] = (Appearance *) malloc(sizeof(Appearance));
appearlist[i] -> num = 1;
strcpy(appearlist[i] -> word, word);
}
}

for (i = 0; i < NUMWORDS && appearlist[i]; i++) {
printf("Word: %s, Appearances: %d\n", appearlist[i] -> word, appearlist[i] -> num);
free(appearlist[i]);
}

return 0;
}

最佳答案

在这种情况下使用goto通常被认为是可以接受的。

替代方案是设置一个变量,以便您可以在从内部循环break之后在外部循环中继续,或者转动您想要的整个段转义到一个单独的函数,然后从该函数返回,而不是使用 goto

我忽略了与问题无关的代码可能存在的任何其他问题!

关于c - 没有 Goto 的更好方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32640164/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com