1、编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间(包括这两个整数)所有整数的和。这里假设先输入最小的整数。例如,如果用户输入的是2和9,则程序将支出2~9之间的所有整数的和为44。
#include <iostream>
using namespace std;
int main() {
int Num1;
int Num2;
int Total;
int i;
cout <<"Please Enter the number :";
cin >> Num1;
cin >> Num2;
cout <<"You enter number is "<<Num1<<"~"<<Num2<<endl;
for (i = Num1; i <=Num2; ++i) {
Total+=i;
}
cout <<"Form "<<Num1<<"~"<<Num2<<", Total is "<< Total <<endl;
return 0;
}
2、使用array对象(而不是数组)和long double(而不是long long)重新编写程序清单5.4,并计算100!的值。
#include <iostream>
#include <array>
const int ArSize = 100;
using namespace std;
int main() {
std::array<long double, ArSize> factorials;
factorials[0] = factorials[1] = 1LL;
for (int i = 2; i <= ArSize; ++i) {
factorials[i] = i * factorials[i-1];
}
for (int i = 0; i <= ArSize; ++i) {
std::cout<<i<<"! = "<<factorials[i]<<std::endl;
}
return 0;
}
3、编写一个要求用户输入数字的程序。每次输入后,程序都将报告到目前为止,所有输入的累积和。当用户输入0时,程序结束。
#include <iostream>
using namespace std;
int main() {
int Number;
int Total;
while (true) {
cout << "Please Enter the Number:";
cin >> Number;
if (Number == 0) {
cout << "The End Number is " << Number << endl;
cout << "The Total is " << Total<<endl;
break;
}
else {
Total += Number;
}
}
return 0;
}
4、Daphne以10%的单利投资了100美元。也就是说,每一年的利润都是投资额的10%,即每年10美元;
利息=0.1X原始存款
而Cleo以5%的复利投资了100美元。也就是说。利息是当前存款(包括获得的利息)的5%
利息=0.5X当前存款
Cleo在第一年投资100美元的适合盈利是5%---得到了105美元,下一年的盈利是105美元的5%--即5.25,以此类推。请编写一个程序,计算多少年后,Cleo的投资价值才能超过Daphne的投资价值,并显示此时两个人的投资价值。
#include <iostream>
using namespace std;
int main() {
float DTiMon = 100;
float CTiMon = 100;
float Interest1;
float Interest2;
int i = 0;
while (true){
Interest1 = 0.1 * 100;
Interest2 = 0.05 * CTiMon;
DTiMon+=Interest1;
CTiMon+=Interest2;
i++;
if (DTiMon<CTiMon)break;
false;
}
cout <<"This is After "<< i <<" years"<<endl;
return 0;
}
5、假设要销售《C++ For Fools》一书。请编写一个程序,输入全年中每个月的销售量(图书数量,而不是销售额)。程序通过循环,使用初始化为月份的字符串char*数组(或string对象数组)逐月进行提示,并将输入的数据存储在一个int数组中。然后,程序计算数组中各元素的总数,并报告这一年的销售情况。
#include <iostream>
using namespace std;
int main() {
const int Asize = 12;
char *Months[Asize]= {"January","February","March","April","May","June","July","August","September","October","November","December"};
int Money[Asize];
int Total;
for (int i = 0; i < Asize; ++i) {
cout << Months[i]<<endl;
cout<< "Enter the Money : ";
cin >> Money[i];
Total+=Money[i];
}
cout<<"This year the Total Number is "<< Total <<endl;
return 0;
}
6、完成编程练习5,但这一次使用一个二维数组来存储输入--3年终每个月的销售量。程序将报告每年销售量以及三年的总销售量。
#include <iostream>
using namespace std;
const int Asize = 12;
const int Year = 3;
int main() {
char * Months[Asize]= {"January","February","March","April","May","June","July","August","September","October","November","December"};
int Money[Year][Asize];
int Total1,Total2;
for (int j = 0; j < Year; ++j) {
cout<<"This is "<<j+1<<" year"<<endl;
Total2 = 0;
for (int i = 0; i < Asize; ++i) {
cout << Months[i]<<endl;
cout<< "Enter the Money : ";
cin >> Money[j][i];
Total1+=Money[j][i];
Total2+=Money[j][i];
}
cout <<"This year the Total Number is "<< Total2<<endl;
}
cout<<"Three years the Total Number is "<< Total1 <<endl;
return 0;
}
7、设计一个名为car的结构,用它存储下述有关汽车的信息:生产商(存储在字符数组或string对象中的字符串)、生产年份(整数)。编写一个程序,向用户询问有多少辆汽车。随后程序使用new来创一个由相应数量的car结构组成的动态数组。接下来,程序提示用户每辆车的生产商(可能有多个单词组成)和年份信息。请注意,这需要特别小心,因为它酱交替读取数值和字符串(参见第四章)。最后,程序将显示每个结构的内容。该程序的运行情况如下:
#include <iostream>
struct Car{
char ProName[20];
int ProYear;
};
using namespace std;
int main() {
int n;
cout <<"Please Enter Car Number : ";
cin >> n;
while (cin.get()!= '\n');
Car *car = new Car[n];
for (int i = 0; i <n ; ++i) {
cout <<"Please Enter the make :";
cin.getline(car[i].ProName,20);
cout <<"Please Enter the year made : ";
cin >> car[i].ProYear;
while (cin.get()!= '\n');
}
cout<<"Here is your collection"<<endl;
for (int j = 0; j < n; ++j) {
cout<<car[j].ProYear<<" "<<car[j].ProName<<endl;
}
delete []car;
return 0;
}
不知道下面这个语句干啥用的==自己的坑。唔我会记得去看的。
while (cin.get()!= '\n');
8、编写一个程序,它使用char数组和循环来每次读取一个单词,知道用户输入done为止。随后,该程序指出用户输入了多少个单词(不包括done在内)。下面是该程序的运行情况。
Enter words(to stop, type the word done):
anteater birthday category dumpaster
envp finagle geometry done for sure
You entered a total of 7 words.
您应在程序中包含头文件cstring,并使用函数strcmp()来进行比较测试。
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char Words[]{};
int i = 0;
cout<<"Enter words(to stop, type the word done): ";
while (true){
cin>>Words;
i++;
if(strcmp(Words,"done") == 0){
cout<<"You entered a total of "<< i-1 <<" words";
break;
}
}
return 0;
}
9、编写一个满足前一个练习中描述的程序,但使用string对象而不是字符数组。请在程序中包含头文件string,并使用关系运算符来比较测试。
#include <iostream>
#include <cstring>
using namespace std;
int main() {
string Words;
int i = 0;
cout<<"Enter words(to stop, type the word done): ";
while (true){
cin>>Words;
i++;
if(Words.compare("done") == 0){
cout<<"You entered a total of "<< i-1 <<" words";
break;
}
}
return 0;
}
10。编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应行数的星号,其中第一行包括一个星号,第二行包括两个星号,以此类推,每一行包括的字符数等于用户指定的行数,在星号不够的情况下,在星号前面加上句点,该程序的运行情况如下。
Enter number of rows :5
....*
...**
..***
.****
*****
#include <iostream>
using namespace std;
int main() {
int Factor;
int Per;
int Per1;
int Per2;
cout <<"Enter number of rows: ";
cin>> Factor;
for (Per = 0; Per <Factor; ++Per) {
Per1 = Factor - Per;
for (Per2 = 1; Per2 < Per1; ++Per2) {
cout<<".";
}
for ( ; Per2<=Factor ; ++Per2) {
cout<<"*";
}
cout<<endl;
}
return 0;
}