| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2047 人关注过本帖
标题:求大神帮帮忙,这个程序哪儿有问题啊,为什么编译完运行后“录入信息时出现 ...
只看楼主 加入收藏
BY左边小亚
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2017-7-4
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:12 
求大神帮帮忙,这个程序哪儿有问题啊,为什么编译完运行后“录入信息时出现的是这种情况呢”,知道的麻烦帮帮忙吧,谢谢了!!
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
//#include <windows.h>

struct Room ;
struct Customer ;
typedef struct Room * Proom;
typedef struct Customer * Pcustomer;

struct Room  
{
    int RoomNumber;//房号
    int RoomType;//房型
    int Price;//单价(每床)
    int Number; //已住人数   
    Proom next;
};

struct Customer  
{
    char name[10],ID[19]; //顾客姓名,顾客身份证号
    char native[5];//籍贯
    char sex[5];//性别
    int  age[2];//年龄
    int  state;//房型
    int  room_number;//房号
    int  check_in_date;//入住日期
    int  departure_date ;//退房日期
    Pcustomer next;
};

Proom head_room = NULL;
Pcustomer head_customer = NULL;
Proom r_room = NULL;    //房间链表尾指针
Pcustomer r_customer = NULL;//顾客链表尾指针

Proom update_room_add(int room_num);

bool read_room()    //读取room文件来创建链表
{
    FILE *filep;
    if((filep = fopen("e://Room.txt","a+")) == NULL)
    {
        printf("文件打开失败!");
        getch();
        return 0;
    }
    Proom temp_node = NULL;
    temp_node = (Proom)malloc(sizeof(struct Room));
    temp_node->next = NULL;
    head_room = r_room = temp_node;
    while(true)
    {
        if(feof(filep)||fread(temp_node,sizeof(struct Room),1,filep)!=1)
        {
            free(temp_node);
            if (temp_node == head_room)
                head_room = r_room = NULL;
            else
                r_room->next = NULL;
            break;
        }
        r_room->next = temp_node;
        r_room = r_room->next;
        temp_node = (Proom)malloc(sizeof(struct Room));
        temp_node->next = NULL;
    }
    return 1;
}

bool write_room() //保存room链表(即房间信息)到room文件
{
    FILE *filep;
    if((filep = fopen("e://Room.txt","w+")) == NULL)
    {
        printf("文件打开失败!");
        getch();
        return 0;
    }
    Proom temp_node = head_room;
    while( temp_node != NULL )
    {
        fwrite(temp_node,sizeof(struct Room),1,filep);
        temp_node = temp_node->next;
    }
    fclose(filep);
    return 1;
}

bool read_customer()//读取customer文件来创建customer链表
{
    FILE *fileq;
    if((fileq = fopen("e://Customer.txt","a+")) == NULL)
    {
        printf("文件打开失败!"); 
        getch();
        return 0;
    }
    Pcustomer temp_node = (Pcustomer)malloc(sizeof(struct Customer));
    head_customer = r_customer = temp_node;
    while(true)
    {
        if(feof(fileq)||fread(temp_node,sizeof(struct Customer),1,fileq)!=1)
        {
            free(temp_node);
            if (temp_node == head_customer)
                head_customer = r_customer = NULL;
            else
                r_customer->next = NULL;
            break;
        }    
        r_customer->next = temp_node;
        r_customer = r_customer->next;
        temp_node = (Pcustomer)malloc(sizeof(struct Customer));
        temp_node->next = NULL;
    }    
    fclose(fileq);
    return 1;
}

bool write_customer()//保存customer链表信息到customer文件
{
    FILE *filep;
    if((filep = fopen("e://Customer.txt","w+")) == NULL)
    {
        printf("文件打开失败!");
        getch();
        return 0;
    }
    Pcustomer temp_node = head_customer;
    while( temp_node != NULL )
    {
        fwrite(temp_node,sizeof(struct Customer),1,filep);
        temp_node = temp_node->next;
    }
    fclose(filep);
    return 1;
}

