c语言问题,急啊,在线等啊!!!

2025-01-01 10:43:37
推荐回答(3个)
回答1:

#include
#include
#include

struct phonebook
{
char name[10];
float number;
char E_mail[30];
char information[50];
struct phonebook * next;
};

struct phonebook *create_phonebook(); /* 创建链表(创建)*/
struct phonebook *insert_phonebook(struct phonebook *head, struct phonebook *new_book);/*插入节点(添加)*/
struct phonebook *delete_phonebook(struct phonebook *head,char *name);/*删除节点(删除)*/

/*struct phonebook * rewrite_phonebook(struct phonebook * head);/*修改结构体(修改)*/
void seach_phonebook (struct phonebook * head); /*打印联系人*/

void main ()
{
struct phonebook *head=NULL, *p;
int choice;
char name[10], *temp;
temp=name;

do
{
puts("-------------------菜 单-----------------------");
printf("1.创建\t2.添加\t3.删除\t4.修改\t5.查询\t0.退出\n");
puts("-------------------------------------------------");

printf("请选择:");
scanf("%d", &choice);

switch(choice)
{
case 1 : head = create_phonebook(); break;
case 2 : p=(struct phonebook *)malloc( sizeof(struct phonebook) );

if(p=NULL)
{
printf("ERRO! NO space available . Pleasse select again.\n");
}

else
{
printf("请输入新的联系人名字(汉语拼音):\n");
scanf("%s", p->name);

printf("请输入新的联系人电话号码:\n");
scanf("%f",p->number);

printf("请输入新的联系人E-Mail:\n");
scanf("%s",p->E_mail);

printf("请输入新的联系人信息(用英语表达):\n");
scanf("%s",p->information);

head = insert_phonebook(head,p);
}

break;
case 3 : printf("请输入要删除的通讯录的名字:\n");

gets(temp); /* 检查此处*/
head=delete_phonebook(head, temp);
break;
/*case 4 : rewrite_phonebook(head);*/
case 5 : seach_phonebook(head);
break;
}
}while(choice != 0);
}

/*
函数功能:创建通讯录
*/
struct phonebook * create_phonebook()
{
struct phonebook * head=NULL,*p;
int i;

while (1)
{
if ((p = (struct phonebook *) malloc(sizeof(struct phonebook)))==NULL);
{
printf("ERRO! NO space available . Pleasse select again.\n");
exit(1);
}

printf("请选择:0.退出\t1.继续创建通讯录:\n");
scanf("%d",&i);

if (i==0) break;
else
{
printf("请输入新的联系人名字(汉语拼音):\n");
scanf("%s",&p->name);
printf("请输入新的联系人电话号码:\n");
scanf("%f",p->number);
printf("请输入新的联系人E-Mail:\n");
scanf("%s",p->E_mail);
printf("请输入新的联系人信息(用英语表达):\n");
scanf("%s",p->information);
head=insert_phonebook(head,p);
}
return head;
}
}

/*
函数功能:添加通讯录
*/
struct phonebook * insert_phonebook(struct phonebook * head,struct phonebook * new_book)
{
struct phonebook *p=head,*q;
if (head == NULL)
{
new_book->next=NULL;
return new_book;

}
else
{

while ((strcmp(p->name,new_book->name))<0)
{
q=p;
p=p->next;

if (p==NULL)
break;
}

if (p==head)
{
new_book->next=head;
return new_book;
}

else if (p==NULL)
{
q->next=new_book;
new_book->next=NULL;
}

else
{
q->next=new_book;
new_book->next=p;
}
}
return head;
}

/*
函数功能:删除联系人
*/
struct phonebook *delete_phonebook(struct phonebook * head,char *name)
{
struct phonebook *p1, *p2=head;

if (head==NULL)
printf("列表不能为空\n");

else
{
while (strcmp(p2->name,name)!=0&&p2->next!=NULL)
{
p1=p2;
p2=p2->next;
}
if (strcmp(p2->name,name)==0)
{
if (p2==head)
{
p1=p2->next;
free(head);
return p1;
}

else
{
p1->next=p2->next;
free(p2);
}
}

else printf("名字未找到\n");
}

return head;
}

/*
函数功能:查找联系人
*/
void seach_phonebook(struct phonebook *head)
{
char name[10];
struct phonebook *head1=head;

printf("请输入联系人姓名(汉语拼音)\n");

scanf("%s",name);

do
{
if (strcmp(head1->name,name)==0)
{
printf("姓名:%s\t电话号码:%f\tE_mail:%s\t信息:\t",head1->name,head1->number,head1->E_mail,head1->information);
break;
}

}while(1);
}
你的内存处理好像也有问题(我没给你修改),我给你修改的不止你提出的那点,别的地方也有不少,还有你的程序风格可以再加加油(现在也不错)!!在C语言中最好不要那么多的嵌套,可读性差!你的程序只能运行一次,想想怎么可以多运行.

回答2:

这个问题还让别人废脑子,这么简单,网上的资料很多啊。

回答3:

楼主,不好办啊~
楼上已经答了第一,二题,我就从第三题开始了~
#include
int fun(int x)
{
int a,b,c;
a=x%10;
b=((x-a)/10)/10;
c=((x-a)/10)-(b*10);
if(x=a*a*a+b*b*b+c*c*c)
return 1;
else
return 0;
}