1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
| //Code model
public class FileReader
{
public struct ResultatComparaison
{
public double min;
public double max;
public double average;
public double standardDeviation;
}
private string dir1;
private string dir2;
private Collection<String> res = new Collection<string>();
/// <summary>
/// Get the result dictionnary
/// </summary>
public Collection<String> Res
{
get { return res; }
}
/// <summary>
/// Constructor
/// </summary>
public FileReader(string d1, string d2)
{
dir1 = d1;
dir2 = d2;
}
/// <summary>
/// Read a text file
/// </summary>
/// <param name="nomFichier">A System.String that represents the file name</param>
private void FichierTexte(string nomFichier, List<String> fileList2)
{
StreamReader sr = null;
StreamReader sr2 = null;
string line;
string line2;
string fileNameWithoutPath = System.IO.Path.GetFileName(nomFichier);
try
{
//A modifier dépendant du nom des fichiers
if (File.Exists(System.IO.Path.Combine(dir2, fileNameWithoutPath)) && fileList2.Contains(System.IO.Path.Combine(dir2, fileNameWithoutPath)))
{
// Ouverture des fichiers
sr2 = new StreamReader(System.IO.Path.Combine(dir2, fileNameWithoutPath));
sr = new StreamReader(nomFichier);
//traitement première ligne
line2 = sr2.ReadLine().Trim();
line = sr.ReadLine().Trim();
//header of file treatment
string[] header = line.Split(' ');
string[] header2 = line2.Split(' ');
Collection<Double> val = new Collection<double>(); ;
if (string.Equals(header[0], header2[0]) && string.Equals(header2[1], header[1]))
{
int lineCount = 1;
while (line != null)
{
line = sr.ReadLine();
line2 = sr2.ReadLine();
if (!String.IsNullOrEmpty(line))
{
line = line.Trim();
line2 = line2.Trim();
//treatment of lines
string[] valTmp = line.Split(' ');
string[] valTmp2 = line2.Split(' ');
string[] values = ResizeArray(valTmp);
string[] values2 = ResizeArray(valTmp2);
for (int i = 0; i < values.Length; i++)
{
double v;
double v2;
double.TryParse(values[i], System.Globalization.NumberStyles.Any, CultureInfo.InvariantCulture, out v);
double.TryParse(values2[i], System.Globalization.NumberStyles.Any, CultureInfo.InvariantCulture, out v2);
double f = Math.Abs(v - v2);
val.Add(f);
}
lineCount++;
}
}
double[] array = new double[val.Count];
val.CopyTo(array, 0);
ResultatComparaison r;
r.average = AverageFromDoubleArray(array);
r.max = Maximum(val);
r.min = Minimum(val);
r.standardDeviation = StandardDeviation(val, r.average);
val.Clear();
res.Add(fileNameWithoutPath.Replace(" ", "") + " " +
r.min.ToString("0.0000000000") + " " +
r.max.ToString("0.0000000000") + " " +
r.average.ToString("0.0000000000") + " " +
r.standardDeviation.ToString("0.0000000000"));
}
}
}
finally
{
// Fermeture streamreader
if (sr != null) sr.Close();
if (sr2 != null) sr2.Close();
}
}
private string[] ResizeArray(string[] array)
{
List<string> tmp = new List<string>(array.Length);
for (int i = 0; i < array.Length; i++)
{
if (!string.IsNullOrEmpty(array[i])) tmp.Add(array[i]);
}
string[] res = new string[tmp.Count];
tmp.CopyTo(res);
tmp.Clear();
return res;
}
#region Math
private double StandardDeviation(Collection<double> doubleList, double average)
{
double sumOfDerivation = 0;
foreach (double value in doubleList)
{
sumOfDerivation += (value) * (value);
}
double sumOfDerivationAverage = sumOfDerivation / doubleList.Count;
return Math.Sqrt(sumOfDerivationAverage - (average * average));
}
private double Maximum(Collection<double> val)
{
//calculation of max, min, avg and sd
double max = double.MinValue;
foreach (double d in val)
{
if (d > max) max = d;
}
return max;
}
private double Minimum(Collection<double> val)
{
//calculation of max, min, avg and sd
double min = double.MaxValue;
foreach (double d in val)
{
if (d < min) min = d;
}
return min;
}
/// <summary>
/// Calculate the average of all elements in a double array.
/// </summary>
/// <param name="dblArray">The double array to get the
/// average from.</param>
/// <returns>The average of the double array</returns>
private double AverageFromDoubleArray(double[] dblArray)
{
double dblResult = 0;
foreach (double dblValue in dblArray)
dblResult += dblValue;
return dblResult / dblArray.Length;
}
#endregion
public void Treatment(List<String> fileList1, List<String> fileList2)
{
foreach (string fileName in fileList1)
{
FichierTexte(fileName, fileList2);
}
}
/// <summary>
/// Clear result dictionnary
/// </summary>
public void ClearRes()
{
this.res.Clear();
}
} |
Partager