C++ Primer Plus 第六版(中文版)第二、三、四章(完美修订版)编程练习答案

//本博主所写的代码仅为阅读者提供参考;

//若有不足之处请提出,博主会尽所能修改;

//附上课后编程练习题目;

//若是对您有用的话请点赞或分享提供给它人;


//--------------------------------------------------------------------------------------------------------------;
在这里插入图片描述
//--------------------------------------------------------------------------------------------------------------;

//2.7 - 1.cpp

#include <iostream>
using namespace std;

int main()
{
    
    
    cout << "My name is C++ Primer Plus." << endl;
    cout << "My address is in library." << endl;

    return 0;
}

//-------------

//2.7 - 2.cpp

#include <iostream>
using namespace std;

int main()
{
    
    
    double long_distance;

    cout << "Enter a distance for long unit: ";
    cin >> long_distance;

    cout << long_distance << " long distance ";
    cout << "is equal to ";
    long_distance *= 220.0;
    cout << long_distance << " yard distance." << endl;

    return 0;
}

//-------------

//2.7 - 3.cpp

#include <iostream>
using namespace std;

void show_mice();
void show_running();

int main()
{
    
    
    show_mice();
    show_mice();
    show_running();
    show_running();

    return 0;
}

void show_mice()
{
    
    
    cout << "Three blind mice" << endl;
    return;
}

void show_running()
{
    
    
    cout << "See how they run" << endl;
    return;
}

//-------------

//2.7 - 4.cpp

#include <iostream>
using namespace std;

int main()
{
    
    
    int age_total_months;

    cout << "Enter your age: ";
    cin >> age_total_months;
    cout << "Your age includes " << age_total_months * 12 << " months." << endl;

    return 0;
}

//-------------

//2.7 - 5.cpp

#include <iostream>
using namespace std;

double temperature(double temp);

int main()
{
    
    
    double celsius;

    cout << "Please enter a Celsius value: ";
    cin >> celsius;
    cout << celsius << " degrees Celsius is ";
    cout << temperature(celsius);
    cout << " degrees Fahrenheit." << endl;

    return 0;
}

double temperature(double temp)
{
    
    
    return 1.8 * temp + 32.0;
}

//-------------

//2.7 - 6.cpp

#include <iostream>
using namespace std;

double transform(double temp);

int main()
{
    
    
    double light_years;

    cout << "Enter the number of light years: ";
    cin >> light_years;
    cout << light_years << " light years = ";
    cout << transform(light_years);
    cout << " astronomical units." << endl;

    return 0;
}

double transform(double temp)
{
    
    
    return temp * 63240.0;
}

//-------------

//2.7 - 7.cpp

#include <iostream>
using namespace std;

void show_time(int hour, int minute);

int main()
{
    
    
    int hour, minute;

    cout << "Enter the number of hours: ";
    cin >> hour;
    cout << "Enter the number of minutes: ";
    cin >> minute;
    show_time(hour, minute);

    return 0;
}

void show_time(int hour, int minute)
{
    
    
    cout << "Time: " << hour << ":" << minute << endl;
    return;
}

//-------------

//3.7 - 1.cpp

#include <iostream>
using namespace std;

int main()
{
    
    
    const int INCH_PER_FEET = 12;
    int height;

    cout << "Please enter your height in inch: ___\b\b\b";
    cin >> height;
    cout << "Your height is " << height / INCH_PER_FEET << " feet and ";
    cout << height % INCH_PER_FEET << " inches." << endl;

    return 0;
}

//-------------

//3.7 - 2.cpp

#include <iostream>
using namespace std;

int main()
{
    
    
    const int INCH_PER_FEET = 12;         //1英尺为12英寸;
    const double POUND_PER_KG = 2.2;      //1千克为2.2磅;
    const double METER_PER_INCH = 0.0254; //1英寸为0.0254米;
    double height_feet, height_inch, weight, meter;

    cout << "Please enter your height in feet and inch: ";
    cin >> height_feet >> height_inch;
    cout << "Please enter your weight in pound: ";
    cin >> weight;

    cout << "Your height is " << height_feet * INCH_PER_FEET + height_inch << " inches." << endl;
    meter = (height_feet * INCH_PER_FEET + height_inch) * METER_PER_INCH;
    cout << "Your BMI is " << (weight / POUND_PER_KG) / (meter * meter) << "." << endl;

    return 0;
}

//-------------

//3.7 - 3.cpp

#include <iostream>
using namespace std;

int main()
{
    
    
    const double MINUTE_PER_DEGREE = 60.0;
    const double SECOND_PER_MINUTE = 60.0;
    double degree, minute, second, total;

    cout << "Enter a latitude in degrees, minutes, and seconds:" << endl;
    cout << "First, enter the degrees: ";
    cin >> degree;
    cout << "Next, enter the minutes of arc: ";
    cin >> minute;
    cout << "Finally, enter the seconds of arc: ";
    cin >> second;

    total = degree + (minute / MINUTE_PER_DEGREE) + (second / (MINUTE_PER_DEGREE * SECOND_PER_MINUTE));
    cout << degree << " degrees, ";
    cout << minute << " minutes, ";
    cout << second << " seconds  = ";
    cout << total << " degrees" << endl;

    return 0;
}

