1.编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间(包括这两个整数)所有整数的和。这里假设先输入较小的整数。例如,如果用户输入的是2和9,则程序将指出2~9之间所有整数的和为44。
#include <iostream>
int main()
{
using namespace std;
cout << "请输入两个整数"<<endl;
int a, b, temp;
cout << "a:_\b";
cin >> a;
cout << "b:_\b";
cin >> b;
if(a > b)
{
temp = a;
a = b;
b = temp;
}
int i,sum = 0;
for(i = a; i <= b; i++)
sum += i;
cout << a << "~" << b << "的和为" << sum << endl;
return 0;
}
2.使用aray对象(而不是数组)和 long double(而不是 long long)重新编写程序清单5.4,并计算100!的值。
#include <iostream>
#include <array>
const int ArSize = 100;
int main()
{
array<long double, ArSize+1> factorials;
factorials[0] = factorials[1] = 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;
}
return 0;
}
3.编写一个要求用户输入数字的程序。每次输入后,程序都将报告到目前为止,所有输入的累计和。当用户输入0时,程序结束。
#include <iostream>
int main()
{
using namespace std;
cout << "请输入数字,以计算输入的数字之和,输入0结束输入"<< endl;
int a, sum = 0;
while(cin >> a && a != 0)
sum += a;
cout << "所输入数字之和: " << sum << endl;
return 0;
}
- Daphne以10%的单利投资了100美元。也就是说,每一年的利润都是投资额的10%,即每年10美元:
利息=0.10×原始存款
而Cleo以5%的复利投资了100美元。也就是说,利息是当前存款(包括获得的利息)的5%,
利息=0.05×当前存款
Cleo在第一年投资100美元的盈利是5%得到了105美元。下一年的盈利是105美元的5%即25美元,依此类推。请编写一个程序,计算多少年后,Cleo的投资价值才能超过 Daphne的投资价值并显示此时两个人的投资价值。
#include <iostream>
const double DanLi = 0.1;
const double FuLi = 0.05;
const double TouZi = 100.0;
int main()
{
using namespace std;
double DapJiaZhi,CleoJiaZhi;
double DapLiXi, CleoLiXi;
DapLiXi = TouZi * DanLi;
CleoLiXi = TouZi * FuLi;
DapJiaZhi = TouZi + DapLiXi;
CleoJiaZhi = TouZi + CleoLiXi;
int year = 1;
while(CleoJiaZhi < DapJiaZhi)
{
year++;
DapJiaZhi += DapLiXi;
CleoLiXi = CleoJiaZhi * FuLi;
CleoJiaZhi += CleoLiXi;
}
cout << year << "年后Cleo的投资价值才能超过 Daphne的投资价值"<<endl;
cout << "Cleo的投资价值: "<< CleoJiaZhi << endl;
cout << "Daphne的投资价值: "<< DapJiaZhi << endl;
return 0;
}
5.假设要销售《C++ For Fools》一书。请编写一个程序,输入全年中每个月的销售量(图书数量,而不是销售额).程序通过循环,使用初始化为月份字符串的char*数组(或 string对象数组)逐月进行提示,并将输入的数据储存在一个int数组中。然后,程序计算数组中各元素的总数,并报告这一年的销售情况。
#include<iostream>
#include<array>
int main()
{
using namespace std;
array<int,12> array;
string str[12] = {"january","february","march","april","may",
"june","july","august","september","october","november","december"};
cout << "请输入12个月份的销售量"<< endl;
int sum = 0;
for (int i = 0; i != 12; i++)
{
cout << str[i] << ":" << "_\b";
cin >> array[i];
while(array[i] < 0)
{
cout << "销售量为正数,请重新输入"<< endl;
cout << str[i] << ":" << "_\b";
cin >> array[i];
}
sum += array[i];
}
cout << "该年的销售量为:"<< sum << endl;
return 0;
}
6.完成编程练习5,但这一次使用一个二维数组来存储输入—3年中每个月的销售量。程序将报告每年销售量以及三年的总销售量。
#include<iostream>
#include<array>
int main()
{
using namespace std;
array<array<int,12>,3> array;
string str[12] = {"january","february","march","april","may",
"june","july","august","september","october","november","december"};
cout << "请输入3年12个月份的销售量"<< endl;
int sum[3] {0, 0, 0};
int total = 0;
for(int j = 0; j != 3; j++)
{
cout << "请输入第"<<j+1<<"年的销售量"<< endl;
for (int i = 0; i != 12; i++)
{
cout << str[i] << ":" << "_\b";
cin >> array[i][j];
while(array[i][j] < 0)
{
cout << "销售量为正数,请重新输入"<< endl;
cout << str[i] << ":" << "_\b";
cin >> array[i][j];
}
sum[j] += array[i][j];
}
}
for(int j = 0; j != 3; j++)
{
cout <<"第"<< j <<"年的销售量为:"<< sum[j] << endl;
total += sum[j];
}
cout << "3年的销售总量: " << total ;
return 0;
}
7.设计一个名为car的结构,用它存储下述有关汽车的信息:生产商(存储在字符数组或 string对象中的字符串)、生产年份(整数).编写一个程序,向用户询问有多少辆汽车。随后,程序使用new来创建个由相应数量的car结构组成的动态数组。接下来,程序提示用户输入每辆车的生产商(可能由多个单词组成)和年份信息。请注意,这需要特别小心,因为它将交替读取数值和字符串(参见第4章).最后,程序将显示每个结构的内容。该程序的运行情况如下:
How many cars do you wish to catalog? 2
Car #1:
Please enter the make: Hudson Hornet
Please enter the year made: 1952
Car #2:
Please enter the make: Kaiser
Please enter the year made: 1951
Here is your collection:
1952 Hudson Hornet
1951 Kaiser
#include<iostream>
#include<cstring>
struct Car
{
std::string Company;
int MadeYear;
} ;
int main()
{
using namespace std;
cout << "How many cars do you wish to catalog?"<< endl;
int cars;
cin >> cars;
cin.get();
Car *pt = new Car [cars];
for (int i = 0; i != cars; i++)
{
cout << "Car#" << i+1 << endl;
cout << "Please enter the make: ";
getline(cin,pt[i].Company);
cout << "Please enter the year made: ";
cin >> pt[i].MadeYear;
cin.get();
}
cout << "Here is your collection:"<< endl;
for (int i = 0; i != cars; i++)
cout << pt[i]. MadeYear <<" " << pt[i].Company << endl;
delete [] pt;
return 0;
}
8.编写一个程序,它使用一个char数组和循环来每次读取一个单词,直到用户输入done为止。随后该程序指出用户输入了多少个单词(不包括done在内).下面是该程序的运行情况:
Enter words (to stop, type the word done):
anteater birthday category dumpster
envy finagle geometry done for sure
You entered a total of 7 words
您应在程序中包含头文件 cstring,并使用函数 strcmp()来进行比较测试。
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
char str[20];
cout << "Enter words (to stop, type the word done):"<< endl;
int total = 0;
cin >> str;
while(strcmp(str, "done"))
{
total++;
cin >> str;
}
cout << "您输入的单词总共有"<<total<<"个"<< endl;
return 0;
}
9.编写一个满足前一个练习中描述的程序,但使用 string对象而不是字符数组。请在程序中包含头文件 string,并使用关系运算符来进行比较测试。
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
string str;
cout << "Enter words (to stop, type the word done):"<< endl;
int total = 0;
while(cin >> str && str != "done")
total++;
cout << "您输入的单词总共有"<<total<<"个"<< endl;
return 0;
}
10.编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应行数的星号,其中第一行包括一个星号,第二行包括两个星号,依此类推。每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号前面加上句点。该程序的运行情况如下:
#include <iostream>
int main()
{
using namespace std;
unsigned int row = 0;
cout << "Enter number of rows: ";
cin >> row;
for (unsigned int i = 1; i <= row; i++)
{
for (unsigned int j = i; j < row; j++)
{
cout << ".";
}
for (unsigned int j = 0; j < i; j++)
{
cout << "*";
}
cout << endl;
}
return 0;
}