c++ primer plus 第六版编程练习答案第五章

该章节第二题程序没问题,但不懂为什么

5.1

#include<stdafx.h>
#include <iostream>
using namespace std;

int main()

{
	int max, min;
	long long sum=0;
	cout << "enter the max number: ";
	cin >> max;
	cout << "enter the min number; ";
	cin >> min;
	for (int i = max ; i >= min; i--)
		sum = i + sum;
    cout << "sum of the integer between max and min is: "<<sum<<endl;
	return 0;
}
注意:for循环括号中的最后一条语句是在执行了循环体之后执行的

5.2 

#include<stdafx.h>
#include <iostream>
#include<array>
const int Arsize = 100;
using namespace std;

int main()

{
	array<long double, Arsize+1> factorials;
	factorials[0] = factorials[1] = 1;
	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;
	//cout << "size of long double :" << sizeof(long double) << endl;
	//cout << "size of long long:" << sizeof(long long) << endl;
	return 0;
}

问题1:当定义array的时候用array<long double, Arsize> factorials;会出现array subscript out of range 的错误;若用   array<long double, Arsize+1> factorials;则可以正确显示结果;:

问题2:若将定义array行改为:array<long long, Arsize+1> factorials;即用long long类型的数据,计算出的阶乘结果出现错误,用sizeof(long long)和sizeof(long double)查看两种数据类型所占的字节数都显示的是8位,那么也就是说不是long long数据类型长度不够

5.3

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

using namespace std;

int main()

{
	int num;
	long long sum=0;
	cout << "enter a number:";
	cin >> num;
	while (num != 0)//不能用(num=cin.get)!=0作为测试条件,此时会将数字看做字符,将其ASCII码存入num,且会将输入完成的换行符作为下一个输入
	{
		sum = sum + num;
		cout << "now the sum of the number you enter is:" << sum << endl;
		cout << "enter next number: ";
		cin >> num;

	}
	return 0;
}

5.4

#include<stdafx.h>
#include <iostream>
const int principal = 100;
const int D_intter_per_ye = 10;//Daphne每年利息都相同
const double C_interest = 0.05;//Cleo的利率,最好将数据类型定义为相同的,否则可能造成结果错误
using namespace std;

int main()

{
	double Daphne=principal;
	double Cleo = principal;
	int year = 0;
	while (Daphne>=Cleo)
	{
		year++;
		Daphne =D_intter_per_ye+ Daphne;//直接用加上每年的利率,不用乘,运算速度更快
		Cleo = Cleo * C_interest + Cleo;

	}
	cout << year << " years later Cleo's deposit lager than Daphne's\n"
		<< "now,Cloe's deposit is: " << Cleo << endl
		<< "now,Daphne's deposit is: " << Daphne << endl;
	return 0;
}

5.5

#include<stdafx.h>
#include <iostream>
const int monthes = 12;
using namespace std;

int main()

{
	const char *month[monthes] =
	{
		"Jan.","Feb.","Mar.","Apr.","May.","Jun.",
		"Jul.","Aug.","Sept.","Oct.","Nov.","Dec"

	};
	int sales[monthes],sum=0;
	for (int i = 0; i < monthes; i++)
	{
		cout << "enter the sales of " << month[i]<<": ";
		cin >> sales[i];
		sum = sum + sales[i];
	}
	cout << "Total sales this year is: " << sum;
	return 0;
}

5.6

#include<stdafx.h>
#include <iostream>
const int monthes = 12;
const int years = 3;
using namespace std;

int main()

{
	const char *month[monthes] =
	{
		"Jan.","Feb.","Mar.","Apr.","May.","Jun.",
		"Jul.","Aug.","Sept.","Oct.","Nov.","Dec"

	};
	int sales[years][monthes];
	int sum[years] = {0};
	int total = 0;
	for (int i = 0; i < years; i++)
	{
		cout << "enter No." << i + 1 << " year's sales of <C++ For Fools> each month\n";
		for (int j = 0; j < monthes; j++)
		{
			cout << month[j] << ": ";
			cin >> sales[i][j];
			sum[i] = sum[i] + sales[i][j];
			total = total + sales[i][j];
		}
		cout << "No." << i + 1 << "year's total sales is: " << sum[i]<<endl;
	}
	cout << " total sales of three years is: " << total;
	return 0;
}

5.7

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

using namespace std;
struct car
{
	string manufacturer;
	int  prod_year;
};
int main()

{
	int number;
	cout << "How many cars do you wish to catalig? ";
	cin >> number;
	cin.get();//用于吸收输入数字后的换行符,否则将会将其认为下一个字符的输入
	car *c_message= new car[number];//用new创建一个包含number个元素的car结构
	for (int i = 0; i < number; i++)
	{
		cout << "Car #" << i + 1 << ":\n";
		cout << "Please enter the make: ";
		getline(cin,(c_message+i)->manufacturer);
		cout << "Please enter the year made: ";
		cin >> (c_message+i)->prod_year;
		cin.get();//用于吸收输入数字后的换行符,否则将会将其认为下一个汽车的生产商名称
	}
	cout << "Here is your collection:\n";
	for (int i = 0; i < number; i++)
	{
		cout << (c_message + i)->prod_year << " " << (c_message)->manufacturer<<endl;
	}
	return 0;
}

5.8

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

using namespace std;

int main()

{
	char words[20];//用于存放每次输入的单词,其值为最后一次输入的单词
	int w_coun=0;
	cout << "Enter words(to stop,type the word done):\n";
	cin >> words;//输入的是单词,不需识别空格等,用cin就可以
	while (strcmp(words, "done"))
	{
		w_coun++;
		cin >> words;
	}
	cout << "You entered a total of " << w_coun << " words";
	return 0;
}

5.9

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

int main()
{
	using namespace std;
	string word;
    int w_coun = 0;
	cout << "Enter words(to stop,type the word done):\n";
	cin >> word;//输入的是单词,不需识别空格等,用cin就可以
	while (word!="done")
	{
			w_coun++;
			cin >> word;
	}
	cout << "You entered a total of " << w_coun << " words";
	return 0;
}

5.10

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

int main()
{
	using namespace std;
	int row;
	cout << "Enter number of rows: ";
	cin >> row;
	for (int i = 1; i <= row; i++)
	{
		for (int j = 1; j <= row - i; j++)
			cout << ".";
		for (int n = 1; n <= i; n++)
			cout << "*";
		cout << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Schlangemm/article/details/83307236