【问题标题】:Reading int per line and displaying percentage每行读取int并显示百分比
【发布时间】:2016-10-26 15:55:02
【问题描述】:

所以,我目前正在尝试制作一个程序,它读取一个名为“input.txt”的文件,其中存储如下整数,并计算其中有多少百分比大于零、小于零和等于零。

10
-4
0
34
42
-2
0

这是我的代码:

使用命名空间标准;

ifstream inputFile;
int count = 0;
int value,negative,positive,zero;
double negPerc,posPerc,zeroPerc;

inputFile.open("input.txt");
if ( !inputFile.fail()){

    while (inputFile >> value)
          {
            count++;

            if(value < 0)
                negative++;
            if(value == 0)
                zero++;
            if(value > 0)
                positive++;

          }

        }
else
 {
    cout << "\nError, unable to open input file ";
 }

cout << fixed << setprecision(1);

negPerc = (negative/count)*100;
posPerc = (positive/count)*100;
zeroPerc = (zero/count)*100;


cout << "There were " << negPerc << "% negative numbers." << endl;
cout << "There were " << zeroPerc << "% numbers equal to zero." << endl;
cout << "There were " << posPerc << "% numbers greater than zero." << endl;

输出:

There were 1864443476.0% negative numbers.
There were 204178000.0% numbers equal to zero.
There were 0.0% numbers greater than zero.

我仔细检查了我的代码并尝试诊断为什么会这样,但我找不到任何问题。我做错了什么?

【问题讨论】:

  • 分区为int / int,将截断为int。将其中一个投射到double before 部门。例如,您可以将其写为(negative*100.0)/count;。这只是一个错误。
  • 另外,在递增它们之前将负数、正数和零初始化为“0”(就像你对计数所做的那样)。使用 -g -Wall 编译并运行编译器抱怨的所有内容。另外,查看“命名空间污染”(停止使用“使用命名空间 std”)。
  • 另外,请重新考虑您对通常被认为是不良做法的使用:using namespace std;endl(这些是解释链接)。
  • 我正在使用namespace stdendl,因为到目前为止我的教授告诉我这样做(我目前正在参加介绍 C++ 课程)。到时候我很想以另一种方式学习它。
  • @Chris 我按照你告诉我的方式进行了初始化,但它仍然给了我相同的值。

标签: c++ file-io int percentage fileinputstream


【解决方案1】:

这是你的代码,每个人的 cmets 都放在一起供你学习。

// Compile with "g++ yourCPPfile.cpp -o yourExecName -g -Wall"
#include <iostream>
#include <iomanip>
#include <fstream>
using std::ifstream;
#include <cstdio>
using std::cout;

int main() {
    ifstream inputFile;
    int count, value, negative, positive, zero;
    count = negative = positive = zero = 0;
    double negPerc, posPerc, zeroPerc;

    inputFile.open("input.txt");

    if (!inputFile.fail()) {
        while (inputFile >> value) {
            count++;
            if (value < 0)
                negative++;
            if (value == 0)
                zero++;
            if (value > 0)
                positive++;
        }
    }
    else {
        cout << "\nError, unable to open " << inputFile << "!\n";
    }

    // Stays this way until you setPrecision() to something else.
    cout << std::setprecision(1);

    // Troubleshooting!!
    cout << negative << "\n";
    cout << positive << "\n";
    cout << zero << "\n";
    cout << count << "\n";

    // Calculations.
    negPerc = (negative * 100.0 / count);
    posPerc = (positive * 100.0 / count);
    zeroPerc = (zero * 100.0 / count);

    // Your desired result...
    cout << "There were " << std::fixed << negPerc << "% negative numbers." << "\n";
    cout << "There were " << std::fixed << zeroPerc << "% numbers equal to zero." << "\n";
    cout << "There were " << std::fixed << posPerc << "% numbers greater than zero." << "\n";
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 2015-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多