Zadání:
Na vstupu tabulka obsahující data rozdělená na sloupce a řádky, jednotlivé položky v řádku jsou oddělené libovolným množstvím mezer a tabulátorů. Úkolem je tuto tabulku zformátovat, setřídit dle sloupce uvedeného za názvem výstupního souboru, a tabulka musí začínat sloupci uvedenými jako další argumenty (jejich počet je volitelný).
Použití:
Kód: Vybrat vše
zpracovaniTabulky.exe Vstup.txt Vystup.txt zbozi prodejce zbozi mesic
Kód: Vybrat vše
mesic zbozi typ prodejce mnozstvi cena trzba
leden brambory tuzemske Bartak 10895 12 130740
leden brambory vlastni Celestyn 15478 10 154780
leden jablka dovoz Adamec 1321 30 39630
leden melouny dovoz Adamec 120 37 4440
leden okurky dovoz Bartak 4258 32 136256
leden papriky dovoz Bartak 453 51 23103
leden pomerance dovoz Adamec 4568 20 91360
unor brambory tuzemske Celestyn 1258 13 16354
unor brambory vlastni Celestyn 7851 10 78510
unor jablka dovoz Adamec 989 23 22747
unor jablka tuzemske Bartak 1423 13 18499
unor papriky dovoz Bartak 545 49 26705
unor rajcata tuzemske Celestyn 231 42 9702
brezen brambory dovoz Adamec 241 28 6748
brezen brambory tuzemske Celestyn 15854 12 190248
brezen jablka dovoz Adamec 1234 25 30850
brezen jablka vlastni Celestyn 745 12 8940
brezen papriky dovoz Bartak 856 36 30816
brezen pomerance dovoz Adamec 3226 19 61294
Kód: Vybrat vše
prodejce zbozi mesic typ mnozstvi cena trzba
Adamec brambory brezen dovoz 241 28 6748
Celestyn brambory brezen tuzemske 15854 12 190248
Celestyn brambory unor vlastni 7851 10 78510
Celestyn brambory unor tuzemske 1258 13 16354
Celestyn brambory leden vlastni 15478 10 154780
Bartak brambory leden tuzemske 10895 12 130740
Bartak jablka unor tuzemske 1423 13 18499
Adamec jablka brezen dovoz 1234 25 30850
Celestyn jablka brezen vlastni 745 12 8940
Adamec jablka unor dovoz 989 23 22747
Adamec jablka leden dovoz 1321 30 39630
Adamec melouny leden dovoz 120 37 4440
Bartak okurky leden dovoz 4258 32 136256
Bartak papriky unor dovoz 545 49 26705
Bartak papriky brezen dovoz 856 36 30816
Bartak papriky leden dovoz 453 51 23103
Adamec pomerance brezen dovoz 3226 19 61294
Adamec pomerance leden dovoz 4568 20 91360
Celestyn rajcata unor tuzemske 231 42 9702
Kód: Vybrat vše
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections.Specialized;
namespace zpracovaniTabulky
{
class Table
{
private class Row : IComparable<Row>
{
private readonly Table table;
private List<string> data;
public List<string> Data
{
get { return this.data; }
}
public Row(Table table, string[] data)
{
this.table = table;
this.data = new List<string>(data);
}
public string Write(List<int> prefferedColumns, List<int> widths)
{
StringBuilder row = new StringBuilder();
foreach (int index in prefferedColumns)
{
row.AppendFormat("{0,-" + (widths[index] + 1) + "}", data[index]);
}
for (int i = 0; i < data.Count; ++i)
{
if (!prefferedColumns.Contains(i))
{
row.AppendFormat("{0,-" + (widths[i] + 1) + "}", data[i]);
}
}
return row.ToString();
}
#region IComparable<Row> Members
public int CompareTo(Row other)
{
return Comparer<string>.Default.Compare(this.data[table.sortColumn], other.data[table.sortColumn]);
}
#endregion
}
private Row header;
private List<Row> rows;
private List<int> widths;
private List<int> prefferedColumns;
private int sortColumn;
public int Width
{
get { return this.header.Data.Count; }
}
public int Height
{
get { return this.rows.Count; }
}
public string SortColumn
{
set
{
this.sortColumn = header.Data.IndexOf(value);
if (this.sortColumn == -1)
{
throw new ArgumentException("Invalid sort column");
}
this.rows.Sort();
}
}
public List<string> PrefferedColumns
{
set
{
this.prefferedColumns = new List<int>();
foreach (string column in value)
{
int columnIndex = header.Data.IndexOf(column);
if (columnIndex != -1)
{
this.prefferedColumns.Add(columnIndex);
}
else
{
throw new ArgumentException("Invalid column");
}
}
}
}
public void Read(Stream stream)
{
using (StreamReader reader = new StreamReader(stream))
{
string line;
this.widths = new List<int>();
if ((line = reader.ReadLine()) != null)
{
string[] strings = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
this.header = new Row(this, strings);
foreach (string column in strings)
{
widths.Add(column.Length);
}
}
this.rows = new List<Row>();
while ((line = reader.ReadLine()) != null)
{
string[] strings = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
if (strings.Length != this.Width)
{
throw new Exception(string.Format("Invalid number of columns on row {0}", this.Height + 1));
}
this.rows.Add(new Row(this, strings));
for (int i = 0; i < this.widths.Count; i++)
{
if (this.widths[i] < strings[i].Length)
{
this.widths[i] = strings[i].Length;
}
}
}
}
}
public void Write(Stream stream)
{
using (StreamWriter writer = new StreamWriter(stream))
{
writer.WriteLine(header.Write(this.prefferedColumns, this.widths));
foreach (Row row in this.rows)
{
writer.WriteLine(row.Write(this.prefferedColumns, this.widths));
}
}
}
}
class Program
{
private static Table table = new Table();
static void Main(string[] args)
{
// open input file
FileInfo inputFile = new FileInfo(args[0]);
if (!inputFile.Exists)
{
Console.Error.WriteLine("File {0} does not exists", inputFile.Name);
return;
}
// read table
using (FileStream stream = inputFile.OpenRead())
{
table.Read(stream);
}
// sort table
string sortColumn = args[2];
try
{
table.SortColumn = sortColumn;
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
}
// read output columns
List<string> columns = new List<string>();
for (int i = 3; i < args.Length; ++i)
{
columns.Add(args[i]);
}
try
{
table.PrefferedColumns = columns;
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
}
// open file
FileInfo outputFile = new FileInfo(args[1]);
// write table
using (FileStream stream = outputFile.OpenWrite())
{
table.Write(stream);
}
}
}
}