//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";
}
这个程序收集两个数字然后返回哪个值更大。但是,关于单位,当我将它们收集到一个向量中时,它会将它们的值更改为“cm”,以便我可以比较它们的大小。最后,当我打印“最小值是:”时,它返回修改后的值。如何改为打印原始值?谢谢