(1)编写 C++ 程序,要求用户输入两个整数,程序计算并输出这两个整数之间所有整数的和。
#include <iostream>
using namespace std;
int main()
{
// 输入两个整数
cout << "Please input two integers: ";
int a, b;
cin >> a >> b;
int cnt = 0;
for (int i = a; i <= b; i++)
{
cnt += i;
}
cout << "The sum between " << a << " and " << b << " is " << cnt << endl;
return 0;
}
(2)使用 array 对象和 long double 编写程序,并计算 100! 的值。
#include <iostream>
#include <array>
#include <iomanip>
using namespace std;
const int ArSize = 16;
int main()
{
// 使用 array 对象,数据流类型为 long double,长度为 ArSize
array<long double, ArSize> arr;
// 为第一项和第二项赋值
arr[0] = arr[1] = 1;
for (int i = 2; i < ArSize; i++)
{
arr[i] = i * arr[i - 1];
}
// 这里,使用 fixed 固定输出位数而不使用科学计数法
for (int i = 0; i < ArSize; i++)
{
cout << i << "! = " << fixed << arr[i] << endl;
}
return 0;
}
(3)编写 C++ 程序,要求用户输入数字,程序输出到目标位置输入数字的和,输入 0 时停止。
#include <iostream>
using namespace std;
int main()
{
// 不断输入数字
int num, cnt = 0;
cout << "Please enter the number until zero is entered: " << endl;
while (cin >> num)
{
if (num == 0)
{
cout << "Finished!" << endl;
break;
}
cnt += num;
cout << "The sum of the numbers entered so far is " << cnt << endl;
}
return 0;
}
(4)A 投资利息:0.1×原始存款;B 投资利息:0.05×当前存款。两人的原始存款都是 100,编写程序计算多少年后 B 的利息超过 A。
#include <iostream>
using namespace std;
int main()
{
// 表示年份
int year = 1;
// 表示 B 当前存款,初始时为 100
float b_curr = 100;
// 第一年后表示 A 和 B 的利息
float a_v = 10, b_v = 0.05 * b_curr;
// 更新 b_curr
b_curr = 100 + b_v;
// 比较两个浮点数的大小
while ((a_v - b_v) > 1e-5)
{
// 年份数加 1
year++;
// 计算累计利息
a_v += 10;
b_v += 0.05 * b_curr;
// 更新 b_curr
b_curr = 100 + b_v;
}
cout << "After " << year << " years, A's interest exceeds B's." << endl;
return 0;
}
(5)编写 C++ 程序,要求用户输入全年中每个月的销售量,然后计算数组中各元素的和。
#include <iostream>
using namespace std;
// 建立索引与月份的映射
string month[12] = { "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "Novement", "December" };
int main()
{
// 存放全年中每个月的销售量
int books[12];
// 循环读取并统计和
int cnt = 0;
cout << "Please enter the book sales for each month of the year in turn:" << endl;
for (int i = 0; i < 12; i++)
{
cout << month[i] << ": ";
cin >> books[i];
cnt += books[i];
}
cout << "A total of " << cnt << " books are sold this year." << endl;
return 0;
}
(6)在上一题的基础上,使用二维数组存储三年中每个月的销售量以及三年的总销售量。
#include <iostream>
using namespace std;
// 建立索引与月份的映射
string month[12] = { "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "Novement", "December" };
// 建立索引与顺序的映射
string order[3] = { "The first year", "The second year", "The third year" };
int main()
{
// 存放三年中每个月的销售量
int books[3][12];
// 循环读取并统计和
int cnt_all_year = 0;
cout << "Please enter the book sales for each month of the year in turn:" << endl;
for (int i = 0; i < 3; i++)
{
int cnt_per_year = 0;
for (int j = 0; j < 12; j++)
{
cout << order[i] << ", " << month[j] << ": ";
cin >> books[i][j];
cnt_per_year += books[i][j];
cnt_all_year += books[i][j];
}
cout << "A total of " << cnt_per_year << " books are sold this year." << endl;
}
cout << "A total of " << cnt_all_year << " books are sold in three years." << endl;
return 0;
}
(7)设计一个名为 car 的结构,其中包含生产厂商和生产年份两个变量。要求用户首先输入 car 对象个数,然后输入每个对象的具体内容,然后程序输出所有对象的内容。
#include <iostream>
using namespace std;
struct car
{
string manufacturer;
int year;
};
int main()
{
// 向用户询问有多少辆汽车
cout << "How many cars do you wish to catalog? ";
int n;
cin >> n;
// 处理 cin 留在队列中的换行符
cin.get();
// 使用 new 初始化 n 个 car 对象
car* user_cars = new car[n];
// 循环读取每辆车的信息
for (int i = 0; i < n; i++)
{
cout << "Car #" << i + 1 << ":" << endl;
cout << "Please enter the make: ";
getline(cin, user_cars[i].manufacturer);
cout << "Please enter the year made: ";
cin >> user_cars[i].year;
// 处理 cin 留在队列中的换行符
cin.get();
}
// 输出每辆车的信息
cout << "Here is your collection:" << endl;
for (int i = 0; i < n; i++)
{
cout << user_cars[i].year << " " << user_cars[i].manufacturer << endl;
}
return 0;
}
(8)编写 C++ 程序,使用 char 数组循环读取单词,直到用户输入 done 位置,并指出用户输入单词的个数。
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
// 单词计数,遇到
int cnt = 0;
// 结束字符串
char end_str[] = "done";
// 存放输入字符串
char word[20];
// 输入 word 直到遇到 done,即 strcmp 函数返回零时
cin >> word;
while (strcmp(word, end_str) != 0)
{
cnt++;
cin >> word;
}
cout << "You entered a total of " << cnt << " words." << endl;
return 0;
}
(9)使用 string 对象完成上一题。
#include <iostream>
#include <string>
using namespace std;
int main()
{
// 单词计数,遇到
int cnt = 0;
// 结束字符串
string end_str = "done";
// 存放输入字符串
string word;
// 输入 word 直到遇到 done,即 strcmp 函数返回零时
cin >> word;
while (word != end_str)
{
cnt++;
cin >> word;
}
cout << "You entered a total of " << cnt << " words." << endl;
return 0;
}
(10)编写 C++ 程序,要求用户输入一个值 n,程序显示指定行的图案。图案每一行都有 n 个符号,第 i 行有 i 个 ‘*’,不足部分用 ‘.’ 填充,且 ‘.’ 在前。
#include <iostream>
using namespace std;
int main()
{
cout << "Enter number of rows: ";
int N;
cin >> N;
// 每一行有 N-(i+1) 个 '.' 和 i+1 个 '*'
for (int i = 0; i < N; i++)
{
// 输出 '.'
for (int j = 0; j < N - (i + 1); j++)
{
cout << '.';
}
// 输出 '*'
for (int j = 0; j < i + 1; j++)
{
cout << '*';
}
// 换行
cout << endl;
}
return 0;
}