首页 > 编程 > C++ > 正文

C++ Primer Plus 学习笔记 第四章 04编程练习

2019-11-06 06:34:59
字体:
来源:转载
供稿:网友
1.#include<iostream>int main(){    using namespace std;    cout<<"What is your first name? ";    char firstname[20];    cin.getline(firstname,20);    cout<<"What is your last name? ";    char lastname[20];    cin.getline(lastname,20);    char grade;    cout<<"What letter grade do you deserve? ";    cin>>grade;    int age;    cout<<"What is your age? ";    cin>>age;    cout<<"Name: "<<lastname<<", "<<firstname<<endl;    cout<<"Grade: "<<(char)(grade+1)<<endl;//向下调整成绩,如果强制转换类型,输出会变成67    cout<<"Age: "<<age<<endl;    return 0;}2.#include<iostream>#include<string>#include<cstring>int main(){    using namespace std;    //const int ArSize = 20;    string name;//char name[ArSize];    string dessert;//char dessert[ArSize];    cout << "Enter your name:/n";    getline(cin,name);    //cin.getline(name,ArSize);    cout << "Enter your favorite dessert:/n";    getline(cin,dessert);    //cin.getline(dessert,ArSize);    cout <<"I have some delicious "<<dessert;    cout <<"for you, "<<name<<endl;    return 0;}3.#include<iostream>#include<cstring>int main(){    using namespace std;    cout<<"Enter your first name: ";    char firstname[20];    cin>>firstname;    cout<<"Enter your last name: ";    char lastname[20];    cin>>lastname;    char name[40];    char a[]=", ";    strcpy(name,lastname);    strcat(name,a);    strcat(name,firstname);    cout<<"Here's the information in a single string:"<<name<<endl;    return 0;}4.#include<iostream>#include<string>int main(){    using namespace std;    cout<<"Enter your first name: ";    string firstname;    cin>>firstname;    cout<<"Enter your last name: ";    string lastname;    cin>>lastname;    string name;    string a=", ";    name = lastname + a + firstname;    cout<<"Here's the information in a single string:"<<name<<endl;    return 0;}5.#include<iostream>struct CandyBar{    char kind[20];    float weight;    int kaluli;};int main(){    using namespace std;    CandyBar snack =    {        "Mocha Munch",        2.3,        350    };    cout<<"糖块的品牌为:"<<snack.kind<<endl;    cout<<"糖块的重量为:"<<snack.weight<<endl;    cout<<"糖块的卡路里含量为:"<<snack.kaluli<<endl;    return 0;}6.#include<iostream>struct CandyBar{    char kind[20];    float weight;    int kaluli;};int main(){    using namespace std;    CandyBar shuzu[3] =    {        {"Bambi",0.5,3},        {"Bambi",0.5,3},        {"Bambi",0.5,3}    };    cout<<shuzu[0].kind<<" "<<shuzu[0].weight<<" "<<shuzu[0].kaluli<<endl;    cout<<shuzu[1].kind<<" "<<shuzu[1].weight<<" "<<shuzu[1].kaluli<<endl;    cout<<shuzu[2].kind<<" "<<shuzu[2].weight<<" "<<shuzu[2].kaluli<<endl;    return 0;}7.#include<iostream>#include<cstring>#include<string>struct Piza{    char name[20];//公司名称    double zhijing;//直径    float weight;//重量};int main(){    using namespace std;    Piza piza;    cout<<"请输入公司名称";    cin.getline(piza.name,20);    cout<<"请输入披萨的直径";    cin>>piza.zhijing;    cout<<"请输入披萨的重量";    cin>>piza.weight;    cout<<"公司名称:"<<piza.name<<endl;    cout<<"直径:"<<piza.zhijing<<endl;    cout<<"重量:"<<piza.weight<<endl;    return 0;}8.#include<iostream>#include<cstring>#include<string>struct Piza{    char name[20];//公司名称    double zhijing;//直径    float weight;//重量};int main(){    using namespace std;    Piza * piza = new Piza;//使用new为结构体分配内存。    cout<<"请输入披萨的直径";    cin>>piza->zhijing;    cin.get();    /*cin读取数字时,将回车键生成的换行符留在了队列中,再使用cin.getline(),将会认为是空行,使用cin.get()解决。*/    cout<<"请输入公司名称";    cin.getline(piza->name,20);    cout<<"请输入披萨的重量";    cin>>piza->weight;    cout<<"公司名称:"<<piza->name<<endl;    cout<<"直径:"<<piza->zhijing<<endl;    cout<<"重量:"<<piza->weight<<endl;    return 0;}9.#include<iostream>#include<string>#include<cstring>using namespace std;//如果要在main函数外面使用string,把名称空间放在mian函数外面struct CandyBar{    string kind;    float weight;    int kaluli;};int main(){     CandyBar * shuzu = new CandyBar [3];     shuzu[0].kind="Bambi";//如果结构体中kind的类型为char数组,这里会报错const char和char     shuzu->weight=3.3;     shuzu->kaluli=55;    cout<<shuzu[0].kind<<" "<<shuzu[0].weight<<" "<<shuzu[0].kaluli<<endl;    return 0;}10.#include<iostream>int main(){    using namespace std;    //array<double,3> arr;不能用array    double arr[3];    cout<<"请输入第一次40米跑的成绩(s):";    cin>>arr[0];    cout<<"请输入第二次40米跑的成绩(s):";    cin>>arr[1];    cout<<"请输入第三次40米跑的成绩(s):";    cin>>arr[2];    double anunal=(arr[1]+arr[2]+arr[3])/3;    cout<<"平均成绩为:"<<anunal<<endl;    return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选