双链表关于酒店信息的小课设作业

输入10家酒店的信息,每家酒店含有成员名为“序号、店名、地址、电话、营业天数、每天营业额、营业总额、营业成本、利润、名次”,分别编写六个函数求:

(1)输入一家酒店的序号,查询该家酒店的信息并输出,若不存在显示没找到。

(2)输入一家新酒店的信息,按序号顺序将该家酒店的信息插入后输出。

(3)输入一个已存在的酒店信息,删除该家酒店的信息后输出。

(4)求每家酒店的营业总额(营业总额=营业天数*每天营业额)和利润(利润=营业总额-营业成本)并输出;

(5)求所有酒店的营业天数、营业总额、营业成本、利润的平均值并输出;

(6)按利润大小排出名次后输出其信息。

#include<stdio.h>
#include<malloc.h> 

#define _CRT_SECURE_NO_WARNINGS

struct hotel {
    int num;
    char name[20];
    char address[20];
    char tel[20];
    int days;
    float daily_turnover;
    float total_turnover;
    float cost;
    float profit;
    int position;
    struct hotel* prior, * next;
};

struct hotel* creat() {
    struct hotel* s, * r;
    struct hotel* L;
    L = (struct hotel*)malloc(sizeof(struct hotel));//创建头结点
    r = L;
    FILE* fp = fopen("C:\\Users\\Administrator\\Desktop\\数据结构课设\\a.txt", "r");
    int i = 0;
    if (fp == NULL)
    {
        printf("can not open file!\n");

    }
    while (!feof(fp))
    {
        s = (struct hotel*)malloc(sizeof(struct hotel));//创建头结点
        fscanf(fp, "%d %s %s %s %d %f %f %f %f %d",
            &s->num, &s->name, &s->address, &s->tel, &s->days, &s->daily_turnover, &s->total_turnover, &s->cost, &s->profit, &s->position);
        /*    printf("%d %s %s %s %d %f %f %f %f %d\n",
                s->num, s->name, s->address, s->tel, s->days, s->daily_turnover, s->total_turnover, s->cost, s->profit, s->position);
            */r->next = s;
            s->prior = r;
            r = s;
    }
    r->next = NULL;
    return L->next;
}

void print(struct hotel* head) {
    struct hotel* s = head;
    printf("┌───────┬────────────────┬────────────────┬────────────────┬─────┬───────────────┬─────────────────┬───────────────┬───────────────┬──────────────┐\n");
    printf("│序号\t│   名      称   │   地      址   │   电      话   │ 天数│  日营业额     │  总营业额       │ \t成本 \t   │  利润\t   │\t排名\t  │\n");

    while (s) {
        printf("├───────┼────────────────┼────────────────┼────────────────┼─────┼───────────────┼─────────────────┼───────────────┼───────────────┼──────────────┤\n");
        printf("│  %d\t│     %s   │   %s    │    %s           │  %d │  %f    │ %f \t   │%f\t   │ %f\t   │\t %d\t  │\n",
            s->num, s->name, s->address, s->tel, s->days, s->daily_turnover, s->total_turnover, s->cost, s->profit, s->position);
        s = s->next;
    }
    printf("└───────┴────────────────┴────────────────┴────────────────┴─────┴───────────────┴─────────────────┴───────────────┴───────────────┴──────────────┘\n");

    printf("\n");
}

void search(struct hotel* head, int num) {
    struct hotel* p = head;
    while (p && p->num != num)
    {
        p = p->next;
    }
    if (p == NULL) {
        printf("没找到指定学号的学生!\n"); //这边为什么不能写p->num == num
    }
    else {
        printf("%d %s %s %s %d %f %f %f %f %d\n",
            p->num, p->name, p->address, p->tel, p->days, p->daily_turnover, p->total_turnover, p->cost, p->profit, p->position);

    }
}

struct hotel save(struct hotel* head) {
    struct hotel* p = head;
    FILE* fp;
    fp = fopen("C:\\Users\\Administrator\\Desktop\\数据结构课设\\b.txt", "w+");
    while (p)
    {
        fprintf(fp, "%d %s %s %s %d %f %f %f %f %d\n",
            p->num, p->name, p->address, p->tel, p->days, p->daily_turnover, p->total_turnover, p->cost, p->profit, p->position);
        p = p->next;
    }
}

struct hotel* insert(struct hotel* head, struct hotel* h) {
    struct hotel* p = head;
    struct hotel* q;
    while (p && h->num != p->num)//判断序号是否重复
    {

        if (h->num == p->num)

        {
            printf("序号有误,请重新输入\n");
            break;
        }
        p = p->next;
    }
    if (p == NULL) {//序号如果不重复
        p = head;
        while (p->next && p->next->num < h->num)//链表后没东西
        {
            p = p->next;

            /*    if (p->next->num == NULL) {
                    p->next = h;
                    h->prior = p;
                    h->next = NULL;
                    break;*/

        }//此时p是最后一个节点
        q = p->next;//要判断是不是最后一个,如果是,最后一个null无前驱


        if (q)
        {
            q->prior = h;
            h->prior = p;
            h->next = q;
            p->next = h;

            return head;
        }
        else {
            p->next = h;
            h->prior = p;
            h->next = NULL;
            return head;
        }

    }
}

