无法从电话簿代码中删除联系人。其他功能工作正常,但是当我删除一个联系人时,它将停止程序。
我尝试查找其他示例,但还没有找到对我有用的东西。
# Create a CFG
from nltk import CFG
from nltk.parse.generate import generate
grammar = CFG.fromstring("""
Story -> Introduction MainQuest End
LocationInfo -> 'He found himself in a small village where he grew up.'
Introduction -> 'Long ago there was a boy who decided to become a knight.'
MainQuest -> LocationInfo 'He had to get a sword first to fight monsters' Navigate
Navigate -> '[He could go west]' GoodEnd | '[He could go east]' BadEnd
GoodEnd -> 'And he lived happily ever after.'
BadEnd -> 'Finally he died painfully.'
End -> 'The End'
""")
#print(grammar.start())
#print(grammar.productions())
for sentence in generate(grammar, n=2):
print('\n'.join(sentence))
print('\n')
这是我的代码打印出来的内容。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct contact { // Data structure that holds contact information
char FirstName[10]; // Array for first name
char LastName[10]; // Array for last name
int PhoneNum; // Phone number
};
int main() {
// Setting up variables
int Function = 0;
int Choice = 0;
char FName[200][10];
char LName[200][10];
int PNum = 0;
int n = 1;
int size = 1;
struct contact *con = (struct contact *)malloc(size * sizeof(struct contact));
int b = 0, a = 0;
int DelCon = 0;
do { // Will loop through the main function until the user decides to exit the program
// Prints out the main menu of the phone book
printf("\nPhone Book");
printf("\n[1] Add a contact");
printf("\n[2] Delete a contact");
printf("\n[3] Show contacts");
printf("\n[4] Exit program");
printf("\n\nWhat function would you like to use?\n"); // Asks for user input
scanf("%d", &Choice);
switch (Choice) {
case 1: // Case to add a contact into the phone book
printf("\nYou chose to add a contact.");
printf("\nFirst name: ");
scanf("%s", &FName[b]);
printf("\nLast name: ");
scanf("%s", &LName[b]);
printf("\nPhone number (Numbers only): ");
scanf("%d", &PNum);
printf("\nRecord added to the phone book");
// Records the information given into the structure
if (n >= size)
{
size = size * 2;
con = (struct contact*)realloc(con, size * sizeof(struct contact));
}
a = (n - 1);
strcpy(con[a].FirstName, FName[b]);
strcpy(con[a].LastName, LName[b]);
con[a].PhoneNum = PNum;
b = (b + 1);
n++;
// Prints out the given information
printf("\n\nNew contact:");
printf("\nFirst name: %s", con[a].FirstName);
printf("\nLast name: %s", con[a].LastName);
printf("\nPhone number: %d", con[a].PhoneNum);
printf("\nContact number is %d", a);
printf("\n");
break;
case 2: // Case to delete a contact from the phone book
printf("\nYou chose to delete a contact.");
printf("\nWhich contact would you like to delete? Specify by contact number. ");
scanf("%d", &DelCon);
a = DelCon;
char DelF = 0;
char DelL = 0;
int DelNum = 0;
strcpy(con[a].FirstName, DelF);
strcpy(con[a].LastName, DelL);
strcpy(con[a].PhoneNum, DelNum);
printf("\nContact deleted");
printf("\n");
break;
case 3: // Case to see all of the entered contacts
printf("\nYou chose to show the contacts.");
for (a = 0; a < (n - 1); a++) {
printf("\nContact #%d", a);
printf("\nFirst name: %s", con[a].FirstName);
printf("\nLast name: %s", con[a].LastName);
printf("\nPhone number: %d", con[a].PhoneNum);
printf("\n");
}
break;
case 4:
printf("Goodbye!");
break;
}
} while (Choice != 4);
return 0;
}
应该删除联系人并返回菜单。
答案 0 :(得分:2)
进程退出,返回值3221225477
编译时要做的第一件事是询问最高警告级别,如果我对您的代码执行gcc -pedantic -Wextra
,则编译器会向这些行发出信号:
char DelF = 0; char DelL = 0; int DelNum = 0; strcpy(con[a].FirstName, DelF); strcpy(con[a].LastName, DelL); strcpy(con[a].PhoneNum, DelNum);
strcpy 是错误的,因为第二个参数是 char / int ,而期望的是 char * (带有空结束符) ,行为是不确定的,在您的情况下是崩溃。请注意, PhoneNum 是一个 int 而不是一个char数组,所以如果我能说的话,这是最糟糕的。
如果目标是清空2个字符串并重置电话号码,请执行以下操作:
con[a].FirstName[0] = 0;
con[a].LastName[0] = 0;
con[a].PhoneNum = 0;
更正后,执行为:
Phone Book
[1] Add a contact
[2] Delete a contact
[3] Show contacts
[4] Exit program
What function would you like to use?
1
You chose to add a contact.
First name: test
Last name: 1
Phone number (Numbers only): 1234567
Record added to the phone book
New contact:
First name: test
Last name: 1
Phone number: 1234567
Contact number is 0
Phone Book
[1] Add a contact
[2] Delete a contact
[3] Show contacts
[4] Exit program
What function would you like to use?
1
You chose to add a contact.
First name: Test
Last name: 2
Phone number (Numbers only): 8901234
Record added to the phone book
New contact:
First name: Test
Last name: 2
Phone number: 8901234
Contact number is 1
Phone Book
[1] Add a contact
[2] Delete a contact
[3] Show contacts
[4] Exit program
What function would you like to use?
3
You chose to show the contacts.
Contact #0
First name: test
Last name: 1
Phone number: 1234567
Contact #1
First name: Test
Last name: 2
Phone number: 8901234
Phone Book
[1] Add a contact
[2] Delete a contact
[3] Show contacts
[4] Exit program
What function would you like to use?
2
You chose to delete a contact.
Which contact would you like to delete? Specify by contact number. 0
Contact deleted
Phone Book
[1] Add a contact
[2] Delete a contact
[3] Show contacts
[4] Exit program
但是,如果没有真正删除该联系人,则即使在您的列表中将其清空,如果我在删除后执行命令3,也会显示该联系人:
You chose to show the contacts.
Contact #0
First name:
Last name:
Phone number: 0
Contact #1
First name: Test
Last name: 2
Phone number: 8901234
您需要从 con 中删除该条目。您还需要检查要删除的元素等级的有效性。
请注意, n 是 strange ,因为它不包含条目数,但包含一个或多个条目,当该书为空时,其值已经为1,然后为2,等等
b 对于 n 是多余的,因为它的值为n-1
。它是 FName 和 LName 的索引,但是我没有看到这两个2D数组的兴趣,您可以直接将 scanf 转换为 > con 或仅将它们定义为简单的字符数组。最好的方法是删除 b 和 FName 和 LName 。
DelCon 也是没有用的,因为它只是设置 a 的一个中介。
功能从不使用。
当您 scanf 字符串限制输入字符串的大小时,不要冒从接收器冒出来的风险。
考虑到我之前的评论,修改后的版本是:
int main() {
// Setting up variables
int Choice = 0;
int n = 0;
int size = 1;
struct contact *con = (struct contact *)malloc(size * sizeof(struct contact));
int a;
do { // Will loop through the main function until the user decides to exit the program
// Prints out the main menu of the phone book
printf("\nPhone Book");
printf("\n[1] Add a contact");
printf("\n[2] Delete a contact");
printf("\n[3] Show contacts");
printf("\n[4] Exit program");
printf("\n\nWhat function would you like to use?\n"); // Asks for user input
scanf("%d", &Choice);
switch (Choice) {
case 1: // Case to add a contact into the phone book
if (n == size)
{
size = size * 2;
con = (struct contact*)realloc(con, size * sizeof(struct contact));
}
// Records the information given into the structure
printf("\nYou chose to add a contact.");
printf("\nFirst name: ");
scanf("%9s", con[n].FirstName);
printf("\nLast name: ");
scanf("%9s", con[n].LastName);
printf("\nPhone number (Numbers only): ");
scanf("%d", &con[n].PhoneNum);
printf("\nRecord added to the phone book");
// Prints out the given information
printf("\n\nNew contact:");
printf("\nFirst name: %s", con[n].FirstName);
printf("\nLast name: %s", con[n].LastName);
printf("\nPhone number: %d", con[n].PhoneNum);
printf("\nContact number is %d", n);
printf("\n");
n += 1;
break;
case 2: // Case to delete a contact from the phone book
printf("\nYou chose to delete a contact.");
printf("\nWhich contact would you like to delete? Specify by contact number. ");
scanf("%d", &a);
if ((a < 0) || (a >= n))
printf("\ninvalid rank of contact");
else {
/* if you do not have bcopy use memmove */
bcopy(&con[a+1], &con[a], (n - 1 - a) * sizeof(struct contact));
n -= 1;
printf("\nContact deleted");
}
printf("\n");
break;
case 3: // Case to see all of the entered contacts
printf("\nYou chose to show the contacts.");
for (a = 0; a < n; a++) {
printf("\nContact #%d", a);
printf("\nFirst name: %s", con[a].FirstName);
printf("\nLast name: %s", con[a].LastName);
printf("\nPhone number: %d", con[a].PhoneNum);
printf("\n");
}
break;
case 4:
puts("\nGoodbye!");
break;
default:
puts("\ninvalid choice");
}
} while (Choice != 4);
return 0;
}
编译和执行:
pi@raspberrypi:/tmp $ gcc -pedantic -Wextra b.c
pi@raspberrypi:/tmp $ ./a.out
Phone Book
[1] Add a contact
[2] Delete a contact
[3] Show contacts
[4] Exit program
What function would you like to use?
1
You chose to add a contact.
First name: test
Last name: 1
Phone number (Numbers only): 1234567
Record added to the phone book
New contact:
First name: test
Last name: 1
Phone number: 1234567
Contact number is 0
Phone Book
[1] Add a contact
[2] Delete a contact
[3] Show contacts
[4] Exit program
What function would you like to use?
1
You chose to add a contact.
First name: Test
Last name: 2
Phone number (Numbers only): 8901234
Record added to the phone book
New contact:
First name: Test
Last name: 2
Phone number: 8901234
Contact number is 1
Phone Book
[1] Add a contact
[2] Delete a contact
[3] Show contacts
[4] Exit program
What function would you like to use?
3
You chose to show the contacts.
Contact #0
First name: test
Last name: 1
Phone number: 1234567
Contact #1
First name: Test
Last name: 2
Phone number: 8901234
Phone Book
[1] Add a contact
[2] Delete a contact
[3] Show contacts
[4] Exit program
What function would you like to use?
2
You chose to delete a contact.
Which contact would you like to delete? Specify by contact number. 2
invalid rank of contact
Phone Book
[1] Add a contact
[2] Delete a contact
[3] Show contacts
[4] Exit program
What function would you like to use?
2
You chose to delete a contact.
Which contact would you like to delete? Specify by contact number. 0
Contact deleted
Phone Book
[1] Add a contact
[2] Delete a contact
[3] Show contacts
[4] Exit program
What function would you like to use?
3
You chose to show the contacts.
Contact #0
First name: Test
Last name: 2
Phone number: 8901234
Phone Book
[1] Add a contact
[2] Delete a contact
[3] Show contacts
[4] Exit program
What function would you like to use?
4
Goodbye!
pi@raspberrypi:/tmp $