英文:
C++: Outputting the original input rather than the modified input
问题
这个程序收集两个数字然后返回哪个值更大。然而,关于单位,当我将它们收集到一个向量中时,它会将它们的值转换为“cm”,以便我可以比较它们的大小。最后,当我打印“最小值是:”时,它返回修改过的值。我该如何打印原始值?谢谢
英文:
//units
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
int main()
{
double cm;
double m = 100.0;
double in = 2.54;
double ft = 30;
std::vector<double> numbers;
std::cout << "Please enter two doubles with their units (cm, m, in, ft): ";
double input = 0;
std::string unit = "";
while(std::cin >> input >> unit)
{
if (unit == "m")
{
double inputInCm = input*m;
numbers.push_back(inputInCm);
}
else if (unit == "ft")
{
double inputInFt = input*m;
numbers.push_back(inputInFt);
}
else if (unit == "in")
{
input *= in;
numbers.push_back(input);
}
else if (unit == "cm")
{
numbers.push_back(input);
}
else
std::cout << "This is not a valid unit/you did not enter a unit!\n";
}
sort(numbers.begin(), numbers.end());
if(numbers[0] == numbers[1])
std::cout << "These numbers are equal.\n";
else if(numbers[1] - numbers[0] < 1.0/100)
std::cout << "Thesee numbers are almost equal.\n";
else
std::cout << "The smaller value is: " << numbers[0] << ".\n"
<< "The bigger value is: " << numbers[1] << ".\n";
}
This program collects two numbers then returns which value is bigger. However, with regards to the units, when I collect them into a vector it changes their value into the "cm" so I can compare their size. At the end, when I am printing "The smallest value is: ", it returns the modified value. How do I print the original value instead? Thanks
答案1
得分: -1
我建议创建一个自定义结构并在该结构上实现运算符。请查看下面的示例:
//单位
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
struct measurement_t
{
double value;
std::string unit;
double to_cm()
{
if (unit == "m")
return 100.0 * value;
else if (unit == "ft")
return 30.0 * value;
else if (unit == "in")
return 2.54 * value;
else
return value;
}
};
bool operator<(measurement_t meas1, measurement_t meas2)
{
return meas1.to_cm() < meas2.to_cm();
}
bool operator==(measurement_t meas1, measurement_t meas2)
{
return meas1.to_cm() == meas2.to_cm();
}
std::ostream& operator<< (std::ostream& os, measurement_t meas)
{
os << meas.value << " " << meas.unit;
return os;
}
int main()
{
std::vector<measurement_t> numbers;
std::cout << "Please enter two doubles with their units (cm, m, in, ft): ";
double input = 0;
std::string unit = "";
while(std::cin >> input >> unit)
{
if (unit == "m" || unit == "ft" || unit == "in" || unit == "cm")
{
measurement_t meas {input, unit};
numbers.push_back(meas);
}
else
{
std::cout << "This is not a valid unit/you did not enter a unit!\n";
}
}
sort(numbers.begin(), numbers.end());
if(numbers[0] == numbers[1])
std::cout << "These numbers are equal.\n";
else if(numbers[1].to_cm() - numbers[0].to_cm() < 1.0/100)
std::cout << "These numbers are almost equal.\n";
else
std::cout << "The smaller value is: " << numbers[0] << ".\n"
<< "The bigger value is: " << numbers[1] << ".\n";
}
这种方式可以解决问题,因为您始终将值与单位存储在一起,并实现了打印和排序功能,以便您可以像处理其他浮点值一样对测量值进行排序。
此外,代码在后续开发中更容易重用,易于根据需要扩展功能。
英文:
I would recommend creating a custom structure and implementing operators on that structure. Check out example bellow:
//units
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
struct measurement_t
{
double value;
std::string unit;
double to_cm()
{
if (unit == "m")
return 100.0 * value;
else if (unit == "ft")
return 30.0 * value;
else if (unit == "in")
return 2.54 * value;
else
return value;
}
};
bool operator<(measurement_t meas1, measurement_t meas2)
{
return meas1.to_cm() < meas2.to_cm();
}
bool operator==(measurement_t meas1, measurement_t meas2)
{
return meas1.to_cm() == meas2.to_cm();
}
std::ostream& operator<< (std::ostream& os, measurement_t meas)
{
os << meas.value << " " << meas.unit;
return os;
}
int main()
{
std::vector<measurement_t> numbers;
std::cout << "Please enter two doubles with their units (cm, m, in, ft): ";
double input = 0;
std::string unit = "";
while(std::cin >> input >> unit)
{
if (unit == "m" || unit == "ft" || unit == "in" || unit == "cm")
{
measurement_t meas {input, unit};
numbers.push_back(meas);
}
else
{
std::cout << "This is not a valid unit/you did not enter a unit!\n";
}
}
sort(numbers.begin(), numbers.end());
if(numbers[0] == numbers[1])
std::cout << "These numbers are equal.\n";
else if(numbers[1].to_cm() - numbers[0].to_cm() < 1.0/100)
std::cout << "Thesee numbers are almost equal.\n";
else
std::cout << "The smaller value is: " << numbers[0] << ".\n"
<< "The bigger value is: " << numbers[1] << ".\n";
}
This way you solve your problem because you always have a value with the unit stored together and you implement printing and sorting capabilities so you can sort measurements as any other floating value.
Also code is much more easily reused in later development and its easy to expand functionalities as needed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论