我正在修复一个程序,但是我遇到了一个问题,我无法真正意识到代码中的错误。我将不胜感激任何帮助。 我没有发布所有代码,但我认为通过这部分你可以了解它。 使用以下函数输入()我想将用户命令的数据添加到列表中。 例如。用户给出命令:“en james bond 007 gun”[“en”表示在命令提示符下“输入”]
'james'应该是名字,'bond'是姓氏,007是金额,其余是描述。我使用strtok来“剪切”命令,然后我将每个名字放在临时数组上。然后我调用InsertSort,以便将数据放在链表上,但按字母顺序排列,具体取决于用户提供的姓氏。我希望按顺序保留列表,并将每次元素放在正确的位置。
此外,InsertSort()和SortedInsert()可能存在错误......因为数据不会像应该的那样写在列表中。
/* struct for all the data that user enters on file */
typedef struct catalog
{
char short_name[50];
char surname[50];
signed int amount;
char description[1000];
struct catalog *next;
}catalog,*catalogPointer;
catalogPointer current;
catalogPointer head = NULL;
void enter(void)//user command: en <name> <surname> <amount> <description>
{ int n,j=2,k=0;
char temp[1500];
char command[1500];
while (command[j]!=' ' && command[j]!='\0')
{ temp[k]=command[j];
j++;
k++;
}
temp[k]='\0';
char *curToken = strtok(temp," ");
printf("temp is:%s \n",temp);
char short_name[50],surname[50],description[1000];
signed int amount;
//short_name=(char *)malloc(sizeof (char *));
//surname=(char *)malloc(sizeof (char *));
//description=(char *)malloc(sizeof (char *));
//amount=(int *)malloc(sizeof (int *));
printf("\nWhat you entered for saving:\n");
for (n = 0; curToken !='\0'; ++n)
{
if (curToken)
{
strncpy(short_name, curToken, sizeof (char *)); /
}
printf("Short Name: %s \n",short_name);
curToken = strtok(NULL," ");
if (curToken)
strncpy(surname, curToken, sizeof (char *)); /
printf("SurName: %s \n",surname);
curToken = strtok(NULL," ");
if (curToken)
{
char *chk;
amount = (int) strtol(curToken, &chk, 10);
if (!isspace(*chk) && *chk != 0)
fprintf(stderr,"Warning: expected integer value for amount, received %s instead\n",curToken);
}
printf("Amount: %d \n",amount);
curToken = strtok(NULL,"\0");
if (curToken)
{ strncpy(description, curToken, sizeof (char *));
}
printf("Description: %s \n",description);
break;
}
if (findEntryExists(head, surname) != NULL)
printf("\nAn entry for <%s %s> is already in the catalog!\nNew entry not entered.\n",short_name,surname);
else
{
printf("\nTry to entry <%s %s %d %s> in the catalog list!\n",short_name,surname,amount,description);
InsertSort(&head,short_name, surname, amount, description);
printf("\n**Entry done!**\n");
}
// Maintain the list in alphabetical order by surname.
}
/********Uses special case code for the head end********/
void SortedInsert(catalog** headRef, catalogPointer newNode,char short_name[],char surname[],signed int amount,char description[])
{
strcpy(newNode->short_name, short_name);
strcpy(newNode->surname, surname);
newNode->amount=amount;
strcpy(newNode->description, description);
// Special case for the head end
if (*headRef == NULL||(*headRef)->surname >= newNode->surname)
{ newNode->next = *headRef;
*headRef = newNode;
}
else
{ // Locate the node before the point of insertion
catalogPointer current = *headRef;
catalogPointer temp=current->next;
while ( temp!=NULL )
{
if(strcmp(temp->surname,newNode->surname)<0 )
current = temp;
}
newNode->next = temp;
temp = newNode;
}
}
// Given a list, change it to be in sorted order (using SortedInsert()).
void InsertSort(catalog** headRef,char short_name[],char surname[],signed int amount,char description[])
{
catalogPointer result = NULL; // build the answer here
catalogPointer current = *headRef; // iterate over the original list
catalogPointer next;
while (current!=NULL)
{
next = current->next; // tricky - note the next pointer before we change it
SortedInsert(&result,current,short_name,surname,amount,description);
current = next;
}
*headRef = result;
}
运行程序我得到了这些奇怪的东西(垃圾?)......
选择您的选择:
詹姆斯邦德007枪你的命令是:en james bond 007枪
temp是:james
您输入的保存内容:简短
姓名:詹姆斯 姓:
金额:0描述:0T Г
尝试输入詹姆斯00T Г 目录清单!
完成输入!
此外,我还面临着如何在此计划中使用'malloc'的问题。
提前致谢。 。 。
更新: 我做了另一个入门功能。但不知何故奇怪我得到了strcpy的细分! 就是这样:
catalogPointer newEntry (char short_name[], char surname[], signed int amount, char description[])
{
catalogPointer newNode,first,second,tmp;
first=head;
second=NULL;
strcpy(newNode->short_name, short_name); //SEGMENTATION
strcpy(newNode->surname, surname);
newNode->amount=amount;
strcpy(newNode->description, description);
while (first!=NULL)
{ if (strcmp(surname,first->surname)>0)
second=first;
else if (strcmp(surname,first->surname)==0)
{
if (strcmp(short_name,first->short_name)>0)
second=first;
}
first=first->next;
}
if (second==NULL)
{ newNode->next=head;
head=newNode;
}
else
{ tmp=second->next;
newNode->next=tmp;
first->next=newNode;
}
}
答案 0 :(得分:3)
问题的直接原因在于:
while (command[j]!=' ' && command[j]!='\0')
{ temp[k]=command[j];
j++;
k++;
}
您在这里做的是将command
的内容复制到temp
,直到第一个空格字符。也就是说,您只复制"james"
。因此,在短名称之后,strtok
不再需要解析,因此其他缓冲区不会被正确初始化并且只包含垃圾。
更新:要在第一个空格字符之后复制,请使用strchr
和strcpy
代替该循环:
char* params = strchr(command, ' ') + 1;
strcpy(temp, params);
除此之外,您在此处错误地指定了目标缓冲区的大小:
strncpy(surname, curToken, sizeof (char *));
应该是
strncpy(surname, curToken, 49);
其次:
中的循环表达式for (n = 0; curToken !='\0'; ++n)
应该是
for (n = 0; curToken; ++n)
否则,在解析完所有标记后,循环将无法正确终止。
答案 1 :(得分:2)
您似乎没有填充command
任何内容,因此您可以对堆栈中的垃圾进行标记。