2016-10-26 4 views
1

그래서 저는 현재 "input.txt"라는 파일을 읽고 다음과 같이 저장된 정수로 읽는 프로그램을 만들려고합니다. 그 중 몇 퍼센트가 0보다 큰지 계산합니다 0이고 0입니다. 여기 int를 한 줄에 읽고 백분율을 표시합니다.

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; 

및 outout : 나는이 두 배로

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

내 코드를 확인하고 이런 식으로 이유를 진단하려고했지만 나는 어떤 문제를 찾을 수 없습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

+2

부서는 'int/int'이며, 이는'int '로 자릅니다. 그 중 한 명을 부서 전의 '이중'*에 던지십시오. 예를 들어,'(negative * 100.0)/count; '라고 쓸 수 있습니다. 그것은 단지 하나의 오류입니다. – BoBTFish

+0

이들은 모두 정수 - 음수 & 개수 -이므로 정수 나누기를 얻습니다. 이는 예상하지 못하는 것입니다. – UKMonkey

+5

또한 음수, 양수 및 0을 "0"으로 초기화합니다 (예 : count로 실행 한 것과 같음). -g -Wall로 컴파일하고 컴파일러가 불평하는 모든 것을 실행하십시오. 또한 "네임 스페이스 오염"을 조사하십시오 ("네임 스페이스 표준 사용"사용을 중단하십시오). – Chris

답변

0

다음은 모두가 여러분의 의견을 수렴하여 작성한 코드입니다.

// 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; 
}