C++plus 4.13

来源:互联网 发布:查看当前进程 linux 编辑:程序博客网 时间:2024/06/06 02:46

C++11针对数组初始化的新规则:

1、在初始化数组的时候,可以省略等号

2、在初始化的时候,大括号中不填写任何东西的时候,默认将所有元素设置为零

3、列表初始化禁止缩窄变换。(缩窄变换是指将字符长度较大的类型在比它字符类型中初始化)

1、编写一个C++程序,如下述输出示例所示的那样请求并显示信息:

What is your first name? Betty Sue

What is your last name? Yewe

What letter grade do you deserve? B

What is your age? 22

Name: Yewe , Betty Sue

Grade : C

Age: 22

注意,该程序应该接受的名字包含多个单词。另外,程序将向下调整成绩,即上调一个字母。假设用户请求A、B或C,所以不必担心D和F之间的空档。

#include <iostream>void Person();using namespace std;int main() {    Person();    return 0;}void Person(){    const int size = 20;    char FiName[size];    char LaName[size];    char Gre;    int Age;    cout<<"What is your first name? ";    cin.get(FiName,size).get();    cout <<"What is your last name? ";    cin.get(LaName,size).get();    cout <<"What letter grade do you deserve? ";    cin >> Gre;    cout <<"What is your age? ";    cin >> Age;    cout << "Name: "<<LaName<<" , "<< FiName<<endl;    Gre++;    cout << "Grade : "<< Gre<<endl;    cout << "Age:"<<Age <<endl;}



2、修改程序清单4.4,使用C++string类而不是char数组。


#include <iostream>void Person();using namespace std;int main() {    Person();    return 0;}void Person(){    string Name;    string Dessert;    cout <<"Enter your name: \n";    getline(cin,Name);    cout<<"Enter your favorite dessert: \n";    getline(cin,Dessert);    cout <<"I have delicious "<< Dessert <<" for you ,"<< Name <<".\n"<<endl;}


3、编写一个程序,它要求用户首先输入其名,然后输入其姓,然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用char数组和头文件cstring中的函数。下面是该程序运行时的情形:

Enter your first name: Flip

Enter your last name: Fleming

Here's the information in a single string : Fleming,Flip

针对char的或许需要使用get和getline来获取一行。getline可以自动将换行符替换为结束符进行存储。但是get不会。如果在获取两个字符的过程中,使用一个get后紧接着使用第二个get获取。是无法获取到第二个字符的。第二个get里存储的是上一个字符的换行符。所以如果想获取两个字符在获取完第一个字符后在获取第二个字符之间需要多添加一个空的get()来放置上一个字符的换行符。

串联char字符需要使用strcat函数和strcpy函数。strcat函数的作用是会将后面的字符串复制到前面的字符数组的后面。strcpy函数的作用是将后面的字符串复制到前面的字符数组中。

串联char字符的扩展函数strncat和strncpy函数。是能够将后面的字符串添加后会超出前一个字符数组长度的字符串添加进去。官方话:它们接收指出目标数组最大允许长度的第三参数。


#include <iostream>void Person();using namespace std;int main() {    Person();    return 0;}void Person(){    const int Size = 20;    char factor[Size] =" , ";    char FiName[Size];    char LaName[Size];    cout <<"Enter your first name: ";    cin.get(FiName,Size).get();    cout <<"Enter your last name: ";    cin.get(LaName,Size).get();    strcat(LaName,factor);    strcat(LaName,FiName);    cout <<"Here's the information in a single string : "<< LaName <<endl;}



4、编写一个程序,它要求用户首先输入其名,然后输入其姓,然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用string对象和头文件string中的函数。下面是该程序运行时的情形:

Enter your first name: Flip

Enter your last name: Fleming

Here's the information in a single string : Fleming,Flip

#include <iostream>void Person();using namespace std;int main() {    Person();    return 0;}void Person(){    string FiName;    string LaName;    cout <<"Enter your first name: ";    getline(cin,FiName);    cout <<"Enter your last name: ";    getline(cin,LaName);    LaName+=", "+FiName;    cout <<"Here's the information in a single string : "<< LaName <<endl;}


这里需要注意的是。LaName+=","+" "+FiName;不能酱紫写哦。具体为啥QwQ我也不知道。等我知道了来填坑。

5、结构CandyBar包含3个成员。第一个成员存储了糖块的品牌。第二个成员存储糖块的重量(可以有小数),第三个成员存储了糖块的卡路里含量(整数)。请编写一个程序,声明这个结构。创建一个名为snack的CandyBar变量,并将其成员初始化为"Mocha Munch"、2.3和350。初始化应在声明snack时进行。最后,程序显示snack变量的内容。

#include <iostream>struct CandyBar{    char Brand[20];    float Weight;    int Cal;};using namespace std;int main() {    CandyBar snack={            "Mocha Munch",            2.3,            350    };    cout <<"The Candy's name is "<<snack.Brand<<", it weight is "<<snack.Weight<<", it cal is "<<snack.Cal<<endl;    return 0;}


