wordcount(代码片段)

彩民 彩民     2022-10-26     772

关键词:

一.Githubhttps://github.com/JiejieCM/WordCount

二.psp表格

因为做的时间比较零散,大多是估计,所以表格可能不太准确。

PSP2.1

PSP阶段

预估耗时

(分钟)

实际耗时

(分钟)

Planning

计划

 30

 30

· Estimate

· 估计这个任务需要多少时间

1440

1920

Development

开发

1200

1200

· Analysis

· 需求分析 (包括学习新技术)

600

600

 

· Design Spec

· 生成设计文档

 0

 0

· Design Review

· 设计复审 (和同事审核设计文档)

 0

 0

· Coding Standard

· 代码规范 (为目前的开发制定合适的规范)

 0

 0

· Design

· 具体设计

 60

 60

· Coding

· 具体编码

 300

 600

· Code Review

· 代码复审

 100

 100

· Test

· 测试(自我测试,修改代码,提交修改)

 120

 240

Reporting

报告

 100

 150

· Test Report

· 测试报告

60

120 

· Size Measurement

· 计算工作量

 20

 0

· Postmortem & Process Improvement Plan

· 事后总结, 并提出过程改进计划

 20

 30

 

合计

 1300

 1650

 三.解题思路

使用c++,vs2015

拿到题目时,看到基础功能觉得自己还是可以完成的,因为想巩固基本语言c++,于是选择了使用c++进行编程。由于自己代码能力比较差,所以暂时只完成了基础功能,扩展功能写了一些但有点问题,所以没有提交。

【基础功能】

wc.exe -c file.c     //返回文件 file.c 的字符数
wc.exe -w file.c     //返回文件 file.c 的单词总数
wc.exe -l file.c     //返回文件 file.c 的总行数
wc.exe -o outputFile.txt     //将结果输出到指定文件outputFile.txt
统计字符:这个很简单,直接用函数strlen得出。
统计行数:读入文件时,我采用getline逐行读入,循环累加数则为文件行数。
统计单词:单词的统计则稍微复杂一点,这里我设置一个变量flag,用flag对此时读入的是否是单词进行标记,从而进行计数。
[参考]:http://blog.csdn.net/henry19850318/article/details/5929023

【指令读入与分析】
读取控制台指令是我以前没有接触过的,经查阅资料发现可以直接利用main函数的argv参数。
当控制台指令为wc.exe -c test.c时,这三个参数分别对应argv[]的argv[0],argv[1],argv[2],以此类推。
扫描指令,当遇到“-”时说明此事读入的是指令,然后根据“c”“w”“l”“o”来区分其执行的不同操作,否则读入的则为文件名(输入或者输出文件),从而对文件进行操作。
此时我遇到的问题是,当只有一个命令参数如-c时,传入文件名显然是argv[2],但传入多个参数时,不一定是
argv[2],如是对参数进行遍历,当有“-”时,读入的是命令参数,此处用结构体进行标记。
不是“-”的话此时传入的即为文件名。
[参考]:http://blog.csdn.net/henry19850318/article/details/5929023

【读取文件及输出文件】
这里要用到头文件
fstream,这里我直接用了两个函数,fstream infile(filename, ios::in),读取文件名为filename的文件,ios::in表示仅对文件进行读取,将结果输出到文件中使用了函数in.open(outputfile, ios::trunc)
将内容输出到outputfile中。在对文件进行读取时,采用了逐行读取,即getline(str, 256)。
四.代码分析
设置结构体,判断指令
struct Command 
	bool _c;
	bool _w;
	bool _l;
	bool _o;
	bool _s;
	Command() 
		_c = false;
		_w = false;
		_l = false;
		_o = false;
		_s = false;
	
;

主要统计函数