int insert_post_link_room () //插入房间
{
    Proom temp_node = (Proom)malloc(sizeof(struct Room));
    if (temp_node == NULL)
    {
        printf( "out of space!!!\n");
        return 0;
    }

    printf("请输入房间号\n");
    scanf("%d", &temp_node->RoomNumber);
    printf("请输入房型:1<标准间>,2<双人间>,3<总统房>\n");
    scanf("%d", &temp_node->RoomType);
    printf("请输入价格\n");
    scanf("%d", &temp_node->Price);
    printf("请输入已住人数\n");
    scanf("%d", &temp_node->Number);

     temp_node->next=NULL;
    if (head_room == NULL)
    {
        head_room = r_room = temp_node;
    }
    else
    {
        r_room->next= temp_node;
        r_room = temp_node;
    }
    return 0;
}

int insert_post_link_customer ()//顾客信息插入
{
    Pcustomer temp_node = (Pcustomer)malloc(sizeof(struct Customer));
    if (temp_node == NULL)
    {
        printf( "out of space!!!\n");
        return 0;
    }
    printf("请输入姓名\n");
    scanf("%s", &temp_node->name);
    printf("请输入籍贯\n");
    scanf("%s", &temp_node->native);
    printf("请输入年龄\n");
    scanf("%d", &temp_node->age);
    printf("请输入身份证号码\n");
    scanf("%s", &temp_node->ID);
    printf("请输入性别\n");
    scanf("%s", &temp_node->sex);
    printf("请输入房号\n");
    scanf("%d", &temp_node->room_number);
    printf("请选择房型:1<标准间>,2<双人间>,3<总统房>\n");
    scanf("%d", &temp_node->state);
    printf("请输入入住日期(输入八位数,如20170703)\n");
    scanf("%d", &temp_node->check_in_date);
    printf("请输入退房日期(输入八位数,如20170704)\n");
    scanf("%d", &temp_node->departure_date);

    temp_node->next =NULL;

    if (head_customer == NULL)
    {
        head_customer = r_customer = temp_node;
    }
    else
    {
        r_customer->next = temp_node;
        r_customer = temp_node;
    }

    update_room_add(r_customer->room_number);
    return 1;
    
}

int is_null_list_room(Proom list)
{
    return (list->next == NULL);
}

int is_null_list_customer(Pcustomer list)
{
    return (list->next == NULL);
}

Proom  check_room_number(int room_number )//按房号查询出保存房间信息节点
{
    Proom p = head_room;
    while ( p != NULL  &&  p->RoomNumber != room_number)
        p = p->next;
    return p;
}

Pcustomer  check_customer_name(char name[10] )//按房客姓名查询顾客信息节点
{
    Pcustomer q = head_customer;
        while (q!=NULL&&0!=strcmp(q->name,name))
        q=q->next;
    return q;
}

void printf_room( Proom room)//打印单个房间信息
{
    printf(    "%d    %d    %d    %d\n",
        room->RoomNumber,room->RoomType,room->Price,room->Number);
}

void printf_all_room()//打印房间信息
{
    printf( "房号    房型    单价    已住人数\n");
    Proom room = head_room;
    while ( room != NULL )
    {
        printf_room(room);
        room = room->next;
    }
}

void printf_customer( Pcustomer customer)//打印单个顾客信息
{    
    printf(    "%s    %s  %s    %d    %d    %s       %d       %d      %d\n",
        customer->name,customer->native,customer->sex,customer->age,customer->state,customer->ID,
        customer->room_number,customer->check_in_date,customer->departure_date);
}

void printf_all_customer()//打印全部顾客信息
{
    printf( "姓名    性别   籍贯     年龄    房型    身份证号             房号       入住日期       离开日期\n");
    Pcustomer customer = head_customer;
    while ( customer != NULL )
    {
        printf_customer(customer);
        customer = customer->next;
    }
}

int check_empty_room()                            //查询空房
{
    Proom p = head_room;
    int num_empty_room = 0;
    printf( "房号    房型    价格    已住人数    \n");
    while ( p != NULL )
    {    
        if ( p->Number == 0 )
        {
            printf_room(p);
            num_empty_room++;
        }
        p = p->next;
    }
    return num_empty_room;    
}

int sum_empty_room()//空房间总数
{
    printf("共有空房间%d\n",check_empty_room());
    return 0;
}


Proom update_room_less(int room_num) //更新房间信息(离店人数减少)
{
    Proom p = check_room_number(room_num);
    p->Number = p->Number - 1;
    return 0;
}

