《C++ Primer Plus(第6版)》编程练习代码 Chapter 5
来源:互联网 发布:ubuntu tftp 命令 编辑:程序博客网 时间:2024/05/17 04:23
Chapter 5
5.1
#include<iostream>int main(){ using namespace std; int min, max, sum = 0; cout << "Enter min number: "; cin >> min; cout << "Enter max number: "; cin >> max; for (int i = min; i <= max; i++) { sum += i; } cout << "Result: " << sum; cin.get(); cin.get();}
5.2
#include<array>#include<iostream>int main(){ using namespace std; const int N = 101; array<long double, N> factorials; factorials[0] = factorials[1] = 1LL; for (int i = 2; i < N; i++) factorials[i] = i * factorials[i - 1]; for (int i = 0; i < N; i++) cout << i << "! = " << factorials[i] << endl; cin.get(); cin.get();}
5.3
#include<iostream>int main(){ using namespace std; long sum = 0, x; for (cin >> x; x != 0; cin >> x) { sum += x; cout << "Sum result: " << sum << endl; } cout << "Program ends."; cin.get(); cin.get();}
5.4
#include<iostream>int main(){ using namespace std; double Daphne, Cleo, SD, SC; int i = 1; Daphne = Cleo = 100; SD = Daphne * 0.1; SC = Cleo * 0.05; while (SD > SC) { SD += Daphne * 0.1; SC += (SC + Cleo) * 0.05; ++i; } cout << "After " << i << " years, Cleo > Daphne.\n"; cout << "Daphne's value: " << SD << endl; cout << "Cleo;s value: " << SC << endl; cin.get(); cin.get();}
5.5
#include<iostream>#include<string>int main(){ using namespace std; string month[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; int sale[12], sum = 0; for (int i = 0; i < 12; i++) { cin >> sale[i]; sum += sale[i]; } cout << "Sales in this year:\n\n"; for (int i = 0; i < 12; i++) { cout << month[i] << ": \t" << sale[i] << endl; } cout << "Sum of sales: " << sum; cin.get(); cin.get();}
5.6
#include<iostream>#include<string>int main(){ using namespace std; string month[12] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; int sale[12][3], sum = 0; for (int i = 0; i < 12; i++) for (int j = 0; j < 3; j++) { cin >> sale[i][j]; sum += sale[i][j]; } cout << "Sales in three years:\n\n"; for (int i = 0; i < 12; i++) { cout << month[i] << ": \t" ; for (int j = 0; j < 3; j++) { cout << sale[i][j] << "\t"; } cout << endl; } cout << "Sum of sales: " << sum; cin.get(); cin.get();}
5.7
#include<iostream>#include<string>using namespace std;struct car{ string make; int year;};int main(){ int numOfCar; cout << "How many cars do you wish to catalog? "; cin >> numOfCar; cin.get(); car * carInfo = new car[numOfCar]; for (int i = 0; i < numOfCar; i++) { cout << "Car #" << i+1 << ":\n" << "Please enter the make: "; getline(cin, (carInfo + i)->make); cout << "Please enter the year made: "; (cin >> carInfo[i].year).get(); } cout << "Here is your collection:\n"; for (int i = 0; i < numOfCar; i++) { cout << (carInfo + i)->year << " " << (carInfo + i)->make << endl; } delete [] carInfo; cin.get(); cin.get(); return 0;}
5.8
#include<iostream>#include<cstring>using namespace std;int main(){ cout << "Enter words (to stop, type the word done):\n"; char word[30]; int n = 0; cin >> word; while (strcmp(word, "done")) { cin >> word; n++; } cout << "You entered a total of " << n << " words."; cin.get(); cin.get();}
5.9
#include<iostream>#include<cstring>#include<string>using namespace std;int main(){ cout << "Enter words (to stop, type the word done):\n"; string word; int n = 0; cin >> word; while (word != "done") { cin >> word; n++; } cout << "You entered a total of " << n << " words."; cin.get(); cin.get();}
5.10
#include<iostream>using namespace std;int main(){ cout << "Enter numbet of rows: "; int row; cin >> row; for (int i = 1; i <= row; i++) { for (int j = 0; j < (row - i); j++) cout << "."; //print"." for (int j = 0; j < i; j++) cout << "*"; //print"*" cout << endl; } cin.get(); cin.get();}
阅读全文
0 0
- 《C++ Primer Plus(第6版)》编程练习代码 Chapter 5
- 《C++ Primer Plus(第6版)》编程练习代码 Chapter 6
- 《C++ Primer Plus(第6版)》编程练习代码 Chapter 2
- 《C++ Primer Plus(第6版)》编程练习代码 Chapter 3
- 《C++ Primer Plus(第6版)》编程练习代码 Chapter 4
- 《C++ Primer Plus(第6版)》编程练习代码 Chapter 7
- 《C++ Primer Plus(第6版)》编程练习代码 Chapter 8
- 《C Primer Plus(第5版)中文版》第6章编程练习第1题
- 《C Primer Plus(第5版)中文版》第6章编程练习第14题
- 《C Primer Plus(第5版)中文版》第6章编程练习第15题
- 《C Primer Plus(第5版)中文版》第6章编程练习第16题
- 《C Primer Plus(第5版)中文版》第7章编程练习第6题
- C++Primer Plus第6版 4.13编程练习答案
- 《C Primer Plus(第5版)中文版》第7章编程练习第5题
- 《C Primer Plus(第5版)中文版》第7章编程练习第1题
- 《C Primer Plus(第5版)中文版》第7章编程练习第2题
- 《C Primer Plus(第5版)中文版》第7章编程练习第3题
- 《C Primer Plus(第5版)中文版》第7章编程练习第4题
- 数据分析的最后一英里
- 克服信息过载的视觉工具
- 在安卓上运行TensorFlow:让深度学习进入移动端
- O'Reilly Report:评估机器学习的模型
- 为Spark ML扩展结构化流计算:集成朴素贝叶斯机器学习方法和定制化导出(sink)的实验方法
- 《C++ Primer Plus(第6版)》编程练习代码 Chapter 5
- 吉他介绍
- Netty数据传输-服务端 queue-server
- O'Reilly精品图书推荐:Spark高级数据分析
- 为什么企业需要关注深度学习
- 何为人工智能?
- 如何用100美元和TensorFlow来造一个能“看”东西的机器人
- Jmeter学习—002—调试工具—HTTP Mirror Server
- O'Reilly Report: 现代制造业的大数据分析