void count(fstream &outfile, int *cnt)  //统计函数  

	char str[256];
	int flag;
	while (outfile.getline(str, 256))
	
		cnt[2]++;
		cnt[0] = cnt[0] + strlen(str);
		flag = 1;
		for (int i = 0; i < strlen(str); i++)
		
			if (str[i] >= \'A\' && str[i] <= \'z\'&& flag == 1)

			
				cnt[1]++;//统计单词数  
				flag = 0;
			
			if (str[i] == \' \' || str[i] == \',\' || str[i] == \'\\t\' || str[i] == \'\\n\')
				flag = 1;
		

	

	return;

主函数

int main(int argc, char* argv[])


	int i;
	char filename[MAX_PATH_LENGTH];
	char outputfile[MAX_PATH_LENGTH] = "result.txt";
	int cnt[3] =  0 ;
	vector<string> files;
	Command command;

	for (i = 1;i < argc;i++) 
		if (argv[i][0] == \'-\')
		
			if (argv[i][1] == \'c\')
				command._c = true;
			if (argv[i][1] == \'w\')
				command._w = true;
			if (argv[i][1] == \'l\')
				command._l = true;
			if (argv[i][1] == \'o\')
			
				command._o = true;
				i++;
				strcpy_s(outputfile, argv[i]);
			
			if (argv[i][1] == \'s\') 
				i++;
				command._s = true;
			
		
		else
			strcpy_s(filename, argv[i]);
	
	if (command._s = true) 
		getFileName(filename, files);
		for (i = 0;i < files.size();i++)
		
			if (strcmp(argv[i], "-c") == 0)
				cout << files[i] << "," << "字符数:" << cnt[0] << endl;
			if (strcmp(argv[i], "-w") == 0)
				cout << files[i] << "," << "单词数:" << cnt[1] << endl;
			if (strcmp(argv[i], "-l") == 0)
				cout << files[i] << "," << "行数:" << cnt[2] << endl;
			if (strcmp(argv[i], "-o") == 0)
			
				ofstream in;//将结果输出到output文件
				in.open(outputfile, ios::trunc);
				in << "字符数:" << cnt[0] << endl;
				in << "单词数:" << cnt[1] << endl;
				in << "行数:" << cnt[2] << endl;
			
		
	
	else 
		fstream infile(filename, ios::in);//打开文件用于读取
		count(infile, cnt);
		for (i = 0;i < argc; i++) 
			if (strcmp(argv[i], "-c") == 0)
				cout << filename << "," << "字符数:" << cnt[0] << endl;
			if (strcmp(argv[i], "-w") == 0)
				cout << filename << "," << "单词数:" << cnt[1] << endl;
			if (strcmp(argv[i], "-l") == 0)
				cout << filename << "," << "行数:" << cnt[2] << endl;
			if (strcmp(argv[i], "-o") == 0)
			
				ofstream in;//将结果输出到output文件
				in.open(outputfile, ios::trunc);
				in << "字符数:" << cnt[0] << endl;
				in << "单词数:" << cnt[1] << endl;
				in << "行数:" << cnt[2] << endl;
			
		
		infile.close();
	

五.测试

根据不同指令
1.wc.exe -c test.c

 

2.wc.exe -w test.c

 

3.wc.exe -l test.c

 

4.wc.exe -c -w test.c

 

5.wc.exe -c -l test.c

 

6.wc.exe -w -l test.c

 

7.wc.exe -c -w -l test.c

 8.wc.exe -o result.txt


取多个文件进行测试
9.文件test1

10.文件test2

六.参考
【1】http://blog.csdn.net/henry19850318/article/details/5929023
【2】http://blog.csdn.net/u010166404/article/details/46353263

wordcount(代码片段)

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.IO;namespaceWordCountclassProgramstaticvoidMain(string[]args)Typetype=newType() 查看详情

wordcount(代码片段)

1.Github项目地址https://github.com/chaindreamjet/WordCount2.PSP表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划 20 30·Estimate·估计这个任务需要多少时间 20 30Development开发 700 940·Analysis·需求分析(包括 查看详情

mapreduce编写wordcount程序代码实现(代码片段)

MapReduce经典案例代码(wordcount)以经典的wordcount为例,通过自定义的mapper和reducer来实现单词计数packagecom.fwmagic.mapreduce;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.fs.Path;importorg.apache.hadoop.io. 查看详情

分享知识-快乐自己:运行(wordcount)案例(代码片段)

运行wordcount案例:一):大数据(hadoop)初始化环境搭建二):大数据(hadoop)环境搭建三):运行wordcount案例四):揭秘HDFS五):揭秘MapReduce六):揭秘HBase七):HBase编程-----------------------------------------------------------------Hadoo... 查看详情

wordcount(代码片段)

一.码云地址https://i.cnblogs.com/EditPosts.aspx?opt=1https://gitee.com/lttzy/wordcount二.PSP表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划 30 20·Estimate·估计这个任务需要多少时间 20 10Development开发 45 查看详情

wordcount(代码片段)

一.github:https://github.com/JiejieCM/WordCount二.pspPSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划 30 30·Estimate·估计这个任务需要多少时间3030Development开发 3天 5天·Analysis·需求分析(包括学习新技术) 2天&nbs... 查看详情

wordcount(代码片段)

WordCount一.个人Gitee地址:https://gitee.com/godcoder979/(该项目完整代码在这里) 二.项目简介:该项目是一个统计文件字符、单词、行数等数目的应用程序,通过输入命令来执行你想要的操作。所用语言:java命令格式:wc.exe[para]&... 查看详情

wordcount(代码片段)

一、Gitee项目地址https://gitee.com/YSS_MYGit/WordCount二、基本思路 我看到项目后,首先想到的是使用C#语言编写,因为我对C#语言比较熟悉些。此项目有两个类,一个是Program类,该类是程序的入口,主要用于传参,另一个是WC类,... 查看详情

wordcount(代码片段)

一.Gitee地址:https://gitee.com/zjgss99/WordCount 二.项目分析:对程序设计语言源文件统计字符数、单词数、行数,统计结果以指定格式输出到默认文件中,以及其他扩展功能,并能够快速地处理多个文件。命令格式:wc.exe[para] ... 查看详情

个人项目wordcount(代码片段)

1.项目GitHub地址:https://github.com/FPXBao/wordcount2.解题思路:分析程序基本需求,将其功能分为三个函数调用:主函数intmain();功能函数Ccount();Wcount();Lcount();并进行相关知识学习。3.代码说明:主函数:#include<stdio.h>#include<stdlib.... 查看详情

wordcount(代码片段)

   码云地址:https://gitee.com/qaqxx88/wc1WordCount需求说明 (1)对源文件进行字符数,行数,单词数的统计并且放在与wc.exe相同目录下的result.txt中  (2)基本功能  wc.exe -c file.c   &nbs 查看详情

wordcount基础功能(代码片段)

Gitte地址:https://gitee.com/gyuyue/WordCount PSP表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划 15 20·Estimate·估计这个任务需要多少时间 30 30Development开发 60 80·Analysis·需求分析(包括学习新技术)... 查看详情

wordcount代码实现及测试(代码片段)

...者:201631062515201631062415码云地址:https://gitee.com/heshuxiang/WordCount/tree/master2.项目需求  对程序设计语言源文件统计字符数、单词数、行数,统计结果以指定格式输出到默认文件中,以及其他扩展功能,并能够快速地处理多... 查看详情

wordcount基本功能(代码片段)

项目的gitee地址:https://gitee.com/ITtoto/WordCount 开发语言:C语言PSP2.1表格PSP2.1PSP阶段预估耗时(分钟)实际耗时(分钟)Planning计划20 16·Estimate·估计这个任务需要多少时间 30 27Development开发 120 160·Analysis·需... 查看详情

wordcount(代码片段)

   Gitee地址:https://gitee.com/dyh1621838953/WordCount1此项目我只实现了基本功能,即字符、行数、单词数的计算以及将结果输出到文件中。该项目用C#语言进行实现。解题思路:   根据题目要求所知,需要读取一... 查看详情

[mapreduce_1]运行wordcount示例程序(代码片段)

 0.说明  MapReduce实现WordCount示意图&& WordCount代码编写    1. MapReduce实现WordCount示意图     1.Map:预处理阶段,将原始数据映射成每个K-V,发送给reduce  2.Shuffle:混洗(分类),将相同的Key... 查看详情

wordcount(代码片段)

gitee地址:https://gitee.com/yzyindex/wordcount需求分析通过程序设计,编写一个可执行文件exe能够对源程序文件进行统计字符数、单词数、行数,统计结果可以以指定的格式输出到默认文件中。可执行程序命名为:wc.exe,该程序处理用... 查看详情

wordcount-软测作业(代码片段)

1.github地址 https://github.com/zgwe/wordCount2.PSP2.1PSP阶段预估耗时实际耗时(分钟)(分钟)Planning计划 10 10·Estimate·估计这个任务需要多少时间 20 10Development开发 40 60·Analysis·需求分析(包括学习新技术)  查看详情