Kod: Zaznacz cały
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
void initialize (int& lc, int list[])
{
int j;
lc = 0;
for(j=0; j < 26 ; j++)
list[j] = 0;
}
void characterCount(char ch, int list[])
{
int i;
ch = toupper(ch);
i = static_cast<int>(ch)- static_cast<int>('A');
if (0 <= i && i < 26)
list[i]++;
}
void copyText (ifstream& intext, ofstream& outtext, char& ch, int list[])
{
while (ch != '\n')
{
outtext << ch;
characterCount(ch, list);
intext.get(ch);
}
outtext<<ch;
}
void writeTotal(ofstream& outtext, int lc, int list[])
{
int i;
outtext << endl <<endl;
outtext << "The number of lines = " << lc <<endl;
for (i = 0; i < 26 ; i++)
outtext << static_cast<char>(i+ static_cast<int>('A'))
<<" count = " << list[i] <<endl;
}
int main()
{
int lineCount;
int letterCount[26];
char ch;
ifstream infile;
ofstream outfile;
cout<<"Program reads a given text, outputs the text as it is, \n"
<<"and also prints the number of lines and the number \n of times each letter appears in the text. \n ";
infile.open("textin.txt");
outfile.open("textout.txt");
initialize(lineCount, letterCount);
infile.get(ch);
while(infile)
{
copyText(infile, outfile, ch, letterCount);
lineCount++;
infile.get(ch);
}
writeTotal(outfile, lineCount, letterCount);
infile.close();
outfile.close();
cout<<endl;
system("PAUSE");
}