查看: 903|回复: 9
|
有谁可以教我吗。。。
[复制链接]
|
|
我的问题是:write a program that will read a txt file, copy it into
another text file and display the number of words, lines and
paragraphs.
以下是我的答案。。。。不过有ERROR有谁可以帮我吗???
#include <fstream>
#include <string>
using namespace std;
//function prototypes
void initializeVar(int& nwords, int& nlines, int& nparagraphs);
int updateCount(int nwords, nlines, nparagraphs);
int processBlank(_________);
int copyText(______);
void printTotal(int nwords, int nlines, int nparagraphs);
int main()
{
//variables declaration
int nwords;
int nlines;
int nparagraphs;
char ch;
ifstream inFile; //declares inFile to be an input file stream variable
ofstream outFile; //declares outFile to be an output file stream variable
//open files
inFile.open(“a:\\prog.txt”); //open the input file
outFile.open(“a:\\prog.txt”); //open the output file
inFile.get(ch); //read a character
while (!inFile.eof())
{
while (not end of line) //<------------****** 我猜我应该是stuck在这边.
{
processBlank(parameters);
copyText(parameters);
}
updateCount(int nwords, nlines, nparagraphs);
inFile.get(ch); //read a character;
.
.
.
}
printTotal(parameters);
//close files
inFile.close();
outFile.close();
return 0;
}
void initializeVar(int& nwords, int& nlines, int& nparagraphs) //Initializes all the variables of the
//function main
{
int nwords = 0
int nlines = 0
int nparagraphs = 0
}
int processBlank(int _______) //Reads and writes blanks. If hits nonblank, it increments number of
//words in a line. The number of words in line is set back to zero in the
//function updateCount. Exits after processing the blanks.
{
int
.
.
.
}
int copyText(int __ char ______) //Reads and writes the nonblank characters. If hits blank, it exits.
{
int
.
if (char = " ")
(exit)
}
int updateCount(int nwords, nlines, nparagraphs) //Updates total word count, increments the number of lines and sets
//the number of words on a line back to zero. If no words on line, it
// increments the number of paragraphs
{
int f = 0;
for (f=1, f>=0, f++)
wordCount = f;
nlines++ ; //counter
if (nwords =0)
nparagraphs++;
}
void printTotal(int nwords, int nlines, int nparagraphs) //Prints number of words, lines and
paragraphs
{
cout << "Number of words: " << nwords << endl;
cout << "Number of lines: " << nlines << endl;
cout << "Number of paragraphs: << nparagraphs << endl;
} |
|
|
|
|
|
|
|
发表于 1-3-2006 12:43 PM
|
显示全部楼层
#include <fstream>
#include <string> // ** ERROR
using namespace std;
//function prototypes
void initializeVar(int& nwords, int& nlines, int& nparagraphs);
int updateCount(int nwords, nlines, nparagraphs);
int processBlank(_________); // ** ERROR
int copyText(______);
void printTotal(int nwords, int nlines, int nparagraphs);
int main()
{
//variables declaration
int nwords;
int nlines;
int nparagraphs;
char ch;
ifstream inFile; //declares inFile to be an input file stream variable
ofstream outFile; //declares outFile to be an output file stream variable
//open files // ** ERROR
inFile.open(“a:\\prog.txt”); //open the input file
outFile.open(“a:\\prog.txt”); //open the output file
inFile.get(ch); //read a character
while (!inFile.eof())
{ // ** ERROR
while (not end of line) //<------------****** 我猜我应该是stuck在这边.
{
processBlank(parameters);
copyText(parameters);
}
updateCount(int nwords, nlines, nparagraphs);
inFile.get(ch); //read a character;
.
.
.
}
printTotal(parameters);
//close files
inFile.close();
outFile.close();
return 0;
}
// ** ERROR
void initializeVar(int& nwords, int& nlines, int& nparagraphs) //Initializes all the variables of the
//function main
{
int nwords = 0
int nlines = 0
int nparagraphs = 0
}
// ** ERROR
int processBlank(int _______) //Reads and writes blanks. If hits nonblank, it increments number of
//words in a line. The number of words in line is set back to zero in the
//function updateCount. Exits after processing the blanks.
{
int
.
.
.
}
// ** ERROR
int copyText(int __ char ______) //Reads and writes the nonblank characters. If hits blank, it exits.
{
int
.
if (char = " ")
(exit)
}
// ** ERROR
int updateCount(int nwords, nlines, nparagraphs) //Updates total word count, increments the number of lines and sets
//the number of words on a line back to zero. If no words on line, it
// increments the number of paragraphs
{
int f = 0;
for (f=1, f>=0, f++)
wordCount = f;
nlines++ ; //counter
if (nwords =0)
nparagraphs++;
}
void printTotal(int nwords, int nlines, int nparagraphs) //Prints number of words, lines and
paragraphs
{
cout << "Number of words: " << nwords << endl;
cout << "Number of lines: " << nlines << endl;
cout << "Number of paragraphs: << nparagraphs << endl; // ** ERROR
}
看到我 BLUR BLUR 。。
你的PROGRAM 能跑吗? 很多 ERRORS 噢 。。 |
|
|
|
|
|
|
|
楼主 |
发表于 1-3-2006 01:45 PM
|
显示全部楼层
就是不能RUN咯。。。。
你可以帮帮我FIX ERROR吗???
或者教我FIX也可以。。。谢谢。。。 |
|
|
|
|
|
|
|
发表于 1-3-2006 04:08 PM
|
显示全部楼层
原帖由 cheewooi7849 于 1-3-2006 01:45 PM 发表
就是不能RUN咯。。。。
你可以帮帮我FIX ERROR吗???
或者教我FIX也可以。。。谢谢。。。
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ifstream infile;
infile.open ("c:\\test.txt", ifstream::in);
while (infile.good())
cout << (char) infile.get(); // Read char by char
infile.close();
return 0;
}
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
std::string s;
std::ifstream f( "c:\\test.txt" );
if( !f ) {
cerr << "Couldn't open file\n";
}
while( getline(f,s) ) { // Read line by line
cout << s << '\n';
}
return 0;
}
给你一点点 read char and read line sample。。。
接下来的要看你啦。。
[ 本帖最后由 夏日么么茶 于 1-3-2006 05:12 PM 编辑 ] |
|
|
|
|
|
|
|
发表于 1-3-2006 04:19 PM
|
显示全部楼层
write a program that will read a txt file, copy it into
another text file and display the number of words, lines and
paragraphs.
Visual Basic 简单来讲是酱 。。
private sub read()
open "A.txt" as input #1 // read from file
open "B.txt" as output #2 // create new file to write
do while not eof(1)
line input #1, myString
print #2, myString
k = len(myString) 'string length = number of char
i = i+1 'count number of lines
' paragraphs 自己想 。。
loop
close #1, #2
end |
|
|
|
|
|
|
|
楼主 |
发表于 2-3-2006 12:42 PM
|
显示全部楼层
我已经重做了。。。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//Function prototypes
void initializeVar(int& x, int&y, int& z, int&w);
void updateCount(int& wordsLine, int& linesTotal, int& paraTotal, int&
wordsTotal);
void processBlank(ifstream& in, ofstream& out, char& ch, int&
wordsTotal);
void copyText(ifstream& in, ofstream& out, char& ch);
void printTotal(int& wordsTotal, int& linesTotal, int& paraTotal,
ofstream& y);
int main()
{
//variables declaration
int wordsLine;
int wordsTotal;
int linesTotal;
int paraTotal;
char letter;
ifstream inData;
ofstream outData;
inData.open("c:\\inData.txt");
outData.open("c:\\outData.txt");
initializeVar(wordsLine, wordsTotal, linesTotal, paraTotal);
inData.get(letter);
while (inData)
{
outData << inData.rdbuf();
cout << "Running."<< endl;
processBlank(inData, outData, letter, wordsTotal);
cout << wordsTotal;
copyText(inData, outData, letter);
updateCount(wordsLine, linesTotal, paraTotal, wordsTotal);
inData.get(letter);
}
printTotal(wordsTotal, linesTotal, paraTotal, outData);
inData.close();
outData.close();
cout << endl;
cin.get();cin.get();
return 0;
}
void initializeVar(int& x, int& y, int& z, int& w)
{
x = 0;
y = 0;
z = 0;
w = 0;
}
void processBlank(ifstream& in, ofstream& out, char& letter, int&
wordsTotal)
{
while (!in.eof())
in.get(letter);
if (letter != ' ')
++wordsTotal;
return;
}
void copyText(ifstream& in, ofstream& out, char& ch)
{
while ((ch != ' ') && (ch != '\n') && (ch != '\t') && in)
{
out.put(ch);
in.get(ch);
}
return;
}
void updateCount(int& wordsLine, int& linesTotal, int& paraTotal, int&
wordsTotal)
{
if(wordsLine == 0)
paraTotal++;
else
linesTotal++;
return;
}
void printTotal(int& wordsTotal, int& linesTotal, int& paraTotal,
ofstream& y)
{
y << endl;
y << endl << "Total number of words: " << wordsTotal << ".";
y << endl << "Total number of lines: " << linesTotal << ".";
y << endl << "Total number of paragraphs: " << paraTotal << ".";
return;
}
可是为什么TOTAL OF WORDS,LINES和PARAGRAPHS都算错的??
可以帮我修正吗???谢谢。。。 |
|
|
|
|
|
|
|
发表于 2-3-2006 03:18 PM
|
显示全部楼层
不明白为什么你要写到火车路酱长。。。
很简单罢了。。 帮你做 Copy and write file, total words and lines.
其它的你自己SETTLE
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
//variables declaration
int length=0;
int line=0, i;
std::string s;
std::ifstream inData( "c:\\inData.txt" );
std:fstream outData("c:\\outData.txt" );
while (getline(inData, s)){
i=0;
do{
outData.put(s[ i]);
i++;
}while(s[ i] != '\0');
outData.put('\n');
outData.flush();
length += s.length();
line++;
}
cout << "Total word(s): " << length << endl;
cout << "Line(s): " << line << endl;
inData.close();
outData.close();
return 0;
}
[ 本帖最后由 夏日么么茶 于 2-3-2006 03:31 PM 编辑 ] |
|
|
|
|
|
|
|
楼主 |
发表于 3-3-2006 10:53 AM
|
显示全部楼层
夏日么么茶非常谢谢你。。。。
真的是感激不尽。。。 |
|
|
|
|
|
|
|
楼主 |
发表于 4-3-2006 07:09 PM
|
显示全部楼层
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
//this function simply defines what characters you allow to be part of a word
//at the moment A-Z and a-z
bool isValidWordChar(char c)
{
return ((c>=65 && c<=90) || (c>=97 && c<=122));
}
//count how many words in a string
int wordsinString(const string &s)
{
int retVal=0;
//same principle as counting paragraphs but just work through characters in line
//instead of lines
bool previousBlank=true;
string::const_iterator i=s.begin(), send=s.end();
while(i!=send){
bool isValidChar = isValidWordChar(*i);
if( isValidChar && previousBlank){
retVal++;
previousBlank=false;
}else if(!isValidChar)
previousBlank=true;
i++;
}
return retVal;
}
int main()
{
int para=0,lines=0,words=0;
ifstream fin("c:\\myfile.txt");
string line;
bool previousBlank=true;
while(getline(fin,line))
{
//each line read is another line
lines++;
//count if new paragraph
//paragraph defined as being a non-blank line following a blank line
//this would be the start of a new paragraph
if(line.length()>0 && previousBlank){
para++;
previousBlank=false;
}else if(line.length()<1)
previousBlank=true;
//check words
words += wordsinString(line);
//output original line
cout << line << endl;
}
//output stats
cout << endl << "Number of paragraphs: " << para << endl;
cout << "Number of lines: " << lines << endl;
cout << "Number of words: " << words << endl;
return 0;
}
夏日麽麽茶。。。酱对吗??? |
|
|
|
|
|
|
|
发表于 6-3-2006 10:04 AM
|
显示全部楼层
原帖由 cheewooi7849 于 4-3-2006 07:09 PM 发表
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
//this function simply defines what characters you allow to be part of a word
//at the momen ...
那你跑时出来的答案是你要的吗?
如果是 。。 就对啦。。 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|