C++ Primer Plus 第六版_编程练习(4)(Chapter_five 1-5)
2016-06-26 14:43
585 查看
编程工具用得好,搞起研究事半功倍。导师的意见是,学好C++,其他编程都不怕。暂时没有迫切的实战需要,于是决定从最基础的学起,挑了《C++ Primer Plus (第六版)》这本书,开始啃吧。编程练习还是一定要做的,每天一点,记在这里。
5_1
5_2
5_3
5_4
5_5
5_1
#include <iostream> using namespace std; int main() { int lower_bound_; int upper_bound_; cout << "Enter the lower bound: "; cin >> lower_bound_; cout << "Enter the upper bound: "; cin >> upper_bound_; int i; int sum = 0; for (i = lower_bound_; i <= upper_bound_; i++) { sum = sum + i; } cout << "Summer of the number between them is " << sum << endl; return 0; }
5_2
#include <iostream> #include <array> using namespace std; const int Asize = 101; int main() { array<long double, Asize> factorials; factorials[0] = factorials[1] = 1; for (int i = 2; i < Asize; i++) { factorials[i] = factorials[i - 1] * i; } for (int j = 0; j < Asize; j++) { cout << j << "! = " << factorials[j] << endl; } return 0; }
5_3
#include <iostream> using namespace std; int main() { double i; double sum = 0; cout << "Enter a number(0 to qiut): "; cin >> i; while (i != 0) { sum = sum + i; cout << "Summer of the number that you have entered is " << sum << endl; cout << "Enter a number(0 to qiut): "; cin >> i; } cout << "You have entered 0." << endl; return 0; }
5_4
#include <iostream> using namespace std; const double Value = 100; const double Pre_1 = 0.10; const double Pre_2 = 0.05; int main() { double value_Daphne = Value; double value_Cleo = Value; int count_ = 1; while (true) { value_Daphne += Value * Pre_1; value_Cleo += value_Cleo * Pre_2; if (value_Cleo > value_Daphne) { cout << "After " << count_ << " years, Cleo will get much more value than Daphne." << endl; cout << "Cleo's value: " << value_Cleo << endl; cout << "Daphne's value: " << value_Daphne << endl; break; } count_++; } return 0; }
5_5
#include <iostream> #include <string> using namespace std; int main() { string month[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; int num[12] = {}; int sum = 0; cout << "Enter the sales of each month: " << endl; for (int i = 0; i < 12; i++) { cout << month[i] << " : "; cin >> num[i]; sum += num[i]; } cout << "《C++ For Fools》 has been sold " << sum << " this year." << endl; return 0; }
相关文章推荐
- C++数组
- 函数
- stl关联容器
- getline()这个函数
- C++ Primer 5th - 2.1 基础内建类型
- C++ Primer 5th - 1.5 类
- C++ Primer Plus 第六版_编程练习(1)(Chapter_two 1-7)
- C++ Primer Plus 第六版_编程练习(2)(Chapter_three 1-7)
- C++ Primer Plus 第六版_编程练习(3)(Chapter_four 1-10)
- 《C++ Primer》笔记
- 浅拷贝、深拷贝与引用计数
- Vector的简单使用
- c++语句补充
- c++基础复习之表达式
- c++基础复习之数组与指针
- 《C++primer》第二章--变量和基本内置类型
- 复制控制
- C++中cin如何输入字符串
- 读书笔记(四)---C++ Primer
- ~第七章:类型与语句