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

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

使用的编译环境为 Visual Studio 2017   

默认省略了如下内容:                           

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

     using namespace std;

1、C++11 新增加的 for 循环写法:

int main(int argc, const char *argv[])
{
	double prices[5] = { 4.99, 10.99, 6.87, 7.99, 8.49 };
	for (double x : prices)
		cout << x << std::endl;
	return 0;
}

2、Write a program that requests the user to enter two integers.The program should then calculate and report the sum of all the integers between and including the two integers.At this point, assume that the smaller integer is entered first. For example, if the user enters 2 and 9, the program should report that the sum of all the integers from 2 through 9 is 44.

int main(int argc, const char *argv[])
{
	int min;
	int max;
	int sum = 0;
	cout << "Please enter the minor number: ";
	cin >> min;
	cout << "Please enter the major number: ";
	cin >> max;
	for (int i = min; i < max + 1; i++)
		sum += i;
	cout << "The sum from " << min << " to " << max << " is: " << sum << endl;

	return 0;
}

3、Redo Listing 5.4 using a type array object instead of a built-in array and type long double instead of long long. Find the value of 100!

#include <array>
const int ArSize = 101; // example of external declaration
int main()
{
	array<long double, ArSize> factorials = {};
	factorials[1] = factorials[0] = 1LL;

	for (int i = 2; i < ArSize; i++)
		factorials[i] = i * factorials[i - 1];
		
	for (int i = 0; i < ArSize; i++)
		cout << i << "! = " << factorials[i] << endl;
	return 0;
}

4、Write a program that asks the user to type in numbers.After each entry, the program should report the cumulative sum of the entries to date.The program should terminate when the user enters 0.

#if 0
int main(int argc, const char *argv[])
{
	int num;
	int sum = 0;
enter:
	cout << "Please enter an integer: " << endl;
	cin >> num;
	if (num == 0)
		goto exit;
	cout << "The number you enter is: " << num << endl;
	sum += num;
	cout << "So far, the sum is: " << sum << endl;
	goto enter;
exit:

	return 0;
}
#else
int main(int argc, const char *argv[])
{
	int num;
	int sum = 0;

	cout << "Please enter an integer: " << endl;
	while ((cin >> num) && num != 0)
	{
		sum += num;
		cout << "So far, the sum is: " << sum << endl;
		cout << "Please enter an integer: " << endl;
	}
	return 0;
}
#endif
5、 Daphne invests $100 at 10% simple interest.That is, every year, the investment earns 10% of the original investment, or $10 each and every year:
        interest = 0.10
× original balance
At the same time, Cleo invests $100 at 5% compound interest.That is, interest is 5% of the current balance, including previous additions of interest:
        interest = 0.05
× current balance

Cleo earns 5% of $100 the first year, giving her $105.The next year she earns 5% of $105, or $5.25, and so on.Write a program that finds how many years it takes for the value of Cleo’s investment to exceed the value of Daphne’s investment and then displays the value of both investments at that time.

int main(int argc, const char *argv[])
{
	int year = 0;	
	double Daphne = 100.0;
	double Cleo = 100.0;
	for (; Cleo <= Daphne; year++)
	{
		Daphne += 0.1 * 100;
		Cleo *= 1.05;
	}
	cout << year << " years later, the value of Cleo’s investment to exceed Daphne’s" << endl;
	cout << "The value of Cleo’s investment is: " << Cleo << endl;
	cout << "The value of Daphne’s investment is: " << Daphne << endl;

	return 0;
}

6、You sell the book C++ for Fools.Write a program that has you enter a year’s worth of monthly sales (in terms of number of books, not of money).The program should use a loop to prompt you by month, using an array of char * (or an array of string objects, if you prefer) initialized to the month strings and storing the input data in an array of int.Then, the program should find the sum of the array contents and report the total sales for the year.

const int MONTH = 12;
const char *months[MONTH] = {
	"Jan", "Feb", "Mar", "Apr",
	"May", "Jun", "Jul", "Aug",
	"Sep", "Oct", "Nov", "Dec"};
int main(int argc, const char *argv[])
{
	int sales[MONTH] = {};
	int sum = 0;
	for (int i = 0; i < MONTH; i++)
	{
		cout << "Please enter the sales of <<C++ For Fools>> in " << months[i] << ":" << endl;
		cin >> sales[i];
		sum += sales[i];
	}
	cout << "The total sales of <<C++ For Fools>> in this year is: " << sum << endl;

	return 0;
}

7、Do Programming Exercise 5 but use a two-dimensional array to store input for 3 years of monthly sales. Report the total sales for each individual year and for the combined years.

