作为一项任务,我们应该简单地复制D.S. Malik的C ++书中的代码。这称为“文本处理”。我已经准确地复制了它,并再读了一遍,试图找出问题所在。
应该将infile文件复制并粘贴到infile文件中,然后计算字母数并列出字母在文本中出现的次数。它粘贴文本;但是,无论文本中的最后一个字符是什么,它都会无限次地重复该字符。对我来说,它只是在文本末尾重复多次。
在我编写代码之前,我将在下面显示输入文本。
文件内文字
The first device known to carry out calculation was the abacus.
The abacus was invented in Asia but was used in ancient Babylon,
Chine, and throughout Europe until the late middle ages. The aba-
cus uses a system of sliding beads in a rack for addition and sub-
traction. In 1642, the French philosopher and mathematician Blaise
Pascal invented the calculating device called the Pascaline. It
had eight movable dials on wheels and could calculate sums up to
eight figures long. Both the abacus and Pascaline could perform
only addition and subtraction operations. Later in the 17th cen-
tury, Gottfried von Leibniz invented a device that was able to add,
subtract, multiply, and divide.
文件中应包含的内容
Today we live in an era where information is processed almost at the
speed of light. Through computers, the technological revolution is
drastically changing the way we live and communicate with one
another. Terms such as "the Internet," which were unfamiliar just
a few years ago, are very common today. With the help of computers you
can send letters to, and receive letters from, loved ones within
seconds. You no longer need to send a re´sume´ by mail to apply for a
job; in many cases you can simply submit your job application via
the Internet. You can watch how stocks perform in real time, and
instantly buy and sell them. Students regularly "surf" the Internet
and use computers to design their classroom projects. They also use
powerful word-processing software to complete their term papers.
Many people maintain and balance their checkbooks on computers.
The number of lines = 15
A count = 53
B count = 7
C count = 30
D count = 19
E count = 81
F count = 11
G count = 10
H count = 29
I count = 41
J count = 4
K count = 3
L count = 31
M count = 26
N count = 50
O count = 59
P count = 21
Q count = 0
R count = 45
S count = 48
T count = 62
U count = 24
V count = 7
W count = 15
X count = 0
Y count = 20
Z count = 0
代码
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
void initialize(int& lc, int list[]);
void copyText(ifstream& intext, ofstream& outtext, char& ch, int list[]);
void characterCount(char ch, int list[]);
void writeTotal(ofstream& outtext, int lc, int list[]);
int main()
{
//Step 1; Declare variables
int lineCount;
int letterCount[26];
char ch;
ifstream infile;
ofstream outfile;
infile.open("textin.txt"); //Step 2
if (!infile) //Step 3
{
cout << "Cannot open the input file."
<< endl;
return 1;
}
outfile.open("textout.out"); //Step 4
initialize(lineCount, letterCount); //Step 5
infile.get(ch); //Step 6
while (infile) //Step 7
{
copyText(infile, outfile, ch, letterCount); //Step 7.1
lineCount++; //Step 7.2
infile.get(ch); //Step 7.3
}
writeTotal(outfile, lineCount, letterCount); //Step 8
infile.close(); //Step 9
outfile.close(); //Step 9
return 0;
}
void initialize(int& lc, int list[])
{
lc = 0;
for (int j = 0; j < 26; j++)
list[j] = 0;
} //end initialize
void copyText(ifstream& intext, ofstream& outtext, char& ch, int list[])
{
while (ch != '\n') //process the entire line
{
outtext << ch; //output the character
characterCount(ch, list); //call the function character count
intext.get(ch); //read the next character
}
outtext << ch; //output the newline character
} //end copyText
void characterCount(char ch, int list[])
{
int index;
ch = toupper(ch); //Step a
index = static_cast<int>(ch) - static_cast<int>('A'); //Step b
if (0 <= index && index < 26) //Step c
list[index]++;
}//end characterCount
void writeTotal(ofstream& outtext, int lc, int list[])
{
outtext << endl << endl;
outtext << "The number of lines = " << lc << endl;
for (int index = 0; index < 26; index++)
outtext << static_cast<char>(index + static_cast<int>('A')) << " count = " << list[index] << endl;
} //end writeTotal