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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
#include <iostream>
#include <fstream>
#include <string.h>
#include <climits>
using namespace std;
int fTotalToys(int toys[50], int counter)
{
int result=0;
for (int i=0; i<counter-1; i++)
{
result+=toys[i];
}
return result;
}
int fOverFive(int toys[50], int counter)
{
int result=0;
for (int i=0; i<counter-1; i++)
{
if(toys[i]>=500)
{
result++;
}
}
return result;
}
int fGreatElf(int toys[50], int counter)
{
int result=0;
int maxim=0;
for (int i=0; i<counter-1; i++)
{
if(toys[i]>=maxim)
{
maxim = toys[i];
result = i;
}
}
return result;
}
int fLeastElf(int toys[50], int counter)
{
int result=0;
int minimum=INT_MAX;
for (int i=0; i<counter-1; i++)
{
if(toys[i]<=minimum)
{
minimum = toys[i];
result = i;
}
}
return result;
}
void printResults(string elf[50],int toys[50], int counter)
{
ofstream outf("elves.out");
string rating;
outf << "Name Number of toys Rating\n";
for (int i=0; i<counter-1; i++)
{
outf << elf[i] << "\t" ;
if(elf[i].length()<=7)
outf<<"\t";
if(elf[i].length()<12)
outf<<"\t";
outf<< toys[i] << "\t\t\t";
if(toys[i]<1000)
outf<<"\t";
if (toys[i]>=500)
{
rating = "*****";
}
else if ((toys[i]>=300) && (toys[i]<=499))
{
rating = "***";
}
else if ((toys[i]>=200) && (toys[i]<=299))
{
rating = "*";
}
else if (toys[i]<200)
{
rating = "-";
}
outf << rating << "\n";
}
outf << "Total number of toys is :"<< fTotalToys(toys, counter)<<"\n";
outf << "The number of elves who made more than 500 toys is :"<< fOverFive(toys, counter)<<"\n";
outf << "The elf who made the most toys is :"<< elf[fGreatElf(toys, counter)] << "\n";
outf << "The elf who made the least toys is :"<< elf[fLeastElf(toys, counter)] << "\n";
outf.close();
}
int main()
{
int toys[50];
string elf[50];
string cElf, greatElf, leastElf;
ifstream inf("elves.dat");
for (int i=0; i<50; i++)
{
toys[i]=0;
elf[i]=" ";
}
int ceToys=0, counter=0;
while (!inf.eof())
{
inf >> cElf;
elf[counter]=cElf;
inf >> ceToys;
toys[counter]=ceToys;
counter++;
}
inf.close();
printResults(elf, toys, counter);
return 0;
}
|