C++ prime Plus 第五章编程练习答案


总结

for循环、while循环和do while循环
char ch;
cin >> ch; // 会忽略空格、换行符和制表符,将输入的下一个字符读入到ch中。
cin.get(ch); // 不管该字符是什么
char finiched[] = “done”;
char words[20];
strcmp(finiched, words); // 如果字符串相同返回true
string str = “abc”;
if (str != finiched){} // string对象可以通过==和!=比较字符串


一、第一题

题目:编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间(包括这两个整数)所有整数的和。这里假设先输入较小的整数。例如,如果用户输入的是2和9,则程序将指出2-9之间所有整数的和为44.

#include <iostream>
int SumNumber(int minNum_, int maxNum_);
int main() {
	int minNum, maxNum;
	int Sum;
	std::cin >> minNum >> maxNum;
	Sum = SumNumber(minNum, maxNum);
	std::cout << Sum;
}
int SumNumber(int minNum_, int maxNum_) {
	int Sum = 0;
	for (minNum_; minNum_ <= maxNum_; minNum_++) {
		Sum += minNum_;
	}
	return Sum;
}

在这里插入图片描述

二、第二题

题目:使用array对象(而不是数组)和long double(而不是long long)重新编写程序清单5.4,并计算100!的值。

#include <iostream>
#include <array>
const int ArSize = 101;
int main() {
	std::array<long double, ArSize>factorials;
	factorials[0] = factorials[1] = 1;
	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;
}

在这里插入图片描述
在这里插入图片描述

三、第三题

题目:编写一个要求用户输入数字的程序。每次输入后,程序都将报告到目前为止,所有输入的累计和。当用户输入0时,程序结束。

#include <iostream>
int main() {
	double inputNum;
	double totalNum = 0;
	bool result = true;
	while (result) {
		std::cin >> inputNum;
		if (inputNum != 0) {
			totalNum += inputNum;
			std::cout << "累计和:"<<totalNum<<std::endl;
			continue;
		}
		break;
	}
	return 0;
}

在这里插入图片描述

四、第四题

题目: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() {
	double DapheMoney = 100;
    double CleoMoney = 100;
    int years = 0;
    do {
    years++;
    DapheMoney = DapheMoney + 0.1 * 100;
    CleoMoney = CleoMoney * 1.05;
    } while (CleoMoney <= DapheMoney);
    std::cout << years << " years later: " << "DapheMoney: " << DapheMoney << " CleoMoney: " << CleoMoney;
    return 0;
}

在这里插入图片描述

五、第五题

题目:假设要销售《C++ For Fools》一书。请编写一个程序,输入全年中每个月的销售量(图书数量,而不是销售额)。程序通过循环,使用初始化为月份字符串的char*数组(或string对象数组)逐月进行提示,并将输入的数据储存在一个int数组中。然后,程序计算数组中各元素的总数,并报告这一年的销售情况。

#include <iostream>
int main() {
    const int ArSize = 12;
    const std::string BooksMonth[ArSize] = {
    "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "Decmber"
    };
    int BooksNum[ArSize] = {};
    int TotalSum = 0;
    for (int i = 0; i < ArSize; i++) {
    std::cout << BooksMonth[i] << ": ";
    std::cin >> BooksNum[i];
    TotalSum += BooksNum[i];    
    }
    std::cout << "total sum: " << TotalSum;
    return 0;
}

在这里插入图片描述

六、第六题

题目:完成编程练习5,但这一次使用一个二维数组来存储输入——3年中每个月的销售量。程序将报告每年的销售量以及三年的总销售量。

#include <iostream>
int main() {
    const int ArSize = 12;
    const std::string BooksMonth[ArSize] = {
    "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "Decmber"
    };
    int BooksNum[3][ArSize + 1] = {};
    int count = 0;
    while (count < 3)
    {
    for (int i = 1; i < ArSize; i++) {
        std::cout << BooksMonth[i] << ": ";
        std::cin >> BooksNum[0][i];
        BooksNum[count][12] += BooksNum[0][i];
    }
    count++;
    }
    std::cout << "total sum: " << BooksNum[0][12] + BooksNum[1][12] + BooksNum[2][12];
    return 0;
}

在这里插入图片描述

七、第七题

题目:设计一个名为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 <string>
using std::cout;
using std::cin;
struct car {
    std::string makeproducer;
    int year;
};
int main() {
    int carsnum = 0;
    cout << "how many cars do you wish to catalog? ";
    cin >> carsnum;
    cin.get();    
    car* carlog = new car[carsnum];
    for (int i = 0; i < carsnum; i++) {
        cout << "car #" << i+1 << ":" << std::endl;
        cout << "please enter the make: ";
        getline(cin, carlog[i].makeproducer);
        cout << "please enter the year made: ";
        cin>>carlog[i].year;
        cin.get();
    }
    cout << "here is your collection" << std::endl;
    for (int i = 0; i < carsnum; i++) {
        cout << carlog[i].year << " " << carlog[i].makeproducer << std::endl;
    }
    return 0;
}

在这里插入图片描述

八、第八题

题目:编写一个程序,它使用一个char数组和循环来每次读取一个单词,直到用户输入done为止。随后,该程序指出用户输入了多少个单词(不包括done在内)。下面是该程序的运行情况:
Enter words (to stop, type the word done):
anteater birthday category dumpster
envy finagle geometry done for sure
You entered a toal of 7 words.
您应该在程序中包含头文件cstring,并使用函数strcmp()来进行比较测试。

#include <iostream>
using namespace std;

const int SIZE = 20;
const char FINISHED[] = "done";
int main() {
    cout << "Enter words (to stop, type the word done):"<<endl;
    int counter = 0;
    char words[SIZE];
    while (strcmp(FINISHED, words) != 0) {
        counter++;
        cin >> words;
        cin.get();
    }
    cout << "You entered a total of " << counter - 1 << " words.";
    return 0;
}

在这里插入图片描述

九、第九题

题目:编写一个满足前一个练习中描述的程序,但使用string对象而不是字符数组。请在程序中包含头文件string,并使用关系运算符来进行比较测试。

#include <iostream>
using namespace std;

const int SIZE = 20;
const string FINISHED = "done";
//const char FINISHED[] = "done";
int main() {
    cout << "Enter words (to stop, type the word done):" << endl;
    int counter = 0;
    string words;
    while (FINISHED != words) {
        counter++;
        cin >> words;
        cin.get();
    }
    cout << "You entered a total of " << counter - 1 << " words.";
    return 0;
}

在这里插入图片描述

十、第十题

题目:编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应的行数的星号,其中第一行包括一个星号,第二行包括两个星号,依次类推。每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号前面加上句点。该程序的运行情况如下:
Enter number of rows: 5
….*
…**
…***
.****


#include <iostream>
using std::cout;
using std::cin;
int main() {
    cout << "Enter number of rows: ";
    int rows;
    cin >> rows;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < rows - i-1; j++) {
            cout << ".";
        }
        for (int j = 0; j< i + 1; j++) {
            cout << "*";
        }
        cout << std::endl;
    }
    return 0;
}

在这里插入图片描述

谢谢观看,祝顺利。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值