Closed. This question is off-topic. It is not currently accepting answers. Learn more。
想改进这个问题吗?Update the question所以堆栈溢出的值小于aa>。
去年关门了。
我有切分错误:11
运行菜单/命令后,我想将输入添加到列表中。想弄清楚但我无能为力
程序应该将输入保存在列表中,然后在从用户获得选项后打印它们。等等,但为什么我得到分割失败?
如果
(注意:如果您使用一组空大括号(例如
但是,当您
要修剪后面的换行符,请使用:
您需要在每次使用
即使进行了这些更改,您的
另外,在你分配
您必须验证
要调节内存使用,应该为
由于
如果你遇到任何其他障碍你都无法克服,请告诉我,我很乐意进一步帮助你。
想改进这个问题吗?Update the question所以堆栈溢出的值小于aa>。
去年关门了。
我有切分错误:11
运行菜单/命令后,我想将输入添加到列表中。想弄清楚但我无能为力
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <assert.h>
#define MAX_LENGTH 1023
//**********************************************************************
// Linked List Definitions
// Define your linked list node and pointer types
// here for use throughout the file.
//
// ADD STATEMENT(S) HERE
typedef struct contact
{
char familyName[MAX_LENGTH+1];
char firstName[MAX_LENGTH+1];
char address[MAX_LENGTH+1];
char phoneNumber[MAX_LENGTH+1];
struct contact *link;
//pointer to next node.
} contact;
//**********************************************************************
// Linked List Function Declarations
//
// Functions that modify the linked list.
// Declare your linked list functions here.
//
// ADD STATEMENT(S) HERE
//**********************************************************************
// Support Function Declarations
//
//Teacher's function:
void safegets (char s[], int arraySize); // gets without buffer overflow
void familyNameDuplicate (char familyName[]); // marker/tester friendly
void familyNameFound (char familyName[]); // functions to print
void familyNameNotFound (char familyName[]); // messages to user
void familyNameDeleted (char familyName[]);
void phoneNumberFound (char phoneNumber[]);
void phoneNumberNotFound (char phoneNumber[]);
void printPhoneBookEmpty (void);
void printPhoneBookTitle (void);
//My funcs:
void newContact (char familyName[], char firstName[], char address[], char phoneNumber[], contact **p);//want to modify something
void contactDel (contact **p, char familyName[]);
/* remove head */
contact *contact_search (contact *c, char familyName[]);
/* By Family Name*/
contact *phone_search (contact *c, char phoneNumber[]);
void print_all (contact *head);
void DelAll (contact **p);
//**********************************************************************
// Program-wide Constants
//
const char NULL_CHAR = '\0';
const char NEWLINE = '\n';
//**********************************************************************
// Main Program
//
int main (void)
{
contact *head=NULL;
contact *temp=NULL;
const char bannerString[]
= "Personal Phone Book Maintenance Program.\n\n";
const char commandList[]
= "Commands are I (insert), D (delete), S (search by name),\n"
" R (reverse search by phone #), P (print), Q (quit).\n";
// Declare linked list head.
// ADD STATEMENT(S) HERE TO DECLARE LINKED LIST HEAD.
// announce start of program
printf("%s",bannerString);
printf("%s",commandList);
char response;
char res[MAX_LENGTH+1];
char input[MAX_LENGTH+1];
char familyName[MAX_LENGTH+1];
char firstName[MAX_LENGTH+1];
char address[MAX_LENGTH+1];
char phoneNumber[MAX_LENGTH+1];
//to pass char arrays to insert function
do
{
printf("\nCommand?: ");
safegets(input,MAX_LENGTH+1);
// Response is first char entered by user.
// Convert to uppercase to simplify later comparisons.
response = toupper(input[0]);
if (response == 'I')
{
// Insert an phone book entry into the linked list.
// Maintain the list in alphabetical order by family name.
// ADD STATEMENT(S) HERE
// USE THE FOLLOWING PRINTF STATEMENTS WHEN PROMPTING FOR DATA:
printf(" family name: ");
fgets(familyName, MAX_LENGTH+1, stdin);
printf(" first name: ");
fgets(firstName, MAX_LENGTH+1, stdin);
printf(" address: ");
fgets(address, MAX_LENGTH+1, stdin);
printf(" phone number: ");
fgets(familyName, MAX_LENGTH+1, stdin);
newContact(familyName, firstName, address, phoneNumber, &head);
}
else if (response == 'D')
{
// Delete an phone book entry from the list.
// ADD STATEMENT(S) HERE
printf("\nEnter family name for entry to delete: ");
fgets(res,MAX_LENGTH,stdin);
contactDel(&head, res);
}
else if (response == 'S')
{
// Search for an phone book entry by family name.
printf("\nEnter family name to search for: ");
fgets(familyName, MAX_LENGTH, stdin);
temp=contact_search(head, familyName);
if (temp == NULL)
familyNameFound(res);
else
{
printf("%s\n%s\n%s\n%s\n\n", temp->familyName, temp->firstName, temp->address, temp->phoneNumber);
}
// ADD STATEMENT(S) HERE
}
else if (response == 'R')
{
// Search for an phone book entry by phone number.
//ADD STATEMENT(S) HERE
printf("\nEnter phone number to search for: ");
fgets(phoneNumber,MAX_LENGTH,stdin);
temp = phone_search(head, phoneNumber);
if (temp==NULL)
phoneNumberNotFound(phoneNumber);
else
{
phoneNumberFound(phoneNumber);
printf("\n%s\n%s\n%s\n%s\n", temp->familyName, temp->firstName, temp->address, temp->phoneNumber);
}
}
else if (response == 'P')
{
// Print the phone book.
// ADD STATEMENT(S) HERE
print_all(head);
}
else if (response == 'Q')
{
;// do nothing, we'll catch this below
}
else
{
// do this if no command matched ...
printf("\nInvalid command.\n%s\n",commandList);
}
} while (response != 'Q');
// Delete the whole phone book linked list.
// ADD STATEMENT(S) HERE
DelAll(&head);
// Print the linked list to confirm deletion.
// ADD STATEMENT(S) HERE
print_all (head);
return 0;
}
//**********************************************************************
// Support Function Definitions
// Function to get a line of input without overflowing target char array.
void safegets (char s[], int arraySize)
{
int i = 0, maxIndex = arraySize-1;
char c;
while (i < maxIndex && (c = getchar()) != NEWLINE)
{
s[i] = c;
i++;
}
s[i] = NULL_CHAR;
}
// Function to call when user is trying to insert a family name
// that is already in the book.
void familyNameDuplicate (char familyName[])
{
printf("\nAn entry for <%s> is already in the phone book!\n"
"New entry not entered.\n",familyName);
}
// Function to call when a family name was found in the phone book.
void familyNameFound (char familyName[])
{
printf("\nThe family name <%s> was found in the phone book.\n",
familyName);
}
// Function to call when a family name was not found in the phone book.
void familyNameNotFound (char familyName[])
{
printf("\nThe family name <%s> is not in the phone book.\n",
familyName);
}
// Function to call when a family name that is to be deleted
// was found in the phone book.
void familyNameDeleted (char familyName[])
{
printf("\nDeleting entry for family name <%s> from the phone book.\n",
familyName);
}
// Function to call when a phone number was found in the phone book.
void phoneNumberFound (char phoneNumber[])
{
printf("\nThe phone number <%s> was found in the phone book.\n",
phoneNumber);
}
// Function to call when a phone number was not found in the phone book.
void phoneNumberNotFound (char phoneNumber[])
{
printf("\nThe phone number <%s> is not in the phone book.\n",
phoneNumber);
}
// Function to call when printing an empty phone book.
void printPhoneBookEmpty (void)
{
printf("\nThe phone book is empty.\n");
}
// Function to call to print title when whole phone book being printed.
void printPhoneBookTitle (void)
{
printf("\nMy Personal Phone Book: \n");
}
//**********************************************************************
// Add your functions below this line.
// ADD STATEMENT(S) HERE
void newContact (char familyName[], char firstName[], char address[], char phoneNumber[], contact **head)
{
contact *testPtr=NULL;
testPtr = contact_search (*head, familyName);
if (strcmp(testPtr->familyName, familyName)==0)
{
familyNameDuplicate(familyName);
return;
}
else
testPtr = *head;
for ( ; strcmp(testPtr->familyName, familyName) < 0; testPtr = testPtr -> link)
;
contact *c = (contact *)malloc (sizeof(contact));//new node
strcpy(c->familyName, familyName);
strcpy(c->firstName, firstName);
strcpy(c->address, address);
strcpy(c->phoneNumber, phoneNumber);
if (head==NULL)
{
c->link = *head;
*head=c;
}
else
{
;
}
}
void contactDel (contact **p, char familyName[]) /* remove head */
{
contact *control=contact_search(*p, familyName);
if (control != NULL)
{
contact *n = control;
control = control -> link;
free(n);
}
}
void DelAll (contact **p)
{
if (*p != NULL)
{
contact *n = *p;
*p = (*p)->link;
free(n);
}
return;
}
contact *contact_search (contact *c, char familyName[]) /* By Family Name*/
{
while (c != NULL)
{
if (strcmp(c->familyName, familyName)==0)
{
return c;
}
c = c->link;
}
return NULL;
}
contact *phone_search (contact *c, char phoneNumber[])
{
while (c != NULL)
{
if (strcmp(c->phoneNumber, phoneNumber)==0)
{
return c;
}
c = c->link;
}
return NULL;
}
void print_all (contact *head)
{
if (head == NULL)
{
printPhoneBookEmpty();
}
else
{
printPhoneBookTitle();
while (head != NULL)
{
printf("%s\n%s\n%s\n%s\n\n", head->familyName, head->firstName, head->address, head->phoneNumber);
head = head->link;
}
}
}
程序应该将输入保存在列表中,然后在从用户获得选项后打印它们。等等,但为什么我得到分割失败?
最佳答案
除了上面的testPtr
是NULL
之外,您还将遇到以下问题:
for ( ; strcmp(testPtr->familyName, familyName) < 0; testPtr = testPtr -> link)
;
如果
testPtr
为空,则不能调用strcmp(testPtr->familyName
,也不能调用testPtr = testPtr -> link
。(这是插入时的最后一个SegFault)(注意:如果您使用一组空大括号(例如
;
)而不是尾随的for (...) {}
,则更容易阅读)但是,当您
Search (S)
时,您将永远不会匹配testPtr->familyName
,因为在使用fgets
阅读之后,您的familyName
包含:+---+---+---+---+---+---+---+---+---+
| S | o | m | e | n | a | m | e | \n| <== note trailing '\n' included
+---+---+---+---+---+---+---+---+---+
要修剪后面的换行符,请使用:
if (!fgets(familyName, MAX_LENGTH, stdin)) {
fprintf (stderr, "error: user canceled input.\n");
continue;
}
size_t len = strlen (familyName); /* get length */
if (len && familyName[len - 1] == '\n') /* check last char '\n' */
familyName[--len] = 0; /* overwrite with nul-char */
else {
/* handle line too long */
}
您需要在每次使用
fgets
时修剪后面的换行符,并且还应该处理“行太长”的情况。您也可以使用任何其他字符串函数来定位尾随的'\n'
,例如char *nl = strchr (familyName, '\n'); if (nl) *nl = 0;
,等等。。创建一个简短的函数来处理这个杂务将节省大量的代码重复。即使进行了这些更改,您的
Search (S)
仍然会报告一个空列表——但您的SegFault已被修复。解决那个问题就交给你了。另外,在你分配
contact *c = (contact *)malloc (sizeof(contact));//new node
您必须验证
(c != NULL)
因为malloc
可能会失败,或者您可以耗尽可用内存(这越来越可能为10*2
中的每个字符串分配contact
字节)。另请参见:Do I cast the result of malloc?要调节内存使用,应该为
familyName
、firstName
、address
和phoneNumber
分配内存。strdup
提供了一种简单的执行方式。只需将contacts中的每个字符串声明为指针,然后在读取(例如familyName
和fgets
并修剪换行符)之后,只需允许strdup
分配所需的确切空间并将familyName
复制到新的内存块。那就很简单了c->familyName = strdup (familyName);
由于
strdup
分配,您还应该验证它是否成功,例如。if (!c->familyName) {
/* handle error */
}
如果你遇到任何其他障碍你都无法克服,请告诉我,我很乐意进一步帮助你。
09-06 15:50