软件工程(第三周)

  用了VS,现学了一波C#,感觉和JAVA差不多。与是就用C#把单词统计的程序写了一下。

  废话不多说,先上代码。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Text_Screen
 8 {
 9     /**
10      * 用于统计文档中的字符,单词,以及行数
11      */
12     class CharStatistics
13     {
14         private int charCount;  //用于记录字符数
15         private int wordCount;  //用于记录单词数
16         private int lineCount;  //用于记录行数
17 
18         public CharStatistics() {
19             this.SetChar(0);
20             this.SetWord(0);
21             this.SetLine(0);
22         }
23 
24         public void IncreaseChar()
25         {
26             this.charCount++;
27         }
28 
29         public void IncreaseWord()
30         {
31             this.wordCount++;
32         }
33 
34         public void IncreaseLine()
35         {
36             this.lineCount++;
37         }
38 
39         public void SetChar(int charCount)
40         {
41             this.charCount = charCount;
42         }
43 
44         public void SetWord(int wordCount)
45         {
46             this.wordCount = wordCount;
47         }
48 
49         public void SetLine(int LineCount)
50         {
51             this.lineCount = LineCount;
52         }
53 
54         public int GetChar()
55         {
56             return this.charCount;
57         }
58 
59         public String ToString()
60         {
61             String a = null;
62             a = "字符数:" + this.charCount + "\n单词数:" + this.wordCount + "\n行数:" + this.lineCount;
63             return a;
64         }
65     }
66 }
CharStatistics

  这是第一个类,功能类似于一个计数器吧。可以统计文本里面的字符(包括空格和符号),单词数,行数.

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.IO;
 6 using System.Threading.Tasks;
 7 
 8 namespace Text_Screen
 9 {
10     class TextFromFile
11     {
12         public const string fileName = "MyFile.txt";
13         private CharStatistics myCharStatistics = new CharStatistics();
14         private String myString;
15 
16         /**
17          * 把文件里的所有字符输入到myString
18          * 打印myString里面的字符
19          * 返回myString
20          */
21         public String StringFromFile()
22         {
23             myCharStatistics = new CharStatistics();
24             myCharStatistics.SetLine(1);
25             if (!File.Exists(fileName))
26             {
27                 Console.WriteLine("{0}文件不存在!",fileName);
28                 return null;
29             }
30 
31             StreamReader myStreamReader = new StreamReader(fileName);
32 
33             myString = myStreamReader.ReadToEnd();
34             Console.Write(myString + "\n");
35             return myString;
36         }
37 
38         /*
39          * 统计字符串中的字符(包括空格和一些其他符号)
40          * 统计字符串中单词,行数
41          */
42         public void CountText() {
43             int i = 1;
44             while (i < myString.Length)
45             {
46                 if ((myString[i-1] >= 'a' && myString[i-1] <= 'z') || (myString[i-1] >= 'A' && myString[i-1] <= 'Z'))
47                 {
48                     //这是一个字母
49                     if ( !((myString[i] >= 'a' && myString[i] <= 'z') || (myString[i] >= 'A' && myString[i] <= 'Z')))
50                     {
51                         myCharStatistics.IncreaseWord();
52                     }
53                     myCharStatistics.IncreaseChar();
54                     i++;
55                     if (i == myString.Length)
56                     {
57                         myCharStatistics.IncreaseWord();
58                     }
59                 }
60                 else
61                 {                    
62                     //这是一个符号
63                     if (myString[i-1] == '\n')
64                     {
65                         myCharStatistics.IncreaseLine();
66                         i++;
67                     }
68                     else
69                     {
70                         myCharStatistics.IncreaseChar();
71                         i++;
72                     }
73                 }
74             }
75             
76             Console.Write(myCharStatistics.ToString());
77         }
78 
79         /*
80          * 判断字符串中文件是否为空
81          * 返回一个布尔值
82          */
83         public bool IsStringNull() {
84             if (myString == null)
85             {
86                 return true;
87             }
88             else {
89                 return false;
90             } 
91         }
92     }
93 }
TextFromFile

  第二个类,也可以说是这个程序的主体类.里面的方法主要有如下几个:

  1. 从文档中读取字符串到myString.
  2. 统计字符串中的各项数据:字符,单词,行.
  3. 判断myString是否为空,返回一个布尔值.

   

 1 namespace Text_Screen
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             TextFromFile myTextFile = new TextFromFile();
 8             myTextFile.StringFromFile();
 9             if (myTextFile.IsStringNull())
10             {
11                 Console.Write("文件为空");
12             }
13             else{
14                 myTextFile.CountText();
15             }
16         }
17     }
18 }
main

  main函数,生成一个TextFromFile类的对象并实例化,调用方法统计数据.

  

  因为是程序用的是Ascll字符集,所以并不能支持Unicode,导致注释部分全部为"???"

  这次作业就先写到这吧.

转载于:https://www.cnblogs.com/enhe/p/5310044.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值