C++ 计算负数和大于 X 的数

标签 c++

我正在做一个项目,其中一个 txt 文件中有一个数字列表。 C++ 程序提取这些数字,并根据它们是否满足特定条件将它们放入适当的“桶”中。我想将小于 0 的数字和大于 200 的数字一起放入桶中。但我似乎无法识别负数。想法?

//GET IT STARTED IN HERE
int main()
{
//VARIABLES ETC
int score;

//SET SCORE RANGES TO 0
int bucket[9] = {0,0,0,0,0,0,0,0,0};


ifstream scoreFile;
string file;


//OPEN UP THE SCORE FILE
cout << "Enter path to Score File: ";
cin >> file;

scoreFile.open(file.c_str());

if (file == "kill" || file == "KILL" || file == "Kill") {
    cout << "Program terminated with KILL command" << endl;
    return 0;
}
else
{
//CHECK FOR BAD PATH
while (!scoreFile)
{
    cerr << "Wrong path" << endl;
    cout << "Try path again: ";
    cin >> file;
    scoreFile.clear();
    scoreFile.open(file.c_str());
}
}
//LOOK AT ALL THE SCORES FROM LAST WEEKS TEST
int scoreRow(1);

//CHECK EACH ONE AND ADD IT TO THE APPROPRIATE BUCKET
while (scoreFile >> score)
{
    if (score <= 24)
        bucket[0]++;

    else if (score <= 49)
        bucket[1]++;

    else if (score <= 74)
        bucket[2]++;

    else if (score <= 99)
        bucket[3]++;

    else if (score <= 124)
        bucket[4]++;

    else if (score <= 149)
        bucket[5]++;

    else if (score <= 174)
        bucket[6]++;

    else if (score <= 200)
        bucket[7]++;

    //ADDED TWO EXTRA SCORES IN THE FILE TO TEST THIS AREA

    else if (score < 0 || score > 200)
        bucket[8]++;

    scoreRow++;
} 

//OUTPUT SOME RESULTS
cout << endl << "SCORE EVALUATION"<< endl;
cout << "Amount of students who scored    0 - 24: " << bucket[0] << endl;
cout << "Amount of students who scored   25 - 49: " << bucket[1] << endl;
cout << "Amount of students who scored   50 - 74: " << bucket[2] << endl;
cout << "Amount of students who scored   75 - 99: " << bucket[3] << endl;
cout << "Amount of students who scored 100 - 124: " << bucket[4] << endl;
cout << "Amount of students who scored 125 - 149: " << bucket[5] << endl;
cout << "Amount of students who scored 150 - 174: " << bucket[6] << endl;
cout << "Amount of students who scored 175 - 200: " << bucket[7] << endl;
cout << "Scores out of Range: " << bucket[8] << endl;

}

最佳答案

您想将最后一个条件放在顶部。

   //CHECK EACH ONE AND ADD IT TO THE APPROPRIATE BUCKET
    while (scoreFile >> score)
    {
        if (score < 0 || score > 200)
            bucket[8]++;

        else if (score <= 24)
            bucket[0]++;

        else if (score <= 49)
            bucket[1]++;

        else if (score <= 74)
            bucket[2]++;

        else if (score <= 99)
            bucket[3]++;

        else if (score <= 124)
            bucket[4]++;

        else if (score <= 149)
            bucket[5]++;

        else if (score <= 174)
            bucket[6]++;

        else if (score <= 200)
            bucket[7]++;

        scoreRow++;
    } 

关于C++ 计算负数和大于 X 的数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26642747/

相关文章:

c++ - 从 .cpp 文件调用 .mm 函数的链接器错误

c++ - 如何定义指向函数指针的指针以及如何在 C++ 中使用它?

C++数组指针

c++ - 具有多个比较功能的单个 std::map/multimap?

c++ - 将 C++ 类转换为字节数组

java - 如何获得填字游戏的多个解决方案?

c++ - 为什么不打开 char 值达到 case 0xC2?

c++ - WinHttpGetIEProxyConfigForCurrentUser 能否从 WPAD 获取代理

C++ 从字符串中获取值

c++ - 计算 sha256 以使用已弃用的 openssl 代码的替代方法