Proom update_room_add(int room_num) //更新房间信息(进店人数增加)
{
    Proom p = check_room_number(room_num);
    p->Number = p->Number + 1;
    return 0;
}

int  delete_customer(char name[10])//退房
{
    Pcustomer p,q;
    q = p = head_customer;
    if (p == NULL)
    {
        return 0;
    }
    if (strcmp(p->name,name) == 0)
    {
        int room_num = p->room_number;
        update_room_less(room_num);
        if (r_customer == head_customer)
        {
            printf("退房成功!\n");
            head_customer = r_customer = NULL;
            return 1;
        }
        head_customer = head_customer->next;
        free(p);
        printf("退房成功!\n");
        return 1;
    }
    p = p->next;
    while (p != NULL&& 0 != strcmp(p->name,name))
    {
        p = p->next;
        q = q->next;
    }
    if(p == NULL)
    {
        printf("此人不在此店!\n");
        return 0;
    }
    else
    {
        int room_num = p->room_number;
        update_room_less(room_num);
        if (p == r_customer)
        {
            r_customer = q;
        }
        q->next = p->next;
        free(p);
        printf("退房成功!\n");
        return 1;    
    }
}

Pcustomer update_customer(char name[10])//住客换房
{
    delete_customer(name);
    insert_post_link_customer();
    return NULL;
}

