C++ primer plus(第六版)学习笔记、习题答案(4)

来源:互联网 发布:java自带线程池 编辑:程序博客网 时间:2024/05/22 17:44

买了一本刘未鹏的的《暗时间》,如获至宝,我知道我浪费了太多的时间,看了他的一篇讲招聘的博客,知道自己今后要如何做,要看那些书了,其实看完他们的人生历程,他们的博客后,突然感觉自己在这里小打小闹,些不出什么新奇的东西,但我得做个有始有终的人吧,还是坚持把C++primer plus的读书笔记和习题答案写完吧,大牛都是从这样开始的吧,也完成自己刚刚买这本书后在心底暗下的决心吧?

第五章:循环和关系表达式


第一部分:学习笔记

1. 提示;在for和括号之间加一个空格,省略函数和括号之间的空格。

2.

cout.setf (ios::boolaplha)

cout显示true和false

3.++,--前缀首先赋值一个副本,复制后其值才加一

4.逗号,其值是第二部分的值

5.cin, cin.get(), cin.get(char)

cin会忽略空格,换行符,后两个不会 ,他们都会发送给cin的输入缓冲,头文件iostream会将cin.get(ch)的参数声明为应用类型。



第二部分:习题答案

5.1

// 2014/12/10#include <iostream>int main(){using namespace std;int i = 0, j = 0;int sum = 0;cout << "Enter i:";cin >> i;cout << "Enter j:";cin >> j;for (i; i <= j; i++){sum += i;}cout << "the sum between i and j is :" << sum;cin.get();cin.get();cin.get();return 0; }

5.2

// 2014/12/10#include <iostream>#include <array>using namespace std;int main(){int const size = 101;array<long double,size> fac;fac[0] = fac[1] = 1;for (int i = 2; i < size; i++){fac[i] = i * fac[i - 1];}for(int i = 0; i < size; i++)cout << i <<"! = " << fac[i] << endl;cin.get();cin.get();return 0; }

5.3

// 2014/12/10#include <iostream>#include <array>using namespace std;int main(){double num;double sum = 0;cout << "please input number,when you input zero, the program will close:";cin >> num;while (num){sum += num;cout << "the sum is :" << sum << endl << "please enter the other number:";cin >> num;}cin.get();cin.get();return 0; }

5.4

//test 3_1//2014/12/10#include<iostream>using namespace std;int main(){const double s1 = 0.1;const double s2 = 0.05;double bj1 = 100, bj2 = 100;int i = 1;while (bj2 <= bj1){bj1 = 100 + 100 * s1 * i;bj2 = bj2 + bj2 * s2;i++;}cout << "after " << i << "years " << "cleo's profit is more than Daphne's " << endl;cout << "cleo's profits is :" << bj2 << "Daphne's profits is :" << bj1;cin.get();cin.get();return 0;}

5.5
//2014/12/10#include<iostream>#include<string>using namespace std;int main(){const int n = 12;string yue[n] = {"January","February","March","April","May","June","July","August","September","October","November","December"};int num[n];int sum = 0;for (int i = 0 ; i < n; i++){cout << "please input " << yue[i] << " sale number is :" ;cin >> num[i];sum += num[i];//cout << endl;}cout << "the years sale number is ;" << sum;cin.get();cin.get();return 0;}

5.6

//test 3_1//2014/12/10#include<iostream>#include<string>using namespace std;int main(){const int n = 12;const int m = 3;string yue[n] = {"January","February","March","April","May","June","July","August","September","October","November","December"};int num[m][n];long every_year[3] = {0};long sum = 0;for (int i = 1 ; i <= m; i++){for (int j = 0; j < n; j++){cout << "please input " << "the " << i << "year " << yue[j] << " sale number is :" ;cin >> num[i][j];every_year[i-1] += num[i][j];}sum += every_year[i-1];}for (int i = 1; i <= m; i++){cout << "the " << i << " years sale number is ;" << every_year[i-1] << endl;}cout << m << " years sale number are " << sum;cin.get();cin.get();return 0;}

5.7
////2014/12/10#include<iostream>#include<string>using namespace std;int main(){struct car{string make;int year;};int n;cout << "how many cars do you wish to catalog? ";cin >> n;car *ptr = new car[n];for (int i= 0; i < n; i++){cout<< "car #" << i + 1 << endl;cout << "please enter the make";cin >> ptr[i].make;cout << "please enter the year:";cin >> ptr[i].year;}cout << "here is your collection:" << endl;for (int i = 0; i <n; i++){cout << ptr[i].make << "  " << ptr[i].year << endl;}delete[] ptr;cin.get();cin.get();return 0;}

后面的明天补上










0 0