5.9.1
#include<iostream> int main() { using namespace std; int one, two, temp, sum = 0; cout << "input first interger: "; cin >> one; cout << "input second interger: "; cin >> two; for (temp = one; temp <= two; ++temp) sum += temp; cout << "Add from " << one << " to " << two << " is " << sum; }
5.9.2
#include<iostream> #include<array> const int ArSize = 16; int main() { using namespace std; array<long double, ArSize> factorials; factorials[1] = factorials[0] = 1.0; 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; }
5.9.3
#include<iostream> int main() { using namespace std; int number, sum = 0; cout << "Please input interger number,end with 0." << endl; cin >> number; while (number) { sum += number; cout << "Now,sum is " << sum << endl; cin >> number; } }
5.9.4
#include<iostream> const double interest_C = 0.05; const double interest_D = 0.10; int main() { using namespace std; double deposit_D, deposit_C; deposit_C = deposit_D = 100.0; int year = 0; do { deposit_C *= 1.05; deposit_D += (0.1 * 100); ++year; } while (deposit_C <= deposit_D); cout << "deposit_C is " << deposit_C << endl; cout << "deposit_D is " << deposit_D << endl; cout << year << " years C's deposit more than D"; }
5.9.5
#include<iostream> #include<string> int main() { using namespace std; string months[12] = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月", }; int sales[12]; int sum = 0; for (int i = 0; i < 12; ++i) { cout << "请输入第" << months[i] << "的销售额: "; cin >> sales[i]; } for (int i = 0; i < 12; i++) { sum += sales[i]; } cout << "这一年的销售额是:" << sum; }
5.9.6
#include<iostream> #include<string> int main() { using namespace std; string months[12] = { "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月", }; int sales[3][12]; int sum_years[3] = { 0,0,0 }; int sum = 0; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 12; j++) { cout << "请输入第" << i + 1 << "年," << "第" << months[j] << "的销售额: "; cin >> sales[i][j]; } } for (int i = 0; i < 3; i++) { cout << "第 " << i+1 << " 年销售额为:"; for (int j = 0; j < 12; j++) { sum_years[i] += sales[i][j]; } cout << sum_years[i] << endl; } cout << "三年的总销售额为:"; for (int i = 0; i < 3; i++) { sum += sum_years[i]; } cout << sum; }
5.9.7
#include<iostream> #include<string> using namespace std; struct car { string manufacturer; int production_date; }; int main() { int car_number; cout << "How many cars do you have? "; cin >> car_number; car *cars = new car[car_number]; for (int i = 0; i < car_number; i++) { cout << "Car # " << i + 1 << endl; cout << "Please enter the make: "; cin >> cars[i].manufacturer; cout << "Please enter the year made: "; cin >> cars[i].production_date; } cout << "Here is your collection: "; for (int i = 0; i < car_number; i++) { cout << cars[i].production_date << " " << cars[i].manufacturer << endl; } }
5.9.8
#include<iostream> #include<cstring> int main() { using namespace std; char charr[20]; cout << "Enter words (to stop,type the word done): "; int sum; for (sum=0;strcmp(charr,"done");sum++) { cin >> charr; } cout << "for sure "; cout << "You enter a total of " << sum-1 << " words"; }
5.9.9
#include<iostream> #include<string> int main() { using namespace std; string charr; cout << "Enter words (to stop,type the word done): "; int sum; for (sum = 0; charr!="done"; sum++) { cin >> charr; } //cout << "for sure "; cout << "You enter a total of " << sum - 1 << " words"; }
5.9.10
#include<iostream> int main() { using namespace std; int rows; cout << "Enter number of rows: "; cin >> rows; char **charr = new char*[rows];//开辟行 for (int i = 0; i < rows; i++) { *(charr+i) = new char[rows];//开辟列 } for (int i = 0; i < rows; i++) { for (int j = rows - 1; j >= 0; j--) { if ((j + i) >= 4) charr[i][j] = '*'; else charr[i][j] = '.'; } } for (int i = 0; i < rows; ++i) { for (int j = 0; j < rows; ++j) cout << charr[i][j]; cout << endl; } //cout << charr[2][0]; }