题目:5.1
编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间(包括这两个整数)所有整数的和。这里假设先输入较小的整数。例如,用户输入的是2和9,则程序将指出2~9之间所有整数的和为44。
代码:
void Func()
{
int sum = 0;
int min;
int max;
cout << "请输入最小值:";
cin >> min;
cout << "请输入最大值:";
cin >> max;
for (int i = min; i <= max; i++)
{
sum += i;
}
cout << "最终结果为:" << sum << endl;
}
题目:5.2
使用
array
对象(而不是数组)和long double
(而不是long long
)重新编写程序清单5.4,并计算 100! 的值。
代码:
void func()
{
array<long double, 101>arr;
arr[0] = arr[1] = 1;
for (int i = 2; i <= 100; i++)
{
arr[i] = i * arr[i - 1];
}
cout << "100!=" << arr[100] << endl;
}
问题:c++ array模板类的使用
在C++中,STL(Standard Template Library)提供了一个名为
std::array
的模板类,用于定义数组。使用std::array
能够提供更多的功能和安全性,相比原始的数组更加推荐使用。
//声明和初始化std::array:
std::array<int, 5> myArray = {1, 2, 3, 4, 5}; // 声明一个包含5个整数的std::array,并初始化值
std::array<double, 3> prices = {2.5, 3.5, 4.5}; // 声明一个包含3个双精度浮点数的std::array,并初始化值
//访问std::array元素:
int value = myArray[2]; // 访问第3个元素(下标从0开始)
double price = prices.at(1); // 使用at()方法访问第2个元素
//遍历std::array:
for (int i = 0; i < myArray.size(); i++) {
std::cout << myArray[i] << " ";
}
for (double p : prices) {
std::cout << p << " ";
}
使用std::array能够通过成员函数和迭代器提供更多的操作和安全性。
推荐在C++中使用std::array来代替原始数组,
以便更容易地维护和管理数组。
题目:5.3
编写一个要求用户输入数字的程序。每次输入后,程序都将报告到目前为止,所有输入的累计和。当用户输入 0 时,程序结束。
代码:
void Func()
{
int sum = 0;
int n;
cout << "请输入数字:";
cin >> n;
while (n != 0)
{
sum += n;
cout << "当前总和为:" << sum << endl;
cout << "请输入数字:";
cin >> n;
}
}
题目:5.4
Daphne以10%的单利投资了100美元。也就是说,每一年的利润都是投资额的10%,即每年10美元。而Cleo以5%的复利投资了100美元。也就是说,利息是当前存款(包括获得的利息)的5%,Cleo在第一年投资100美元的盈利是5%—得到了105美元。下一年的盈利是105美元的5%—即5.25美元,依此类推。请编写一个程序,计算多少年后,Cleo的投资价值才能超过Daphne的投资价值,并显示此时两个人的投资价值。
void Func()
{
double Daphne = 100;
double Cleo = 100;
int year = 0;
while (Cleo <= Daphne)
{
year++;
Daphne += 10;
Cleo += Cleo * 0.05;
}
cout << "超过的年份为:" << year << endl;
cout << "Daphne=" << Daphne << endl;
cout << "Cleo=" << Cleo << endl;
}
题目:5.5
假设要销售《C++ For Fools》一书。请编写一个程序,输入全年中每个月的销售量(图书数量,而不是销售额)。程序通过循环,使用初始化为月份字符串的
char *
数组(或string
对象数组)逐月进行提示,并将输入的数据储存在一个int数组中。然后,程序计算数组中各元素的总数,并报告这一年的销售情况。
代码:
void Func()
{
string arr[12] = { "一月","二月","三月","四月","五月","六月","七月","八月","九月",
"十月","十一月","十二月" };
int count[12] = { 0 };
int total = 0;
for (int i = 0; i < 12; i++)
{
cout << arr[i] << ":";
cin >> count[i];
total += count[i];
}
cout << "总数为:" << total << endl;
}
题目:5.6
完成编程练习5,但这一次使用一个二维数组来存储输入——3年中每个月的销售量。程序将报告每年销售量以及三年的总销售量。
代码:
void Func()
{
string arr[12] = { "一月","二月","三月","四月","五月","六月","七月","八月","九月",
"十月","十一月","十二月" };
int arr[3][12];
int total = 0;
for (int i = 0; i < 3; i++)
{
cout << "第" << i + 1 << "年数据:" << endl;
for (int j = 0; j < 12; j++)
{
cin >> arr[i][j];
total += arr[i][j];
}
}
cout << "总数为:" << total << endl;
}
题目:5.7
设计一个名为
car
的结构体,用它存储下述有关汽车的信息:生产商(存储在字符数组或string
对象中的字符串)、生产年份(整数)。编写一个程序,向用户询问有多少辆汽车。随后,程序使用new来创建一个由相应数量的car
结构体组成的动态数组。接下来,程序提示用户输入每辆车的生产商(可能由多个单词组成)和年份信息。请注意,这需要特别小心,因为它将交替读取数值和字符串(参见第4章)。最后,程序将显示每个结构的内容。该程序的运行情况如下:How many cars do you wish to catalog? 2 Car #1: Please enter the maker: Hudson Hornet Please enter the year made: 1952 Car #2: Please enter the maker: Kaiser Please enter the year made: 1951 Here is your collection: 1952 Hudson Hornet 1951 Kaiser
代码:
struct car
{
string maker;
int year;
}car;
void Func()
{
cout << "How many cars do you wish to catalog?";
int count;
cin >> count;
struct car* mycar = nullptr;
vector<struct car*>ans;
for (int i = 0; i < count; i++)
{
mycar = new struct car;
cout << "Please enter the maker:";
cin >> mycar->maker;
cout << "Please enter the year made:";
cin >> mycar->year;
ans.push_back(mycar);
}
//输出结果
cout << "Here is your collection:";
for (int i = 0; i < ans.size(); i++)
{
cout << ans.at(i)->year<<" ";
cout << ans.at(i)->maker << " ";
}
}
题目:5.8
编写一个程序,它使用一个
char
数组和循环,每次读取一个单词,直到用户输入done
为止。随后,该程序指出用户输入了多少个单词(不包括done在内)。下面是该程序的运行情况:Enter words (type 'done' to stop): anteater birthday category dumpster envy finagle genometry done for sure
You entered a total of 7 words
您应在程序中包含头文件
cstring
,并使用函数strcmp()
来进行比较测试。
代码:
void Func()
{
char arr[20];
cout << "Enter words (type 'done' to stop):";
int count = 0;
do
{
cin >> arr;
if (strcmp(arr, "done") != 0)
{
count++;
}
} while (strcmp(arr, "done") != 0);
cout << "You entered a total of " << count << " words." << endl;
}
题目:5.9
编写一个满足前一个练习中描述的程序,但使用
string
对象而不是字符数组。请在程序中包含头文件string
,并使用关系运算符来进行 比较测试。
代码:
void Func()
{
int count = 0;
cout << "Enter words (type 'done' to stop):";
string ch;
do
{
cin >> ch;
if (ch != "done")
{
count++;
}
} while (ch != "done");
cout << "You entered a total of " << count << " words" << endl;
}
题目:5.10
编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应行数的星号,其中第一行包括一个星号,第二行包括两个星号,依此类推。每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号前面加上句点。该程序的运 行情况如下:
代码:
void Func()
{
int row;
cout << "请输入行号:";
cin >> row;
for (int i = 0; i < row; i++)
{
//先输入'.'
for (int j = 0; j < row - (i + 1); j++)
{
cout << ".";
}
//输入'*'
for (int j = 0; j < i + 1; j++)
{
cout << "*";
}
cout << endl;
}
}