一.问题描述

统计一篇英文文章中单词的个数与行数。

二.设计思路

三.流程图

四.伪代码 

1

五.代码实现 

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string word;
int main()
{
	ofstream ofs;
	ofs.open("D:\\VisualStudio2022\\Word.txt", ios::out);
	if (!ofs)
	{
		cout << "文件打开失败" << endl;
		return 0;
	}
	getline(cin, word);
	ofs << word;
	ofs.close();
	ifstream ifs;
	ifs.open("D:\\VisualStudio2022\\Word.txt", ios::in);
	if (!ifs)
	{
		cout << "文件打开失败" << endl;
		return 0;
	}
	int ans = 0;
	char c;
	while ((c = ifs.get())!=EOF)
	{
		if (c == ' ')
			ans++;
	}
	ifs.close();
	cout << "单词数量为:" << ans+1 << endl;
	return 0;
}

 

posted on 2023-05-18 21:17  leapss  阅读(14)  评论(0)    收藏  举报