using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace d2l { class Program { //d2l static void Main(string[] args) { if (args.Length < 2 || args.Length > 32) { Console.Error.WriteLine("Usage: d2l []"); return; } if (!File.Exists(args[0])) { Console.Error.WriteLine("Can't find {0}", args[0]); return; } if (!File.Exists(args[1])) { Console.Error.WriteLine("Can't find {0}", args[1]); return; } int questions = 6; if (args.Length == 3) { if (!int.TryParse(args[2], out questions)) { Console.Error.WriteLine("Question count was invlaid"); return; } } Dictionary grades = new Dictionary(); foreach (string gradeline in File.ReadAllLines(args[1])) { string[] parts = gradeline.Split(','); string student = parts[0].Trim(); int grade = 0; for (int i = 1; i <= questions; i++) { int score = 0; if (!int.TryParse(parts[i], out score)) { Console.Error.WriteLine("{0} non-parsable question score {1}", student, parts[i]); } grade += score; } if (!grades.ContainsKey(student)) { grades.Add(student, grade); //Console.WriteLine("{0}={1}", parts[0], grade); } else { Console.Error.WriteLine("{0} submitted multiple results", student); } } bool header = true; foreach (string templateline in File.ReadAllLines(args[0])) { if (header) { Console.WriteLine(templateline); header = false; } else { string[] parts = templateline.Split(','); string student = parts[0].Trim().Substring(1).Trim(); int grade = 0; if (grades.ContainsKey(student)) { grade = grades[student]; Console.Error.WriteLine("{0} scored {1}", student, grade); grades[student] = -1;//mark as recorded } else { Console.Error.WriteLine("{0} does not have a grade", student); } if (parts[4].Trim() == "") { parts[4] = grade.ToString(); } string mergeline = string.Join(",", parts); Console.WriteLine(mergeline); } } foreach(KeyValuePair entry in grades) { if (entry.Value != -1) { Console.Error.WriteLine("{0} is unknown", entry.Key); } } //Console.ReadKey(); } } }