int count_day( int, int, int, int ); //计算两个日期中间的天数
int leap( int );//平闰年计算
int sum_data( Pcustomer customer ) //计算顾客所住天数
{ 
    int year, day, day1, day2; 
    int s_year, s_month, s_day, e_year, e_month, e_day;//s为起始的日期,e为终止日期
    
    e_day = customer->departure_date%100;
    e_month = customer->departure_date/100%100;
    e_year = customer->departure_date/10000;
    
    s_day = customer->check_in_date%100;
    s_month = customer->check_in_date/100%100;
    s_year = customer->check_in_date/10000;
     
    if ( s_year < e_year ) //不同年的时间判断
    { 
        day = count_day ( s_year, s_month, s_day , 0 ); 
        for (year=s_year+1;year<e_year; year++ )//年份累加
        
            if (leap(year))//闰年
                day += 366; 
            else   //平年
                day += 365; 
    
            day += count_day (e_year, e_month, e_day, 1); 
    } 
    
    else if ( s_year == e_year ) //同一年的时间判断
    { 
        day1 = count_day ( s_year, s_month, s_day, 1 ); 
        day2 = count_day ( e_year, e_month, e_day, 1 ); 
        day = day2-day1;
    } 
    else
        day = 1; 
    return day; 
} 
//计算天数的核心 
int count_day ( int year, int month, int day, int flag ) 
{ 
    static int day_tab[2][12] = {{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
    int p, i, s; 
    if (leap(year))                                                                                //闰年为1选择后面的用
        p = 1; 
    else 
        p = 0; 
    if (flag) 
    { 
        s = day;
        for ( i = 1; i < month; i++ ) 
            s += day_tab[p][i-1];
    } 
    else
    {
        s = day_tab[p][month] - day; 
        for ( i = month + 1; i <= 12; i++ ) 
            s += day_tab[p][i-1]; 
    } 
    return ( s );
} 
//平闰年的判断 
int leap(int year) 
{ 
    if ( year%4 == 0&&year%100!=0||year % 400 ==0) 
        return 1; 
    else
        return 0; 
}

void sum_customer_price( Pcustomer customer)//顾客应付总价格
{
    int num_price;
    Proom p = check_room_number(customer->room_number );
    if (NULL != p)
    {
        num_price = sum_data (customer) * ( p->Price );
        printf( "房客应付款%d元\n",num_price );
    }
}

int total_customer_numer(Proom room)//顾客总人数
{
    Proom p;
    p = head_room;
    int num = 0;
    while ( p != NULL )
    {
        num=num+p->Number;
        p=p->next;
    }
    return num;
}

int total_price_day( Proom head_room)//当天总收入
{
    Proom p;
    p = head_room;
    int num_price = 0;
    while ( p != NULL )
    {
        if ( p->RoomType == 3 )
        {
            num_price=num_price +(p->Price) * (p->Number);
            p=p->next;
        }
        else 
        {
            num_price=num_price +p->Price;
            p=p->next;
        }
    }
    return num_price;
}

//按单价给客房排序(插入排序)
void sort_insert(Proom room)
{
    Proom pre,now,p,q;
    pre = head_room->next;
    if ( pre == NULL )    return;
    now = pre->next;
    if ( now == NULL )    return;
    while ( now != NULL )
    {
        q = head_room;
        p = head_room->next;
        while (p != now && p->Price <= now->Price )
        {
            q = p;
            p = p->next;
        }
        if( p == now )
        {
            pre = pre->next;
            now = pre->next;
            continue;
        }
        
        pre->next = now->next;
        q->next = now;
        now->next = p;
        now = pre->next;
    }
}

//按房号给住客信息排序(冒泡排序)
void sort_bubble()
{
    Pcustomer p, q, pre;
    pre = head_customer;
    p = head_customer->next;
    if ( p = NULL )    return;
    q = p->next;
    if ( q = NULL )    return;
    int noswap;
    int num_customer = total_customer_numer(head_room);
    for ( int i = 0; i < num_customer-1; i++ )
    {
        noswap = 1;
        for ( int j = 0; j < num_customer-i-1; j++ )
            //while ( q != NULL )
            if ( p->room_number > q->room_number )
            {
                pre->next = q;
                p->next = q->next;
                q->next = p;
                q = p->next;
                noswap = 0;
            }
            if ( noswap ) break;
    }
}

void update_room_type(int num)// 修改房间信息(修改房型)
{
    int num_type;
    Proom p;
    p = check_room_number(num);
    if (p == NULL)
    {
        printf("查无此项\n");
        return ;
    }
    printf("请输入修改后的房型\n");
    scanf("%d", &num_type);
    p->RoomType = num_type;
    printf("修改成功!\n");
    write_room();
}

//程序入口和主页面
void main()
{
    if (read_room() == 0)
        return ;
    if (read_customer() == 0)
        return ;
    int n[5] = {0};
    char name[10];
    do{
        printf("                         ************欢迎光临*************\n");
        printf("                                 *  1.客房管理            \n");
        printf("                                 *  2.住客信息管理        \n");
        printf("                                 *  3.查询统计            \n");
        printf("                                 *  0.退出系统            \n");
        printf("                         *********************************\n");
        printf("                                     请选择:");
        scanf("%d",&n[0]);
        switch(n[0])
        {
        case 1://客房信息管理界面
            {    
                do 
                {
                    printf("                         --------------------------\n");
                    printf("                              *  1.录入客房信息     \n");
                    printf("                              *  2.修改客房信息     \n");
                    printf("                              *  0.返回上一级       \n");
                    printf("                         --------------------------\n");
                    printf("                                   请选择:");
                    scanf("%d",&n[1]);
                    switch (n[1])
                    {
                    case 1 :
                        insert_post_link_room();
                        write_room();
                        break;
                    case 2 :
                        printf("请输入房号:");
                        scanf("%d", &n[4]);
                        update_room_type(n[4]);
                        break;
                    default:
                        break;
                    } 
                }while(n[1] != 0);
                
            }
            break;
        case 2://宾客登记
            {
                do{
                    printf("                         --------------------------\n");
                    printf("                              *  1.查询空房            \n");
                    printf("                              *  2.住客登记            \n");
                    printf("                              *  3.更换房间            \n");
                    printf("                              *  4.住客退房            \n");
                    printf("                              *  0.返回上一级           \n");
                    printf("                         -------------------------\n");
                    printf("                              选择:");
                    scanf("%d",&n[2]);
                    
                    switch(n[2])
                        
                    {
                    case 1:
                        {
                            printf("空房有\n:");
                            int num = check_empty_room();
                            printf("空房总数为%d\n",num);
                        }
                        break;
                    case 2:
                        if (insert_post_link_customer() == 0)
                            break;
                        sum_customer_price(r_customer);
                        write_customer();
                        break;
                    case 3:
                        printf("请输入住客姓名");
                        scanf("%s", &name);
                        update_customer(name);
                        write_room();
                        write_customer();
                        break;
                    case 4:
                        printf("请输入住客姓名");
                        scanf("%s", &name);
                        delete_customer(name);
                        write_room();
                        write_customer();
                        break;
                    default:
                        break;
                    }
                }while(n[2] != 0);                
            }
            
            break;
            
        case 3:
            {
                do{
                    printf("                         --------------------------\n");
                    printf("                              *  1.查询空房            \n");
                    printf("                              *  2.房间信息            \n");
                    printf("                              *  3.顾客信息            \n");
                    printf("                              *  4.查询顾客            \n");
                    printf("                              *  5.查询房间            \n");
                    printf("                              *  6.顾客人数            \n");
                    printf("                              *  7.空房总数            \n");
                    printf("                              *  8.今日收入            \n");
                    printf("                              *  0.返回上一级          \n");
                    printf("                         --------------------------\n");
                    printf("                                  请选择:");
                    scanf("%d",&n[3]);
                    
                    switch(n[3])
                    {
                    case 1:
                        {
                            int num;
                            printf("空房有:%d\n",num);
                            num = check_empty_room();
                            printf("空房总数为%d\n",num);
                        }
                        break;
                    case 2:
                        printf_all_room();                                    
                        break;
                    case 3:
                        printf_all_customer();
                        break;
                    case 4:
                        {
                            printf("请输入住客姓名\n");
                            scanf("%s", &name);
                            Pcustomer p = check_customer_name(name);
                            if (p == NULL)
                            {
                                printf("查无此人\n");
                            }
                            else
                            {
                                printf("您要查找的人是:\n");
                                printf( "姓名    性别   籍贯     年龄    房型    身份证号                                   房号           入住日期         离开日期\n");
                                printf_customer( p );
                            }
                        }
                        break;
                    case 5:
                        printf("请输入房号\n");
                        scanf("%d", &n[4]);
                        printf("您要查找的房间信息为:\n");
                        printf( "房号    房型    单价    已住人数\n");
                        printf_room( check_room_number(n[4]));
                        break;
                    case 6:
                        printf("顾客总人数为%d\n", total_customer_numer(head_room));
                        break;
                    case 7:
                        sum_empty_room();
                        break;
                    case 8:
                        printf("今日收入总数为:%d\n", total_price_day(head_room));
                        break;
                    default:
                        break;
                    }
                    
                }while(n[3] != 0);
            }        
            break;    
        }
    }while(n[0] != 0);
}

[local]1[/local]


课程设计.png (25.85 KB)
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: int next NULL printf return 
2017-07-05 09:44
BY左边小亚
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2017-7-4
收藏
得分:0 
在线等,麻烦知道的人给指导一下,好急啊,谢谢!!
2017-07-05 09:46
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:4 
大概是指针问题  出现了内存非法访问

DO IT YOURSELF !
2017-07-05 10:02
BY左边小亚
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2017-7-4
收藏
得分:0 
回复 3楼 wp231957
哦,您能给我指导改下吗?谢谢您了
2017-07-05 10:08
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册

DO IT YOURSELF !
2017-07-05 10:22
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
我测试两个模块 没啥毛病啊

DO IT YOURSELF !
2017-07-05 10:22
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
收藏
得分:4 
你的文件Room.txt是空的还是?
read_room里如果文件是空的,就把有用的指针都置空了,导致后面用到了空指针
if (feof(filep) || fread(temp_node, sizeof(struct Room), 1, filep) != 1)
        {
            free(temp_node);
            if (temp_node == head_room)
                head_room = r_room = NULL;
            else
                r_room->next = NULL;
            break;
        }
2017-07-05 10:23
BY左边小亚
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2017-7-4
收藏
得分:0 
回复 6楼 wp231957
有呢,
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册
2017-07-05 10:26
wmf2014
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:216
帖 子:2039
专家分:11273
注 册:2014-12-6
收藏
得分:4 
你要先录入客房信息,再录入顾客信息就可以了。否则登记顾客后没房可住,head_room仍然是NULL,当然出现错误。

能编个毛线衣吗?
2017-07-05 10:26
BY左边小亚
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2017-7-4
收藏
得分:0 
回复 7楼 grmmylbs
空的呢,而且好像每次都录入存储不上信息。
2017-07-05 10:27
快速回复:求大神帮帮忙,这个程序哪儿有问题啊,为什么编译完运行后“录入信息时 ...
数据加载中...
 
   



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

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