C++ Primer Plus 第六版编程练习——第4章

★★★★★备注★★★★★

使用的编译环境为 Visual Studio 2017   

默认省略了如下内容:                           

     #include "stdafx.h"                           
     #include <iostream>                               

     using namespace std;


1、Write a C++ program that requests and displays information as shown in the following example of output:
    What is your first name? Betty Sue
    What is your last name? Yewe
    What letter grade do you deserve? B
    What is your age? 22
    Name: Yewe, Betty Sue
    Grade: C

    Age: 22

const int SIZE = 20;

struct stu
{
	char first_name[SIZE];
	char last_name[SIZE];
	char grade;
	int age;
};

int main(int argc, const char *argv[])
{
	stu person;
	cout << "What is your first name? ";
	cin.getline(person.first_name, SIZE);
	cout << "What is your last name? ";
	cin.getline(person.last_name, SIZE);
	cout << "What letter grade do you deserve? ";
	cin >> person.grade;
	cout << "What is your age? ";
	cin >> person.age;
	cout << "Name: " << person.last_name << ", " << person.first_name << endl;
	cout << "Grade: " << person.grade << endl;
	cout << "Age: " << person.age << endl;

	return 0;
}

2、Rewrite Listing 4.4, using the C++ string class instead of char arrays.

#include <string>
int main(int argc, const char *argv[])
{
	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;
	cout << " for you, " << name << ".\n";

	return 0;
}

3、Write a program that asks the user to enter his or her first name and then last name, and that then constructs, stores, and displays a third string, consisting of the user’s last name followed by a comma, a space, and first name. Use char arrays and functions from the cstring header file.A sample run could look like this:
    Enter your first name: Flip
    Enter your last name: Fleming

    Here’s the information in a single string: Fleming, Flip

#include <cstring>

const int SIZE = 20;

struct name
{
	char first_name[SIZE];
	char last_name[SIZE];
};

int main(int argc, const char *argv[])
{
	char full_name[2 * SIZE + 1] = {};
	name name = {};
	cout << "Enter your first name: ";
	cin.getline(name.first_name, SIZE);
	cout << "Enter your last name: ";
	cin.getline(name.last_name, SIZE);
	strncpy_s(full_name, name.first_name, strlen(name.first_name));
	strcat_s(full_name, ", ");
	strncat_s(full_name, name.last_name, sizeof(name.last_name));
	cout << "Here’s the information in a single string: " << full_name << endl;

	return 0;
}
4、 Write a program that asks the user to enter his or her first name and then last name, and that then constructs, stores, and displays a third string consisting of the user’s last name followed by a comma, a space, and first name. Use string objects and methods from the string header file.A sample run could look like this:
    Enter your first name: Flip
    Enter your last name: Fleming
    Here’s the information in a single string: Fleming, Flip
#include <string>

int main(int argc, const char *argv[])
{
	string first_name;
	string last_name;
	string full_name;
	cout << "Enter your first name: ";
	getline(cin, first_name);
	cout << "Enter your last name: ";
	getline(cin, last_name);
	full_name = first_name + ", " + last_name;
	cout << "Here’s the information in a single string: " << full_name << endl;
	
	return 0;
}

5、The CandyBar structure contains three members.The first member holds the brand name of a candy bar.The second member holds the weight (which may have a fractional part) of the candy bar, and the third member holds the number of calories (an integer value) in the candy bar.Write a program that declares such a structure and creates a CandyBar variable called snack, initializing its members to "Mocha Munch", 2.3, and 350, respectively.The initialization should be part of the declaration for snack. Finally, the program should display the contents of the snack variable.

const int SIZE = 20;

struct CandyBar
{
	char	brand[SIZE];
	double	weight;
	int		calorie;
};

int main(int argc, const char*argv[])
{
	CandyBar snack = {
		"MochaMunch",
		2.3,
		350,
	};
	cout << snack.brand << endl;
	cout << snack.weight << endl;
	cout << snack.calorie << endl;	

	return 0;
}
6、 The CandyBar structure contains three members, as described in Programming Exercise 5.Write a program that creates an array of three CandyBar structures, initializes them to values of your choice, and then displays the contents of each structure. 
const int SIZE = 20;

struct CandyBar
{
	char	brand[SIZE];
	double	weight;
	int		calorie;
};

void show_info(CandyBar *candy)
{
	cout << "Brand name of the candy bar is: " << candy->brand << endl;
	cout << "Weight of the candy bar is: " << candy->weight << endl;
	cout << "Number of calories in the candy bar is: " << candy->calorie << endl;
}

