WordCount 第二次作业

码云地址:https://gitee.com/lgcj1218/WordCount/tree/master

一.解题思路

本次作业采用的c#语言 按功能分为了三个类 ,计算字符数,计算单词数,计算行数。然后在主程序中new三个类,当需要进行着三个功能时就调用其中的函数。

二.代码说明

1.主程序(包含输出文件的方法)

 static void Main(string[] args)
{
string cmd = "";
while (cmd != "leave")
{
int i;
int charCount, wordCount, lineCount;
string testFile;
string outFile;
Console.Write("wc.exe ");
cmd = Console.ReadLine();
string[] arrMessSplit = cmd.Split(' ');
List<string> arrParameter = new List<string>();
for (i = ; i < arrMessSplit.Length; i++)
{
arrParameter.Add(arrMessSplit[i]);
}
bool isOut = false;
for (i = ; i < arrMessSplit.Length - ; i++)
{ if (arrParameter[i] == "-o")
{
isOut = true;
}
} CharCount cc = new CharCount();
WordCount wc = new WordCount();
LineCount lc = new LineCount();
if (isOut)
{
testFile = arrParameter[arrParameter.Count - ];
for (i = ; i < arrMessSplit.Length - ; i++)
{
if (arrParameter[i] == "-c")
{
cc.CC(testFile);
}
if (arrParameter[i] == "-w")
{
wc.WC(testFile);
}
if (arrParameter[i] == "-l")
{
lc.LC(testFile);
}
}
outFile = arrParameter[arrParameter.Count - ];
FileStream fs = new FileStream(outFile, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.Write("{0},charCount:{1}\r\n", testFile, cc.charCount);
sw.Write("{0},wordCount:{1}\r\n", testFile, wc.wordCount);
sw.Write("{0},lineCount:{1}\r\n", testFile, lc.lineCount);
sw.Flush();
sw.Close();
fs.Close();
Console.WriteLine("WriteToFile:{0}", outFile);
}
else
{
testFile = arrParameter[arrParameter.Count - ];
for (i = ; i < arrMessSplit.Length - ; i++)
{
if (arrParameter[i] == "-c")
{
cc.CC(testFile);
} if (arrParameter[i] == "-w")
{
wc.WC(testFile);
} if (arrParameter[i] == "-l")
{
lc.LC(testFile);
}
}
}
Console.ReadLine();
}
}
}
}

program

2.计算字符数的类

 class CharCount
{ public string testFile;
public int charCount = ;
public void CC(string testFile)
{ int nChar;
char[] symbol = { ' ', ',' };
FileStream file = new FileStream(testFile, FileMode.Open, FileAccess.Read, FileShare.Read);
StreamReader sr = new StreamReader(file);
while ((nChar = sr.Read()) != -)
{
charCount++;
}
Console.WriteLine("{0},charCount:{1}", testFile, charCount);
}
}

charcount

3.计算单词数的类

   class WordCount
{
public string testFile;
public int wordCount=;
public void WC(string testFile)
{ int nChar;
char[] divide = { ' ', ',' };
FileStream file = new FileStream(testFile, FileMode.Open,FileAccess.Read, FileShare.Read);
StreamReader sr = new StreamReader(file);
while ((nChar = sr.Read()) != -)
{
foreach (char c in divide)
{
if (nChar == (int)c)
{
wordCount++;
}
}
}
wordCount++;
Console.WriteLine("{0},wordCount:{1}", testFile, wordCount);
}
}

wordcount

4.计算行数的类

  class LineCount
{
public string testFile;
public int lineCount = ;
public void LC(string testFile)
{ int nChar;
char[] divide = { ' ', ',' };
FileStream file = new FileStream(testFile, FileMode.Open, FileAccess.Read, FileShare.Read);
StreamReader sr = new StreamReader(file);
while ((nChar = sr.Read()) != -)
{
if (nChar == '\n')
{
lineCount++;
}
}
lineCount++;
Console.WriteLine("{0},lineCount:{1}", testFile, lineCount);
}
}

linecount

三.测试设计过程

1.测试写入文件

2.测试计算字符,单词和行数的功能

