美文网首页
CPP_Basic_Code_P4.1-PP4.13.10

CPP_Basic_Code_P4.1-PP4.13.10

作者: 贴墙上的咖啡 | 来源:发表于2017-03-26 22:23 被阅读0次

    CPP_Basic_Code_P4.1-PP4.13.10

    //  The Notes Created by Z-Tech on 2017/2/17.
    //  All Codes Boot on 《C++ Primer Plus》V6.0
    //  OS:MacOS 10.12.4
    //  Translater:clang/llvm8.0.0 &g++4.2.1
    //  Editer:iTerm 2&Sublime text 3
    //  IDE: Xcode8.2.1&Clion2017.1
    
    //P4.1
    #include <iostream>
    int main()
    {
        using namespace std;
        int yams[3];
        yams[0]=7;
        yams[1]=8;
        yams[2]=6;
    
        int yamcosts[3]={20,30,5};
        cout<<"Total yams = ";
        cout<<yams[0]+yams[1]+yams[2]<<endl;
        cout<<"The package with "<<yams[1]<<"yams costs ";
        cout<<yamcosts[1]<<" cents per yam.\n";
        int total=yams[0]*yamcosts[0]+yams[1]*yamcosts[1];
        total=total+yams[2]*yamcosts[2];
        cout<<"The total yam expense is "<<total<<" cents.\n";
    
        cout<<"\nSize of yams array = "<<sizeof yams;//计算数组字节数
        cout<<" bytes.\n";
        cout<<"Size of one element = "<<sizeof yams[0];//计算数组单个成员字节数
        cout<<" bytes.\n";
        return 0;
    }
    
    //P4.2
    #include <iostream>
    int main()
    {
        using namespace std;
        const int Size=15;
        char name1[Size];
        char name2[Size]="C++owboy";
    
        cout<<"Howdy! I'm "<<name2;
        cout<<"! What's yuour name?\n";
        cin>>name1;//这种方式的读取不允许姓名间有空格一类
        cout<<"Well, "<<name1<<", your name has ";
        cout<<strlen(name1)<<" letters and is stored\n";//提取字符串长度
        cout<<"in an array of "<<sizeof(name1)<<" bytes.\n";//数组所占字节数
        cout<<"Your initial is "<<name1[0]<<".\n";//第一个字母即第一个数组元素
        name2[3]='\0';//将数组name2的第四个元素置为"\0"
        cout<<"Here are the first 3 characters of my name: ";
        cout<<name2<<endl;//此时将读取前三个元素后即碰到\0,故只输出前三个
        return 0;
    }
    
    //P4.3
    #include <iostream>
    int main()
    {
        using namespace std;
        const int Arsize=20;
        char name[Arsize];
        char dessert[Arsize];
    
        cout<<"Enter your name:\n";
        cin>>name;//由于此处不支持空格,会导致last name进入后面的cin队列,从而没有机会输入dessert
        cout<<"Enter your favorite dessert:\n";
        cin>>dessert;
        cout<<"I have some delicious "<<dessert;
        cout<<" for you, "<<name<<".\n";
        return 0;
    }
    
    //P4.4
    #include <iostream>
    int main()
    {
        using namespace std;
        const int Arsize=20;//修改数组容量可以储存更多的字符串
        char name[Arsize];
        char dessert[Arsize];
    
        cout<<"Enter your name:\n";
        cin.getline(name,Arsize);//此处使用cin.getline面向行处理,成功克服空格问题
        cout<<"Enter your favorite dessert:\n";
        cin.getline(dessert,Arsize);//再次使用行处理
        cout<<"I have some delicious "<<dessert;
        cout<<" for you, "<<name<<".\n";
        return 0;
    }
    
    //P4.5
    #include <iostream>
    int main()
    {
        using namespace std;
        const int Arsize=20;
        char name[Arsize];
        char dessert[Arsize];
    
        cout<<"Enter your name:\n";
        cin.get(name,Arsize).get();//这种方式会调用读取下一个字符(这里也就是回车)
        cout<<"Enter your favorite dessert:\n";
        cin.get(dessert,Arsize).get();//其实是cin.get(name,value)和cin.get()的合成缩写
        cout<<"I have some delicious "<<dessert;
        cout<<" for you, "<<name<<".\n";
        return 0;
    }
    
    //P4.6
    #include <iostream>
    int main()
    {
        using namespace std;
        cout<<"What year was your house built?\n";
        int year;
        (cin>>year).get();//解决回车按键的问题
        cout<<"What's its street address?\n";
        char address[80];
        cin.getline(address,80);
        cout<<"Year built: "<<year<<endl;
        cout<<"Address: "<<address<<endl;
        cout<<"Done!\n";
        return 0;
    }
    
    //P4,7
    #include <iostream>
    #include <string>
    int main()
    {
        using namespace std;
        char charr1[20];
        char charr2[20]="jaguar";//数组式初始化
        string str1;
        string str2="panther";//C风格字符串的初始化
    
        cout<<"Enter a kind of feline: ";
        cin>>charr1;//可使用cin注入数组
        cout<<"Enter another kind of feline: ";
        cin>>str1;//可使用cin注入string对象
        cout<<"Here are some felines: \n";
        cout<<charr1<<" "<<charr2<<" "<<str1<<" "<<str2<<" "<<endl;
        cout<<"The third letter in "<<charr2<<" is "<< charr2[2]<<endl;//数组法引用
        cout<<"The third letter in "<<str2<<" is "<<str2[2]<<endl;//数组法引用string对象中的元素
        return 0;
    }
    
    //P4.8
    #include <iostream>
    #include <string>
    int main()
    {
        using namespace std;
        string s1="penguin";
        string s2,s3;
        cout<<"You can assign one string object to another:s2=s1\n";
        s2=s1;
        cout<<"s1: "<<s1<<" s2: "<<s2<<endl;
        cout<<"You can assign a C_Style string to a string object.\n";
        cout<<"s2=\"buzzard\"\n";
        s2="buzzard";//直接赋值
        cout<<"s2: "<<s2<<endl;
        cout<<"You can concatenate string:s3=s1+s2\n";
        s3=s1+s2;//直接合并两个字符串
        cout<<"s3:"<<s3<<endl;
        cout<<"You can append strings.\n";
        s1+=s2;//等价于s1=s1+s2,是添加运算符+=的效果
        cout<<"s1+=s2 yield s1 = "<<s1<<endl;
        s2+=" for a day!";//在结尾添加字符串
        cout<<"s2+= \" for a day!\" yield s2 = "<<s2<<endl;//使用string对象无需考虑字符串长度超出数组问题
        return 0;
    }
    
    //P4.9
    #include <iostream>
    #include <string>
    #include <cstring>//C风格字符串
    int main()
    {
        using namespace std;
        char charr1[20];
        char charr2[20]="jaguar";
        string str1;
        string str2="panther";
    
        str1=str2;
        strcpy(charr1,charr2);//C风格字符串复制传递
    
        str1+=" paste";
        strcat(charr1," juice");//C风格字符串结尾添加字符
    
        int len1=str1.size();//子函数字符长度计算
        int len2=strlen(charr1);//C风格字符长度计算
        cout<<"The string "<<str1<<" contains "<<len1<<" characters.\n";
        cout<<"The string "<<charr1<<" contains "<<len2<<" charaters.\n";
        return 0;
    }
    
    //P4.10
    #include <iostream>
    #include <string>
    #include <cstring>
    int main()
    {
        using namespace std;
        char charr[20];
        string str;
    
        cout<<"Length of string in charr before input: "
            <<strlen(charr)<<endl;//此时因未初始化,故字符内容不确定
        cout<<"Length of strinf in str before input: "
            <<str.size()<<endl;//因为是string对象,未初始化前内容自动为0
        cout<<"Enter a line of text:\n";
        cin.getline(charr,20);//经典C数组风格读取输入
        cout<<"You entered: "<<charr<<endl;
        cout<<"Enter another line of text: \n";
        getline(cin,str);//C++ string 对象读取,长度自适应
        cout<<"You entered: "<<str<<endl;
        cout<<"Length of string in charr after input: "
            <<strlen(charr)<<endl;
        cout<<"Length of string in str after input: "
            <<str.size()<<endl;
        return 0;
    }
    
    //P4.11
    #include <iostream>
    struct inflatable
    {
        char name[20];
        float volume;
        double price;
    };//注意结构体声明需要;结尾
    int main()
    {
        using namespace std;
        inflatable guest={"Glorious Gloria",1.88,29.99};//初始化结构成员guest
        inflatable pal={"Audacious Arthur",3.12,32.99};//初始化结构成员pal
        cout<<"Expand your guest list with "<<guest.name;
        cout<<" and "<<pal.name<<"!\n";//pal.name是一个char数组
        cout<<"You can have both for $";
        cout<<guest.price+pal.price<<"!\n";//guest.price是一个double变量
        return 0;
    }
    
    //P4.12
    #include <iostream>
    struct inflatable//结构化声明提倡使用外部式
    {
        char name[20];
        float volume;
        double price;
    };
    int main()
    {
        using namespace std;
        inflatable bouquet={"Sunflowers",0.20,12.49};
        inflatable choice;//未被初始化
        cout<<"bouquet: "<<bouquet.name<<" for $";
        cout<<bouquet.price<<endl;
    
        choice=bouquet;//结构体可以直接赋值,哪怕子结构中有数组
        cout<<"bouquet: "<<choice.name<<" for $";
        cout<<choice.price<<endl;
        return 0;
    }
    
    //P4.13
    #include <iostream>
    struct inflatable
    {
        char name[20];
        float volume;
        double price;
    };
    int main()
    {
        using namespace std;
        inflatable guest[2]=//此处[2]表明有两个结构
        {
           {"Bambi",0.5,21.99},
           {"Godzilla",2000,565.99}
        };//结构初始化之后必须加;
    
        cout<<"The guest "<<guest[0].name<<" and "<<guest[1].name
            <<"\nhave a combined volume of "
            <<guest[0].volume+guest[1].volume<<" cubic feet.\n";
        return 0;
    }
    
    //P4.14
    #include <iostream>
    int main()
    {
        using namespace std;
        int donuts=6;
        double cups=4.5;
        cout<<"donuts value = "<<donuts;
        cout<<" and donuts address = "<<&donuts<<endl;//使用取址运算符&
    
        cout<<"cups avalue = "<<cups;
        cout<<" and cups address = "<<&cups<<endl;
        return 0;
    }
    
    //P4.15
    #include <iostream>
    int main()
    {
        using namespace std;
        int updates=6;
        int* p_updates;//此处定义指针
        p_updates=&updates;//指针必须被赋值初始化
    
        cout<<"Value: updates = "<<updates;
        cout<<" , *p_updates = "<<*p_updates<<endl;//此处为所指向地址里的值
    
        cout<<"Address: &updates = "<<&updates;
        cout<<" , p_updates = "<<p_updates<<endl;//指针本身是一个地址
    
        ++*p_updates;//*p_updates可以像使用int变量一样操作
        cout<<"Now updates = "<<updates<<endl;
        return 0;
    }
    
    //P4.16
    #include <iostream>
    int main()
    {
        using namespace std;
        int higgens=5;
        int* pt=&higgens;
    
        cout<<"Value of higgens = "<<higgens
            <<"; Address of higgens = "<<&higgens<<endl;
        cout<<"Value of *pt = "<<*pt//带*的指针即为对应的值
            <<"; Value of pt = "<<pt<<endl;//不带*即为指针本身,即地址
        return 0;
    }
    
    //P4.17
    #include <iostream>
    int main()
    {
        using namespace std;
        int nights=1001;
        int* pt=new int;//申请了一块数据类型为int的新内存,且指针也指向int
        *pt=1001;
    
        cout<<"nights value = ";
        cout<<nights<<": location "<<&nights<<endl;//nights的地址
        cout<<"int ";
        cout<<"value = "<<*pt<<": location = "<<pt<<endl;
        double* pd=new double;//申请了一块数据类型为double的新内存且指向double
        *pd=10000001.0;
    
        cout<<"double ";
        cout<<"value = "<<*pd<<": location = "<<pd<<endl;//此处只能用pd来访问新申请的内存
        cout<<"location of pointer pd: "<<&pd<<endl;
        cout<<"size of pt = "<<sizeof(pt);//这里计算指针的字节数
        cout<<": size of *pt = "<<sizeof(*pt)<<endl;//这里是指针所指对象的值的字节数
        cout<<"size of pd = "<<sizeof(pd);//这里计算指针的字节数
        cout<<": size of *pd = "<<sizeof(*pd)<<endl;//这里是指针所指对象的值的字节数
        delete pt;
        delete pd;
        return 0;
    }
    
    //P4.18
    #include <iostream>
    int main()
    {
        using namespace std;
        double* p3=new double[3];//new了一个3个元素的double数组
        p3[0]=0.2;
        p3[1]=0.5;
        p3[2]=0.8;
        cout<<"p3[1] is "<<p3[1]<<".\n";
        ++p3;//指针右移1个元素
        cout<<"Now p3[0] is "<<p3[0]<<" and ";
        cout<<"p3[1] is "<<p3[1]<<".\n";
        --p3;//指针移回原位,以备之后释放内存
        delete [] p3;//delete了之前new的内存
        return 0;
    }
    
    //P4.19
    #include <iostream>
    int main()
    {
        using namespace std;
        double wages[3]={10000.0,20000.0,30000.0};
        short stacks[3]={3,2,1};
    
        double* pw=wages;//数组名为数组第一个元素的地址
        short* ps=&stacks[0];//这是上一句显式的写法
    
        cout<<"pw= "<<pw<<", *pw= "<<*pw<<endl;
        ++pw;//右移一个元素,double实际上是8字节
        cout<<"add 1 to the pw pointer:\n";
        cout<<"pw= "<<pw<<", *pw= "<<*pw<<"\n\n";
        cout<<"ps= "<<ps<<", *ps= "<<*ps<<endl;
        ++ps;//右移一个元素,short实际上是2字节
        cout<<"add 1 to ps pointer:\n";
        cout<<"ps= "<<ps<<", *ps= "<<*ps<<"\n\n";
    
        cout<<"access two elements with array notation\n";
        cout<<"stacks[0]= "<<stacks[0]<<",stacks[1]= "<<stacks[1]<<endl;
        //以上是直接访问数组元素的方式
        cout<<"access two elements with pointer notaion\n";
        cout<<"*stacks= "<<*stacks<<",*(stacks+1)= "<<*(stacks+1)<<endl;
        //以上是将数组名当做指针使用,移动提取元素的方式
        cout<<sizeof(wages)<<" = size of wages array\n";//计算整个数组的字节数
        cout<<sizeof(pw)<<" = size of pw pointer\n";//仅计算pw指针的字节数
        return 0;
    }
    
    //P4.20
    #include <iostream>
    #include <cstring>
    int main()
    {
        using namespace std;
        char animal[20]="bear";
        const char* bird="wren";//使用const可防止bird修改值wren
        char* ps;
    
        cout<<animal<<" and "<<bird<<endl;//cout对char类型默认为字符串输出
        cout<<"Enter a kind of animal: ";
        cin>>animal;
        ps=animal;
        cout<<ps<<"!\n";
        cout<<"before using strcpy():\n";
        cout<<animal<<" at "<<(int*)animal<<endl;//使用(int*)强制类型转换显示地址
        cout<<ps<<" at "<<(int*)ps<<endl;
    
        ps=new char[strlen(animal)+1];//申请新内存,且容量以animal为准再加1,以便存储空字符\0到结尾
        strcpy(ps,animal);
        cout<<"after using strcpy():\n";
        cout<<animal<<" at "<<(int*)animal<<endl;
        cout<<ps<<" at "<<(int*)ps<<endl;
        delete [] ps;
        return 0;
    }
    
    //P4.21
    #include <iostream>
    struct inflatable
    {
        char name[20];
        float volume;
        double price;
    };
    int main()
    {
        using namespace std;
        inflatable * ps=new inflatable;//新申请一个动态结构指针
        cout<<"Enter name of inflatable item: ";
        cin.get(ps->name,20);//指针法读取输入
        cout<<"Enter volume in cubic feet: ";
        cin>>(*ps).volume;//数组名引用法读取输入
        cout<<"Enter price: $";
        cin>>ps->price;
        cout<<"Name: "<<(*ps).name<<endl;
        cout<<"Volume: "<<ps->volume<<" cubic feet\n";
        cout<<"Price: $"<<ps->price<<endl;
        delete ps;//释放new出的内存
        return 0;
    }
    
    //P4.22
    #include <iostream>
    #include <cstring>
    using namespace std;
    char* getname(void);//函数声明,返回值为char,不接受参数
    int main()
    {
        char* name;
        name=getname();//第一次调用子函数
        cout<<name<<" at "<<(int*)name<<"\n";
        delete [] name;
    
        name=getname();//第二次调用子函数
        cout<<name<<" at "<<(int*)name<<"\n";
        delete [] name;//释放内存
        return 0;
    }
    
    char* getname()//子函数开始
    {
        char temp[80];//建立临时字符串数组
        cout<<"Enter last name: ";
        cin>>temp;//读取字符串到数组temp
        char* pn=new char[strlen(temp)+1];//新定义恰到好处的指针和所需内存
        strcpy(pn,temp);//复制数据到新的指针地址
    
        return pn;//返回指针地址(char类型,将被识别为字符串本身)
    }
    
    //P4.23
    #include <iostream>;
    
    struct antarctica_years_end//结构定义
    {
        int year;
    };
    
    int main()
    {
        antarctica_years_end s01, s02, s03;//定义三个结构变量
        s01.year = 1998;//结构式赋值
        antarctica_years_end *pa = &s02;//定义结构指针pa指向s02
        pa->year = 1999;//指针式赋值
        antarctica_years_end trio[3];//创建三个元素的结构数组
        trio[0].year = 2003;//结构数组访问成员
        std::cout << trio->year << std::endl;
        //输出结构数组第一个元素结构的year成员 2003
        const antarctica_years_end *arp[3] = {&s01, &s02, &s03};//定义含3指针的指针数组
        //指针式访问第二个元素,即&s02指向结构变量的year成员1999
        const antarctica_years_end **ppa = arp;
        //创建指向上一个指针数组的的指针
        //或者使用const antarctica_years_end **ppb=arp
        std::cout<<(*ppa)->year<<std::endl;
        //ppa解除引用后,元素依然为指针(毕竟为指针数组第一个元素)1998
        //ppb和ppa一样,故+1后指针右移一个元素为&s02,1999
        return 0;
    }
    
    //P4.24
    #include <iostream>
    #include <vector>
    #include <array>
    int main()
    {
        using namespace std;
        double a1[4]={1.2,2.4,3.6,4.8};//传统C语言写法初始化四元素数组
    
        vector<double> a2(4);//C++98方法初始化,动态数组替代品
        a2[0]=1.0/3.0;//初始化各个元素,C++98中没有捷径初始化
        a2[1]=1.0/5.0;
        a2[2]=1.0/7.0;
        a2[3]=1.0/9.0;
    
        array<double,4> a3={3.14,2.72,1.62,1.41};//静态内存分配,常规数组替代品
        array<double,4> a4;//被默认初始化为0
        a4=a3;
    
        cout<<"a1[2]: "<<a1[2]<<" at "<<&a1[2]<<endl;
        cout<<"a2[2]: "<<a2[2]<<" at "<<&a2[2]<<endl;
        cout<<"a3[2]: "<<a3[2]<<" at "<<&a3[2]<<endl;
        cout<<"a4[2]: "<<a4[2]<<" at "<<&a4[2]<<endl;
    
        a1[-2]=20.2;//即*(a1-2),a1数组前移两个元素,已位于数组外部!可使用如:a2.at(1)=2.3
        cout<<"a1[-2]: "<<a1[-2]<<" at "<<&a1[-2]<<endl;
        cout<<"a3[2]: "<<a3[2]<<" at "<<&a3[2]<<endl;
        cout<<"a4[2]: "<<a4[2]<<" at "<<&a4[2]<<endl;
        return 0;
    }
    
    //PP4.13.1
    #include <iostream>
    //#include <cstring>
    struct person
    {
        char name1[20];
        char name2[20];
        char grade;//定义字符型变量
        int age;
    };
    int main()
    {
        using namespace std;
        person* xv=new person;//new一个xv指向结构person
        cout<<"What is your first name? "<<endl;
        cin.getline(xv->name1,20);//使用getline以避免姓名中出现空格,造成\n被读取
        cout<<"What is your last name? "<<endl;
        cin.getline(xv->name2,20);
        cout<<"what letter grade do you deserve? "<<endl;
        cin>>(*xv).grade;//读入输入的成绩值
        cout<<"What is your age? "<<endl;
        cin>>xv->age;
        cout<<"Name: "<<xv->name2<<", "<<xv->name1<<endl;
        cout<<"Grade: "<<++(*xv).grade<<endl;//此处因为是字符型,只需++即可输出下一档次成绩值
        cout<<"Age: "<<xv->age<<endl;
        delete xv;//释放new出来的内存
        return 0;
    }
    
    //PP4.13.2
    #include <iostream>
    #include <string>
    int main()
    {
        using namespace std;
    
        string name;
        string dessert;
    
        cout<<"Enter your name:\n";
        getline(cin,name);//注意和数组的语法区别:cin.getline(name,30)
        cout<<"Enter your favorite dessert:\n";
        cin>>dessert;
        cout<<"I have some delicious "<<dessert;
        cout<<" for you, "<<name<<".\n";
        return 0;
    }
    
    //PP4.13.3
    #include <iostream>
    #include <cstring>
    int main()
    {
        using namespace std;
        char name1[20];
        char name2[20];
        cout<<"Enter your first name: "<<endl;
        cin.getline(name1,20);
        cout<<"Enter your last name: "<<endl;
        cin.getline(name2,20);
        strcat(name2,",");//C风格字符串的添加使用strcat()函数
        strcat(name2,name1);
        cout<<"Here's the information in single string: "<<endl
            <<name2;
        return 0;
    }
    
    //PP4.13.4
    #include <iostream>
    #include <string>
    int main()
    {
        using namespace std;
        string name1,name2,name;
        cout<<"Enter your first name: "<<endl;
        getline(cin,name1);//使用string对象的处理方式
        cout<<"Enter your last name: "<<endl;
        getline(cin,name2);
        name=name2+","+name1;//string类支持直接加合多个字符串且不用考虑长度问题
        cout<<"Here's the information in single string: "<<endl
            <<name;
        return 0;
    }
    
    //PP4.13.5
    #include <iostream>
    #include <string>
    struct CandyBar
    {
        std::string brand="Mocha Munch";
        float weight=2.3;
        int calorie=350;
    }snack;//定义结构时即初始化变量snack
    int main()
    {
        std::cout<<"Brand: "<<snack.brand<<std::endl
                 <<"Weight: "<<snack.weight<<std::endl
                 <<"Calorie: "<<snack.calorie<<std::endl;
        return 0;
    }
    
    //PP4.13.6
    #include <iostream>
    #include <string>
    struct CandyBar
    {
        std::string brand;
        float weight;
        int calorie;
    };
    int main()
    {
        CandyBar species[3];//初始化一个三元素的结构数组species
        species[0].brand="傻子";//数组法初始化
        species[0].weight=25.25;
        species[0].calorie=250;
        (species+1)->brand="聪明者";//数组名即为指针,故也可用指针法给第二个成员结构的元素赋值
        (species+1)->weight=36.66;
        (species+1)->calorie=360;
        CandyBar* xv[3]{&species[0],&species[1],&species[2]};
        //上一行开头处不用const可通过编译,新建指针数组,分别指向三个结构
        xv[2]->brand="普通人";//使用指针数组给第三个成员结构的元素赋值
        xv[2]->weight=22.33;
        xv[2]->calorie=290;
        std::cout<<"Brand1: "<<species[0].brand<<std::endl//输出第一个成员结构的品牌名
                 <<"Weight2: "<<(species+1)->weight<<std::endl//输出第二个成员结构的产品重量
                 <<"Calorie3: "<<xv[2]->calorie<<std::endl;//输出第三个成员结构的产品热量
        return 0;
    }
    
    //PP4.13.7&//PP4.13.8
    #include <iostream>
    #include <string>
    using namespace std;
    struct pizza
    {
        string company_name;
        float pizza_diameter;
        float pizza_weight;
    };
    int main()
    {
        pizza* xv=new pizza;
        cout<<"Enter the name of pizza company: "<<endl;
        getline(cin,xv->company_name);//注意语法细节,括号前面没有"."!!!
        cout<<"Enter the diameter of pizza: "<<endl;
        cin>>xv->pizza_diameter;
        cout<<"Enter the weight of pizza: "<<endl;
        cin>>xv->pizza_weight;
        cout<<"So here's the information you have input: "<<endl;
        cout<<"公司名称: "<<xv->company_name<<endl
            <<"披萨尺寸: "<<xv->pizza_diameter<<"寸"<<endl
            <<"披萨重量: "<<xv->pizza_weight<<"磅";
        delete xv;//注意释放New出的内存
        return 0;
    }
    
    //PP4.13.9
    #include <iostream>
    #include <string>
    struct CandyBar
    {
        std::string brand;
        float weight;
        int calorie;
    };
    int main()
    {
        CandyBar* xv=new CandyBar;//一旦new了切记使用delete!
        xv->brand="傻瓜牌";
        xv->weight=250.55;
        xv->calorie=750;
        std::cout<<"品牌名: "<<xv->brand<<std::endl
                 <<"重量: "<<xv->weight<<std::endl
                 <<"热量: "<<xv->calorie<<std::endl;
        delete xv;
        return 0;
    }
    
    //PP4.13.10
    #include <iostream>
    #include <array>
    int main()
    {
        using namespace std;
        cout.setf(ios_base::fixed,ios_base::floatfield);//修复cout输出的小数点,防止整数时丢尾
        array<float,3> run_score;//注意array的初始化和声明方式,和数组区别
        enum run_times{zero,first,second,third};//试试枚举变量而已,另类的const
        cout<<"Enter your first score of 100meter: "<<endl;
        cin>>run_score[0];
        cout<<"Enter your second score of 100meter: "<<endl;
        cin>>run_score[1];
        cout<<"Enter your third score of 100meter: "<<endl;
        cin>>run_score[2];
        float mean_value=run_score[0]+run_score[1]+run_score[2];
        mean_value=(mean_value)/3;
        cout<<"Your NO."<<first<<" score of 100 meter: "<<run_score[0]<<"s"<<endl;
        cout<<"Your NO."<<second<<" score of 100 meter: "<<run_score[1]<<"s"<<endl;
        cout<<"Your NO."<<third<<" score of 100 meter: "<<run_score[2]<<"s"<<endl;
        cout<<"So your mean value score is: "<<mean_value<<"s"<<endl;
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:CPP_Basic_Code_P4.1-PP4.13.10

          本文链接:https://www.haomeiwen.com/subject/dlqlottx.html