Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Aug 26th, 2008 | Syntax: C# | Size: 3.62 KB | Hits: 153 | Expires: Never
Copy text to clipboard
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Data;
  4. using System.Collections;
  5. namespace retest
  6. {
  7.     class Program
  8.     {
  9.  
  10.         static void Main(string[] args)
  11.         {
  12.             System.IO.StreamReader sr = new System.IO.StreamReader("samplein.txt");
  13.             String fileContent = sr.ReadToEnd();
  14.             sr.Close();
  15.             String cp = @"\s+(-?[\d.]+)";
  16.             Regex re = new Regex(@"(\S+)"+cp+cp+cp+cp+cp+cp+cp+cp+".*?\r\n");
  17.             MatchCollection mc = re.Matches(fileContent);
  18.             DataTable table = new DataTable();
  19.             DataColumn CodeColumn = table.Columns.Add("Code", typeof(string));
  20.             table.Columns.Add("Open", typeof(float));
  21.             table.Columns.Add("High", typeof(float));
  22.             table.Columns.Add("Low", typeof(float));
  23.             table.Columns.Add("Close", typeof(float));
  24.             table.Columns.Add("Change", typeof(float));
  25.             table.Columns.Add("Trade", typeof(float));
  26.             table.Columns.Add("Volume", typeof(float));
  27.             table.Columns.Add("Value", typeof(float));
  28.             table.Columns.Add("Line", typeof(string));
  29.             foreach (Match m in mc)
  30.             {
  31.                 table.Rows.Add(new object[] { m.Groups[1].Value, m.Groups[2].Value, m.Groups[3].Value, m.Groups[4].Value, m.Groups[5].Value, m.Groups[6].Value, m.Groups[7].Value, m.Groups[8].Value, m.Groups[9].Value, m.Groups[0].Value});
  32.             }
  33.             table.AcceptChanges();
  34.             DataRow[] allRows;
  35.             allRows = table.Select();
  36.             DataRow[] foundRows;
  37.             ArrayList list = new ArrayList();
  38.             for (int ii = 0; ii < allRows.Length; ii++)
  39.             {
  40.                 if (!list.Contains(allRows[ii]["Code"]))
  41.                 {
  42.                     list.Add(allRows[ii]["Code"]);
  43.                     string expression = "Code = '" + allRows[ii]["Code"] + "'";
  44.                     foundRows = table.Select(expression);
  45.                     if (foundRows.Length == 2)
  46.                     {
  47.                         float Open = (float)foundRows[0]["Open"] + (float)foundRows[1]["Open"];
  48.                         float High = (float)foundRows[0]["High"] + (float)foundRows[1]["High"];
  49.                         float Low = (float)foundRows[0]["Low"] + (float)foundRows[1]["Low"];
  50.                         float Close = (float)foundRows[0]["Close"] + (float)foundRows[1]["Close"];
  51.                         float Change = (float)foundRows[0]["Change"] + (float)foundRows[1]["Change"];
  52.                         float Trade = (float)foundRows[0]["Trade"] + (float)foundRows[1]["Trade"];
  53.                         float Volume = (float)foundRows[0]["Volume"] + (float)foundRows[1]["Volume"];
  54.                         float Value = (float)foundRows[0]["Value"] + (float)foundRows[1]["Value"];
  55.                         string newline = foundRows[0]["Code"]+"    "+Open+"     "+High+"     "+Low+"     "+Close+"     "+Change+"     "+Trade+"    "+Volume+"     "+Value+Environment.NewLine;
  56.                         string line0 = (string)foundRows[0]["Line"];
  57.                         string line1 = (string)foundRows[1]["Line"];
  58.                         fileContent = fileContent.Replace(line1, "");
  59.                         Console.WriteLine("removed: " + line1);
  60.                         fileContent = fileContent.Replace(line0, newline);
  61.                         Console.WriteLine("  added: " + newline);
  62.                     }
  63.                 }
  64.             }
  65.             System.IO.StreamWriter sw = new System.IO.StreamWriter("sampleout.txt");
  66.             sw.Write(fileContent);
  67.             sw.Close();
  68.         }
  69.     }
  70. }