本文共 6020 字,大约阅读时间需要 20 分钟。
1.编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间(包括这两个整数)的所有整数的和。这里假设先输入较小的整数。例如,如果用户输入的是2和9,则程序指出2~9之间的所有整数和为44。(增加难度,由程序自行判断哪个大哪个小)
答:
#include<iostream>
int main()
{
using namespace std;
int a, b;
cout << "请输入两个数字,系统将自动帮你计算这两个数字之间(包括这两个数字)所有整数的和:" << endl;
cout << "请输入第一个数字: ";
cin >> a;
cout << "请输入第二个数字: ";
cin >> b;
int c, d; //声明变量c和d,预设c比d大,方便计算
if (a > b) { c = a, d = b; }
if (a < b) { d = a, c = b; }
if (a == b) { cout << "你输入的两个数字一样大,那么和为:" << a << endl;system("pause");return 0; }
int total=0; //total为总和
for (c = c;c > d;c--)
{
total = total + c; //total等于之前和与c-1之后的和相加
}
total = total + d;
cout << "按照你输入的数字进行计算,和为:" << total << endl;
system("pause");
return 0;
}
2.使用array对象(而不是数组)和long double(而不是long long)重新编写程序清单5.4,并计算100!的值。
答:
#include<iostream>
#include<array>
int main()
{
using namespace std;
const int a = 16;
array<long double, 101>b; //创建数组,类型long double,数组成员个数15,名字为b
b[0] = b[1] = 1; //预设数组1和0都为1。
cout << b[0] << "! = " << b[0] << endl;
for (int i = 1;i < a;i++)
{
b[i] = i * b[i - 1];
cout << i << "! = " << b[i] << endl;
}
//以上用于显示原来代码输出的结果
for (int i = 16;i <= 100;i++) //这个用于计算到100的阶乘
{
b[i] = i*b[i - 1];
}
cout << 100 << "! = " << b[100] << endl; //输出100的阶乘
system("pause");
return 0;
}
3.编写一个要求用户输入数字程序。每次输入后,程序都将报告到目前为止,所有输入的累计和。当用户输入0时,程序结束。
答:
#include<iostream>
int main()
{
using namespace std;
cout << "本程序用于计算你输入的所有数字的和,到你输入0为止:" << endl;
int a = 0, total = 0, i = 1;
cout << "请输入第" << i << "个数字(输入0则结束):";
cin >> a;
total = total + a;
for (i = 1;a != 0;i++)
{
cout << "请输入第" << i+1 << "个数字(输入0则结束):";
cin >> a;
total = total + a;
}
cout << "你一共输入了" << i-1 << "个数字。他们的总和为:" << total << endl; //因为最后输入的0不算,所以要减i-1
system("pause");
return 0;
}
4.Daphne以10%的单利投资了100美元。也就是说,每一年的礼人都是投资额的10%。即每年10美元;
利息=0.10×原始存款
而Cleo以5%的复利投资了100美元。也就是说,利息是当前存款(包括获得的利息)的5%,:
利息=0.05×当前存款
Cleo在第一年投资100美元的盈利是5%——得到了105美元。下一年的盈利是105美元的5%——即5.25美元,依次类推。请编写一个程序,计算多少年后,Cleo的投资价值才能超过Daphne的投资价值,并显示此时两个人的投资价值。
答:
#include<iostream>
int main()
{
using namespace std;
double D=100, C=100;
int i;
for (i = 1;D >= C;i++) //设定为i为年份,且循环为第i年结束时,两人的资产
{
D = D + 0.1 * 100; //第i年结束时两人的资产
C = C*1.05;
}
cout << "当第" << i - 1 << "年结束时,即到达第" << i << "年,Cleo的资产大于Daphne的资产。\n此时Daphne的资产为:" << D << "美元。\nCleo的资产为:" << C << "美元。" << endl;
system("pause");
return 0;
}
5.假设要销售《C++ For Fools》一书。请编写一个程序,输入全年中每个月的销售类(图书数量,而不是销售额)。程序通过循环,使用初始化为月份字符串的char*数组(或string对象数组)逐月进行提示,并将输入的数据储存在一个int数组中。然后,程序将计算数组中各元素的总数,并报告这一年的销售情况。
答:
#include<iostream>
int main()
{
using namespace std;
int total = 0;
char *moon[12] = { "一","二","三","四", "五","六", "七","八", "九","十","十一","十二" };
cout << "请按要求输入《C++ For Fools》每个月的销售量(本):" << endl;
int abc;
for (int i = 0;i < 12;i++)
{
cout << "请输入" << moon[i] << "月的销售额:";
cin >> abc;
total = total + abc;
}
cout << "全年图书销售总额为:" << total << "本。" << endl;
system("pause");
return 0;
}
6.完成编程练习5,但这一次使用一个二维数组来储存输入——3年中每个月的销售量以及三年的总销售量。
答:
#include<iostream>
int main()
{
using namespace std;
const int year = 3, moon = 12;
int book[year][moon];
int total = 0;
for (int y = 0;y < year;y++)
{
for (int x = 0;x < moon;x++)
{
cout << "请输入第" << y+1 << "年" << x+1 << "月的销售数量:";
cin >> book[year][moon];
total = total + book[year][moon];
}
}
cout << "经过计算,三年图书销售总额为" << total << "本。" << endl;
system("pause");
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 mad: 1952
Car #2:
Please enter the make: Kaiser
Please enter the year mad: 1951
Here is your collection:
1952 Hunson Hornet
1951 Kaiser
答:
#include<iostream>
#include<string>
struct car
{
std::string name;
int year;
};
int main()
{
using namespace std;
int liang; //声明车的数量
cout << "How many cars do you wish to catalog? ";
cin >> liang;
car *your = new car[liang]; //new一个结构数组,数组成员有liang个,即用户输入的数字
for (int i = 0;i < liang;i++) //这里是输入部分的循环
{
cin.get(); //读取掉无意义的换行符或者空格等
cout << "Car #" << i + 1 << endl;
cout << "Please enter the make: ";
getline(cin, your[i].name); //用于读取一行,防止名字之间有空格
cout << "Please enter the year mad: ";
cin >> your[i].year;
}
cout << "Here is your collection:" << endl;
for (int i = 0;i < liang;i++)
{
cout << your[i].year << " " << your[i].name << endl;
}
system("pause");
return 0;
}
8.编写一个程序,它使用一个char数组和循环来每次读取一个单词,直到用户输入done为止。随后,该程序指出用户输入了多少个单词(不包括done在内)。下面是该程序的运行情况:
Enter words (to stop, type the word done):
anteater birthday category dumpster
envy figagle geometry done for sure
You enter a total of 7 words
您应在程序中包含头文件cstring,并使用函数strcmp()来进行比较测试。
答:
#include<iostream>
#include<cstring>
int main()
{
using namespace std;
int i = 0;
cout << "Enter words (to stop, type the word done):" << endl;
char word[20];
cin >> word; //用cin是因为利用cin不读取空格、回车、tab的特点
for (i = 0;strcmp(word,"done");i++) //每次比较失败后,读取下一个,因为不是done,所以成功读取了一个单词,于是i+1
{
cin >> word;
}
cout << "You enter a total of " << i << " words" << endl;
system("pause");
return 0;
}
9.编写一个满足前一个练习中描述的程序,但使用string对象而不是字符数组。请在程序中包含头文件string,并使用关系运算符来进行比较测试。
答:
#include<iostream>
#include<string>
int main()
{
using namespace std;
int i = 0;
cout << "Enter words (to stop, type the word done):" << endl;
string word;
cin >> word; //用cin是因为利用cin不读取空格、回车、tab的特点
for (i = 0;word!="done";i++) //每次比较失败后,读取下一个,因为不是done,所以成功读取了一个单词,于是i+1
{
cin >> word;
}
cout << "You enter a total of " << i << " words" << endl;
system("pause");
return 0;
}
10.编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应行数的星号,其中第一行包括一个星号,第二行包括两个星号,依次类推,每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号前面加上句点。该程序的运行情况如下:
Enter number of rows; 5
....*
...**
..***
.****
*****
答:
#include<iostream>
int main()
{
using namespace std;
int row;
cout << "Enter number of rows; ";
cin >> row;
int i;
for (i = 1;i <= row;i++) //变量i为行,行数不能大于等于row
{
for (int a = 0;a < row - i;a++) //a为输出.的数量,row-i为第i行输出.的数量(即row为5的时候,第1行只能输入5-1个,
cout << ".";
for (int b = 0;b < i;b++) //第i行输出i个*
cout << "*";
cout << endl;
}
system("pause");
return 0;
}
转载地址:http://dybbl.baihongyu.com/