如何修复这个结构? [英] How to fix this structure?

查看:65
本文介绍了如何修复这个结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我正在努力完成我的作业,但我似乎没有

弄清楚如何修复这个似乎输出错误的结构。

这个程序应该做的是使用保存单词和行的结构

使用malloc在数组指针中的数字并打印它们

out alphabetized(无重复项)。

大部分都有效,除了似乎已损坏的字样。这是一个

代码,如果有人可以帮助我,那将会有很大的帮助,因为我已经很长时间没有工作了。我甚至尝试将''\0''更改为

NULL,但程序出错了。

这是我的代码:


#include< stdio.h>

#include< stdlib.h>

#include< string.h>

#include< ctype.h>

typedef struct list

{

int linenum;

char word [30];

}列表;


void main()

{

LIST * p [1000],* holder;

int line = 1;

int pcounter = 0;

char buffer [40];

int buffercounter = 0;

char letter ='''';

int inword = 0;

int i ;

int比较;

int shouldstore = 1;

int x,y;

while(字母! = EOF)

{

letter = getchar();

if((letter> =''a''&&字母< =''z'')||(字母> =

''A''&&字母< =''Z''))

{

letter = toupper(letter);

inword = 1;

buffer [buffercounter] = letter;

buffercounter ++;

}

else if(letter ==''\ n'')

{

line ++;

if(inword == 1)

{

inword = 0;

buffercounter ++;

buffer [buffercounter] =''\''';

buffercounter = 0;

if(pcounter == 0)

{

p [pcounter] =(LIST *)malloc(sizeof(LIST));

strcpy(p [pcounter] - > word,buffer);

p [pcounter] - > linenum = line;

pcounter ++;

}

else

{

for(i = 0;我< Pcounter可; i ++)

{

compare = strcmp(p [i] - > word,buffer);

if(compare == 0)

{

shouldstore = 0;

}

}

if(shouldstore == 0)

{

shouldstore = 1;

}

else

{

p [pcounter] =(LIST *)malloc(sizeof(LIST));

strcpy(p [pcounter] - > word,buffer);

p [pcounter] - > linenum = line;

pcounter ++;

}

}


}

其他

{

buffercounter = 0;

}

}

其他

{

if(inword == 1)

{

inword = 0;

buffercounter ++;

buffer [buffercounter] =''\ 0'';

if(b) pcounter == 0)

{

p [pcounter] =(LIST *)malloc(sizeof(LIST));

strcpy(p [ pcounter] - > word,buffer);

p [pcounter] - > linenum = line;

pcounter ++;

}

else

{

for(i = 0;我< Pcounter可; i ++)

{

compare = strcmp(p [i] - > word,buffer);

if(compare == 0)

{

shouldstore = 0;

}

}

if(shouldstore == 0)

{

shouldstore = 1;

}

else

{

p [pcounter] =(LIST *)malloc(sizeof(LIST));

strcpy(p [pcounter] - > word,buffer);

p [pcounter] - > linenum = line;

pcounter ++;

}

}

}

其他

{

buffercounter = 0;

}

}

}


for(x = 0; x< pcounter; x ++)

for(y = 0; y < pcounter-1; y ++)

{

compare = strcmp(p [y] - > word,p [y + 1] - > word);

if(比较< 0)

{

holder = p [y + 1];

p [ y + 1] = p [y];

p [y] =持有人;

}

}


for(x = 0; x< pcounter; x ++)

{

printf(" ; \ n单词%s在行%d",p [x] - > word,p [x] - > linenum);

}

}

Hi everyone. I am trying to finish my homework, but I seem not to
figure out how to fix this structure that seems to make wrong output.
What this program should do is use structure to save the word and line
number on which it was in array pointer using malloc and print them
out alphabetized (no duplicates).

Most of it works, except the words seem to be corrupted. Here is a
code, if anyone can help me it would help so much since I''ve been
working on this for a really long time. I tried even changing ''\0'' to
NULL, but then program errored out.
Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct list
{
int linenum;
char word[30];
} LIST;

void main()
{
LIST *p[1000], *holder;
int line = 1;
int pcounter = 0;
char buffer[40];
int buffercounter = 0;
char letter = '' '';
int inword = 0;
int i;
int compare;
int shouldstore = 1;
int x,y;
while (letter != EOF)
{
letter = getchar();
if ( (letter >= ''a'' && letter <= ''z'') || (letter >=
''A'' && letter <= ''Z'') )
{
letter = toupper(letter);
inword = 1;
buffer[buffercounter] = letter;
buffercounter++;
}
else if (letter == ''\n'')
{
line++;
if (inword == 1)
{
inword = 0;
buffercounter++;
buffer[buffercounter] = ''\0'';
buffercounter = 0;
if (pcounter == 0)
{
p[pcounter] = (LIST*) malloc (sizeof(LIST));
strcpy( p[pcounter]->word , buffer);
p[pcounter]->linenum = line;
pcounter++;
}
else
{
for (i = 0; i < pcounter; i++)
{
compare = strcmp(p[i]->word, buffer);
if (compare == 0)
{
shouldstore = 0;
}
}
if (shouldstore == 0)
{
shouldstore = 1;
}
else
{
p[pcounter] = (LIST*) malloc (sizeof(LIST));
strcpy( p[pcounter]->word, buffer);
p[pcounter]->linenum = line;
pcounter++;
}
}

}
else
{
buffercounter = 0;
}
}
else
{
if (inword == 1)
{
inword = 0;
buffercounter++;
buffer[buffercounter] = ''\0'';
if (pcounter == 0)
{
p[pcounter] = (LIST*) malloc (sizeof(LIST));
strcpy( p[pcounter]->word , buffer);
p[pcounter]->linenum = line;
pcounter++;
}
else
{
for (i = 0; i < pcounter; i++)
{
compare = strcmp(p[i]->word, buffer);
if (compare == 0)
{
shouldstore = 0;
}
}
if (shouldstore == 0)
{
shouldstore = 1;
}
else
{
p[pcounter] = (LIST*) malloc (sizeof(LIST));
strcpy( p[pcounter]->word, buffer);
p[pcounter]->linenum = line;
pcounter++;
}
}
}
else
{
buffercounter = 0;
}
}
}

for(x = 0; x < pcounter; x++)
for(y = 0; y < pcounter-1; y++)
{
compare = strcmp(p[y]->word, p[y+1]->word);
if( compare < 0)
{
holder = p[y+1];
p[y+1] = p[y];
p[y] = holder;
}
}

for (x = 0; x < pcounter; x++)
{
printf("\nThe word %s was on line %d", p[x]->word, p[x]->linenum);
}
}

推荐答案

2004年5月17日11:44:47 -0700, ch ************ @ earthlink.net (Chris R.)

写道:
On 17 May 2004 11:44:47 -0700, ch************@earthlink.net (Chris R.)
wrote:
void main()


您的第一个问题:这会让您的程序调用未定义的行为。

相反,请使用:


int main(void)


else if(letter ==''\ n'')
{
line ++;
if(inword == 1)
{
inword = 0;
buffercounter ++;


删除最后一行。 buffercounter已经指出了一个字符串后面的结尾,所以这允许额外的字符进入。

buffer [buffercounter] =''\''';
void main()
Your first problem: this makes your program invoke undefined behaviour.
Instead, use:

int main(void)

else if (letter == ''\n'')
{
line++;
if (inword == 1)
{
inword = 0;
buffercounter++;
Remove this last line. buffercounter already points one past the end of
the string, so this allows an extra character to creep in.
buffer[buffercounter] = ''\0'';




而不是这里,你可以把


memset(缓冲区,0,sizeof缓冲区);


在else子句的末尾,并消除了前面的两行。


现在,我们要谈谈检查以确保输入的单词不是' '$

太长了?那个缓冲区可以容纳40个字符,但结构只能是
持有30个字符?而不是strcpy,使用strncpy,否则你正在写

Micorosft质量的代码。


我看起来没有比这更远,因为你唯一的抱怨就是名单上的字样坏了。

-

#include< standard.disclaimer>

_

Kevin D Quitt USA 91387-4454所有统计数据的96.37%组成

根据FCA,此地址不得添加到任何商业广告中邮件列表



Instead of this here, you could put

memset( buffer, 0, sizeof buffer );

at the end of else clause, and eliminate both of the previous lines.

Now, shall we talk about checking to make sure that entered words aren''t
too long? That buffer can hold 40 characters, but the structure can only
hold 30? Instead of strcpy, use strncpy, otherwise you''re writing
Micorosft-quality code.

I didn''t look any farther than this, since your only complaint was the
corruption of the words on the list.
--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list


" Chris R."写道:
"Chris R." wrote:

大家好。我正在努力完成我的作业,但我似乎没有弄清楚如何修复这个似乎输出错误的结构。
这个程序应该做的是使用结构来保存单词和行<使用malloc在数组指针中使用malloc并打印出来的数字
以字母顺序排列(没有重复)。

大多数都可以工作,除了单词似乎已损坏。这是一个
代码,如果有人可以帮助我,那将非常有帮助,因为我已经很长时间没有这个了。我甚至尝试将''\0''改为
NULL,但程序出错了。

这是我的代码:

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

typedef struct list
{ade;
char word [30];
}列表;

void main()
{
LIST * p [1000],* holder;
int line = 1;
int pcounter = 0;
char buffer [40];
int buffercounter = 0;
char letter ='''';
int inword = 0;
int i;
int compare;
int shouldstore = 1;
int x,y;

while(letter!= EOF)

Hi everyone. I am trying to finish my homework, but I seem not to
figure out how to fix this structure that seems to make wrong output.
What this program should do is use structure to save the word and line
number on which it was in array pointer using malloc and print them
out alphabetized (no duplicates).

Most of it works, except the words seem to be corrupted. Here is a
code, if anyone can help me it would help so much since I''ve been
working on this for a really long time. I tried even changing ''\0'' to
NULL, but then program errored out.

Here is my code:

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

typedef struct list
{
int linenum;
char word[30];
} LIST;

void main()
{
LIST *p[1000], *holder;
int line = 1;
int pcounter = 0;
char buffer[40];
int buffercounter = 0;
char letter = '' '';
int inword = 0;
int i;
int compare;
int shouldstore = 1;
int x,y;

while (letter != EOF)




我在这里停了下来,因为你已经有两个明显的错误了。 main

返回int,而不是void,所以说并且这样做。其次,一个字母不能持有EOF,因此字母必须是一个整数。


修复你的过度缩进。使用空格,而不是制表符。不要施放

malloc。使用网络发布限制线路长度为65个字符。


-

Chuck F(cb********@yahoo.com)( cb********@worldnet.att.net)

可用于咨询/临时嵌入式和系统。

< http://cbfalconer.home .att.net>使用worldnet地址!



I stopped here, because you already have two glaring errors. main
returns int, not void, so say and do so. Second, a char cannot
hold EOF, so letter must be an int.

Fix your excessive indentation. Use spaces, not tabs. Don''t cast
malloc. Limit line length to 65 chars for usenet posting.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


" Kevin D. Quitt"写道:
"Kevin D. Quitt" wrote:
buffer [buffercounter] =''\ 0'';
buffer[buffercounter] = ''\0'';



而不是这个在这里,你可以把memset(缓冲区,0,sizeof缓冲区);

放在else子句的末尾,并消除前两行。



Instead of this here, you could put

memset( buffer, 0, sizeof buffer );

at the end of else clause, and eliminate both of the previous lines.



为什么你认为这是一个改进?


Brian Rodenborn


Why do you think that is an improvement?


Brian Rodenborn


这篇关于如何修复这个结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