C++程序编程作业

“Writeaprogramthatinputsthenameofafilefromuser,createsanewfilecalled“output.txt”tosto... “Write a program that inputs the name of a file from user, creates a new file called “output.txt” to store the number of words and lines in the input file.”

作业内容:写一个程序:用户输入一个文件名,然后会创建一个新文件叫“output.txt”,这里面是输入的文件里的单词个数和行数。
最好是英文版的说明,因为是英语教学 。嗯嗯嗯~一楼说的对对对~~~==我要是能会就不问了……
展开
 我来答
小熊玩家1K
2009-12-19 · TA获得超过243个赞
知道小有建树答主
回答量:128
采纳率:0%
帮助的人:0
展开全部
你翻译有问题
应该是
写一个程序:用户输入一个文件名,程序读取这个文件后,创建output.txt,output.txt包含读取文件中单词的个数和文件的行数.

这个里面涉及到几个问题.
第一,单词定义是什么?只要空格隔开的就算单词吗?
第二,假如存在段落之间的空白行,空白行算不算入行数里面?

楼主还是自己思考吧,这个题目很简单的。

/////////
好吧,这个我写的,VC6.0下编译通过.

注意几点
1:如果统计空白行,去掉if(strLine != "")
2:如果是一个字母算一个单词,在获得单词个数不需要那么麻烦
直接在if(字母)后面就加上icount++.目前这个是把空格区分开来的算一个单词
3:如果编译报错unresolved external symbol __endthreadex
请将VC6.0按照如下设置
“Project”->“settings”->“c/c++”
“Catagory” 选择“Code Generation”
“use run-time library”选择“debug multithreaded”
4:测试文件最好放在工程的目录,txt文档.比如1.txt。那么测试就输入1.txt
5:最最后我没有写output.txt,这个你应该在自己做了把....
//////

#include <iostream.h>
#include <fstream.h>
#include "AfxWin.h"

Getlines(CString pathname)
{
CStdioFile file;
file.Open(pathname,CFile::modeRead);
int lines=0;
CString strLine;
while(file.ReadString(strLine))
{
strLine.TrimLeft();
strLine.TrimRight();
if(strLine != "") //这里去掉了空白行数,需要统计空白行数,则去掉.
{
lines ++;
}
}
file.Close();
return lines;
}

Getwords(CString pathName)
{
ifstream fout;
fout.open(pathName);
while(!fout)
{
cout<<"error to open file";
return 0;
}
char x;
BOOL flag=true;
int icount=0; // 做计数器
while(!fout.eof())
{
fout.read(&x,1); //一个字符一个字符的的读
if(((int)x>=65&&(int)x<=90)||((int)x>=97&&(int)x<=122)) //65~90为26个大写英文字母,97~122号为26个小写英文字母
{
if(flag)
{
icount++;
}
flag=false;
}
else
{
flag=true;
}
}
return icount;
}

int main()
{
char sfilename[100];
do{
cout<<"Input the file name:\n";
cin>>sfilename;
int words_num,lines_num;
words_num=Getwords(sfilename);
lines_num=Getlines(sfilename);
cout<<"The number of words is:"<<words_num<<"\n";
cout<<"The number of lines is:"<<lines_num<<"\n";
}
while(1);
return 0;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式