WC-第二次作业的更多相关文章

  1. 软件工程(GZSD2015)第二次作业小结

    第二次作业,从4月7号开始,陆续开始提交作业.根据同学们提交的作业报告,相比第一次作业,已经有了巨大改变,大家开始有了完整的实践,对那些抽象的名词也开始有了直观的感受,这很好.然后有一些普遍存在的问题 ...

  2. 软件工程(GZSD2015) 第二次作业小结

    第二次作业,从4月7号开始,陆续开始提交作业.根据同学们提交的作业报告,相比第一次作业,已经有了巨大改变,大家开始有了完整的实践,对那些抽象的名词也开始有了直观的感受,这很好.然后有一些普遍存在的问题 ...

  3. 耿丹CS16-2班第二次作业汇总

    -- Deadline: 2016-09-28 12:00 -- 作业内容:http://www.cnblogs.com/huangjunlian/p/5891726.html -- 第二次作业总结: ...

  4. JAVA第二次作业展示与学习心得

    JAVA第二次作业展示与学习心得 在这一次作业中,我学习了复选框,密码框两种新的组件,并通过一个邮箱登录界面将两种组件运用了起来.具体的使用方法和其他得组件并没有什么大的不同. 另外我通过查阅资料使用 ...

  5. 20169212《Linux内核原理与分析》第二周作业

    <Linux内核原理与分析>第二周作业 这一周学习了MOOCLinux内核分析的第一讲,计算机是如何工作的?由于本科对相关知识的不熟悉,所以感觉有的知识理解起来了有一定的难度,不过多查查资 ...

  6. 软件工程(QLGY2015)第二次作业点评(随机挑选20组点评)

    相关博文目录: 第一次作业点评 第二次作业点评 第三次作业点评 说明:随机挑选20组点评,大家可以看看blog名字,github项目名字,看看那种是更好的,可以学习,每个小组都会反应出一些问题,希望能 ...

  7. 程序设计第二次作业<1>

    面向对象程序设计第二次作业<1> Github 链接:https://github.com/Wasdns/object-oriented 题目: <1>第一次尝试 我立马认识到 ...

  8. homework-02,第二次作业——寻找矩阵最大子序列和

    经过漫漫漫~~~~~~~~~~~~~~长的编译和调试,第二次作业终于告一段落了 先放出源码,思路后面慢慢道来 #include<stdio.h> #include<stdlib.h& ...

  9. 20169210《Linux内核原理与分析》第二周作业

    <Linux内核原理与分析>第二周作业 本周作业分为两部分:第一部分为观看学习视频并完成实验楼实验一:第二部分为看<Linux内核设计与实现>1.2.18章并安装配置内核. 第 ...

  10. SQL 第二章 作业

    /*第二章 作业*/ create table S ( sno char(2) NOT NULL UNIQUE, sname char(3), city char(2) ); alter table ...

随机推荐

  1. c# XML-Object对象 序列化-反序列化

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  2. C/C++连接查询MYSQL

    1. [代码][C/C++]代码 #include <mysql/mysql.h>#include <stdio.h>#include <string.h>int ...

  3. HDU 4405 Aeroplane chess:期望dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4405 题意: 你在下简化版飞行棋... 棋盘为一个线段,长度为n. 上面有m对传送门,可以直接将你从a ...

  4. laravel基础课程---7、文件处理、闪存、cookie(cookie原理和使用场景)

    laravel基础课程---7.文件处理.闪存.cookie(cookie原理和使用场景) 一.总结 一句话总结: 页面请求服务器的时候是把这个页面中所有的cookie都带上了的,cookie里面也存 ...

  5. Seq2SQL :使用强化学习通过自然语言生成SQL

    论文: https://einstein.ai/static/images/layouts/research/seq2sql/seq2sql.pdf  数据集:https://github.com/s ...

  6. PHP 流程控制

    流程控制 if, else, elseif $a = 5; $b = 9; if ($a > $b): echo "a is bigger than b"; elseif ( ...

  7. CRC16算法之二:CRC16-CCITT-XMODEM算法的java实现

    CRC16算法系列文章: CRC16算法之一:CRC16-CCITT-FALSE算法的java实现 CRC16算法之二:CRC16-CCITT-XMODEM算法的java实现 CRC16算法之三:CR ...

  8. docker安装与操作

    准备和安装 1.到这个路径下下载docker engine: https://get.docker.com/rpm/1.7.1/centos-7/RPMS/x86_64/docker-engine-1 ...

  9. JavaScript的中类型转换

    JavaScript的类型转换 By 大志若愚  (一)转换为字符串 X + '' toString() String() 函数转换为字符串一般是将函数体输出,不过可以重写其toString方法  ( ...

  10. KCF+Opencv3.0+Cmake+Win10 测试

    配置 需要的文件下载 安装CMake,安装opencv3.0.0 在KCFcpp-master 目录下新建一个文件夹,命名为build 打开CMake-GUI配置如下: 点击Configure,编译器 ...