1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
// Lab 9b Exercise 2
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
void openInputFile(double &neg, double &pos, double &zero, double &total);
void negPrecentage(double neg, double total);
void posPrecentage(double pos, double total);
void zeroPrecentage(double zero, double total);
int main()
{
double neg = 0, pos = 0, zero = 0, total = 0 ;
openInputFile(neg, pos, zero, total);
negPrecentage(neg, total);
posPrecentage(pos, total);
zeroPrecentage(zero, total);
return 0;
}
void openInputFile(double &neg, double &pos, double &zero, double &total)
{
const string NAME = "numbers.txt";
ifstream testFile;
double num;
testFile.open(NAME);
if (!testFile.fail())
{
while (testFile >> num)
{
total++;
if (num < 0)
neg++;
else if (num > 0)
pos++;
else
zero++;
}
}
else
{
cout << "File open failed " << endl;
exit(0);
}
}
void negPrecentage(double neg, double total)
{
cout << fixed << setprecision(2) << "The percent of numbers that were negative are " << (neg/total) * 100 << "%" << endl;
}
void posPrecentage(double pos, double total)
{
cout << fixed << setprecision(2) << "The percent of numbers that were positive are " << (pos/total) * 100 << "%" << endl;
}
void zeroPrecentage(double zero, double total)
{
cout << fixed << setprecision(2) << "The percent of numbers that were zero are " << (zero/total) * 100 << "%" << endl;
}
|