gpt4 book ai didi

c - c 程序的意外行为,试图解决 E6-2 K&R

转载 作者:太空宇宙 更新时间:2023-11-04 04:32:14 26 4
gpt4 key购买 nike

<分区>

练习 6-2. 编写一个程序,读取 C 程序并按字母顺序打印每组前 6 个字符相同但之后某处不同的变量名。不算数字符串和注释中的单词。使 6 成为可以从命令行设置的参数。 (来自K&R)

我开始做这个练习,但我卡在了开头。我试图获取整个输入并将其逐行保存在数组中,然后将数组中的指针指向保存的行。后来想到给每个词分配空间,copy到一棵树上。但是,在我作为输入输入时的以下代码中:

abc def;'Ctrl-Z'

#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define MAXLINE 100
#define MAXWORDS 10

int read_input(char *[]);

int main(int argc, char *argv[])
{
char *variables[MAXWORDS] = {0};
int i;
int tmp;


while((tmp = read_input(variables)) != EOF){
for(i=0; i < tmp; i++){
printf("%s ",*(variables+i));
}
}


return 0;
}


/* read_input: mapping all the input words into *variables[], and returning the number of the words + 1 */
int read_input(char *variables[]){
static char s[MAXLINE];
int tmp;
int i, j;

i = 0;
if((tmp = getline(s, MAXLINE)) == EOF)
return EOF;

for(j = 0; j < MAXWORDS && s[i] != '\0'; j++){ /* it does'nt work when j = MAXWORDS because the rest of the line is not saved */
while(!isalpha(s[i]))
i++;
*(variables+j) = &s[i];
while(isalnum(s[i]) || s[i] == '_')
i++;
if(s[i] == '\0')
continue;
s[i++] = '\0';
}
return j;
}

/* getline: read a line into s, return length */
int getline(char *s,int lim)
{
static char end; /* static typed char that saves if the last not saved 'c' was an EOF */
char c;
int i;
if(end == EOF){ /* the last input character was an EOF */
end = 0;
return EOF;
}
for (i=0; i < lim-1 && (c=getchar())!=EOF && c!=';'; ++i)
s[i] = c;
s[i] = '\0';
if(i > 0 && c == EOF)
end = EOF;
else if(i == 0 && c == EOF){
printf("!");
return EOF;
}
else
return i;

}

程序不会立即停止,main 的 while 循环会要求另一个输入...我做错了什么?

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