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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
void OutputHeading (ofstream& fout, string CLASS_EXERCISE,
string PROGRAMMERS_NAME);
void OutputDivider (ofstream& fout, int WIDTH,
char symbol);
void OpenFiles (ofstream& fout, ifstream& fin);
int main (void)
{
int stringLength;
ifstream fin;
ofstream fout;
string fileName;
string sentence;
int lineOfData;
int numberOfAs;
int countOfVowels;
int countOfBlanks;
int lengthOfSentence;
int totalNumberOfLines;
int totalLength;
char ch;
//Output a divider to the output file
OutputDivider ( fout, WIDTH, '-');
//Opens the data files
void OpenFiles (ofstream& fout, ifstream& fin);
//Determine if the input file is empty or doesn't exist
if (!fin)
{
//Set the message
fout << "The input file" << right << setw(2) <<
"is empty or does not exist." << endl;
}
else
{
//Read the sentence from input file
getline (fin,sentence);
if (fin)
{
//Set the message
fout <<"Processing continues, the input file" << right <<
setw(7) << "Lines.txt" << setw(3) <<
"was successfully opened." << endl;
}
}
//Output divider to the file
OutputDivider ( fout, WIDTH, '-');
//Insert blank line
fout << endl;
//Output a divider to the file
OutputDivider ( fout, WIDTH, '-');
//Output Heading to the file
void OutputHeading (ofstream& fout, string CLASS_EXERCISE,
string PROGRAMMERS_NAME);
//Output a divider to the file
OutputDivider ( fout, WIDTH, '-');
//Output column headings to the file
fout << left << "Line" << right << setw(48) << "Length" << right <<
setw(15) << "# of Vowels" << right << setw(11) << "# of As" <<
right << setw(15) << "# of Blanks" << endl;
//Output a divider to the file
OutputDivider ( fout, WIDTH, '-');
//Initialize the counters
lengthOfSentence = 0;
totalNumberOfLines = 0;
totalLength = 0;
//Determine the counters for all three
while (fin)
{
//Determine the length of the sentence
lengthOfSentence = sentence.length();
//Initialize the counters
countOfVowels = 0;
countOfBlanks = 0;
numberOfAs = 0;
//Determine which are vowels, blanks, or As
for (lineOfData = 0; lineOfData < lengthOfSentence; lineOfData++)
{
ch = sentence.at(lineOfData);
ch = tolower(ch);
switch(ch)
{
//Determine which counter
case 'a':
numberOfAs++;
countOfVowels++;
break;
case 'e':
countOfVowels++;
break;
case 'I':
countOfVowels++;
break;
case 'i':
countOfVowels++;
break;
case 'o':
countOfVowels++;
break;
case 'u':
countOfVowels++;
break;
case '/n':
countOfBlanks++;
break;
default:
break;
}
}
//Output the line of data
fout << sentence << setw(28) << lengthOfSentence << setw(13) <<
countOfVowels << setw(12) << numberOfAs << setw(12) <<
countOfBlanks << endl;
//Read line of sentence from input file
getline(fin,sentence);
//Accumulate the total length of sentences
totalLength += lengthOfSentence;
//Accumulate total number of lines
totalNumberOfLines = 7;
}
//Output a divider to the file
OutputDivider ( fout, WIDTH, '-');
//Output the totals
fout << left << "Totals" << right << setw(45) << totalLength << right <<
setw(13) << numberOfAs << endl;
//Output a divider to the file
OutputDivider ( fout, WIDTH, '-');
//Output the number of lines processed
fout << left << "The number of lines is ----------------------->" <<
right << setw(2) << totalNumberOfLines << endl;
//Close output file
fout.close();
//Close the input file
fin.close();
return 0;
}
//-----------------------------------------------------------------------------
//OutputHeading - print the heading to the output file
//-----------------------------------------------------------------------------
void OutputHeading (ofstream& fout, string CLASS_EXERCISE, string PROGRAMMERS_NAME)
{
int stringLength;
//Calculate the centering print for the heading
stringLength = (WIDTH + CLASS_EXERCISE.length())/2;
//Output centered print for the heading
fout << setw(stringLength) << CLASS_EXERCISE << endl;
//Calculate the centering print for the heading
stringLength = (WIDTH + PROGRAMMERS_NAME.length())/2;
//Output centered print for the heading
fout << setw(stringLength) << PROGRAMMERS_NAME << endl;
}
//----------------------------------------------------------------------------
//OutputDivider - Outputs on the screen the symbol
// the number of times specified by numberOfSymbols
//----------------------------------------------------------------------------
void OutputDivider (ofstream& fout, int WIDTH,
char symbol)
{
fout<< setfill(symbol) << setw(WIDTH + 1)
<< ' ' << setfill(' ') << endl;
}
//----------------------------------------------------------------------------
//OpenFiles - Opens the files that are being used
// and returns them
//----------------------------------------------------------------------------
void OpenFiles (ofstream& fout, ifstream& fin)
{
//Open data file
fout.open("Ex4Output.txt");
//Open data file
fin.open("Lines.txt");
}
|