矢量最小值显示为 0

发布于 2024-11-06 09:30:41 字数 1731 浏览 0 评论 0原文

我正在尝试显示向量的最大值和最小值(以及其他内容)。最大值显示正常,但最小值不断出现为 0。唯一一次不为 0 时,它最终会显示数据列表的最终值。

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main()
{
    fstream file;
    file.open("dataWin.txt");

    vector<float> dist;
    while(file.eof() == false)
    {
        string miles;
        getline(file, miles);
        float milesNum = atof(miles.c_str());
        dist.push_back(milesNum);
    }

    float max = 0.0;
    float min = 1.0;
    float sum = 0.0;
    float secHour = 3600;
    float lastRecord = 238857;

    for(int i = 0; i < dist.size(); i++)
    {
        if(dist[i] < min)
            min = dist[i];
        if(dist[i] > max)
            max = dist[i];
        sum += dist[i];
    }

    float avg = (float)sum / dist.size();
    float lastSeconds = (float)lastRecord / (float)avg;
    float deadline = (float)lastSeconds / (float)secHour;   

    cout << "National Aeronautics and Space Administration - NASA" << endl;
    cout << "EYES ONLY" << endl << endl;
    cout << "Statistics on solar object 326 Alba" << endl;
    cout << "Danger level: HIGH" << endl << endl;
    cout << "Maximum travel distance: " << max << " miles per second." << endl;
    cout << "Minimum travel distance: " << min << " miles per second." << endl;
    cout << "Average travel distance: " << avg << " miles per second." << endl << endl;
    cout << "WARNING! OBJECT 326 Alba WILL IMPACT IN " << deadline << " HOURS!" << endl;

    file.close();

    return 0;
}

如何获取要显示的列表的实际最小值?

I'm trying to display both the maximum and minimum value of a vector (along with other things). The max value displays fine, but the minimum value constantly comes up as 0. The only time it's not 0, it ends up displaying the final value of the list of data.

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main()
{
    fstream file;
    file.open("dataWin.txt");

    vector<float> dist;
    while(file.eof() == false)
    {
        string miles;
        getline(file, miles);
        float milesNum = atof(miles.c_str());
        dist.push_back(milesNum);
    }

    float max = 0.0;
    float min = 1.0;
    float sum = 0.0;
    float secHour = 3600;
    float lastRecord = 238857;

    for(int i = 0; i < dist.size(); i++)
    {
        if(dist[i] < min)
            min = dist[i];
        if(dist[i] > max)
            max = dist[i];
        sum += dist[i];
    }

    float avg = (float)sum / dist.size();
    float lastSeconds = (float)lastRecord / (float)avg;
    float deadline = (float)lastSeconds / (float)secHour;   

    cout << "National Aeronautics and Space Administration - NASA" << endl;
    cout << "EYES ONLY" << endl << endl;
    cout << "Statistics on solar object 326 Alba" << endl;
    cout << "Danger level: HIGH" << endl << endl;
    cout << "Maximum travel distance: " << max << " miles per second." << endl;
    cout << "Minimum travel distance: " << min << " miles per second." << endl;
    cout << "Average travel distance: " << avg << " miles per second." << endl << endl;
    cout << "WARNING! OBJECT 326 Alba WILL IMPACT IN " << deadline << " HOURS!" << endl;

    file.close();

    return 0;
}

How can I get the actual min value of the list to display?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方的巷 2024-11-13 09:30:41

问题是,当输入结束时,getline 调用将失败,并且不会更新字符串,然后您将空字符串转换为浮点数,这在 atof 中失败调用(如果atof失败,它将返回0.0)并将该值推入向量。向量中的最小值现在为 0 并且算法正确地将其生成为最小值。

如果您要在 C++ 中进行编码,并假设输入的唯一内容是浮点数和空格,则可以通过直接读取浮点数来使代码更简单:

float miles;
while ( std::cin >> miles ) {
   dist.push_back( miles );
}

更简单的是,您可以通过使用迭代器和STL 中的算法:

std::vector<float> dist;
std::copy( std::istream_iterator<float>( std::cin ), std::istream_iterator<float>(),
           std::back_inserter( dist ) );
std::cout << "Max: " << *std::max_element( dist.begin(), dist.end() ) << "\n";
std::cout << "Min: " << *std::min_element( dist.begin(), dist.end() ) << "\n";
std::cout << "Sum: " << std::accumulate( dist.begin(), dist.end() ) << std::endl;

这只是为了展示现有算法可以做什么。在您的特定情况下,我将使用 std::copy 和 std::istream_iterator 来读取输入,但为了处理已读取的数字,我将展开手动循环(这样所有的最小值、最大值和总和都可以在一次中计算出来,上面的 STL 示例在三次中完成)。

The problem is that when input ends the getline call will fail and it will not update the string, you are then converting an empty string to a float which is failing in the atof call (if atof fails, it will return 0.0) and pushing that value into the vector. The minimum value in the vector is now 0 and the algorithm is correctly yielding it as the minimum.

If you are going to code that in C++, and assuming that the only contents of the input are floats and spaces, you can make the code simpler by reading directly into a float:

float miles;
while ( std::cin >> miles ) {
   dist.push_back( miles );
}

Even simpler, you can avoid the loop altogether by using iterators and algorithms from the STL:

std::vector<float> dist;
std::copy( std::istream_iterator<float>( std::cin ), std::istream_iterator<float>(),
           std::back_inserter( dist ) );
std::cout << "Max: " << *std::max_element( dist.begin(), dist.end() ) << "\n";
std::cout << "Min: " << *std::min_element( dist.begin(), dist.end() ) << "\n";
std::cout << "Sum: " << std::accumulate( dist.begin(), dist.end() ) << std::endl;

This is just to show what can be done with existing algorithms. In your particular case I would use the std::copy and std::istream_iterators for reading the input, but for processing the already read numbers I would unroll a manual loop (so that all min, max and sum can be calculated in one pass, the STL examples above do it in three passes).

要走就滚别墨迹 2024-11-13 09:30:41

这:

while(file.eof() == false)
{
    string miles;
    getline(file, miles);

应该是:

string miles;
while( getline(file, miles) )
{

您从哪里得到需要调用 eof() 的想法?你不这样做,而且你这样做的方式是错误的!

This:

while(file.eof() == false)
{
    string miles;
    getline(file, miles);

should be:

string miles;
while( getline(file, miles) )
{

Where are you picking up the idea you need to call eof() from? You don't, and doing so in the way you are doing it is wrong!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文