//-------------

//3.7 - 4.cpp

#include <iostream>
using namespace std;

int main()
{
    
    
    const int HOUR_PER_DAY = 24;      //1天有24小时;
    const int MINUTE_PER_HOUR = 60;   //1小时有60分钟;
    const int SECOND_PER_MINUTE = 60; //1分钟有60秒;
    long long second;

    cout << "Enter the number of seconds: ";
    cin >> second;

    cout << second << " seconds = ";
    cout << second / (HOUR_PER_DAY * MINUTE_PER_HOUR * SECOND_PER_MINUTE);
    cout << " days, ";
    cout << second / (SECOND_PER_MINUTE * MINUTE_PER_HOUR) % HOUR_PER_DAY;
    cout << " hours, ";
    cout << second % (SECOND_PER_MINUTE * MINUTE_PER_HOUR) / SECOND_PER_MINUTE;
    cout << " minutes, ";
    cout << second % SECOND_PER_MINUTE;
    cout << " seconds" << endl;

    return 0;
}

//-------------

//3.7 - 5.cpp

#include <iostream>
using namespace std;

int main()
{
    
    
    long long world_population;
    long long china_population;

    cout << "Enter the world's population: ";
    cin >> world_population;
    cout << "Enter the population of the China: ";
    cin >> china_population;

    cout << "The population of the China is ";
    cout << double(china_population) / world_population * 100LL;
    cout << "% of the world population." << endl;

    return 0;
}

//-------------

//3.7 - 6.cpp

#include <iostream>
using namespace std;

int main()
{
    
    
    double kilometer, liter;

    cout << "Please enter your driving distance(km): ";
    cin >> kilometer;
    cout << "Please enter your gas consumption(liter): ";
    cin >> liter;

    cout << "You consume " << liter / kilometer * 100;
    cout << " liter gas for driving per 100 km." << endl;

    return 0;
}

//-------------

//3.7 - 7.cpp

#include <iostream>
using namespace std;

int main()
{
    
    
    const double MILE_PER_KM = 0.6214;     //1公里是0.6214英里;
    const double LITER_PER_GALLON = 3.875; //1加仑是3.875升;
    double kilometer, liter;

    cout << "Please enter your driving distance(km): ";
    cin >> kilometer;
    cout << "Please enter your gas consumption(liter): ";
    cin >> liter;

    cout << "You consume " << liter / kilometer * 100;
    cout << " liter gas for driving per 100 km (European style)." << endl;
    cout << "You drive " << kilometer * MILE_PER_KM / liter * LITER_PER_GALLON;
    cout << " miles for per gallon (American style)." << endl;

    return 0;
}

//-------------

//4.13 - 1.cpp

#include <iostream>
#include <string>
using namespace std;

int main()
{
    
    
    int age;
    char grade;
    string fname, lname;

    cout << "What is your first name? ";
    getline(cin, fname);
    cout << "What is your last name? ";
    getline(cin, lname);
    cout << "What letter grade do you deserve? ";
    cin >> grade;
    cout << "What is your age? ";
    cin >> age;

    cout << "Name: " << lname << ", " << fname << endl;
    cout << "Grade: " << ++grade << endl;
    cout << "Age: " << age << endl;

    return 0;
}

//-------------

//4.13 - 2.cpp

#include <iostream>
#include <string>
using namespace std;

int main()
{
    
    
    string name, dessert;

    cout << "Enter your name:\n";
    getline(cin, name);
    cout << "Enter your favorite dessert:\n";
    getline(cin, dessert);
    cout << "I have some delicious " << dessert;
    cout << " for you, " << name << ".\n";

    return 0;
}

//-------------

//4.13 - 3.cpp

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    
    
    const int ArSize = 20;
    char fname[ArSize];
    char lname[ArSize];

    cout << "Enter your first name: ";
    cin.getline(fname, ArSize);
    cout << "Enter your last name: ";
    cin.getline(lname, ArSize);

    char name[strlen(fname) + strlen(lname) + 4]; //+4为了用于连接逗号和空格以及其中的空字符;
    strcpy(name, lname);
    strcat(name, ", ");
    strcat(name, fname);

    cout << "Here's the information in a single string: ";
    cout << name << endl;

    return 0;
}

//-------------

//4.13 - 4.cpp

#include <iostream>
#include <string>
using namespace std;

int main()
{
    
    
    string fname, lname;

    cout << "Enter your first name: ";
    getline(cin, fname);
    cout << "Enter your last name: ";
    getline(cin, lname);

    lname += ", " + fname;
    cout << "Here's the information in a single string: ";
    cout << lname << endl;

    return 0;
}

//-------------

//4.13 - 5.cpp

#include <iostream>
#include <string>
using namespace std;

struct CandyBar
{
    
    
    string brand;
    double candy_weight;
    int candy_calorie;
};

int main()
{
    
    
    CandyBar snack = {
    
    "Mocha Munch", 2.3, 350};

    cout << "Brand : " << snack.brand << endl;
    cout << "Candy_weight: " << snack.candy_weight << endl;
    cout << "Candy_calorie: " << snack.candy_calorie << endl;

    return 0;
}

