C++ primer plus 课后习题个人练习

4 Compound Types

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

Tips: using getline() function to  get input of a string.

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

int main()
{
	string name;
	string dessert;

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

	system("pause");
	return 0;
}

7.William Wingate runs a pizza - analysis service.For each pizza, he needs to record
the following information :
n The name of the pizza company, which can consist of more than one word
n The diameter of the pizza
n The weight of the pizza
Devise a structure that can hold this informationand 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.

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

//Creat Pizza struct.
struct Pizza
{
	string ComName;
	float Diameter;
	float Weight;
};
int main()
{
	Pizza p1;

	cout << "Enter name of pizza company: " << endl;
	getline(cin, p1.ComName);
	cout << "Enter the diameter of the pizza: " << endl;
	cin >> p1.Diameter;
	cout << "Enter the weight of the pizza: " << endl;
	cin >> p1.Weight;

	cout << "Pizza company: " << p1.ComName << endl;
	cout << "Pizza diameter: " << p1.Diameter << endl;
	cout << "Pizza weight: " << p1.Weight << endl;

	system("pause");
	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.

Tips: 1. If using new to allocate a structure, we should use * to get the new struct.

        2. To request the pizza diameter before company, we should use cin.get() to deal with \0 first.

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

//Creat Pizza struct.
struct Pizza
{
	string ComName;
	float Diameter;
	float Weight;
};
int main()
{
	Pizza* p1 = new Pizza;

	cout << "Enter the diameter of the pizza: " << endl;
	cin >> p1->Diameter;
	cin.get();

	cout << "Enter name of pizza company: " << endl;
	getline(cin, p1->ComName);

	cout << "Enter the weight of the pizza: " << endl;
	cin >> p1->Weight;

	cout << "Pizza company: " << p1->ComName << endl;
	cout << "Pizza diameter: " << p1->Diameter << endl;
	cout << "Pizza weight: " << p1->Weight << endl;

	system("pause");
	return 0;
}

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

#include <iostream>
using namespace std;
// CandyBar struct
struct CandyBar
{
	string brand;
	float weight;
	int   calorie;
};
int main(void)
{
	const int Size = 3;
	CandyBar* snack = new CandyBar[Size];
	snack[0] = { "Mocha Munch", 5.34, 400 };
	snack[1] = { "coco coc", 2.33, 600 };
	snack[2] = { "school boy", 5.6, 680 };


	cout << "Brand: " << snack[0].brand << "\tweight: " << snack[0].weight
		<< "\tcalorie: " << snack[0].calorie << endl;
	cout << "Brand: " << snack[1].brand << "    \tweight: " << snack[1].weight
		<< "\tcalorie: " << snack[1].calorie << endl;
	cout << "Brand: " << snack[2].brand << "\tweight: " << snack[2].weight
		<< "\tcalorie: " << snack[2].calorie << endl;

	system("pause");
	return 0;
}

5 Loops and Relational Expressions

1. 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, tthe program should report that the sum of all the integers from 2 through 9 is 44.

#include <iostream>
using namespace std;

int main(void)
{
	int num1;
	int num2;
	int sum = 0;

	cout << "Enter first integer: " << endl;
	cin >> num1;
	cout << "Enter second integer: " << endl;
	cin >> num2;

	for (int i = num1; i <= num2; i++)
	{
		sum += i;
	}

	cout << "The sum of all the integers from " << num1 << " to " << num2 << " is " << sum << endl;

	system("pause");
	return 0;
}

2. 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 <iostream>
#include<array>
using namespace std;

int main(void)
{

	const int ArSize = 101;
	array<long double, ArSize>arr;

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

	system("pause");
	return 0;
}

3. 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.

//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.

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

int main(void)
{
	const int ArSize = 20;
	int num1;
	int sum = 0;

	cout << "Enter a number:" << endl;

	while (cin >> num1 && num1)
	{
		sum += num1;
		cout << "The current sum is:" << sum << endl;
		cout << "Enter a number:" << endl;
	}

	system("pause");
	return 0;
}

4. Daphne's interest = 0.10 × original balance

Cleo's interest = 0.05 × current balance

how many years it takes for the value of Cleo's investment to exceed Daphne's.

#include <iostream>
using namespace std;

int main(void)
{
	int Count = 0;
	int Daphne = 100;
	int Cleo = 100;

	while (Cleo <= Daphne)
	{
		Daphne = 100 * 1.1;
		Cleo = Cleo * 1.05;
		Count++;
	}
	
	cout << "It takes " << Count << " years for Cleo to exceed Daphne." << endl;

	system("pause");
	return 0;
}

5. 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 stringsand 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.

#include <iostream>
using namespace std;

int main(void)
{
	int sum = 0;
	int sales[12];

	//string objects
	/*string Months[12] =
	{
		"Jan.", "Feb.", "Mar.", "Apr.", "May.", "Jun.", "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."
	};*/ 

	// char* array
	const char* Months[12] =
	{
		"Jan.", "Feb.", "Mar.", "Apr.", "May.", "Jun.", "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."
	};

	for (int i = 0; i < 12; i++)
	{
		cout << "Enter your sales of " << Months[i] << endl;
		cin >> sales[i];
		cout << Months[i] << ": " << sales[i] << endl; 
		sum += sales[i];
	}

	cout << "Your annual sales: " << sum << endl;

	system("pause");
	return 0;
}

6. 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.

#include <iostream>
using namespace std;

int main(void)
{
	int sales[3][12];
	int sum[3] = {0};

	//string objects
	string Months[12] =
	{
		"Jan.", "Feb.", "Mar.", "Apr.", "May.", "Jun.", "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."
	};

	for (int j = 0; j < 3; j++)
	{
		for (int i = 0; i < 12; i++)
		{
			cout << "Enter your sales of " << Months[i] << endl;
			cin >> sales[j][i];
			cout << Months[i] << ": " << sales[j][i] << endl;
			sum[j] += sales[j][i];
		}
		cout << j + 1 << " annual sales: " << sum[j] << endl;
	}

	cout << "All three years' sales: " << sum[0] + sum[1] + sum[2] << endl;

	system("pause");
	return 0;
}

7. Design a structure called car that holds :its make, and the year it was built.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.Finally, it should display the contents of each structure.

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

struct Car
{
	string CarMake;
	int CraYear;
};

int main(void)
{
	int CarNum; // number of cars

	cout << "How many cars do you wish to catalog?" << endl;
	cin >> CarNum; //get the number of cars.
	cin.get(); //to handle the \0.

	Car* CarCata = new Car[CarNum]; //create a dynamic array of Car structure.

	for (int i = 0; i < CarNum; i++)
	{
		cout << "Car #" << i + 1 << endl;
		cout << "Please enter the make: " << endl;
		getline(cin, CarCata[i].CarMake);
		cout << "Please enter the year made: " << endl;
		cin >> CarCata[i].CraYear;
		cin.get(); //to deal with the \0
	}
	
	cout << "Here is your collection: " << endl;
	for (int i = 0; i < CarNum; i++)
	{
		cout << CarCata[i].CraYear << " " << CarCata[i].CarMake << endl;
	}

	system("pause");
	return 0;
}

8.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.

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


int main(void)
{
	const int Size = 20;
	char word[Size];
	int count = 0;

	cout << "Enter words(to stop, type the word done) :" << endl;
	cin >> word;
	
	while (strcmp(word, "done"))
	{
		count++;
		cin >> word;
	}

	cout << "You entered a total of " << count << " words." << endl;

	system("pause");
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值