- 编写可输入两个整数的程序,计算输出两个整数间的整数和(包括输入整数本身),比如输入2和9,输出44.
这个我有两种计算,这是第一种:
//练习5.1_1 输入两个整数,输出总和以及平均值,使用简单高斯求和公式计算
#include<iostream>
using namespace std;
int main() {
int x, y; //定义输入整数
float mid, sum, average; //定义中间判断值、总和以及平均值
cout << "Please enter two integer from small to large:";//简单提示让用户从小到大开始输入
cin >> x >> y;
mid = (y - x) % 2;
if (mid == 0)
sum = (x + y)*(y - x) / 2 + (y - x) / 2 + x;
else if (mid != 0)
sum = (x + y)*(y - x + 1) / 2;
average = sum / (y - x + 1); //如果得到两个结果是整数形式,就会触发整数除法,得到丢失精度的商(我的猜测)
cout << "The sum and the average is:" << sum << " and " << average << '.' << endl;
system("pause");
return 0;
}
第二种是利用for循环加一计数
//练习5.1_2 输入两个整数,for循环+1计数得到总和并输出,这样的计算可以进行三次
#include<iostream>
using namespace std;
int main() {
int i, j; //定义输入整数
//float mid,average; //定义中间判断值、总和以及平均值
for (int k = 0; k < 3; k++) {
double sum = 0;
cout << "Please enter two integer from small to large:";//简单提示让用户从小到大开始输入
cin >> i >> j;
for (i; i <= j; i++) {
sum += i;
}
cout << "The sum of two intergers is:" << sum << '.' << endl;
}
system("pause");
return 0;
}
2.重写程序清单5.4,使用array类对象
//练习5.2 使用array重写程序清单5.4
#include<iostream>
#include<array>
using namespace std;
const int Size = 16;
int main() {
array<long long,Size>factor; //定义int类型array对象factor
factor[1] = factor[0]=1LL;
for (int i = 2; i < Size; i++) {
factor[i] = factor[i - 1]*i;
}
for (int j = 0; j < Size; j++) {
cout << j << "!= " << factor[j]<<endl;
}
system("pause");
return 0;
}
- 要求用户输入数字,一直到输入0就结束输入,程序计算累计和。
//练习5.3 编写输入数字的程序,输出结束标志是0,输入后,程序自动输出目前为止的累计和。
#include<iostream>
using namespace std;
//const int Size = 16;
int main() {
double i, sum = 0;
cout << "Please enter a number:";
cin >> i;
while (i) {
sum = sum + i;
cout << "The total of the numbers which was inputed is:" << sum << endl;
cout << "Please enter another number :";
cin >> i;
}
system("pause");
return 0;
}
- 对Daphne和Cleo的投资进行计算,两个人都投入100刀,前者每年定额返回利润(10刀),后者每年返回固定比例的利润(当前存款的5%),计算多少年后Cleo的投资超过Daphne的。
//练习5.4 有两个人,A投了100dollars然后每年得到定额利润10dollars;B也投入100dollars,但每年得到的是当前的5%,
//循环计数得出B超过A资产的年数。
#include<iostream>
using namespace std;
//const int Size = 20;
int main() {
long a,b;
a = b = 100LL;
bool p = true; int i = 0;
while(p) {
a += 10;
b = b * 1.05;
p = (a> b);
cout << i << " years has passed.";
cout << "The money A got :" << a << " dollars.And B has " << b << " dollars." << endl;
i++;
}
cout << i << " years has passed.";
cout << "The money A got :" << a << " dollars.And B has " << b << " dollars." << endl;
system("pause");
return 0;
}
这个代码根据输出语句的使用,也有两个版本,上面的是可以看到两个人每年的资产变化,一直到前者不再大于后者。如果你仅仅想知道一个结果,那就把循环里面的输出语句删掉就可以了,下面的结果展示就是第一版的。
- 对书本C++ For Fools的销售量进行输入存储(分成一年12月),最后输出一年总销售量。
//练习5.5 编写程序存入月销量,最后汇总输出年销量
//一个string,进行每月销量的提示;一个销量存储数组,存入每月销量。
#include<iostream>
#include<string>
using namespace std;
const int month = 12;
int main() {
string m[month] = { "Jan.","Feb.","Mar.","Apr.","May.","Jun.","July","Aug.","Sep.","Oct.","Nov.","Dec." };
int sum=0;
int *numbers=new int[month];
for (int i = 0; i < month; i++) {
cout <<"The sale volume of "<< m[i] << ":";
cin >> numbers[i];
sum += numbers[i];
}
cout << "The sales volume of 12 months is :"<<sum << endl;
delete[] numbers;
system("pause");
return 0;
}
- 习题5的基础上进行改动,使用二维数组进行存储3年内每个月的销售额,最后输出3年总销量。
//练习5.6 编写程序存入月销量,最后汇总输出年销量
//一个string,进行每月销量的提示;一个销量存储二维数组,存入每年每月销量。
//两个for循环计数,最里的输入存储一年内每月销量,合并计数;外面的输出年总销量并计数总销量
#include<iostream>
#include<string>
using namespace std;
const int month = 12;
const int year = 3;
int main() {
string m[month] = { "Jan.","Feb.","Mar.","Apr.","May.","Jun.","July","Aug.","Sep.","Oct.","Nov.","Dec." };
int sum = 0; int summary = 0;
int sales[month][year];
for (int i = 0; i < year; i++) {
for (int j = 0; j < month; j++) {
cout << "The sale volume of " << m[j] << ":";
cin >> sales[i][j]; //输入销量
sum += sales[i][j]; //年总销量
}
cout << "The sales volume of this year is :" << sum << endl;
summary += sum; //总销量
sum = 0; //重新置零,方便进入下一年循环
}
cout << "The sales volume of three year is :" << summary << endl;
system("pause");
return 0;
}
结果和上面练习5的类似,就不做展示了。
7. 设计car结构体,义如其名,用来存储汽车生产商、生产年份的。建立动态结构体数组catalog对用户进行提示,存储输入信息最后进行展示。
//练习5.7 编写信息查询结构,查询用户车辆生产厂商和生产年份
//结构体car,包含信息:生产厂商和生产年份
//建立动态car数组,存储用户输入信息;for循环查询记录生产厂商和生产年份
//for循环输出生产年份和生产厂商
#include<iostream>
#include<string>
using namespace std;
const int max = 10;
struct car
{
string brand;
int year;
};
int main() {
car *catalog =new car[max]; //建立存储用户信息用动态结构体数组
int n;
cout << "How many cars do you wish to catalog?";
cin >> n;
cin.get(); //消化输入产生的换行符,为接下来的字符输入做准备
for (int i = 0; i < n; i++) {
cout << "Car #"<<i+1<<':'<<endl;
cout << "Please enter the manufacturer of the car #"<<i+1<<':';
getline(cin, catalog[i].brand); //输入汽车生产厂商
cout << "Please enter the year of production about your car #"<<i+1<<':';
cin >> catalog[i].year; //输入汽车生产年份
cin.get(); //消化换行
}
cout << "Here is the information of your car.Please check out." << endl;
for (int j = 0; j < n; j++) {
cout <<catalog[j].year<<' '<<catalog[j].brand<< endl;
} //循环输出汽车信息
delete[] catalog; //释放动态结构体数组
system("pause");
return 0;
}
- 循环读取输入单词,输出结束标志‘done’,程序最后输出结束前输入的单词数。
//练习5.8 使用char数组和循环读取输入单词,当用户输入‘done’就停止输入,并输出用户输入的单词数(不包括done)
#include <iostream>
#include <cstring>
using namespace std;
const int Size = 100;
int main()
{
char *record = new char[Size];
cout << "Please enter(When you want to quit,enter 'done'):";
int i = 0;
while (cin >> record)
{
if (strcmp(record, "done"))
{
cin.get();
i++;
}
else
break;
}
cout << "You entered a total of " << i << " words." << endl;
delete[] record;
system("pause");
return 0;
}
上面的程序我是心血来潮在vscode上面编写的,但复制粘贴后背景高亮和一些格式要求有点问题,所以仅供参考。
- 使用string改写练习8,操作会相对简单,所以我偷懒了,hhh。
10.首先,请求用户输入行数,然后输出对应行数星号,空缺由句点补上。
//练习5.10 嵌套循环输出带有星号和句点的图案
//输出提示,图案要多少行
//第一行有一颗星,第二行两颗,都分布在句尾并在星号前加上句号,以此类推
#include<iostream>
using namespace std;
void main() {
int n;
cout << "Please enter the numbers of rows:";
cin >> n;
for (int i = 0; i < n; i++) { //进行对应行数的输出
for (int j = 0; j<n-i-1; j++) { //输出(n-对应行)数的句点
cout << '.';
}
for (int k = 0; k<i+1; k++) { //输出对应行数的星号
cout << "*";
}
cout << endl;
}
system("pause");
}