第4章 斯特劳斯特鲁普钻头一个具有挑战性的一步(至少对我来说!
Chapter 4 Stroustrup Drill. A challenging step (at least for me!)
我正在学习Stroustrups的"使用C++编程原理和实践",并陷入了一个练习。
以下是适应症:
1) Write a program that consists of a while-loop that (each time around the loop) reads in two ints and then prints them. Exit the program when a terminating '|' is entered.
2)Change the program to write out the smaller value is: followed by the smaller of the numbers and the larger value is: followed by the larger value.
3)Augment the program so that it writes the line the numbers are equal (only) if they are equal.
4)Change the program so that it uses doubles instead of ints.
5)Change the program so that it writes out the numbers are almost equal after writing out which is the larger and the smaller if the two numbers differ by less than 1.0/100.
6) Now change the body of the loop so that it reads just one double each time around. Define two variables to keep track of which is the smallest and which is the largest value you have seen so far. Each time through the loop write out the value entered. If it’s the smallest so far, write the smallest so far after the number. If it is the largest so far, write the largest so far after the number.
这是我到目前为止的代码:
int main(){
double number1 = 0;
double number2 = 0;
double maximum = 0;
double minimum = 0;
cout << " Keep entering numbers. If you want to exit the program press alt + z" << endl;
while (cin >> number1 && cin >> number2) {
if (number1 == '@' || number2 == '@') {
break;
}
else if (number1 < number2){
cout << "The smaller value is " << number1 << '.' << endl;
cout << "The larger value is " << number2 << '.' << endl;
maximum = number2;
minimum = number1;
if (number2 - number1 < 0.01) {
cout << "The numbers are almost equal";
}
}
else if ( number1 > number2) {
cout << "The smaller value is " << number2 << '.' << endl;
cout << "The larger value is " << number1 << '.' << endl;
maximum = number1;
minimum = number2;
if (number1 - number2 < 0.01) {
cout << "The numbers are almost equal";
}
}
else {
cout << "Both numbers are the same." << endl;
}
}
有人可以帮我修改它以找到最大、最小的数字吗?我已经阅读了它并找到了一个排序的向量解决方案,但我似乎无法将其应用于我的问题。
非常感谢:)
如果要保持最大值和最小值的运行计数,并且不对现有程序进行结构更改,
你需要"#include <算法>",除非你想推出自己的最小和最大函数。算法>
而不是:
maximum = number1;
minimum = number2;
使用这个:
maximum = (maximum < max(number1, number2)) ? max(number1, number2) : maximum;
minimum = (minimum > min(number1, number2)) ? min(number1, number2) : minimum;
对于练习 6,您不必使用 std::vector。您可以跟踪名为 minn 和 maxn 的单个变量中的最大值和最小值,如以下代码片段所示:
int
main()
{
std::string quit("|");
int i = 0;
std::string s;
double n;
std::vector < double >v = { 0.0, 0.0 };
double maxn = std::numeric_limits < double >::lowest();
double minn = std::numeric_limits < double >::max();
std::vector < double >in_meters;
while (std::cin >> s) {
if (quit.compare(s) == 0)
break;
if (reject(s))
continue;
n = str2meters(s);
std::cout << n << " ";
in_meters.push_back(n);
if (maxn < n) {
std::cout << "the largest so far" << std::endl;
maxn = n;
}
if (minn > n) {
std::cout << "the smallest so far" << std::endl;
minn = n;
}
v[i] = n;
if (i == 1) {
std::sort(v.begin(), v.end());
prn(v);
}
i = (i + 1) % 2;
}
prng(in_meters);
return 0;
}
我在练习 2 和 5 中使用了 std::vector:
void
prn(std::vector < double > &v)
{
const double one_percent = 1.0 / 100.0;
std::cout << "the smaller value is: " << v[0] << std::endl;
std::cout << "the larger value is: " << v[1] << std::endl;
if ((v[1] - v[0]) < one_percent)
std::cout << "are almost equal" << std::endl;
}
和练习 9-11 也:
void
prng(std::vector < double > v)
{
std::sort(v.begin(), v.end());
for (auto k : v)
std::cout << k << " " ;
std::cout << std::endl;
double sum = std::accumulate(v.begin(), v.end(), 0.0);
std::cout << "number of values " << v.size() << std::endl;
std::cout << "sum of values " << sum << std::endl;
}
你让它变得复杂,从这个开始,使用"std_lib_facilities"
#include "....std_lib_facilities.h"
int main() {
cout << "type in two integers or a '|' to terminate the program:n";
int x1 = 0;
int x2 = 0;
while (cin >> x1 >> x2)
{
cout << x1 << ", " << x2 << "n";
}
keep_window_open("x");
}
相关文章:
- 维护unordered_map但同时每一步都需要最低的映射值
- 如何在最后一步使用CryptDecrypt解决NTE_BAD_DATA
- 如何在 C++ 中返回到上一步
- 如何一步一步地正确解释此代码?(编程新手)
- 通过GL_FLOAT,GL_UNSIGNED_INT一步
- 将隐式促销添加到复杂类,最后一步
- 如何在Linux / Windows操作系统上使用文件*.cpp一步编译下面的代码
- 检查下一步是检查员
- 改进malloc()算法的下一步是什么
- 如何在c++骰子游戏中存储第一个值,并在下一步进行比较
- 如何一步一步地遍历目录树
- 有没有一个调试器可以让我在代码中后退一步
- 若在下一步中对象被破坏,为什么不自动移动呢
- 第4章 斯特劳斯特鲁普钻头一个具有挑战性的一步(至少对我来说!
- 一步一步寻找选择排序大θ符号的过程
- 两步到一步的初始化和错误处理
- 带有下一步/后退按钮的Qt对话框/小部件
- 无法理解此代码的最后一步
- 只有当我在 c++ 中按 Enter 键(线程)时,代码如何进入下一步
- 离散傅立叶变换C++-下一步要做什么