修改程序
#include<stdio.h>#include<stdlib.h>
#include<string.h>
#define DSIZE 1024
struct Person
{
char name;
char phone;
};
int len=0;
struct Contacts
{
struct Person person;
}contacts;
void getinput(struct Person *person)
{
printf("输入名字\n");
scanf("%s",person->name);
printf("输入号码\n");
scanf("%s",person->phone);
}
void addperson()
{
struct Person *person;
if(len >= DSIZE)
{
fprintf(stderr,"内存已满\n");
exit(1);
}
getinput(&person);
len++;
}
void displayperson(struct Person person)
{
printf("姓名:%s",person.name);
printf("电话:%s",person.phone);
}
void displaycontacts()
{
int i;
for(i=0;i<len;i++)
{
displayperson(&contacts);
putchar('\n');
}
}
void releasecontacts()
{
free(&contacts);
}
int main(void)
{
int ch;
struct Person* person=NULL;
contacts = (struct Contacts *)malloc(DSIZE*sizeof(*person));
if(contacts == NULL)
{
fprintf(stderr,"内存分配失败\n");
exit(1);
}
while(1)
{
printf("是否录入");
do
{
ch = getchar();
}while(ch!='y'&& ch!='n');
if(ch == 'y')
{
addperson();
}
else if(ch == 'n')
break;
}
displaycontacts();
releasecontacts();
exit(0);
}
如何修改以上程序,使其能正常运行 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DSIZE 1024
struct Person {
char name;
char phone;
};
int len = 0;
struct Contacts {
struct Person person;
};
void getinput(struct Person *person) {
printf("输入名字:");
scanf("%s", person->name);
printf("输入号码:");
scanf("%s", person->phone);
}
void addperson(struct Contacts *contacts) {
if (len >= DSIZE) {
fprintf(stderr, "内存已满\n");
exit(1);
}
getinput(&contacts->person);
len++;
}
void displayperson(struct Person person) {
printf("姓名:%s\n", person.name);
printf("电话:%s\n", person.phone);
}
void displaycontacts(struct Contacts contacts) {
int i;
for (i = 0; i < len; i++) {
displayperson(contacts.person);
printf("\n");
}
}
int main() {
struct Contacts contacts;
int choice;
while (1) {
printf("请选择操作:\n");
printf("1. 添加联系人\n");
printf("2. 显示联系人\n");
printf("3. 退出程序\n");
printf("请输入数字(1-3):");
scanf("%d", &choice);
switch (choice) {
case 1:
addperson(&contacts);
break;
case 2:
displaycontacts(contacts);
break;
case 3:
printf("感谢使用,再见!\n");
return 0;
default:
printf("输入无效,请重新选择。\n");
break;
}
}
return 0;
}
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
页:
[1]