C++ primer plus(中文版) 第五章编程练习题
2017-12-11 19:34
555 查看
编写一个打印1,2,4,8,16,32,64的
编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间(包括这两个整数)所有整数的和。这里假设先输入较小的整数。例如,如果用户输入的是2和9,则程序将指出2~9之间所有整数的和为44.
使用
编写一个要求用户输入数字的程序。每次输入后,程序豆浆报告到目前为止,所有输入的累计和。当用户输入
Daphne以10%的单利投资了100美元。也就是说,每一年的利润都是投资额的10%,即每年10美元:
利息 =0.10 x 原始存款
而Cleo以5%的复利投资了100美元。也就是说,利息是当前存款(包括获得的利息)的5%:
利息 =0.05 x 当前存款
而Cleo在第一年投资100美元的盈利是5%——得到了105美元。下一年的盈利是105美元的5%——即5.25美元,以此类推。请编写一个程序,计算多少年后,Cleo的投资价值才能超过Daphne的投资价值,并显示此时两个人的投资价值。
假设要销售《C++ For Fools》一书。请编写一个程序,输入全年中每个月的销售量(图书数量,而不是销售额)。程序通过循环,使用初始化为月份字符串的char*数组(或string对象数组)逐月进行提示,并将输入的数据存储在一个int数组中。然后,程序计算数组中各元素的总数,并报告这一年的销售情况。
完成以上的练习,但这一次使用一个二维数组来存储输入—–3年中每个月的销售量。程序将报告每年的销售量以及三年的总销售量。
设计一个名为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
编写一个程序,它使用一个char数组和循环来每次读取一个单词,直到用户输入done为止。随后,该程序指出用户输入了多少单词(不包括done在内)。下面是该程序的运行情况:
您应在程序中包含头文件
编写一个满足前一个满足前一个练习中描述的程序,但使用
编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应行数的星号,其中第一行包括一个星号,第二行包括两个星号,依此类推。每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号前面加上句点。该程序的运行情况如下:
for循环,每轮循环都将计数变量的值乘以2。
#include<iostream> int main() { using namespace std; for (int x = 1; x <= 64; x = x * 2) cout << x << endl; cin.get(); return 0; }
编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间(包括这两个整数)所有整数的和。这里假设先输入较小的整数。例如,如果用户输入的是2和9,则程序将指出2~9之间所有整数的和为44.
#include<iostream> int main() { using namespace std; int x, y, max, min, sum=0; cout << "Please input first number:"; (cin >> x).get(); cout << "Please input second number:"; (cin >> y).get(); if (x > y) max = x, min = y; else max = y, min = x; for (min; min <= max; min++) sum += min; cout << "The sum is " << sum; cin.get(); return 0; }
使用
array对象(而不是数组)和l
ong double(而不是
long long)重新编写程序清单5.4,并计算
100!的值。
#include<iostream> #include<array> const int Arsize = 101; int main() { using namespace std; array<long double, Arsize> sum; sum[0] = sum[1] = 1; for (int i = 2; i < Arsize; i++) sum[i] = sum[i - 1] * i; for (int j = 0; j < Arsize; j++) cout << j << "! = " << sum[j] << endl; cin.get(); return 0; }
编写一个要求用户输入数字的程序。每次输入后,程序豆浆报告到目前为止,所有输入的累计和。当用户输入
0时,程序结束。
#include<iostream> int main() { using namespace std; int sum = 0, i, j = 2; cout << "Please input 1 number:"; (cin >> i).get(); sum = i; while (i != 0) { cout << "Please input " << j << " number:"; (cin >> i).get(); ++j; sum += i; } cout << "The total number is:" << sum; cin.get(); return 0; }
Daphne以10%的单利投资了100美元。也就是说,每一年的利润都是投资额的10%,即每年10美元:
利息 =0.10 x 原始存款
而Cleo以5%的复利投资了100美元。也就是说,利息是当前存款(包括获得的利息)的5%:
利息 =0.05 x 当前存款
而Cleo在第一年投资100美元的盈利是5%——得到了105美元。下一年的盈利是105美元的5%——即5.25美元,以此类推。请编写一个程序,计算多少年后,Cleo的投资价值才能超过Daphne的投资价值,并显示此时两个人的投资价值。
#include<iostream> int main() { using namespace std; double daphne = 100, cleo = 100; int i = 0; while (cleo <= daphne) { daphne = daphne + 100 * 0.1; cleo = cleo*(1.05); ++i; } cout << i << " years ago:\nDaphne:$" << daphne << endl << "Cleo:$" << cleo; cin.get(); return 0; }
假设要销售《C++ For Fools》一书。请编写一个程序,输入全年中每个月的销售量(图书数量,而不是销售额)。程序通过循环,使用初始化为月份字符串的char*数组(或string对象数组)逐月进行提示,并将输入的数据存储在一个int数组中。然后,程序计算数组中各元素的总数,并报告这一年的销售情况。
#include<iostream> #include<string> int main() { using namespace std; string month[13] = { "Jan.","Feb.","Mar.","Apr.","May.","June","July","Aug","Step","Oct","Nov.","Dec" }; int sale[13]; int i = 1, sum = 0; while (i!=13) { cout << "Please input " << month[i - 1] << ":"; (cin >> sale[i - 1]).get(); sum += sale[i - 1]; ++i; } cout << "The total sale:" << sum; cin.get(); return 0; }
完成以上的练习,但这一次使用一个二维数组来存储输入—–3年中每个月的销售量。程序将报告每年的销售量以及三年的总销售量。
略
设计一个名为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
#include<iostream> #include<string> using namespace std; struct car { string maker; int year; }; int main() { int size,i=0,j=0; cout << "How many cars do you wish to catalog?"; (cin >> size).get(); car* calalog = new car[size]; while (i < size) { cout << "Car " << i + 1 << "#:\n"; cout << "Please enter the maker:"; getline(cin, calalog[i].maker); cout << "Please enter the year made:"; (cin >> calalog[i].year).get(); i++; } cout << "Here is your collection:\n"; while (j < size) { cout << calalog[j].year << " " << calalog[j].maker << endl; j++; } delete[] calalog; cin.get(); return 0; }
编写一个程序,它使用一个char数组和循环来每次读取一个单词,直到用户输入done为止。随后,该程序指出用户输入了多少单词(不包括done在内)。下面是该程序的运行情况:
Enter words(to stop,type the word done): anteater birthday category dumpster envy finagle geometry done for sure Your entered a total of 7 words.
您应在程序中包含头文件
cstring,并使用函数
strcpy()来进行比较。
#include<iostream> #include<cstring> int main() { using namespace std; char word[80][80]; int i = 0; cout << "Enter words (to stop,type the word done):\n"; cin >> word[0]; while (strcmp(word[i],"done") != 0) { i++; cin >> word[i]; } cout << "You enter a total of " << i << " words."; cin.get(); return 0; }
编写一个满足前一个满足前一个练习中描述的程序,但使用
string对象而不是字符数组。请在程序中包含文件
string,并使用关系运算符来进行比较测试。
#include<iostream> #include<string> int main() { using namespace std; string word; int i = 0; cout << "Enter words(to stop,type the word done):\n"; cin >> word; while (word != "done") { i++; cin >> word; } cout << "Your entered a total of " << i << " words."; cin.get(); return 0; }
编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应行数的星号,其中第一行包括一个星号,第二行包括两个星号,依此类推。每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号前面加上句点。该程序的运行情况如下:
略
相关文章推荐
- c++ primer plus(第6版)中文版 第五章编程练习答案
- 【第五章】C++ Primer plus 的编程练习题(选取部分)
- C++ Primer Plus(第五版)第五章编程练习题6(关于string难点哦)
- c++ primer plus(第6版)中文版 第二章编程练习答案
- c++ primer plus(第6版)中文版 第三章编程练习答案
- c++primer plus第五章编程练习
- c++ primer plus(第6版)中文版 第六章编程练习答案
- C++ Primer Plus 学习笔记 第五章 编程练习
- 【第六章】C++ Primer plus 的编程练习题(选取部分)
- C++ Primer Plus 第六版(中文版)课后编程题----第五章
- C++ Primer Plus 第6版 中文版 第5章编程练习
- C++ Primer Plus第五版 第五章 编程练习答案
- 【第八章】C++ Primer plus 的编程练习题(选取部分)
- c primer plus(第五版)中文版 第五章 编程练习
- C++ Primer Plus 第五版中文版 第六章 编程练习6
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第五章(chapter 5) 1-5
- C++ Primer Plus第六版 第五章 编程练习答案
- 【第十一章】C++ Primer plus 的编程练习题
- C++ primer plus(第六版)第五章练习题
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第五章(chapter 5) 6-10