6、结构CandyBar包含3个成员,如编程练习5所示。请编写一个程序,创建一个包含3个元素CandyBar数组,并将它们初始化为所选择的值,然后显示每个结构的内容。

#include <iostream>struct CandyBar{    char Brand[20];    float Weight;    int Cal;};using namespace std;int main() {    CandyBar snack[3]={            {"Mocha Munch", 2.3, 350},            {"Suuuu Moope", 4.9, 450},            {"Hallo World", 8.0, 670}    };    cout <<"The First Candy's name is "<<snack[0].Brand<<", it weight is "<<snack[0].Weight<<", it cal is "<<snack[0].Cal<<endl;    cout <<"The Second Candy's name is "<<snack[1].Brand<<", it weight is "<<snack[1].Weight<<", it cal is "<<snack[1].Cal<<endl;    cout <<"The Third Candy's name is "<<snack[2].Brand<<", it weight is "<<snack[2].Weight<<", it cal is "<<snack[2].Cal<<endl;    return 0;}


7、William Wingate从事披萨饼分析服务,对于每个披萨饼,他都需要记录下列信息:

披萨饼公司的名称,可以有多个单词组成。

披萨饼的直径。

披萨饼的重量。

请设计一个能够存储这些信息的结构,并编写一个使用这种结构变量的程序。程序将请求用户输入上述信息,然后显示这些信息。请使用cin(或其它方法)和cout。


#include <iostream>#include <string>struct Pizza{    char Brand[50];    float Weight;    float Diameter;}pizzas;using namespace std;int main() {    cout<<"Enter Pizza's Brand: ";    cin.get(pizzas.Brand,50);    cout <<"Enter Pizza's Diameter: ";    cin >> pizzas.Diameter;    cout <<"Enter Pizza's Weight: ";    cin >> pizzas.Weight;    cout <<"This Pizza's Brand is "<< pizzas.Brand<<" Diameter is "<<pizzas.Diameter<<" Weight is "<<pizzas.Weight;    return 0;}


8、完成编程练习7,但使用new来为结构分配内存,而不是声明一个结构变量。另外让程序在请求输入披萨饼公司名称之前输入披萨饼的直径。

#include <iostream>#include <string>struct Pizza {    char Brand[50];    float Weight;    float Diameter;};using namespace std;int main() {    Pizza *pizzas =  new Pizza;    cout<<"Enter Pizza's Brand: ";    cin.get(pizzas->Brand,50);    cout <<"Enter Pizza's Diameter: ";    cin >> pizzas->Diameter;    cout <<"Enter Pizza's Weight: ";    cin >> pizzas->Weight;    cout <<"This Pizza's Brand is "<< pizzas->Brand<<" Diameter is "<<pizzas->Diameter<<" Weight is "<<pizzas->Weight;    return 0;}


9、完成编程练习6,但使用new来动态分配数组,而不是声明一个包含三个元素的CandyBar数组。

#include <iostream>struct CandyBar{    char Brand[20];    float Weight;    int Cal;};using namespace std;int main() {    CandyBar *snack = new CandyBar[3]{            {"Mocha Munch", 2.3, 350},            {"Suuuu Moope", 4.9, 450},            {"Hallo World", 8.0, 670}    };    cout <<"The First Candy's name is "<<snack[0].Brand<<", it weight is "<<snack[0].Weight<<", it cal is "<<snack[0].Cal<<endl;    cout <<"The Second Candy's name is "<<snack[1].Brand<<", it weight is "<<snack[1].Weight<<", it cal is "<<snack[1].Cal<<endl;    cout <<"The Third Candy's name is "<<snack[2].Brand<<", it weight is "<<snack[2].Weight<<", it cal is "<<snack[2].Cal<<endl;    return 0;}


10、编写一个程序,让用户输入三次40码跑的成绩(如果您愿意,也可让用户输入40米跑的成绩),并显示次数和平均成绩。请使用一个array对象来存储数据(如果编译器不支持array类,请使用数组)。

未使用array数组的

#include <iostream>using namespace std;int main() {    int size;    float total;    cout <<"Enter your Test times: ";    cin >>size;    float *Student = new float[size];    cout <<"Enter your 40m every time test grade:";    //先分次录入成绩    for (int j = 0; j < size; ++j) {        cin >> Student[j];    }    //遍历成绩,并将所有成绩相加在一起    for (int i = 0; i <size ; ++i) {        total+=Student[i];    }    cout <<"Your average grade is "<<total/size<<endl;    delete []Student;    return 0;}


使用了array数组的

#include <iostream>#include <array>using namespace std;int main() {    float total;    std::array<float ,3> Student{};    cout <<"Enter your 40m every time test grade:";    //先分次录入成绩    for (int j = 0; j < 3; ++j) {        cin >> Student[j];    }    //遍历成绩,并将所有成绩相加在一起    for (int i = 0; i <3 ; ++i) {        total+=Student[i];    }    cout <<"Your average grade is "<<total/3<<endl;    return 0;}



1 0