| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 413 人关注过本帖
标题:下面是一个C语言程序,但是有点错误,请大侠指教。
取消只看楼主 加入收藏
qiuyuting
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-6-15
结帖率:0
收藏
已结贴  问题点数:10 回复次数:4 
下面是一个C语言程序,但是有点错误,请大侠指教。
#define NULL 0
#define LEN sizeof(struct customer)
#include "string.h"
struct customer
{
    int num;
    char name[20];
    long date;
    struct customer *next;
}cus;

struct customer *creat(void)
{
    struct customer *head,*p1,*p2;
    int n=0;
    p1=(struct customer *)malloc(LEN);
    p2=(struct customer *)malloc(LEN);
    printf("qing shu ru ke hu de xin xi:\n");
    printf("\nfang jian:");
    scanf("%d",&p1->num);
    printf("\n");
    printf("xing ming:");
    scanf("%s",p1->name);
    printf("\n");
    printf("ru zhu shi jian:");
    scanf("%ld",&p1->date);
    printf("\n");
    head=NULL;
        while(p1->num!=0)
            {
               n++;
            if(n==1)
            head=p1;
            else
            p2->next=p1;
            p2=p1;
            p1=(struct customer *)malloc(LEN);
            printf("qing shu ru ke hu de xin xi:\n");
            printf("\nfang jian:");
            scanf("%d",&p1->num);
            printf("\n");
            printf("xing ming:");
            scanf("%s",p1->name);
            printf("\n");
            printf("ru zhu shi jian:");
            scanf("%ld",&p1->date);
            printf("\n");
            }
      p2->next=NULL;
      return(head);
    }

struct customer *insert(struct customer *head,struct customer *consumer)
{
       struct customer *p1,*p2;
        p1=head;
        p2=consumer;
        if(p1->next!=NULL)
        p1=p1->next;
        if(p1->next==NULL)
          {
            p1->next=p2;
            p2->next=NULL;
          }
        return(head);
}


struct customer *del(struct customer *head,int num)
{
        struct customer *p1,*p2;
        if(head==NULL)
        {
            printf("mei you ke hu xin xi\n");
            return(head);
            }
       p1=head;
        while(num!=p1->num&&p1->next!=NULL)
        {
                p2=p1;p1=p1->next;
        }
        if(num==p1->num)
        {
            if(p1==head)
                head=p1->next;
            else
                p2->next=p1->next;
            printf("tui fang xin xi:\n");
            printf("kefang:%d\n",&p1->num);
            printf("xing ming:%s\n",p1->name);
            printf("ru zhu shi jian:%ld\n",&p1->date);
         }
        else
        printf("mei you gai ke hu de xin xi\n");
        return(head);
}

 
struct customer *search(struct customer *head,char c_name[])
{
     struct customer *p;
    p=head;
    while(strcmp(p->name,c_name)&&p->next!=NULL)
    p=p->next;
    if(p->name==c_name)
        {
            printf("gai ke hu de xin xi wei:\n");
            printf("fang jian:%d\nxing ming:%S\nru zhu shi jian:%ld",p->num,p->name,p->date);
        }
    if(p->name!=c_name&&p->next==NULL)
    printf("mei you gai ke hu xin xi!\n");
}


void print(struct customer *head)
{
    struct customer *p;
    printf("\nxian you ke hu xin xi wei:\n");
    p=head;
    if(head!=NULL)
    do
     {
        printf("ke fang:%d\n",p->num);
        printf("xing ming:%s\n",p->name);
        printf("ru zhu shi jian:%ld\n",p->date);
        p=p->next;
       }while(p!=NULL);
}



main()
{
        struct customer *head,*p;
        int m,number;
        char c_name[20];
        printf("\n");
        printf(" *************************************************\n");
        printf(" huan ying jin ru bin guan ke fang guan li xi tong\n");
        printf(" *************************************************\n");
        printf(" *************************************************\n\n");
        printf(" 1:jian ku\n");
        printf(" 2:deng ji\n");
        printf(" 3:tui fang\n");
        printf(" 4:cha xun\n");
        printf(" *************************************************\n\n");
        printf("zhi xing chao zuo:\n");
        scanf("%d",&m);
        if(m==1)
            head=creat();
        else
             if(m==2)
                    {            
                        printf("qing shu ru yao deng ji de ke hu xin xi:");
                        p=(struct customer *)malloc(LEN);
                        scanf("%d %s %ld",&p->num,p->name,&p->date);
                        head=insert(head,p);
                        print(head);
                     }
                else
                    if(m==3)
                     {
                        printf("qing shu ru yao tui fang ke hu de fang jian hao:");
                        scanf("%d",&number);
                        head=del(head,number);
                        print(head);
                     }

                    else
                            if(m==4)
                            {
                                printf("qing shu ru yao cha xun ke hu de xing ming:");
                                scanf("%s",c_name);
                                search(head,c_name);
                                }
                            else
                                    printf("zhi xing chao zuo cuo wu!");
   }

宾馆客房管理软件
软件目标:
(1)某宾馆有301、302、303、304、305五个标准间,每个标准间可住2人;
(2)链表存储结构:姓名、性别、房号、后续指针,按房间号有序;
(3)能实现入住(注意性别)和退房,能按给定姓名、房号查询;

问题:
在开始的结构体
struct customer
{
    int num;
    char name[20];
    long date;
    struct customer *next;
}cus;
中加入性别,安排入住的时区分性别,而且这个代码中感觉房号的设计有点问题,如果运行是从房间1开始输入房客的信息而不是从301开始,
这个程序每个房间只能住一个人,希望能改成两个人。我只知道性别区分分别设个参数,房间区分用循环。有没有人住判断一下,但是感觉这个代码执行的时候是个死循环
帮忙改一下,谢谢。
搜索更多相关主题的帖子: C语言 xing jian long qing 
2011-06-15 13:00
qiuyuting
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-6-15
收藏
得分:0 
现在已经把
struct customer *creat(void)  
struct customer *insert(struct customer *head,struct customer *consumer)  
struct customer *del(struct customer *head,int num)  
搞好了,但是感觉
struct customer *insert(struct customer *head,struct customer *consumer)  
{  
struct customer *p1,*p2;  
p1=head;  
p2=consumer;  
if(p1->next!=NULL)  
p1=p1->next;  
if(p1->next==NULL)  
{  
p1->next=p2;  
p2->next=NULL;  
}  
return(head);  
}  
这个insert函数不对,指教一下
2011-06-15 15:36
qiuyuting
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-6-15
收藏
得分:0 
以下是引用voidx在2011-6-15 15:46:48的发言:

if(p1->next!=NULL)   
    p1=p1->next;
 
if 改成 while。
 
另外,楼主最好在函数里检查一下参数 head 是否为 NULL
if(p1->next==NULL)  
{  
p1->next=p2;  
p2->next=NULL;  
}  
这个中的if要不要也改为while?谢谢
2011-06-15 15:53
qiuyuting
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-6-15
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册

为什么敲过回车之后是这样,而不是输入性别,求解释。谢谢
2011-06-15 16:03
qiuyuting
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-6-15
收藏
得分:0 
回复 7楼 voidx
这在尝试,学C有一个月了,但是这次布置的比较急,还在改,链表学的不是太好。
2011-06-15 16:18
快速回复:下面是一个C语言程序,但是有点错误,请大侠指教。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029659 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved