编程语言
首页 > 编程语言> > C++ Primer Plus(第六版)中文版编程练习答案

C++ Primer Plus(第六版)中文版编程练习答案

作者:互联网

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

1

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

int main()
{
	char firstname[20];
	char lastname[20];
	char grade;
	//enum grade { A,B,C,D,F };
	int age;
	cout << "What's your first name? ";
	cin.getline(firstname, 19);
	cout << "What's your last name? ";
	cin.getline(lastname, 19);
	cout << "What letter grade do you deserve? ";
	cin >> grade;
	cout << "What's your age? ";
	cin >> age;
	cout << "Name: " << firstname << ", " << lastname << endl;
	cout << "Grade: " << char(grade + 1) << endl;//向下调整成绩,即上调一个字母,grade+1
	cout << "Age: " << age << endl;

	system("pause");

	return 0;

}

2

// instr2.cpp -- reading more than one word with getline
#include <iostream>
#include<string>

int main()
{
    using namespace std;
    const int ArSize = 20;

    //char name[ArSize];
    //char dessert[ArSize];

    //cout << "Enter your name:\n";//abcdefghijklmnopqrs
    //cin.getline(name, ArSize);  // reads through newline
    //cout << "Enter your favorite dessert:\n";
    //cin.getline(dessert, ArSize);
    //cout << "I have some delicious " << dessert;
    //cout << " for you, " << name << ".\n";
    // cin.get();

    string name;
    string dessert;
    cout << "Enter your name:\n";
    getline(cin, name);//将一行输入读取到string对象中。
    //istream有类中,有处理double、int等基本数据类型的类方法,没有处理string对象的类方法。
    char arr[ArSize];
    cout << "Enter arr:";
    cin.getline(arr, 20);
    //cin >> name;//只能读取第一个单词,后续单词自动给到下一个读取
    cout << "Enter your favorite dessert:\n";
    getline(cin, dessert);//将一行输入读取到string对象中
    //cin >> dessert;//只能读第一个单词,后续单词自动给到下一个读取
    cout << "I have some delicious " << dessert;
    cout << " for you, " << name << ".\n";
    cout << "arr = " << arr << endl;

    system("pause");
    return 0; 
}

3

// instr2.cpp -- reading more than one word with getline
#include <iostream>
#include<cstring>

int main()
{
    using namespace std;

    char firstname[20];
    char lastname[20];
    char name[40];
    cout << "Enter your first name : ";
    cin.getline(firstname, 19);
    cout << "Enter your last name : ";
    cin.getline(lastname, 19);
    strcpy_s(name, lastname);
    strcat_s(name, ", ");
    strcat_s(name, firstname);
    cout << "Here's the information in a single string : " << name << endl;

    system("pause");
    return 0;
}

4

// instr2.cpp -- reading more than one word with getline
#include <iostream>
#include<string>
using namespace std;

int main()
{
    //char firstname[20];
    //char lastname[20];
    //char name[40];
    //cout << "Enter your first name : ";
    //cin.getline(firstname, 19);
    //cout << "Enter your last name : ";
    //cin.getline(lastname, 19);
    //strcpy_s(name, lastname);
    //strcat_s(name, ", ");
    //strcat_s(name, firstname);
    //cout << "Here's the information in a single string : " << name << endl;

    string firstname;
    string lastname;
    string name;
    cout << "Enter your first name : ";
    getline(cin, firstname);
    cout << "Enter your last name : ";
    getline(cin, lastname);
    /*name = lastname;
    name += ", ";
    name += firstname;*/
    name = lastname + ", " + firstname;
    cout << "Here's the information in a single string : " << name << endl;

    system("pause");
    return 0;
}

5

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

struct CandyBar
{
	string name;
	float weight;
	int calorie;
};

int main()
{
	/*CandyBar snack;
	snack.name = "Mocha Munch";
	snack.weight = 2.3;
	snack.calorie = 350;*/
	CandyBar snack{ "Mocha Munch",2.3,350 };
	cout << "snack's name: " << snack.name << endl << "snack's weight: " << snack.weight << endl << "snack's calorie: " << snack.calorie << endl;

	system("pause");
	return 0;
}

6

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

struct CandyBar
{
	string name;
	float weight;
	int calorie;
};

int main()
{
	/*CandyBar snack;
	snack.name = "Mocha Munch";
	snack.weight = 2.3;
	snack.calorie = 350;*/
	CandyBar snack{ "Mocha Munch",2.3,350 };
	cout << "snack's name: " << snack.name << endl << "snack's weight: " << snack.weight << endl << "snack's calorie: " << snack.calorie << endl;

	/*CandyBar candy[3];
	candy[0] = { "Mocha Munch",2.3,350 };
	candy[1] = { "aer beisi",3.3,650 };
	candy[2] = { "wo wo",6.3,50 };*/
	CandyBar candy[3]{ { "Mocha Munch",2.3,350 },
					   { "aer beisi",3.3,650 },
					   { "wo wo",6.3,50 } };
	cout << "candy[0]: " << endl;
	cout << "candy[0].name: " << candy[0].name << endl << "candy[0].weight: " << candy[0].weight << endl << "candy[0].calorie: " << candy[0].calorie << endl;
	cout << "candy[1]: " << endl;
	cout << "candy[1].name: " << candy[1].name << endl << "candy[1].weight: " << candy[1].weight << endl << "candy[1].calorie: " << candy[1].calorie << endl;
	cout << "candy[2]: " << endl;
	cout << "candy[2].name: " << candy[2].name << endl << "candy[2].weight: " << candy[2].weight << endl << "candy[2].calorie: " << candy[2].calorie << endl;

	system("pause");
	return 0;
}

