第一题
要求:输入两个整数,计算输出两个整数之间的所有整数和。
//5.1
#include<iostream>
int main()
{
using namespace std;
cout << "Enter two int numbers :\n";
int a, b;
cin >> a;
cin >> b;
while (a > b)
{
int i;
i = a,a = b, b = i;
}
int m = 0;
for (int i = a; i <= b; i++)
m += i;
cout << a << " to " << b << " sum is " << m;
return 0;
}
第二题
要求:用array对象重写程序。
//5.2
#include<iostream>
#include<array>
int main()
{
using namespace std;
array<long double, 101>factorials;
factorials[0] = factorials[1] = (long double) 1;
for (int i = 2; i < 101; i++)
{
factorials[i] = i * factorials[i - 1];
}
for (int i = 0; i < 101; i++)
cout << i << "! = " << factorials[i] << endl;
return 0;
}
第三题
要求:输入整数,得到0至该整数的和,并不断相加,直至输入0,程序停止,输出总和。
//5.3
#include<iostream>
int main()
{
using namespace std;
int m=0;
int a=1;
while (a != 0)
{
cout << "Enter number ,the ending is entering 0 :\n";
cin >> a;
for (int b = 0; b < a; b++)
m += b;
cout << "The sum is " << m<<endl;
}
return 0;
}
第四题
要求:比较投资价值,输出超越年数以及此时价值。
//5.4
#include<iostream>
const double start_m = 100.0;
int main()
{
using namespace std;
double Daphne_m = 100.0;
double Cleo_m = 100.0;
double Daphne_f = 0.1;
double Cleo_f = 0.05;
int n;
double m,q;
for (n = 1; Daphne_m >= Cleo_m; n++)
{
q= start_m * Daphne_f ;
Daphne_m += q;
Cleo_m = Cleo_m * (1 + Cleo_f);
}
cout << n << " years ago, Cleo_money >Daphne_money.\n";
cout << "Daphne: " << Daphne_m << endl
<< "Cleo: " << Cleo_m << endl;
return 0;
}
第五题
要求:创建数组(月份与对应销售量),并计算输出销售量总额。
//5.5
#include<iostream>
const int year_per_months = 12;
int main()
{
using namespace std;
cout << "Enter 12 months' books :\n";
int books[year_per_months];
const char* months[year_per_months] =
{ "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
int m = 0;
for (int i = 0; i < year_per_months; i++)
{
cin >> books[i];
m += books[i];
};
for (int i = 0; i < year_per_months; i++)
{
cout << months[i] << ":\t" << books[i] << endl;
};
cout << "Year's Sum is " << m;
return 0;
}
第六题
要求:创建二维数组,报告三年内每月销售量,并计算总和。
//5.6
#include<iostream>
const int y_per_m = 12;
int main()
{
using namespace std;
cout << "2019 -- 2021 the books' sales :\n";
int book[3][y_per_m];
const char* months[y_per_m] =
{ "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
int m = 0;
for (int i = 0; i < 3; i++)
{
for (int n = 0; n < y_per_m; n++)
{
cin >> book[i][n];
m += book[i][n];
}
cout << "\n";
}
int sum = m;
const int year[3] = { 2019,2020,2021 };
for (int n = 0; n < y_per_m; n++)
{
cout << "\t";
cout << months[n];
};
cout << endl;
for (int i = 0; i < 3; i++)
{
cout << year[i] << "\t";
for (int n = 0; n < y_per_m; n++)
{
cout << book[i][n];
cout << "\t";
}
cout << "\n";
}
cout << "The sales is " << sum;
return 0;
}
第七题
要求:设计结构,创建动态数组,通过输入汽车数,循环输入输出每个结构成员信息。
//5.7
#include<iostream>
#include<string>
using namespace std;
struct car
{
string c_type;
int c_time;
};
int main()
{
cout << "How many cars do you with to catalog ?\n";
int m;
cin >> m;
car* cars = new car[m];
for (int i = 0; i < m; i++)
{
cin.get();//在使用getline()之前需要加一个cin.get();
cout << "Car #" << (i + 1)<<endl;
cout << "Please enter the make : ";
getline(cin, cars[i].c_type);
cout << "Please enter the year made : ";
cin >> cars[i].c_time;
cout << endl;
}
cout << "Here is your collection ;\n";
for (int i = 0; i < m; i++)
cout << cars[i].c_time << "\t" << cars[i].c_type << endl;
delete[] cars;
return 0;
}
第八题
要求:用char数组循环读取单词,并指出输入单词数量,终止单词为‘’done‘’。
//5.8
#include<iostream>
#include<cstring>
int main()
{
using namespace std;
int count = 0;
cout << "Enter words (to stop, type the word done ): \n";
char word[20];
cin >> word;
while (strcmp(word,"done"))
{
count++;
cin >> word;
}
cout << "you entered a total of " << count << " words.";
return 0;
}
第九题
要求·:使用string类,完成第八题。
//5.9
#include<iostream>
#include<string>
int main()
{
using namespace std;
int count = 0;
cout << "Enter words (to stop, type the word done ):\n";
string word;
cin >> word;
while (word != "done")
{
count++;
cin >> word;
}
cout << "you entered a total of " << count << " words.\n";
return 0;
}
第十题
要求:创建嵌套循环,得到符号方阵。
//5.10
#include<iostream>
int main()
{
using namespace std;
cout << "Enter number of rows: ";
int rows;
cin >> rows;
for (int i = 1; i <= rows; i++)
{
for (int m = rows - i; m > 0; m--)
{
cout << '.';
};
for (int n = 0; n < i ; n++)
{
cout << '*';
}
cout << "\n";
}
return 0;
}