struct hotel* delet(struct hotel* head, int num) {
    struct hotel* p = head;
    struct hotel* q;
    while (p->num != num && p) {
        p = p->next;
    }
    if (p == NULL) {
        printf("不存在,请重新输入");
    }
    else {
        q = p->prior;
        q->next = p->next;
        if (p->next) {//判断是不是最后一个,否则没有后一个的前驱
            p->next->prior = q;
        }
        return head;
    }
}

struct hotel* sum(struct hotel* head) {
    struct hotel* p = head;
    while (p) {
        p->total_turnover = p->days * p->daily_turnover;
        p->profit = p->total_turnover - p->cost;
        p = p->next;
    }
    return head;
}

int average(struct hotel* head) {
    int days = 0;
    float turnover = 0, cost = 0, profit = 0;
    int len = 0;
    struct hotel* p = head;
    while (p) {
        days += p->days;
        turnover = turnover + p->total_turnover;
        cost = cost + p->cost;
        profit = profit + p->profit;
        len++;
        p = p->next;
    }
    days /= len;
    turnover /= len;
    cost /= len;
    profit /= len;
    printf("\n所有酒店的营业天数的平均值%d、营业总额的平均值%f、营业成本的平均值%f、利润的平均值的平均值%f\n", days, turnover, cost, profit);
}

struct hotel* sort(struct hotel* head) {
    struct hotel* h = (struct hotel*)malloc(sizeof(struct hotel));
    h->next = NULL;
    struct hotel* cur = head;
    while (cur)//当原链表还有数据时
    {
        struct hotel* pre = h;
        struct hotel* next = cur->next;

        while (pre->next && cur->profit >= pre->next->profit) pre = pre->next;
        cur->next = pre->next;

        if (cur->next != NULL) {
            cur->next->prior = cur;//假如cur后面没有结点了
        }


        pre->next = cur;
        cur->prior = pre;
        cur = next;
    }
    struct hotel* p = head;
    int i = 1;
    while (p) {
        p->position = i;
        i++;
        p = p->next;
    }
    return h->next;
}

int main() {
    struct hotel* head;
    struct hotel* h;
    int num;
    char c;
    head = creat();
    print(head);
    do
    {
        printf("                      ┌────────────────────────────────────────────────────────────────┐\n");
        printf("                    │  1.输入一家酒店的序号,查询该家酒店的信息                      │    \n");
        printf("                 │────────────────────────────────────────────────────────────────┤\n");
        printf("                       │  2.输入一家新酒店的信息,酒店的信息插入后输出。          │\n");
        printf("                  │────────────────────────────────────────────────────────────────┤\n");
        printf("                       │  3.输入一个酒店信息,删除该家酒店的信息后输出          │\n");
        printf("                 │────────────────────────────────────────────────────────────────┤\n");
        printf("                      │  4.求每家酒店的营业总额和利润;                     │\n");
        printf("                  │────────────────────────────────────────────────────────────────┤\n");
        printf("                       │  5.求所有酒店的营业天数、营业总额、营业成本、利润的平均值      │\n");
        printf("                  │────────────────────────────────────────────────────────────────┤\n");

        printf("                       │  6.按利润大小排出名次后输出其信息                  │\n");
        printf("                  │────────────────────────────────────────────────────────────────┤\n");
        printf("                      │  7.退出系统                              │\n");
        printf("                 └────────────────────────────────────────────────────────────────┘\n");

        printf("                   请输入功能选项(1..7)                                                                                    ");

        scanf("%s", &c);
        switch (c)
        {
        case '1':
            printf("输入一家酒店的序号,查询该家酒店的信息并输出,若不存在显示没找到。\n");
            int a;
            scanf("%d", &a);
            search(head, a);
            break;

        case '2':
            h = (struct hotel*)malloc(sizeof(struct hotel));
            printf("\n请输入插入酒店的信息:");
            scanf("%d %s %s %s %d %f %f %f %f %d",
                &h->num, &h->name, &h->address, &h->tel, &h->days, &h->daily_turnover, &h->total_turnover, &h->cost, &h->profit, &h->position);
            head = insert(head, h);
            print(head);
            save(head);
            printf("\n");
            break;

        case '3':
            printf("\n输入一个已存在的酒店信息,删除该家酒店的信息后输出");
            scanf("%d", &num);
            head = delet(head, num);
            save(head);
            print(head);
            break;

        case '4':
            printf("\n求每家酒店的营业总额\n");
            head = sum(head);
            print(head);
            printf("\n");
            break;

        case '5':
            average(head);
            printf("\n");
            break;

        case '6':
            sort(head);
            print(head);
            break;

        case '7':exit(0);
        }

    } while (c != '7');

    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值