//-------------

//4.13 - 6.cpp

#include <iostream>
#include <string>
using namespace std;

struct CandyBar
{
    
    
    string brand;
    double candy_weight;
    int candy_calorie;
};

int main()
{
    
    
    CandyBar snack[3] =
    {
    
    
        {
    
    "Mocha Munch", 2.3, 350},
        {
    
    "Mooncake", 3.5, 369},
        {
    
    "Birthdaycake", 6.8, 460}
    };

    cout << "The first candy:" << endl;
    cout << "Brand: " << snack[0].brand << endl;
    cout << "Candy_weight: " << snack[0].candy_weight << endl;
    cout << "Candy_calorie: " << snack[0].candy_calorie << endl;

    cout << "\nThe second candy:" << endl;
    cout << "Brand: " << snack[1].brand << endl;
    cout << "Candy_weight: " << snack[1].candy_weight << endl;
    cout << "Candy_calorie: " << snack[1].candy_calorie << endl;

    cout << "\nThe third candy:" << endl;
    cout << "Brand: " << snack[2].brand << endl;
    cout << "Candy_weight: " << snack[2].candy_weight << endl;
    cout << "Candy_calorie: " << snack[2].candy_calorie << endl;

    return 0;
}

//-------------

//4.13 - 7.cpp

#include <iostream>
#include <string>
using namespace std;

struct Pizza
{
    
    
    string pizza_company;
    double pizza_diameter;
    double pizza_weight;
};

int main()
{
    
    
    Pizza snack;

    cout << "Please enter the name of the Pizza Company: ";
    getline(cin, snack.pizza_company);
    cout << "Please enter the diameter of the pizza: ";
    cin >> snack.pizza_diameter;
    cout << "Please enter the weight of the pizza: ";
    cin >> snack.pizza_weight;

    cout << "The name of the Pizza Company: " << snack.pizza_company << endl;
    cout << "The diameter of the Pizza Company: " << snack.pizza_diameter << endl;
    cout << "The weight of the Pizza Company: " << snack.pizza_weight << endl;

    return 0;
}

//-------------

//4.13 - 8.cpp

#include <iostream>
#include <string>
using namespace std;

struct Pizza
{
    
    
    string pizza_company;
    double pizza_diameter;
    double pizza_weight;
};

int main()
{
    
    
    Pizza *snack = new Pizza;

    cout << "Please enter the diameter of the pizza: ";
    (cin >> snack->pizza_diameter).get();
    cout << "Please enter the name of the Pizza Company: ";
    getline(cin, snack->pizza_company);
    cout << "Please enter the weight of the pizza: ";
    cin >> snack->pizza_weight;

    cout << "The name of the Pizza Company: " << snack->pizza_company << endl;
    cout << "The diameter of the Pizza Company: " << snack->pizza_diameter << endl;
    cout << "The weight of the Pizza Company: " << snack->pizza_weight << endl;
    delete snack;

    return 0;
}

//-------------

//4.13 - 9.cpp

#include <iostream>
#include <string>
using namespace std;

struct CandyBar
{
    
    
    string brand;
    double candy_weight;
    int candy_calorie;
};

int main()
{
    
    
    CandyBar *snack = new CandyBar[3];

    snack[0] = {
    
    "Mocha Munch", 2.3, 350};
    snack[1] = {
    
    "Mooncake", 3.5, 369};
    snack[2] = {
    
    "Birthdaycake", 6.8, 460};

    cout << "The first candy:" << endl;
    cout << "Brand: " << snack[0].brand << endl;
    cout << "Candy_weight: " << snack[0].candy_weight << endl;
    cout << "Candy_calorie: " << snack[0].candy_calorie << endl;

    cout << "\nThe second candy:" << endl;
    cout << "Brand: " << snack[1].brand << endl;
    cout << "Candy_weight: " << snack[1].candy_weight << endl;
    cout << "Candy_calorie: " << snack[1].candy_calorie << endl;

    cout << "\nThe third candy:" << endl;
    cout << "Brand: " << snack[2].brand << endl;
    cout << "Candy_weight: " << snack[2].candy_weight << endl;
    cout << "Candy_calorie: " << snack[2].candy_calorie << endl;
    delete[] snack;

    return 0;
}

//-------------

//4.13 - 10.cpp

#include <iostream>
#include <array>
using namespace std;

int main()
{
    
    
    array<double, 3> scores;

    cout << "Please enter your the results of the first 40 meter race: ";
    cin >> scores[0];
    cout << "Please enter your the results of the second 40 meter race: ";
    cin >> scores[1];
    cout << "Please enter your the results of the third 40 meter race: ";
    cin >> scores[2];

    cout << "You ran three times in total." << endl;
    cout << "Your average score is ";
    cout << (scores[0] + scores[1] + scores[2]) / 3.0;
    cout << "." << endl;

    return 0;
}

//-------------

//------------------------------------------2020年9月20日 ----------------------------------------------;

猜你喜欢

转载自blog.csdn.net/m0_46181359/article/details/108692023
今日推荐