const int MONTH = 12;
const char *months[MONTH] = {
	"Jan", "Feb", "Mar", "Apr",
	"May", "Jun", "Jul", "Aug",
	"Sep", "Oct", "Nov", "Dec" 
};
const char *years[3] = {
	"first year",
	"second year",
	"third year" 
};
int main(int argc, const char *argv[])
{
	int sales[3][MONTH] = {};
	int sum = 0;
	int year_sale[3] = {};
	for (int i = 0; i < 3; i++)
	{
		int temp = 0;
		cout << "Please enter the sales of <<C++ For Fools>> in " << years[i] << ":" << endl;
		for (int j = 0; j < MONTH; j++)
		{
			cout << "Please enter the sales in " << months[j] << ":" << endl;
			cin >> sales[i][j];
			temp += sales[i][j];
		}
		year_sale[i] = temp;;
		sum += year_sale[i];
	}
	for (int i=0; i<3; i++)
	{
		cout << "The sales of <<C++ For Fools>> in " << years[i] << " is: " << year_sale[i] << endl;
	}
	cout << "The total sales in three years is: " << sum << endl;

	return 0;
}
8、 Design a structure called car that holds the following information about an automobile: its make, as a string in a character array or in a string object, and the year it was built, as an integer.Write a program that asks the user how many cars to catalog.The program should then use new to create a dynamic array of that many car  structures. Next, it should prompt the user to input the make (which might consist of more than one word) and year information for each structure. Note that this requires some care because it alternates reading strings with numeric data (see  Chapter 4). Finally, it should display the contents of each structure.A sample run should look something like the following:
        How many cars do you wish to catalog? 2
        Car #1:
        Please enter the make:
Hudson Hornet
        Please enter the year made: 1952
        Car #2:
        Please enter the make:
Kaiser
        Please enter the year made: 1951
        Here is your collection:
        1952 Hudson Hornet

        1951 Kaiser

#include <string>
struct car
{
	string name;
	int year;
};

int main(int argc, const char *argv[])
{
	int num;
	cout << "How many cars do you wish to catalog?" << endl;
	(cin >> num).get();
	car *ps = new car[num];
	for (int i = 0; i < num; i++)
	{
		cout << "Car #" << i + 1 << ":" << endl;
		cout << "Please enter the make: ";
		getline(cin, ps[i].name);
		cout << "Please enter the year made: ";
		(cin >> ps[i].year).get();
	}
	cout << "Here is your collection:" << endl;
	for (int i = 0; i < num; i++)
		cout << ps[i].year << " " << ps[i].name << endl;
	delete[] ps;

	return 0;
}

9、Write a program that uses an array of char and a loop to read one word at a time until the word done is entered.The program should then report the number of words entered (not counting done).A sample run could look like this:
        Enter words (to stop, type the word done):
        anteater birthday category dumpster
        envy finagle geometry done for sure
        You entered a total of 7 words.
You should include the cstring header file and use the strcmp() function to make the comparison test.

#include <cstring>
int main(int argc, const char *argv[])
{
	char word[20];
	int sum = 0;
	cout << "Enter words (to stop, type the word done):" << endl;
	cin >> word;
	while (strcmp(word, "done"))
	{
		sum++;
		cin >> word;
	}
	cout << "You enter a total of " << sum << " words." << endl;

	return 0;
}

10、Write a program that matches the description of the program in Programming Exercise 8, but use a string class object instead of an array. Include the string header file and use a relational operator to make the comparison test.

#include <string>
int main(int argc, const char *argv[])
{
	string word;
	int sum = 0;
	cout << "Enter words (to stop, type the word done):" << endl;
	cin >> word;
	while (word != "done")
	{
		sum++;
		cin >> word;
	}
	cout << "You enter a total of " << sum << " words." << endl;

	return 0;
}

10、Write a program using nested loops that asks the user to enter a value for the number of rows to display. It should then display that many rows of asterisks, with one asterisk in the first row, two in the second row, and so on. For each row, the asterisks are preceded by the number of periods needed to make all the rows display a total number of characters equal to the number of rows.A sample run would look like this:

        Enter number of rows: 5
        ....*

        ...**
        ..***
        .****

        *****

int main(int argc, const char *argv[])
{
	int num = 0;
	cout << "Enter number of rows: ";
	cin >> num;
	for (int i = 0; i < num; i++)
	{
		for (int j = num - i; j > 1; j--)
			cout << ".";
		for (int k = 0; k <= i; ++k)
			cout << "*";
		cout << endl;
	}

	return 0;
}


猜你喜欢

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