第五章 编程练习
来源:互联网 发布:台湾淘宝 编辑:程序博客网 时间:2024/04/28 10:47
编程练习1
#include <iostream>using namespace std;int main(){ int a,b; cout << "Please enter the start number: "; cin >> a; cout << "Please enter the end number: "; cin >> b; int sum = 0; for (int i = a; i<=b; i++) sum = sum + i; cout << " the sum of all integers between " << a << " and " << b << " is " << sum << endl; system("pause"); return 0;}
编程练习2
#include <iostream>#include <array>using namespace std;const int ArSize = 101;int main(){ array<long double, ArSize> factorials; factorials[1] = factorials[0] = 1; for(int i = 2; i < ArSize; i++) { factorials[i] = i * factorials[i-1]; } for(int i = 0; i < ArSize; i++) { cout << i << "! = " << factorials[i] << endl; } system("pause"); return 0;}
编程练习3
#include <iostream>using namespace std;int main(){ int i; int sum = 0; cout << "Please enter a number: "; cin >> i; while(i != 0) { sum = sum + i; cout << "Please enter another number: "; cin >> i; } cout << "The sum of input numbers is : " << sum << endl; system("pause"); return 0;}
编程练习4
#include <iostream>using namespace std;int main(){ float Daphne_Capital = 100; float Cleo_Capital = 100; float interest_fixed = 0.1; float interest_flex = 0.05; int year = 1; do { Daphne_Capital = Daphne_Capital + 100 * interest_fixed; Cleo_Capital = Cleo_Capital * (1 + interest_flex); year++; } while (Daphne_Capital > Cleo_Capital); cout << "after " << year << " years, Cleo has more money than Daphne" << endl; cout << "Daphne has " << Daphne_Capital << " dollars" << endl; cout << "Cleo has " << Cleo_Capital << " dollars" << endl; system("pause"); return 0;}
编程练习5
#include <iostream>using namespace std;int main(){ char * month[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul","Aug", "Sep", "Oct", "Nov", "Dec"}; int sales_month; int sales_year = 0; for (int i = 0; i < 12;i++) { cout << "Enter the number of books sold in " << month[i] << " : " ; cin >> sales_month; sales_year = sales_year + sales_month; } cout << "This year we have sold " << sales_year << " books" << endl; system("pause"); return 0;}
编程练习6
#include <iostream>using namespace std;int main(){ int sales[3][12]; int total_sales = 0; for (int year = 0; year < 3; year++) { int sales_year = 0; for (int month = 0; month < 12; month++) { cout << " Enter the sales of year " << year+1 << " month " << month +1 << " :"; cin >> sales[year][month]; sales_year = sales_year + sales[year][month]; } cout << "This year we have sold " << sales_year << " books" << endl; total_sales = total_sales + sales_year; } cout << "In total we have sold " << total_sales << " books" << endl; system("pause"); return 0;}
编程练习7
这个练习使用了一个很笨的方法,因为结构数组是用动态指针声明的,所以赋值后还要把指针调回原位。学到后面函数之后会有更好的方法
#include <iostream>#include <string>using namespace std;struct car { string brand; int manufacture_year;};int main(){ int car_num; cout << "How many cars do you wish to catalog? "; cin>>car_num; car *car_arr = new car[car_num]; for (int i = 0; i<car_num;i++,car_arr++) { cin.get(); cout << "Car #" << i+1 <<":" << endl; cout << "Please enter the make: "; string name; getline(cin,name); //cin.get(); cout << "Please enter the year made: "; int y; cin>>y; //cin.get(); car_arr->brand = name; car_arr->manufacture_year = y; } for (int i =0; i<car_num;i++) { car_arr--; } cout << "Here is your collection:" << endl; for (int i=0; i<car_num;i++) { cout << car_arr[i].manufacture_year << "\t" << car_arr[i].brand << endl; } system("pause"); return 0;}
编程练习8
#include <iostream>using namespace std;const int ChSize = 100;int main(){ char word[ChSize]; int count = 0; cout << "Please enter words: (type \"done\" to stop)" << endl; do { cin>>word; if(!strcmp(word,"done")) break; else count++; } while(true); cout << "In total you have input " << count << " words!" << endl; system("pause"); return 0;}
编程练习9
#include <iostream>#include <string>using namespace std;int main(){ string word; int count = 0; cout << "Please enter a word: "; getline(cin, word); while(word !="done") { count ++; cout << " Please enter another word: "; getline(cin, word); } cout << "In total you have input " << count << " words!" << endl; system("pause"); return 0;}
编程练习10
#include <iostream>using namespace std;int main(){ int row; cout << "Please input how many rows to display: "; cin >> row; for (int i=0; i<row; i++) { int star = i+1; int dot = row-i-1; for (int j=0; j<dot;j++) cout << " . "; for (int k=0;k<star;k++) cout << " * "; cout << endl; } system("pause"); return 0;}
0 0
- 第五章 编程练习
- Python核心编程 练习 第五章
- c++ primer plus 第五章编程练习
- Python核心编程 练习 第五章
- 《Python核心编程》第五章练习解析
- (八十)第五章编程练习
- python核心编程第五章练习
- python 第五(6)章编程练习
- c++primer plus第五章编程练习
- C primer plus(第五版)编程练习第五章
- C++.Primer.Plus第五版第五章编程练习答案
- C Primer Plus (第五版) 第五章 编程练习 答案
- C++ Primer Plus第五版 第五章 编程练习答案
- 编程练习(第五周)
- Java2游戏编程读书笔记(第五章练习参考答案)
- C++ Primer Plus 第五版 第六章 编程练习 7
- 《C Primer Plus 第五版》第十章编程练习
- C++ Primer Plus第六版 第五章 编程练习答案
- Subsets II
- 第五章 复习题
- 用反射修改一个私有委托
- JS Array.sort()排序函数
- 电路关键元件讲解
- 第五章 编程练习
- 前端:调试时 导入外部Js到网页
- 第十一章 编译器:代码生成
- kubernetes1.4 基础篇:Learn Kubernetes 1.4 by 6 steps(3):Step 2. Using kubectl to Create a Deployment
- 计算两个时间戳的差
- Knockout <!--ko-->与PartialView
- 百度地图起点与终点的距离计算(有偿求助)
- 勾股定理一日一证连载157
- Maven从零开始_3_Maven依赖