zoukankan      html  css  js  c++  java
  • C第12章-----堆

    #include <stdio.h>

    #include <stdlib.h>

     

    //声明Person结构

    //struct Person{

    //    float heightInMeters;

    //    int weightInKilos;

    //};

     

    //声明Person类型

    typedef struct{

        float heightInMeters;

        int weightInKilos;

    }Person;

     

    //float bodyMassIndex(Person p){

    //    return p.weightInKilos / (p.heightInMeters * p.heightInMeters);

    //}

     

    float bodyMassIndex(Person *p){

        return p->weightInKilos / (p->heightInMeters * p->heightInMeters);

    }

     

    int main(int argc, const char * argv[]) {

        

        //使用malloc()函数可以得到一块内存缓冲区,当程序不在使用这块缓冲区时,可以调用free()函数,释放相应的内存,将其返回给堆。

        //使用sizeof()来获得缓冲区的准确大小。

        //为一个Person结构分配内存

        Person *mikey = (Person *)malloc(sizeof(Person));

        

        //为一个Person结构分配内存

        mikey->weightInKilos = 96;

        mikey->heightInMeters = 1.7;

        

        //计算并输出BMI

        float mikeyBMI = bodyMassIndex(mikey);

        printf("mikey has a BMI of %f ", mikeyBMI);

        

        //释放占用的内存,使之能够被重用

        free(mikey);

        

        //将指针变量赋为空

        mikey = NULL;

        

        return 0;

        

    //    //struct Person mikey;

    //    //Person mikey;

    //    mikey.heightInMeters = 1.7;

    //    mikey.weightInKilos = 96;

    //

    //    //struct Person aaron;

    //    //Person aaron;

    //    aaron.heightInMeters = 1.97;

    //    aaron.weightInKilos = 84;

    //

    //    printf("mikey is %.2f meters tall. ", mikey.heightInMeters);

    //    printf("mikey weights %d kilograms. ", mikey.weightInKilos);

    //    printf("aaron is %.2f meters tall. ", aaron.heightInMeters);

    //    printf("aaron weights %d kilograms. ", aaron.weightInKilos);

    //

    //    float bmi;

    //    bmi = bodyMassIndex(mikey);

    //    printf("mikey has a BMI of %.2f ", bmi);

    //

    //    bmi = bodyMassIndex(aaron);

    //    printf("aaron has a BMI of %.2f ", bmi);

    //

    //    return 0;

    }

  • 相关阅读:
    设计模式之工厂模式-抽象工厂(02)
    1036 跟奥巴马一起编程 (15 分)
    1034 有理数四则运算 (20 分)
    1033 旧键盘打字 (20 分)
    1031 查验身份证 (15 分)
    大学排名定向爬虫
    1030 完美数列 (25 分)二分
    1029 旧键盘 (20 分)
    1028 人口普查 (20 分)
    1026 程序运行时间 (15 分)四舍五入
  • 原文地址:https://www.cnblogs.com/turningli/p/10695781.html
Copyright © 2011-2022 走看看