答案仅供参考,实际运行效果取决于运行平台和运行软件
1.编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间包括这两个整数)所有整数的和。这里假设先输入较小的整数。例如,如果用户输入的是2和则程序将出29之间所有整数的和为44
#include <iostream>
using namespace std;
int main()
{
int num1, num2, sum = 0;
cout << "Please enter the first integer: ";
cin >> num1;
cout << "Please enter the second integer: ";
cin >> num2;
for (int i = num1; i <= num2; i++)
{
sum += i;
}
cout << "Sum of " << num1 << " to " << num2 << " are " << sum << endl;
return 0;
}
2.使用array对象(而不是数组)和ong double(而不是lng long)重新编写序清单5.4,并计算100!的值
#include <iostream>
#include <array>
using namespace std;
int main()
{
const int ArSize = 101;
array<long double, ArSize> factorials;
factorials[0] = factorials[1] = 1L;
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>
using namespace std;
int main()
{
long long num;
long long sum = 0LL;
while (cout << "Please enter an integer(0 to quit): ", cin >> num && num != 0)
{
//↑逗号运算符只取最后的结果作为判断条件;
sum += num;
cout << "Sum of all integers are " << sum << endl;
}
return 0;
}
4.Daphn以10%的单利投资了100美元。也就是说每年的利润都是投资额的10%即每年10美元:
利息 = 0.10X 原始存款
而 CIeo 以 5%的复利投资了 100 美元。也就是说,利息是当前存款《包括获得的利息)的 5%,
利息= 0.05X当前存款Cleo 在第一年投资 100 美元的盈利是5%一一得到了 105 美元下一年的盈利是 105 美元的 5%一一即5.25 美元,依此类推。请编写一个程序,计算多少年后,CIco 的投资价值才能超过 Daphne 的投资价值并显示此时两个人的投资价值。
#include <iostream>
using namespace std;
int main()
{
int n = 0;
double daphne_money = 100;
double cleo_money = 100;
while (cleo_money <= daphne_money)
{
cout << "Year " << ++n << ':' << endl;
daphne_money += 10;
cleo_money += cleo_money * 0.05;
cout << "Cleo's money = " << cleo_money;
cout << ", Daphne's money = " << daphne_money << endl;
}
cout << "After " << n << " years, ";
cout << "Cleo's money";
cout << " > Daphne's money." << endl;
return 0;
}
5.假设要销售《C++ For Fools》 一书。请编写一个程序,输入全年中每个月的销售量(图书数量,而不是销售额)程序通过循环,使用初始化为月份字符的 har *数组(或 string 对象数组)逐月进行提示并将输入的数据储存在一个 iit 数组中。然后,程序计算数组中各元素的总数,并报告这一年的销售情况
#include <iostream>
#include <string>
using namespace std;
int main()
{
const int ArSize = 12;
const string months[ArSize] =
{
"January", "February","March",
"April", "May", "June", "July",
"August","September", "October",
"November", "December"
};
int sum = 0, sales_volume[ArSize];
for (int i = 0; i < ArSize; i++)
{
cout << "Please enter number of books sold (";
cout << months[i] << "): ";
cin >> sales_volume[i];
}
for (int i = 0; i < ArSize; i++)
{
sum += sales_volume[i];
}
cout << "A total of " << sum << " <<C++ For Fools>> books were sold in a year." << endl;
return 0;
}
.6.完成编程练习 5,但这一次使用一个二维数组来存钻输入一一3 年中每个月的销售量。程序将报告每年销售量以及三年的总销售量。
#include <iostream>
#include <string>
using namespace std;
const int NUM = 3;
const int ArSize = 12;
int show_result(int (*x)[ArSize], int n);
int main()
{
const string months[ArSize] =
{
"January", "February","March",
"April", "May", "June", "July",
"August","September", "October",
"November", "December"
};
int sum, total, sales_volume[NUM][ArSize];
for (int i = 0; i < NUM; i++)
{
cout << "Year " << i + 1 << ": " << endl;
for (int j = 0; j < ArSize; j++)
{
cout << "Please enter number of books sold (";
cout << months[j] << "): ";
cin >> sales_volume[i][j];
}
cout << endl;
}
sum = total = show_result(sales_volume, 0);
cout << "A total of " << sum << " <<C++ For Fools>> books were sold in the first year." << endl;
total += sum = show_result(sales_volume, 1);
cout << "A total of " << sum << " <<C++ For Fools>> books were sold in the second year." << endl;
total += sum = show_result(sales_volume, 2);
cout << "A total of " << sum << " <<C++ For Fools>> books were sold in the third year." << endl;
cout << "A total of " << total << " <<C++ For Fools>> books were sold in three years." << endl;
return 0;
}
int show_result(int (*x)[ArSize], int n)
{
int sum = 0;
for (int i = 0; i < ArSize; i++)
{
sum += x[n][i];
}
return sum;
}
7.设计一个名为 car 的结构,用它存储下述有关汽车的信息:生产商《存储在字特数组成 sting 对象中的字符串)、生产年份(整数)。编写一个程序,向用户询问有多少辆汽车。随后,程序使用 new 来创建一个由相应数量的 C 结构组成的动态数组。接下来,程序提示用户输入车的生产商(可能由多个单词组成)和年份信息。请注意,这需要特别小心,因为它将交替读取数值和字符串《参见第 4 章)。最后程序将显示每个结构的内容。该程序的运行情况如下:
How many cars do you wish to catalog? 2
Car i1:
Please enter the make: ludgom formet
Pleage enter the year made: 1952
Car 月2:
Pleame ontes the make: Kaiger
Please enter the year made: 1951
Here is your collection;
1952 Fudgon Hornet
195] Kaiser
#include <iostream>
#include <string>
using namespace std;
struct car
{
string producer;
int year_of_introducion;
};
int main()
{
int num;
cout << "How many cars do you wish to catalog? ";
(cin >> num).get();
car *many_cars = new car[num];
for (int i = 0; i < num; i++)
{
cout << "Car #" << i + 1 << ':' << endl;
cout << "Please enter the make: ";
getline(cin, many_cars[i].producer);
cout << "Please enter the year made: ";
(cin >> many_cars[i].year_of_introducion).get();
}
cout << "Here is your collection:" << endl;
for (int i = 0; i < num; i++)
{
cout << many_cars[i].year_of_introducion;
cout << ' ' << many_cars[i].producer << endl;
}
delete[] many_cars;
return 0;
}
8.编写一个程序,它使用一个 char 数组和环来每次读取一个单词,直用户输入 done 为止。随后该程序指出用户输入了多少个单词(不包括 done 在内)。下面是该程序的运行情况:
Enter words (to stop, type the word donel :
anteater birthday category dumpater
envy finagle geometry dome for sure
You entered a total of 7 words
您应在程序中包含头文件 cstring,并使用雨数 stremp()来进行比较测试
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
const int ArSize = 256;
char str[ArSize];
unsigned int count = 0;
cout << "Enter words (to stop, type the word done):" << endl;
while (cin >> str, strcmp("done", str))
{
++count;
}
cout << "You entered a total of " << count << " words." << endl;
return 0;
}
9.编写一个满足前一个练习中描述的程序,但使用 sting 对象而不是字符数组。请在程序中包含文件 string,并使用关系运算符来进行比较测试。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
unsigned int count = 0;
cout << "Enter words (to stop, type the word done):" << endl;
while (cin >> str, str != "done")
{
++count;
}
cout << "You entered a total of " << count << " words." << endl;
return 0;
}
10.编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应行数的星号,其中第一行包括一个星号,第二行包括两个星号,依此类推。每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号前面加上句点。该程序的运行情况如下:
Enter qumber of rOwa: 5
....*
...**
..***
.****
*****
#include <iostream>
using namespace std;
int main()
{
int row;
cout << "Enter number of rows: ";
cin >> row;
for (int i = 1; i <= row; i++)
{
for (int j = i; j <= row - 1; j++)
{
cout << ".";
}
for (int j = 1; j <= i; j++)
{
cout << "*";
}
cout << endl;
}
return 0;
}