C++:输出原始输入而不是修改后的输入。

huangapple go评论61阅读模式
英文:

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 &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;
#include &lt;cmath&gt;
struct measurement_t
{
double value;
std::string unit;
double to_cm()
{
if (unit == &quot;m&quot;)
return 100.0 * value;
else if (unit == &quot;ft&quot;)
return 30.0 * value;
else if (unit == &quot;in&quot;)
return 2.54 * value;
else
return value;
}
};
bool operator&lt;(measurement_t meas1, measurement_t meas2)
{
return meas1.to_cm() &lt; meas2.to_cm();
}
bool operator==(measurement_t meas1, measurement_t meas2)
{
return meas1.to_cm() == meas2.to_cm();
}
std::ostream&amp; operator&lt;&lt; (std::ostream&amp; os, measurement_t meas)
{
os &lt;&lt; meas.value &lt;&lt; &quot; &quot; &lt;&lt; meas.unit;
return os;
}
int main()
{
std::vector&lt;measurement_t&gt; numbers;
std::cout &lt;&lt; &quot;Please enter two doubles with their units (cm, m, in, ft): &quot;;
double input = 0;
std::string unit = &quot;&quot;;
while(std::cin &gt;&gt; input &gt;&gt; unit)
{
if (unit == &quot;m&quot; || unit == &quot;ft&quot; || unit == &quot;in&quot; || unit == &quot;cm&quot;)
{
measurement_t meas {input, unit};
numbers.push_back(meas);
}
else
{
std::cout &lt;&lt; &quot;This is not a valid unit/you did not enter a unit!\n&quot;;
}
}
sort(numbers.begin(), numbers.end());
if(numbers[0] == numbers[1])
std::cout &lt;&lt; &quot;These numbers are equal.\n&quot;;
else if(numbers[1].to_cm() - numbers[0].to_cm() &lt; 1.0/100)
std::cout &lt;&lt; &quot;Thesee numbers are almost equal.\n&quot;;
else
std::cout &lt;&lt; &quot;The smaller value is: &quot; &lt;&lt; numbers[0] &lt;&lt; &quot;.\n&quot;
&lt;&lt; &quot;The bigger value is: &quot; &lt;&lt; numbers[1] &lt;&lt; &quot;.\n&quot;;
}

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.

huangapple
  • 本文由 发表于 2023年2月19日 18:55:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定