7

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

struct Pizza
{
	string name;
	float diameter;
	float weight;
};

int main()
{
	Pizza p1;
	cout << "Enter Pizza's name: ";
	getline(cin, p1.name);
	cout << "Enter Pizza's diameter: ";
	cin >> p1.diameter;
	cout << "Enter Pizza's weight: ";
	cin >> p1.weight;
	cout << "Pizza's name: " << p1.name << endl << "Pizza's diameter: " << p1.diameter << endl << "Pizza's weight: " << p1.weight << endl;

	system("pause");
	return 0;
}

8

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

struct Pizza
{
	string name;
	float diameter;
	float weight;
};

int main()
{
	//Pizza* p = new Pizza;
	//cout << "Enter Pizza's name: ";
	//getline(cin, p->name);
	getline(cin, (*p).name);
	//cout << "Enter Pizza's diameter: ";
	//cin >> p->diameter;
	cin >> (*p).diameter;
	//cout << "Enter Pizza's weight: ";
	//cin >> p->weight;
	cin >> (*p).weight;
	//cout << "Pizza's name: " << (*p).name << endl << "Pizza's diameter: " << (*p).diameter << endl << "Pizza's weight: " << (*p).weight << endl;
	//delete p;

	Pizza* p = new Pizza;
	cout << "Enter Pizza's diameter: ";
	cin >> p->diameter;
	//cin >> (*p).diameter;
	cin.get();
	cout << "Enter Pizza's name: ";
	getline(cin, p->name);
	//getline(cin, (*p).name);
	cout << "Enter Pizza's weight: ";
	cin >> p->weight;
	//cin >> (*p).weight;
	cout << "Pizza's name: " << (*p).name << endl << "Pizza's diameter: " << (*p).diameter << endl << "Pizza's weight: " << (*p).weight << endl;
    delete p;

	system("pause");
	return 0;
}

9

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

struct CandyBar
{
	string name;
	float weight;
	int calorie;
};

int main()
{
	///*CandyBar snack;
	//snack.name = "Mocha Munch";
	//snack.weight = 2.3;
	//snack.calorie = 350;*/
	//CandyBar snack{ "Mocha Munch",2.3,350 };
	//cout << "snack's name: " << snack.name << endl << "snack's weight: " << snack.weight << endl << "snack's calorie: " << snack.calorie << endl;

	///*CandyBar candy[3];
	//candy[0] = { "Mocha Munch",2.3,350 };
	//candy[1] = { "aer beisi",3.3,650 };
	//candy[2] = { "wo wo",6.3,50 };*/
	//CandyBar candy[3]{ { "Mocha Munch",2.3,350 },
	//				   { "aer beisi",3.3,650 },
	//				   { "wo wo",6.3,50 } };
	//cout << "candy[0]: " << endl;
	//cout << "candy[0].name: " << candy[0].name << endl << "candy[0].weight: " << candy[0].weight << endl << "candy[0].calorie: " << candy[0].calorie << endl;
	//cout << "candy[1]: " << endl;
	//cout << "candy[1].name: " << candy[1].name << endl << "candy[1].weight: " << candy[1].weight << endl << "candy[1].calorie: " << candy[1].calorie << endl;
	//cout << "candy[2]: " << endl;
	//cout << "candy[2].name: " << candy[2].name << endl << "candy[2].weight: " << candy[2].weight << endl << "candy[2].calorie: " << candy[2].calorie << endl;

	CandyBar* p = new CandyBar[3];
	p[0] = { "Mocha Munch",2.3,350 };
	p[1] = { "aer beisi",3.3,650 };
	p[2] = { "wo wo",6.3,50 };
	cout << "p[0]: " << endl;
	cout << "p[0].name: " << p[0].name << endl << "p[0].weight: " << p[0].weight << endl << "p[0].calorie: " << p[0].calorie << endl;
	cout << "p[1]: " << endl;
	cout << "p[1].name: " << p[1].name << endl << "p[1].weight: " << p[1].weight << endl << "p[1].calorie: " << p[1].calorie << endl;
	cout << "p[2]: " << endl;
	cout << "p[2].name: " << p[2].name << endl << "p[2].weight: " << p[2].weight << endl << "p[2].calorie: " << p[2].calorie << endl;
	delete[]p;

	system("pause");
	return 0;
}

10

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

const int times = 3;

int main()
{
	array<float, times> arr;
	float result = 0;
	/*cout << "Enter the result of the 50-meter run: ";
	cin >> arr[0];
	cout << "Enter the result of the 50-meter run: ";
	cin >> arr[1];
	cout << "Enter the result of the 50-meter run: ";
	cin >> arr[2];*/
	for (int i = 0; i < times; i++)
	{
		cout<< "Enter the result of the 50-meter run: ";
		cin >> arr[i];
		result += arr[i];
	}

	float average = result / times;

	cout << "Times: " << times << endl;
	cout << "Average result: " << average << endl;
	cout << "Total result: " << result << endl;

	system("pause");
	return 0;
}

标签:snack,cout,weight,int,C++,第六版,Plus,include,name
来源: https://blog.csdn.net/yuandenixin/article/details/117047623