zoukankan      html  css  js  c++  java
  • BJFU-216-基于链式存储结构的图书信息表的修改

    #include<stdio.h>
    #include<stdlib.h>
    #define MAX 100
    
    typedef struct Book{
        double no;
        char name[MAX];
        double price;
        struct Book * next;
    }Book,*BList;
    //创建列表
    void CreatList(BList &B)
    {
        //头插法创建单链表
        B = (BList)malloc(sizeof(Book));
        B->next = NULL;
        BList rear = B;
        while(1)
        {
           BList p = (BList)malloc(sizeof(Book));
           scanf("%lf",&p->no);
           scanf("%s",p->name);
           scanf("%lf",&p->price);
           if(p->no==0&&p->name[0]=='0'&&p->price==0) break;
           rear->next = p;
           p->next = NULL;
           rear = p;
    
        }
    }
    //遍历
    void traverse(BList B)
    {
        BList p = B->next;
        while(p)
        {
            printf("%.0f ",p->no);
            printf("%s ",p->name);
            printf("%.2f",p->price);
            printf("
    ");
            p = p->next;
        }
    }
    //获得平均值并返回其值
    double getAvar(BList B)
    {
        BList p = B->next;
        double sum=0.0;
        int i = 0;
        while(p)
        {
            sum+=p->price;
            p = p->next;
            i++;
        }
    
        return sum/i;
    }
    //按题意修改价格
    void alter(BList &B)
    {
        BList p = B->next;
        while(p)
        {
            if(p->price<getAvar(B)) p->price = p->price*(1+0.2);
            else p->price = p->price*(1+0.1);
            p = p->next;
        }
    }
    
    
    int main()
    {
        BList B;
        CreatList(B);
        printf("%.2f
    ",getAvar(B));
        alter(B);
        traverse(B);
        return 0;
    }
  • 相关阅读:
    bootstrap媒体查询
    Qt用Zip压缩文件夹的一些坑
    QCanvasItem介绍-QT3
    C盘无损扩容
    ArcGis连接oracle失败:ORA-6413:连接未打开
    通过ArcMap发布服务
    windows系统下使用cd命令
    C语言运算符优先级
    c/c++ 指针
    c++数组易错点总结
  • 原文地址:https://www.cnblogs.com/wwww2/p/11745525.html
Copyright © 2011-2022 走看看