C++ Primer Plus(第六版)第四章编程练习参考答案

来源:互联网 发布:sql语句添加表字段例子 编辑:程序博客网 时间:2024/05/04 01:20

答案出于菜鸟之手,希望大家参考,提出建议!!!

一、

#include<iostream>using namespace std;int main(){    char fname[20];    char lname[20];    //char grade;    cout<<"What is your first name? ";    cin.getline(fname,20);          //getline(),读取接受换行符。    cout<<"what is your last name? ";    //cin.getline(lname,20);    cin>>lname;     //同上    char grade;    cout<<"What letter grade do you desreve? ";    cin>>grade;    int i=grade;        //得到ASCII值    int j=i+1;    grade=j;    int age;    cout<<"What is your age? ";    cin>>age;    cout<<"Nmae: "<<lname<<", "<<fname<<endl;    cout<<"Grade: "<<grade<<endl;    cout<<"Age: "<<age<<endl;    return 0;}

二、

#include<iostream>#include<string>using namespace std;int main(){    string str1;    string str2;    cout<<"Enter your name: ";    getline(cin,str1);    cin.get();      //获取换行符(有问题)。应该getline()函数已经获取    //cin>>str1;    cout<<"Enter your favorite desster: ";    getline(cin,str2);    //cin>>str2;    cout<<"I have some delicous "<<str2<<" for you,"<<str1<<"."<<endl;    return 0;}

三、

#include<iostream>#include<cstring>using namespace std;int main(){    char ch1[20];    char ch2[20];    cout<<"Enter your first name: ";    cin.getline(ch1,20);    cout<<"Enter your last name: ";    cin.getline(ch2,20);    strcat(ch1,", ");       //strcat()连接函数    strcat(ch1,ch2);    cout<<"Here's the information in a single string: "<<ch1<<endl;    return 0;}

四、

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

五、

#include<iostream>using namespace std;struct CandyBar{    char brand[20];     //品牌    double weight;      //重量    int calorie;        //卡路里};int main(){    CandyBar snack={"Mocha Munch", 2.3, 350};    cout<<"The snack brand is "<<snack.brand<<endl;    cout<<"The snack weight is "<<snack.weight<<endl;    cout<<"The snack calorie is "<<snack.calorie<<endl;    return 0;}

六、

#include<iostream>using namespace std;struct CandyBar{    char brand[20];     //品牌    double weight;      //重量    int calorie;        //卡路里};int main(){    CandyBar snack[3]=    {        {"shaka laka", 1.23, 789},        {"jiejie wa", 2.34, 456},        {"ji wa zi", 3.45, 123}    };    cout<<"The first snack is:"<<endl;    cout<<"The snack brand is "<<snack[0].brand<<endl;    cout<<"The snack weight is "<<snack[0].weight<<endl;    cout<<"The snack calorie is "<<snack[0].calorie<<endl;    cout<<"The second snack is:"<<endl;    cout<<"The snack brand is "<<snack[1].brand<<endl;    cout<<"The snack weight is "<<snack[1].weight<<endl;    cout<<"The snack calorie is "<<snack[1].calorie<<endl;    cout<<"The last snack is:"<<endl;    cout<<"The snack brand is "<<snack[2].brand<<endl;    cout<<"The snack weight is "<<snack[2].weight<<endl;    cout<<"The snack calorie is "<<snack[2].calorie<<endl;    return 0;}

七、

#include<iostream>using namespace std;struct WW{    char name[20];    double dia;     //diameter---直径    double weight;};int main(){    WW pizza;    cout<<"Please input company name: ";    cin.getline(pizza.name,20);    cout<<"Please input pizza diameter (cm) : ";    cin>>pizza.dia;    cout<<"Please input pizza weight (kg): ";    cin>>pizza.weight;    cout<<"Your pizza name is "<<pizza.name<<"."<<endl;    cout<<"diameter is "<<pizza.dia<<" cm."<<endl;    cout<<"weight is "<<pizza.weight<<" kg."<<endl;    return 0;}

八、

#include<iostream>using namespace std;struct WW{    char name[20];    double dia;     //diameter---直径    double weight;};int main(){    WW * pizza=new WW;    cout<<"Please input company name: ";    cin.getline(pizza->name,20);        //method 1    cout<<"Please input pizza diameter (cm) : ";    cin>>(*pizza).dia;      //method 2    cout<<"Please input pizza weight (kg): ";    cin>>pizza->weight;    cout<<"Your pizza name is "<<pizza->name<<"."<<endl;    cout<<"diameter is "<<pizza->dia<<" cm."<<endl;    cout<<"weight is "<<(*pizza).weight<<" kg."<<endl;    delete [] pizza;    return 0;}

九、

#include<iostream>#include<string>using namespace std;struct CandyBar{    string brand;       //品牌    double weight;      //重量    int calorie;        //卡路里};int main(){    CandyBar * ps=new CandyBar[3];    ps[0].brand="shaka";    ps[0].weight=1.23;    ps[0].calorie=123;    ps[1].brand="laka";    ps[1].weight=2.3456;    ps[1].calorie=234;    ps[2].brand="jiejie";    ps[2].weight=7.4523;    ps[2].calorie=35689;    cout<<"The first is:"<<endl;    cout<<"The snack brand is "<<ps[0].brand<<endl;    cout<<"The snack weight is "<<ps[0].weight<<endl;    cout<<"The snack calorie is "<<ps[0].calorie<<endl;    cout<<"The snack is:"<<endl;    cout<<"The snack brand is "<<ps[1].brand<<endl;    cout<<"The snack weight is "<<ps[1].weight<<endl;    cout<<"The snack calorie is "<<ps[1].calorie<<endl;    cout<<"The last is:"<<endl;    cout<<"The snack brand is "<<ps[2].brand<<endl;    cout<<"The snack weight is "<<ps[2].weight<<endl;    cout<<"The snack calorie is "<<ps[2].calorie<<endl;    delete [] ps;    return 0;}

十、

#include<iostream>using namespace std;int main (){    double grade[3];    double ave;    //method one    cout<<"Please enter thrice grade!!!"<<endl;    cout<<"Enter the first grade: ";    cin>>grade[0];    cout<<"Enter the seconf grade: ";    cin>>grade[1];    cout<<"Enter the last grade: ";    cin>>grade[2];    ave=(grade[0]+grade[1]+grade[2])/3;    cout<<"First: "<<grade[0]<<endl;    cout<<"Second: "<<grade[1]<<endl;    cout<<"Last: "<<grade[2]<<endl;    cout<<"The average grade is: "<<ave<<endl;    // method two    for(int i=1; i<=3; ++i)    {        cout<<"输入第"<<i<<"次成绩: ";        cin>>grade[i-1];    }    ave=(grade[0]+grade[1]+grade[2])/3;    cout<<"The average grade is: "<<ave<<endl;    return 0;}
2 0
原创粉丝点击