C++ Prime Plus 第六版 第四章 複合類型 編程練習 參考答案 詳細版

以下答案本人在linux vscode中均已親自測試編譯通過,完美運行.

4.1

#include<iostream>
using namespace std;
int main()
{
    const int size = 30;
    char name1[size]={0}; 
    char name2[size]={0};
    char grade;
    int Age;
    cout << "what is your first name ? ";
    cin.getline(name1,size);
    cout << "what is your last name ? ";
    cin.getline(name2,size);
    cout << "what letter grade do you deserve ? ";
    cin >> grade;
    cout << "what is your Age ? ";
    cin >> Age;
    cout << "name:" << name1 << ',' << name2 << endl;
    cout << "grade:" << grade << endl;
    cout << "Age:" << Age << endl;
}

4.2

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

4.3

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    const int size = 30;
    char name1[size]={0}; 
    char name2[size]={0};
    cout << "what is your first name ? ";
    cin >> name1;
    cout << "what is your last name ? ";
    cin >> name2;
    cout << "here's the information in a single:" << name1 << ", " << name2 << endl;
}

4.4

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string name1, name2;
    cout << "what is your first name ? ";
    cin >> name1;
    cout << "what is your last name ? ";
    cin >> name2;
    cout << "here's the information in a single:" << name1 << ", " << name2 << endl;
}

4.5

#include<iostream>
#include<string>
using namespace std;
struct CandyBar
{
    string name;   //品牌
    double weight; //重量
    int calorie;   //卡路里
};
int main()
{
    CandyBar snack{"Mocha Munch",2.3,350};
    cout << "name:" << snack.name << endl;
    cout << "weight:" << snack.weight << endl;
    cout << "calorice:" << snack.calorie << endl;
}

4.6

#include<iostream>
#include<string>
using namespace std;
struct CandyBar
{
    string name;   //品牌
    double weight; //重量
    int calorie;   //卡路里
};
int main()
{
    int i;
    CandyBar snack[3]{{"Mocha Munch",2.3,350},{"Wujia sweet",2.8,280},{"lijia beautiful",3.0,190}};
    for(i=0;i<3;i++)
    {   
        cout << "name:" << snack[i].name << endl;
        cout << "weight:" << snack[i].weight << endl;
        cout << "calorice:" << snack[i].calorie << endl;
    }
}

4.7

#include<iostream>
#include<string>
using namespace std;
struct CandyBar
{
    string name;     //披薩餅公司名稱
    double diameter; //披薩餅直徑
    double weight;   //披薩餅重量
};
int main()
{
    CandyBar pizza;
    cout << "Please enter the brand of pizza company:";
    getline(cin,pizza.name);
    cout << "Please enter the diameter of the pizza:";
    cin >> pizza.diameter;
    cout << "Please enter the pizza weight:";
    cin >> pizza.weight;
    cout << "the brand of pizza company:" << pizza.name << endl;
    cout << "the diameter of the pizza:" << pizza.diameter << endl;
    cout << "the pizza weight:" << pizza.weight << endl;
}

4.8

#include<iostream>
#include<string>
using namespace std;
struct CandyBar
{
    string name;     //披薩餅公司名稱
    double diameter; //披薩餅直徑
    double weight;   //披薩餅重量
};
int main()
{
    CandyBar * pizza = new CandyBar;
    cout << "Please enter the diameter of the pizza:";
    cin >> pizza->diameter;
    cout << "Please enter the brand of pizza company:";
    cin.get();   //由於上一次輸入結束後,錄入一個換行符,導致下條語句判斷爲輸入結束,直接輸出結果,本條語句用來接收那個換行符
    getline(cin,pizza->name);
    cout << "Please enter the pizza weight:";
    cin >> pizza->weight;
    cout << "the brand of pizza company:" << pizza->name << endl;
    cout << "the diameter of the pizza:" << pizza->diameter << endl;
    cout << "the pizza weight:" << pizza->weight << endl;
}

4.9

#include<iostream>
#include<string>
using namespace std;
struct CandyBar
{
    string name;   //品牌
    double weight; //重量
    int calorie;   //卡路里
};
int main()
{
    int i;
    CandyBar * snack = new CandyBar [3]{{"Mocha Munch",2.3,350},{"Wujia sweet",2.8,280},{"lijia beautiful",3.0,190}};
    for(i=0;i<3;i++)
    {   
        cout << "name:" << snack[i].name << endl;
        cout << "weight:" << snack[i].weight << endl;
        cout << "calorice:" << snack[i].calorie << endl;
    }
    delete [] snack;
}

4.10

#include<iostream>
#include<array>
using namespace std;
int main()
{
    int i;
    double ave = 0.0,sum = 0.0;
    array<double,3> score;
    for(i=0;i<3;i++)
    {   
        cout << "Please enter the result of the 40-meter race:";
        cin >> score[i];
        sum += score[i];
    }
    ave = sum / 3;
    for(i=0;i<3;i++)   
        cout << "第 " << i+1 <<" 次40米跑成績:" << score[i] << endl;
    cout << i <<" 次40米跑的平均成績是:" << ave << endl;
}

 

發佈了15 篇原創文章 · 獲贊 1 · 訪問量 646
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章