0

我的代码遇到的问题是 min 总是读取 0。原因是我没有正确地将它初始化为“random.txt”的第一个值。我该怎么做?

这是我的代码:

using namespace std;

int main()
{

    ifstream inputFile;
    //open file
    inputFile.open("random.txt");
    int numCount = 0;
    int number, max, min, i, x;
    double average, sum;

    while(inputFile >> number)
    {
        for (i = 0; i < numCount; i++)
        {
            x = number;
            if (x < min)
            {
                min = x;
            }
            else if (x > max)
            {
                max = x;
            }
        }

        numCount++;
        sum = number + sum;
    }
    if (numCount > 0)
    {
        average = sum/numCount;
    }

    inputFile.close();

    cout << "Number of numbers is: " << numCount << endl;
    cout << "Sum of numbers is: " << sum << endl;
    cout << "Average of numbers is: " << average << endl;
    cout << "Max of numbers is: " << max << endl;
    cout << "Min of numbers is: " << min << endl;

    return 0;
}
4

4 回答 4

2

Initialize min to the largest possible value, and max to the lowest possible value.

Like

int min = std::numeric_limits<int>::max();
int max = std::numeric_limits<int>::min();

See here for a reference of std::numeric_limits.


As hinted to by WhozCraig in a comment to this answer, your if-else if construct is not the best if the numbers in the file are just getting lower, and no values are bigger than the previous value.

You might want to do either what WhozCraig suggests in the comment, or change to two separate if statements.


Sample

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

int main()
{
    std::ifstream inputFile("random.txt");
    int number, max=0, min=0;
    int numCount = 0;
    double average=0, sum=0;

    if (inputFile >> number)
    {
        min = max = number;
        numCount = 1;
        while(inputFile >> number)
        {
            if (number < min)
                min = number;
            else if (number > max)
                max = number;

            sum += number;
            ++numCount;
        }

        average = static_cast<double>(sum)/numCount;
    }

    cout << "Number of numbers is: " << numCount << endl;
    cout << "Sum of numbers is: " << sum << endl;
    cout << "Average of numbers is: " << average << endl;
    cout << "Max of numbers is: " << max << endl;
    cout << "Min of numbers is: " << min << endl;

    return 0;
}
于 2013-10-16T06:42:14.223 回答
0

将 min 初始化为可能的最大整数值:

  1. 包含<climits>头文件(详见此处
  2. 初始化min = INT_MAX.

这会很好用,但如果文件中根本没有数字,则不会按预期工作。要解决这个问题,您应该使用一个布尔变量(初始化为false),并将其设置为循环true内部。while因此,在循环退出后,您将能够判断min和的值max是真实的,还是只是未更改的初始值(另一种方法是在循环之后测试是否min更大than max- 如果是,则文件为空)。

另请注意,循环似乎for是多余的,甚至没有为第一个数字执行。

于 2013-10-16T06:53:08.500 回答
0
ifstream inputFile;
//open file
inputFile.open("random.txt");
int numCount = 0;
int number = 0, max = 0, min = 0, i = 0;
double average = 0, sum = 0;
bool first = true;
while (inputFile >> number)
{
    if (first){
        first = false;
        max = min = number;
    }
    if (number < min)
    {
        min = number;
    }
    else if (number > max)
    {
        max = number;
    }
    numCount++;
    sum = number + sum;
}

这是我编辑的。我希望你能得到你的答案。

于 2013-10-16T07:37:18.723 回答
0

由于您的数字是 int,因此尝试将 min 初始化为 INT_MAX,将 max 初始化为 INT_MIN。您可以在 limits.h 中找到这些宏。

http://www.cplusplus.com/reference/climits/

于 2013-10-16T06:43:29.763 回答