Untitled
By: a guest | Aug 26th, 2008 | Syntax:
C# | Size: 3.62 KB | Hits: 153 | Expires: Never
using System;
using System.Text.RegularExpressions;
using System.Data;
using System.Collections;
namespace retest
{
class Program
{
static void Main(string[] args)
{
System.IO.
StreamReader sr
= new System.IO.
StreamReader("samplein.txt");
String fileContent = sr.ReadToEnd();
sr.Close();
String cp = @"\s+(-?[\d.]+)";
Regex re
= new Regex
(@"(\S+)"+cp
+cp
+cp
+cp
+cp
+cp
+cp
+cp
+".*?\r\n");
MatchCollection mc = re.Matches(fileContent);
DataTable table
= new DataTable
();
DataColumn CodeColumn
= table.
Columns.
Add("Code",
typeof(string));
table.
Columns.
Add("Open",
typeof(float));
table.
Columns.
Add("High",
typeof(float));
table.
Columns.
Add("Low",
typeof(float));
table.
Columns.
Add("Close",
typeof(float));
table.
Columns.
Add("Change",
typeof(float));
table.
Columns.
Add("Trade",
typeof(float));
table.
Columns.
Add("Volume",
typeof(float));
table.
Columns.
Add("Value",
typeof(float));
table.
Columns.
Add("Line",
typeof(string));
foreach (Match m in mc)
{
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});
}
table.AcceptChanges();
DataRow[] allRows;
allRows = table.Select();
DataRow[] foundRows;
ArrayList list
= new ArrayList
();
for (int ii = 0; ii < allRows.Length; ii++)
{
if (!list.Contains(allRows[ii]["Code"]))
{
list.Add(allRows[ii]["Code"]);
string expression = "Code = '" + allRows[ii]["Code"] + "'";
foundRows = table.Select(expression);
if (foundRows.Length == 2)
{
float Open = (float)foundRows[0]["Open"] + (float)foundRows[1]["Open"];
float High = (float)foundRows[0]["High"] + (float)foundRows[1]["High"];
float Low = (float)foundRows[0]["Low"] + (float)foundRows[1]["Low"];
float Close = (float)foundRows[0]["Close"] + (float)foundRows[1]["Close"];
float Change = (float)foundRows[0]["Change"] + (float)foundRows[1]["Change"];
float Trade = (float)foundRows[0]["Trade"] + (float)foundRows[1]["Trade"];
float Volume = (float)foundRows[0]["Volume"] + (float)foundRows[1]["Volume"];
float Value = (float)foundRows[0]["Value"] + (float)foundRows[1]["Value"];
string newline = foundRows[0]["Code"]+" "+Open+" "+High+" "+Low+" "+Close+" "+Change+" "+Trade+" "+Volume+" "+Value+Environment.NewLine;
string line0 = (string)foundRows[0]["Line"];
string line1 = (string)foundRows[1]["Line"];
fileContent = fileContent.Replace(line1, "");
Console.WriteLine("removed: " + line1);
fileContent = fileContent.Replace(line0, newline);
Console.WriteLine(" added: " + newline);
}
}
}
System.IO.
StreamWriter sw
= new System.IO.
StreamWriter("sampleout.txt");
sw.Write(fileContent);
sw.Close();
}
}
}