Advertisement
dimipan80

School System

May 18th, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.60 KB | None | 0 0
  1. /* Write a program that reads a list of student grade entries and prints the average grade of every subject for each student.
  2.  * At the first line a number n stays which says how many lines will follow.  Each of the next n lines holds information in format: <First name> <Last name> <subject> <score>
  3.  * Print on the console in a row for each student in the following format: <First name> <Last name>: [<subject> - <average score>, <subject> - <average score>, …]
  4.  * The subjects of each student should be printed in alphabetical order. Students should be printed in alphabetical order of their full name. The average grade should be rounded to the second decimal. */
  5.  
  6. namespace School_System
  7. {
  8.     using System;
  9.     using System.Collections.Generic;
  10.     using System.Linq;
  11.  
  12.     class SchoolSystem
  13.     {
  14.         private static SortedDictionary<string, SortedDictionary<string, List<double>>> students;
  15.  
  16.         static void Main(string[] args)
  17.         {
  18.             students = new SortedDictionary<string, SortedDictionary<string, List<double>>>();
  19.             GetInputInformation();
  20.             PrintResults();
  21.         }
  22.  
  23.         private static void GetInputInformation()
  24.         {
  25.             int n = int.Parse(Console.ReadLine());
  26.             for (int i = 0; i < n; i++)
  27.             {
  28.                 string[] inputs = Console.ReadLine()
  29.                     .Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
  30.  
  31.                 string fullName = string.Format("{0} {1}", inputs[0], inputs[1]);
  32.                 if (!students.ContainsKey(fullName))
  33.                 {
  34.                     students[fullName] = new SortedDictionary<string, List<double>>();
  35.                 }
  36.  
  37.                 if (!students[fullName].ContainsKey(inputs[2]))
  38.                 {
  39.                     students[fullName][inputs[2]] = new List<double>();
  40.                 }
  41.  
  42.                 students[fullName][inputs[2]].Add(double.Parse(inputs[3]));
  43.             }
  44.         }
  45.  
  46.         private static void PrintResults()
  47.         {
  48.             foreach (var student in students)
  49.             {
  50.                 Console.Write("{0}: [", student.Key);
  51.                 int counter = student.Value.Count;
  52.                 foreach (var subject in student.Value)
  53.                 {
  54.                     counter--;
  55.                     Console.Write("{0} - {1:F2}", subject.Key, subject.Value.Average());
  56.                     if (counter > 0)
  57.                     {
  58.                         Console.Write(", ");
  59.                     }
  60.                 }
  61.  
  62.                 Console.WriteLine("]");
  63.             }
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement