第五章内容参考上一篇的技术点分析:
[C Primer plus 心得]5.循环关系表达式
1.编写一个要求用户输入两个整数的程序,该程序将计算并输出这两个整数之间(包括这两个整数)所有的整数的和。这里假设先输入较小的整数,例如如果用户输入的是2和9,则程序将指出2-9之间所有整数的和为44.
答案:
#include <iostream>using namespace std;int main(void){ int first = 0; int second = 0; int sum = 0; cout << "Please Enter two integer number:\n"; cin >> first; cin >> second; for (int i = first; i <= second; i ) { sum = i; } cout << "The sum value is " << sum <<endl; return 0;}
2.使用array对象(而不是数组)和long double(而不是long long )重新编写程序清单5.4,并计算100!(100的阶乘)的值答案:
#include <iostream>#include <array>using namespace std;const int ArSize = 101;int main(){ array<long double, ArSize> arr; arr[0] = 1; arr[1] = 1; for (int i = 2; i < ArSize; i ){ arr[i] = i*arr[i - 1]; } for (int i = 0; i < ArSize; i ){ cout << i << "! = " << arr[i] << endl; } return 0;}
3.编写一个要求用户输入数字的程序,每次输入后,程序都将报告到目前为止,所有输入的累积和,当用户输入0时,程序结束。答案:
#include <iostream>using namespace std;int main(){ int input = 0; int sum = 0; cout << "Please Enter integer number:\n"; cin >> input; while (input != 0) { sum = input; cout << "The total input value is " << sum <<endl; cin >> input; } return 0;}
4.Daphne以10%的单利投资了100美元,每一年利润都是投资额的10%,即每年10美元。而Cleo以5%的复利投资了100美元,利息是当前存款(包括获得的利息)的5%,Cleo在第一年投资100美元的盈利是5%得到了105美元,下一年的盈利是105美元的5%,以此类推编写一个程序,计算多少年后,Cleo的投资价值才能超过Daphne的投资价值,并显示两个人的投资价值。
答案:
#include <iostream>using namespace std;int main(){ double daphne_value = 100; double cleo_value = 100; int count = 0; while (cleo_value <= daphne_value) { daphne_value = 100*0.1; cleo_value = daphne_value * 0.05; count ; } cout << "count is "<< count <<endl; cout << "daphne's value is " << daphne_value <<endl; cout << "Cleo's value is " << cleo_value <<endl; return 0;}
5.假设要销售《c for fool》一书。请编写一个程序,输入全年中每个月的销售量(图书数量而不是销售额).程序通过循环,使用初始化为月份字符串的char*数组(或string对象数组) 逐月进行提示,并将输入的数据存储在一个int数组中,然后程序计算数组中各元素的总数,并报告这一年的销售情况。
答案:
#include <iostream>#include <array>using namespace std;int main(){ int arry_size = 12; int total = 0; int sales[arry_size] = {0}; string month[arry_size]={" Jan ", " Feb "," Mar "," Apr "," May "," Jun "," Jul "," Aug "," Sept "," Oct "," Nov "," Dec "}; for (int i =0; i< arry_size; i ) { cout<<"Please enter the sales of"<<month[i]<<"is: "; cin>>sales[i]; total = sales[i]; } for(int i = 0; i < arry_size; i ) { cout<<"The sales of "<<month[i]<<" is: "<<sales[i]<<endl; } cout<<"The sales of a year is "<<total<<endl; return 0;}
6.完成编程练习5, 但这一次使用一个二维数组来存储输入---3年中每个月的销售量。程序将报告每年销售量以及三年的总销售量。答案:
#include <iostream>using namespace std;int main(){ int arry_size = 12; int year_size = 3; int total[year_size] = {0}; int sales[year_size][arry_size] = {0}; string month[arry_size]={" Jan ", " Feb "," Mar "," Apr "," May "," Jun "," Jul "," Aug "," Sept "," Oct "," Nov "," Dec "}; for (int i = 0; i< year_size; i ) { for (int j = 0; j < arry_size; j ) { cout<<"Please enter the sales of"<<month[i]<<"in: " << (i 1) << " years is :"; cin>>sales[i][j]; total[i] = sales[i][j]; } } for(int i = 0; i < arry_size; i ) { cout<<"The sales of "<<(i 1)<<" year is: "<<total[i]<<endl; } cout<<"The sales of three year is "<<(total[0] total[1] total[2])<<endl; return 0;}
7.设计一个名为car的结构,用它存储下述有关汽车的信息: 生产商(存储在字符数组或string对象中的字符串)、 生产年份(整数)。编写一个程序,向用户询问有多少辆汽车。随后,程序使用new来创建一个由相应数量的car结构组成的动态数组。 接下来,程序提示用户输入每辆车的生产商(可能由多个单词组成)和年份信息。请注意,这需要特别小心,因为它将交替读取 数值和字符串(参见第4章)。最后,程序将显示每个结构的内容。
答案:
#include <iostream>using namespace std;struct car{ string manufacturer; int year;};int main(){ int count = 0; car *car_info =NULL; cout << "How many cars do you wish to catalog?\n"; cin >> count; car_info = new car[count]; for (int i = 0; i < count; i ) { cout << "Please enter the manufacturer of " << (i 1) << " car:"; cin >> car_info[i].manufacturer; cout << "Please enter the year of " << (i 1) << " car:"; cin >> car_info[i].year; } cout<<"Here is your collection:"<<endl; for (int i = 0; i < count; i ) { cout << "The "<< (i 1) << " car's manufacturer is "<<car_info[i].manufacturer; cout << " and the year is "<<car_info[i].year <<endl; } delete []car_info; return 0;}
8.编写一个程序,它使用一个char数组和循环来每次读取一个单词,直到用户输入done为止。随后,该程序指出用户输入了多少个单词(不包括done在内)。
下面是该程序的运行情况:
Enter words (to stop, type the word done): anteater birthday category dumpster envy finagle geometry done for sure You entered a toal of 7 words.
您应该在程序中包含头文件cstring,并使用函数strcmp()来进行比较测试
答案:
#include <iostream>#include <cstring>using namespace std;int main(){ int count = 0; char words[50] = {0}; cout << "Enter words (to stop, type the word done)\n"; cin >> words; while(strcmp(words, "done") != 0) { count ; cin >> words; } cout <<"for sure You entered a toal of " << count << " words" << endl; return 0;}
9.编写一个满足前一个练习中描述的程序,但是用string对象而不是字符数组。请在程序中包含头文件string,并使用关系运算符来进行比较测试。
答案:
#include <iostream>#include <string>using namespace std;int main(){ int count = 0; string words; cout << "Enter words (to stop, type the word done)\n"; cin >> words; while(words != "done") { count ; cin >> words; } cout <<"for sure You entered a toal of " << count << " words" << endl; return 0;}
10.编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行,然后程序将显示相应的行数的星号,其中第一行包括一个星号,第二行包括两个星号,以此类推,每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号前面加上句点。
输出:
Enter number of rows: 5
. . . . *
. . . * *
. . * * *
. * * * *
* * * * *
答案:
#include <iostream>using namespace std;int main(){ int rows = 0; cout << "Enter number of rows: "; cin >> rows; for (int i = 0; i < rows; i ) { for (int j = rows; j > i 1; j--) { cout << ". "; } for (int k = 0; k <= i; k ) { cout << "* "; } cout <<endl; } return 0;}