int main(int argc, const char*argv[])
{
	CandyBar snack[3] = {
		{"MochaMunch", 2.3, 350,},
		{"WhiteRabbit", 1.7, 248,},
		{"Alps", 5.0, 666},
	};
	show_info(&snack[0]);
	show_info(&snack[1]);
	show_info(&snack[2]);

	return 0;
}

7、William Wingate runs a pizza-analysis service. For each pizza, he needs to record the following information:
        The name of the pizza company, which can consist of more than one word
        The diameter of the pizza
        The weight of the pizza

Devise a structure that can hold this information and write a program that uses a structure variable of that type.The program should ask the user to enter each of the preceding items of information, and then the program should display that information. Use cin (or its methods) and cout.

const int SIZE = 30;
struct pizza
{
	char company[SIZE];
	double diameter;
	double weight;
};

void show_info(pizza *piz)
{
	cout << "Company: " << piz->company << endl;
	cout << "Diameter: " << piz->diameter << endl;
	cout << "Weight: " << piz->weight << endl;
	return;
}

int main(int argc, const char *argv[])
{
	pizza piz;
	cout << "Please enter the pizza company: " << endl;
	cin.getline(piz.company, SIZE);
	cout << "Please enter the pizza diameter: " << endl;
	cin >> piz.diameter;
	cout << "Please enter the pizza weight: " << endl;
	cin >> piz.weight;
	show_info(&piz);

	return 0;
}

8、Do Programming Exercise 7 but use new to allocate a structure instead of declaring a structure variable.Also have the program request the pizza diameter before it requests the pizza company name.

const int SIZE = 30;
struct pizza
{
	char company[SIZE];
	double diameter;
	double weight;
};

void show_info(pizza *piz)
{
	cout << "Company: " << piz->company << endl;
	cout << "Diameter: " << piz->diameter << endl;
	cout << "Weight: " << piz->weight << endl;
	return;
}

int main(int argc, const char *argv[])
{
	pizza *piz = new pizza;
	
	cout << "Please enter the pizza diameter: " << endl;
	cin >> piz->diameter;
	getchar();
	cout << "Please enter the pizza company: " << endl;
	cin.getline(piz->company, SIZE);
	cout << "Please enter the pizza weight: " << endl;
	cin >> piz->weight;
	show_info(piz);
	delete piz;

	return 0;
}

9、Do Programming Exercise 6, but instead of declaring an array of three CandyBar structures, use new to allocate the array dynamically.

const int SIZE = 20;

struct CandyBar
{
	char	brand[SIZE];
	double	weight;
	int		calorie;
};

void show_info(CandyBar *candy)
{
	cout << "Brand name of the candy bar is: " << candy->brand << endl;
	cout << "Weight of the candy bar is: " << candy->weight << endl;
	cout << "Number of calories in the candy bar is: " << candy->calorie << endl;
}

int main(int argc, const char*argv[])
{
	CandyBar *snack = new CandyBar[3];
	strcpy_s(snack[0].brand, "MochaMunch");
	snack[0].weight = 2.3;
	snack[0].calorie = 350;
	strcpy_s(snack[1].brand, "WhiteRabbit");
	snack[1].weight = 1.7;
	snack[1].calorie = 248;
	strcpy_s(snack[2].brand, "Alps");
	snack[2].weight = 5.0;
	snack[2].calorie = 666;

	for (int i = 0; i < 3; i++)
	{
		show_info(&snack[i]);
	}

	delete[] snack;
	return 0;
}

10、Write a program that requests the user to enter three times for the 40-yd dash (or 40-meter, if you prefer) and then displays the times and the average. Use an array object to hold the data. (Use a built-in array if array is not available.)

#include <array>
const int COUNT = 3;
int main(int argc, const char *argv[])
{
	array<double, COUNT> score = {};
	cout << "Please enter your first score in 40 meter sprint: " << endl;
	cin>> score[0];
	cout << "Please enter your second score in 40 meter sprint: " << endl;
	cin >> score[1];
	cout << "Please enter your third score in 40 meter sprint: " << endl;
	cin >> score[2];

	double average = (score[0] + score[1] + score[2]) / COUNT;
	cout << "Your final score in 40 meter sprint is: " << average << endl;

	return 0;
}


猜你喜欢

转载自blog.csdn.net/wenfei11471/article/details/80625174