c语言课程设计-旅馆管理系统 基于链表

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011469138/article/details/81194605

自己写的一个基础的管理系统,使用链表实现,能实现登入、建立链表、插入信息、查询信息、信息排序、信息删减、保存链表至本地和从本地数据中创建链表等功能
这里写图片描述
首先是链表的建立和节点插入,我是首先建立链表(第一次使用软件),之后的信息添加则通过添加节点实现

struct save *creat()//建立链表函数 
{   

    struct save *p1,*p2,*head;

    int n=0;

    head=NULL;

    p1=p2=(struct save *)malloc(sizeof(save));

    printf("************************************\n");

    printf("请输入入住信息:\n\n");

    printf("------------------------------------\n");   
    printf("请输入房号:\n\n");

    scanf("%d",&p1->num);
    printf("------------------------------------\n");

    printf("请输入性别:\n\n");

    scanf("%s",&p1->sex);
    printf("------------------------------------\n");
    printf("请输入姓名:\n\n");

    scanf("%s",&p1->name);
    printf("------------------------------------\n");
    printf("请输入身份证号码:\n\n");

    scanf("%s",&p1->id);
    printf("------------------------------------\n");
    printf("请输入联系电话:\n\n");

    scanf("%s",&p1->phone);
    printf("------------------------------------\n");
    printf("请输入入住时长:\n\n");

    scanf("%d",&p1->day);
    printf("------------------------------------\n");
    printf("************************************\n");
    head=p1;
    head->next=NULL;
    return head;
}
void add(struct save *head)//添加节点函数 
{
    struct save *p;
    struct save *p2;
    p2=(struct save*)malloc(sizeof(save));
    p=head;

    while(p->next!=NULL)
    {

        p=p->next;
    }
        p->next=p2;
    printf("************************************\n");
        printf("请输入入住信息:\n\n");
    printf("------------------------------------\n");
        printf("请输入房号:\n\n");

        scanf("%d",&p2->num);
    printf("------------------------------------\n");
        printf("请输入性别:\n\n");

        scanf("%s",&p2->sex);
    printf("------------------------------------\n");
        printf("请输入姓名:\n\n");

        scanf("%s",&p2->name);
    printf("------------------------------------\n");
        printf("请输入身份证号码:\n\n");

        scanf("%s",&p2->id);
    printf("------------------------------------\n");
        printf("请输入联系电话:\n\n");

        scanf("%s",&p2->phone);
    printf("------------------------------------\n");
        printf("请输入入住时长:\n\n");

        scanf("%d",&p2->day);
    printf("------------------------------------\n");
        p2->next=NULL;

}

接下来是查询模块,有按房号查询和名字查询两种,都是遍历链表比对关键字实现

void idsearch(struct save *head,int n)//房号查询函数 
{
    struct save *p;
    p=head;
    int ok=0;
    if(head!=NULL)
    {
        while(p!=NULL)
        {
            if(p->num==n)
            {   printf("*************************************\n");
                printf("%d房的住户姓名为%s\n",n,p->name);  
                printf("*************************************\n\n");
                ok=1;
                return;
            }
            else
                p=p->next;  
        }

    }
    if(ok==0)
        printf("该房号没有住户\n\n");

}
void namesearch(struct save *head,char nam[20])// 
{
    struct save *p;
    p=head;
    int ok=0;
    if(head!=NULL)
    {
        while(p!=NULL)
        {
            if(strcmp(p->name,nam)==0)
            {   printf("*************************************\n");
                printf("住户%s%d号房\n",p->name,p->num);   
                printf("*************************************\n\n");
                ok=1;
                return;
            }
            else
                p=p->next;  
        }

    }
    if(ok==0)
        printf("该住户不存在\n\n");

}

删除(退房模块)

