zoukankan      html  css  js  c++  java
  • HashSet/List 排序

    要求:用户从外部导入TXT文件(input textfile),该文件有三列,分别是storeNum,MosrName,MosrNum,如下是一个input textfile的Case:

    5099,Scoresby,5659
    5250,Scoresby,5659
    5211,Scoresby,5659
    5131,Scoresby,5659
    5099,Scoresby,5659
    5250,Scoresby,5659
    5250,Scoresby,5659
    5131,Scoresby,5659
    5099,Scoresby,5659
    5130,Scoresby,5659
    5131,Scoresby,5659
    5211,Scoresby,5659
    5250,Scoresby,5659

    为了程序后续处理该文件,需要验证如下两点:
    1)input textfile中可以出现空行,如有空行要忽略它。

    2)向用户报告哪些行的storeNum是重复的,报告的格式如下:

    Line #1,5,10, has replicated storeNum value : x.

    Line #2,3, has replicated storeNum value : y.

    第一个问题的简单的处理思路是:读取到一行后过滤空白符,再判断改行的长度是否为零。

    第二个问题的有两个思路:

    方法1:将各行读到HashSet中,由于HashSet是集合其本身不会被排序,所以可将HashSet排序(HashSet.OrderBy(Comparer))的副本保存到List中再做后序处理。

    方法2:由于HashSet本身不会被排序(但是副本可以被排序),所以直接考虑用List。

    这两个方法都会将原始的Input textfile中的行被排序,最后提示错误信息将和原始Input textfile不一致,解决这个问题的一个处理办法是:

    将原始的Input textfile加上行号,这样即使被排序后,仍然可以知道这行在原始的Input textfile是第几行。

    完整的实现如下:

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.IO;
      6 
      7 namespace ValidReplicatedLineVer3
      8 {
      9     class Program
     10     {
     11         static void Main(string[] args)
     12         {
     13             string filePath = @"E:inputfile.txt";
     14             StreamReader sr = new StreamReader(filePath);
     15 
     16             List<Line> list = new List<Line>();
     17             int num = 1;
     18 
     19 
     20             while (!sr.EndOfStream)
     21             {
     22                 string strTemp =  sr.ReadLine();
     23                 if (strTemp.Trim().Length == 0)
     24                 {
     25                     continue;
     26                 }
     27 
     28                 string[] strArr = strTemp.Split(new char[] { ',' });
     29                 Line line = new Line();
     30                 line.lineNum = num++;
     31                 line.storeNum = strArr[0];
     32                 line.mosrName = strArr[1];
     33                 line.mosrNum = strArr[2];
     34 
     35                 list.Add(line);
     36             }
     37             sr.Close();
     38 
     39             list = list.OrderBy(x => x.storeNum).ToList();
     40 
     41             foreach (Line l in list)
     42             {
     43                 StringBuilder sb1 = new StringBuilder();
     44                 sb1.AppendLine(Convert.ToString(l.lineNum) + ',' + l.storeNum + ',' + l.mosrName + ',' + l.mosrNum);
     45                 Console.Write(sb1);
     46             }
     47 
     48             StringBuilder sb = new StringBuilder();
     49             sb = ValidReplicatedLines(list);
     50             Console.WriteLine(sb);
     51           
     52         }
     53 
     54         static StringBuilder ValidReplicatedLines(List<Line> list )
     55         {
     56             bool isFirst = true;
     57             string storeNum = null;
     58             StringBuilder sb = new StringBuilder();
     59             StringBuilder sbRet = new StringBuilder();
     60             for (int i = 0; i < list.Count - 1; i++)
     61             {
     62                 if ((list[i].storeNum == list[i + 1].storeNum) && ((i + 1) != list.Count))
     63                 {
     64                     if (isFirst)
     65                     {
     66                         sb.Append("Line #" + Convert.ToString(list[i].lineNum) + "," + Convert.ToString(list[i + 1].lineNum) + ",");
     67                         isFirst = false;
     68                         storeNum = list[i].storeNum;
     69                     }
     70                     else
     71                     {
     72                         sb.Append(Convert.ToString(list[i + 1].lineNum) + ',');
     73                     }
     74                 }
     75                 else
     76                 {
     77                     if (sb.ToString().Trim().Length != 0)
     78                     {
     79                         sb.Append(" replicated value: " + storeNum);
     80                         isFirst = true;
     81                         sbRet.AppendLine(sb.ToString());
     82                         sb.Remove(0, sb.Length);
     83                     }
     84                 }
     85             }
     86             if (sb.ToString().Trim().Length != 0)
     87             {
     88                 sb.Append("replicated value: " + storeNum);
     89                 sbRet.AppendLine(sb.ToString());
     90             }
     91 
     92             return sbRet;
     93         }
     94 
     95         public class Line
     96         {
     97             public int lineNum;
     98             public string storeNum;
     99             public string mosrName;
    100             public string mosrNum;
    101         }
    102     }
    103 }
    View Code
  • 相关阅读:
    记录k8s:k8s1.8.4无坑离线安装
    Aspose.Words 16.8 破解版、添加自定义HTML导出Jpeg压缩质量配置
    Mint linux 自定义上下文菜单实现ZIP压缩文件无乱码解压
    Mint Linux 安装 DotnetCore 遭遇无法修正错误,因为您要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系
    微信多开技巧
    Pycharm5注册方式
    控制Linux下 mono 服务的启动停止
    Cesium 源码笔记[2] CesiumWidget模块的实例化过程 ver1.67
    Cesium 源码打包入门 [ver1.69]
    Cesium 源码笔记[1] Viewer模块实例化的大致过程 ver1.67
  • 原文地址:https://www.cnblogs.com/freecodeX/p/3825801.html
Copyright © 2011-2022 走看看