//5.9-1
#include<iostream>
#include<stdlib.h>
using namespace std;
int main() {
int i, j;
cout << "Please enter two numbers:\n";
cin >> i;
cin >> j;
cout << "The sum is " << (i + j)*(j - i+1) / 2 << " .\n";
system("pause");
return 0;
}
//5.9-2
#include<iostream>
#include<stdlib.h>
#include <array>
using namespace std;
int main() {
const int SIZE = 101;
array<long double, SIZE> factorials = { 1LL,1LL };
for (int i = 2; i < SIZE; i++) {
factorials[i] = i*factorials[i - 1];
}
for (int i = 0; i < SIZE; i++)
{
cout << i << " != " << factorials[i] << endl;
}
system("pause");
return 0;
}
//5.9-3
#include<iostream>
#include<stdlib.h>
using namespace std;
int main() {
int sum=0, i;
cout << "Plaese enter number,0 to quit: ";
cin >> i;
while (i!=0)
{
sum += i;
cout << "The sum is " << sum << " .\n";
cout << "Plaese enter number,0 to quit: ";
cin >> i;
}
system("pause");
return 0;
}
//5.9-4
#include<iostream>
#include<stdlib.h>
using namespace std;
const float single_interest = 0.1;
const float compound_interest = 0.05;
int main() {
int i=1;
float sum_of_single = 100.0;
float sum = 100.0;
float sum_of_compound = 100.0;
while (sum_of_compound<=sum_of_single)
{
cout << "The " << i << " years:\n";
i++;
cout << "The sum of single is " << sum_of_single << endl;
sum_of_single = sum_of_single + sum*single_interest;
cout << "The sum of compound is " << sum_of_compound << endl;
sum_of_compound *= (1 + compound_interest);
}
cout << "After " << i << " years, " << "the sum of compound is heigher than the sum of single.\n";
cout << "The sum of single is " << sum_of_single << endl;
cout << "The sum of compound is " << sum_of_compound << endl;
system("pause");
return 0;
}
//5.9-5
#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;
const int MONTHS = 12;
int main() {
string month[MONTHS] = { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
int arr[MONTHS] = {};
int sum = 0;
for (int i = 0; i < MONTHS; i++) {
cout << month[i] << ": ";
cin >> arr[i];
}
for (int i = 0; i < MONTHS; i++) {
cout << month[i] << ": " << arr[i] << endl;
sum += arr[i];
}
cout << "The sum is " << sum << endl;
system("pause");
return 0;
}
//5.9-6
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
const int YEAR = 3;
const int MONTHS = 12;
int main() {
int sum_1 = 0, sum_2 = 0, sum_3 = 0;
int sum = 0;
string month[MONTHS] = { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
int arr[YEAR][MONTHS] = {};
for (int j = 0; j < YEAR; j++) {
cout << "The " << j + 1 << " year:" << endl;
for (int i = 0; i < MONTHS; i++) {
cout << month[i] << ": ";
cin >> arr[j][i];
}
}
for (int j = 0; j < YEAR; j++) {
cout << "The " << j + 1 << " year:" << endl;
for (int i = 0; i < MONTHS; i++) {
cout << month[i] << ": ";
cout << arr[j][i] << endl;
}
}
system("pause");
return 0;
}
//5.9-7
#include<iostream>
#include <string>
#include<stdlib.h>
using namespace std;
struct Car {
string producer;
int year;
};
int main() {
int num;
cout << "How many cars do you wish to catalog? ";
(cin >> num).get();
Car *car = new Car[num];
for (int i = 0; i < num; i++) {
cout << "Car " << i + 1 << "#:\n";
cout << "Please enter the make: ";
getline(cin, car[i].producer);
cout << "Please enter the year made: ";
(cin >> car[i].year).get();
}
cout << "Here is you collection:" << endl;
for (int i = 0; i < num; i++) {
cout << car[i].year << " " << car[i].producer << endl;
}
system("pause");
return 0;
}
//5.9-8
#include<iostream>
#include<stdlib.h>
using namespace std;
const int SIZE = 256;
int main() {
char word[SIZE];
unsigned int count = 0;
cout << "Enter words(to stop,type the word done):\n";
while (cin >> word)//cin使用空白(空格、制表符、换行符)来确定字符串结束的位置。意味着cin每次只能接受一次单词,但遇到空格后,下一个单词自动放入输入队列中。
{
if (strcmp("done", word)==0) {
break;
}
count++;
}
cout << "You entered a total of " << count << " words"<<endl;
system("pause");
return 0;
}
5.9-8注意点: cin使用空白(空格、制表符、换行符)来确定字符串结束的位置。意味着cin每次只能接受一次单词,但遇到空格后,下一个单词自动放入输入队列中。
参考:https://blog.csdn.net/weixin_39883581/article/details/113921948 第四章 字符串输入
//5.9-9
#include<iostream>
#include <string>
#include<stdlib.h>
using namespace std;
const int SIZE = 256;
int main() {
string word;
unsigned int count = 0;
cout << "Enter words(to stop,type the word done):\n";
while (cin >> word)//cin使用空白(空格、制表符、换行符)来确定字符串结束的位置。意味着cin每次只能接受一次单词,但遇到空格后,下一个单词自动放入输入队列中。
{
if (word == "done") {
break;
}
count++;
}
cout << "You entered a total of " << count << " words" << endl;
system("pause");
return 0;
}
//5.9-10
#include<iostream>
#include<stdlib.h>
using namespace std;
int main() {
int num = 0;
cout << "Enter the number of rows:" << endl;
cin >> num;
for (int i = 0; i < num; i++) {
for (int j = num - i - 1; j > 0; j--) {
cout << ".";
}
for (int k =0;k<i+1; k++) {
cout << "*";
}
cout << endl;
}
system("pause");
return 0;
}