struct save *exit(struct save *head,int num)
{
    struct save *p,*p2,*p3;
//  p=p2=p3=(struct save*)malloc(sizeof(save));
    p3=NULL;
    p=head;
    int ok=0;
    if(head!=NULL)
    {
        while(p!=NULL)
        {   if(p->num!=num) {p3=p;  p=p->next;}
            if(p->num==num)
            {
                char x;
                printf("------------------------------------\n");
                printf("%d房的住户姓名为%s\n",num,p->name);
                printf("------------------------------------\n");
                printf("该住户的房费为%d元\n",p->day*price);
                printf("------------------------------------\n");
                printf("是否确定退房?<y/n>\n");
                printf("------------------------------------\n");
                getchar();
                scanf("%c",&x);
                if(x=='y')
                {   
                    if(p==head)

                            head=head->next;


                    else
                    p3->next=p->next;
                    printf("退房成功!\n\n");


                }
                ok=1;
                return head;
            }

        }

    }
    if(ok==0)
        printf("该房号没有住户\n\n");

}

接下来还有比较难的文件操作部分
1.保存至本地

void print(struct save *head)//打印和保存函数 
{   
    sort(head);
    struct save *p;

    p=head;

    FILE *w =fopen("output.txt","w");
    if(w==NULL)
   {
       printf("打开文件失败!");
       return; 
   }
    if(head!=NULL)
    {
        while(p!=NULL)
        {
            printf("*************************************\n");
            printf("房号:\n");
                printf("%d\n",p->num);
                fprintf(w,"%d ",p->num);
                printf("------------------------------------\n");
            printf("住户性别\n");
                printf("%s\n",p->sex);
                fprintf(w,"%s ",p->sex);
                printf("------------------------------------\n");
            printf("住户姓名:\n");
                printf("%s\n",p->name);
                fprintf(w,"%s ",p->name);
                printf("------------------------------------\n");
            printf("住户身份证号码:\n");
                printf("%s\n",p->id);
                fprintf(w,"%s ",p->id);
                printf("------------------------------------\n");
            printf("住户电话号码:\n");
                printf("%s\n",p->phone);
                fprintf(w,"%s ",p->phone);
                printf("------------------------------------\n");
            printf("住户入住时长:\n");
                printf("%d\n",p->day);
                fprintf(w,"%d\n",p->day);
                printf("------------------------------------\n\n");
            p=p->next;

        }
    //  fprintf(w,"\n");
        fclose(w);
    }

    else
        printf("当前旅馆没有住户\n\n");

}

2.从本地数据建表

struct save *filecreat()//文件读取函数 
{   
    struct save *head; 
    head = NULL;
    int num1;
    char ch;
    char sex1[100];
    char name1[100];
    char id1[100];
    char phone1[100];
    int day1;
    struct save *p = NULL;
    struct save *q;
    FILE * r= fopen("output.txt","r");
    if(r==NULL)
    {
        printf("文件打开失败!");
        return NULL; 
    }
    head = p;
    while(fscanf(r,"%d",&num1) != EOF)
    {
        fscanf(r,"%s",sex1);
        fscanf(r,"%s",name1);
        fscanf(r,"%s",id1);
        fscanf(r,"%s",phone1);
        fscanf(r,"%d",&day1);
        q=(struct save*)malloc(sizeof(save));
        q->num=num1;
       strcpy(q->sex,sex1);
       strcpy(q->name,name1);   
       strcpy(q->id,id1);
       strcpy(q->phone,phone1);
       q->day=day1;
        q->next=NULL;
        if(p == NULL)
        {
            p = q;
            head = p;
            continue;
        }
        else{
            p->next = q;
        }
        //cout<<"debug"<<endl;
        p = q;
    }

    fclose(r);
    return head;
}

全部源代码我已经上传github,需要的同学可以参考
https://github.com/ZeroT900sk/hotel
注意其中的cpp源代码和两个txt文件都要下载,output.txt是储存本地信息,user.txt是保存密码

猜你喜欢

转载自blog.csdn.net/u011469138/article/details/81194605
今日推荐