coasterka

#UniTaskZodiacSign

Mar 25th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.75 KB | None | 0 0
  1. //Задача 6. Да се напише програма, която чете рождени дати от файл (всяка дата на отделен ред) и извежда коя зодия е човекът.
  2. //Зодиите са предварително записани в масив от структури във формат - зодия, от месец,от ден, до месец, до ден
  3. //некоректните дати се пропускат от обработката, а не прекъсват програмата.
  4. //да се изчисли общия брой на хората от всяка зодия.
  5.  
  6. //Birthdays.txt
  7. /* 27/03/2012
  8. 02/04/2012
  9. asdas
  10. asdasd
  11. 13/04/2013
  12. 13/08/2013
  13. 13/12/2013 */
  14.  
  15.  
  16.     using System;
  17.     using System.Collections.Generic;
  18.     using System.IO;
  19.  
  20.     public class Program
  21.     {
  22.         internal static void Main(string[] args)
  23.         {
  24.             System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");
  25.  
  26.             var astralSignsStatistic = new Dictionary<string, int>();
  27.  
  28.             using (var reader = new StreamReader(@"../../Birthdays.txt"))
  29.             {
  30.                 for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
  31.                 {
  32.                     var birthday = new DateTime();
  33.  
  34.                     if (DateTime.TryParse(line, out birthday))
  35.                     {
  36.                         var astralSign = GetAstralSign(birthday);
  37.                         if (astralSignsStatistic.ContainsKey(astralSign))
  38.                         {
  39.                             astralSignsStatistic[astralSign]++;
  40.                         }
  41.                         else
  42.                         {
  43.                             astralSignsStatistic.Add(astralSign, 1);
  44.                         }
  45.                     }
  46.                     else
  47.                     {
  48.                         Console.WriteLine("Invalid BirthDay");
  49.                     }
  50.                 }
  51.             }
  52.  
  53.             Console.WriteLine("Summary");
  54.  
  55.             foreach (var sign in astralSignsStatistic)
  56.             {
  57.                 Console.WriteLine("Sign: {0}, Number: {1}", sign.Key, sign.Value);
  58.             }
  59.         }
  60.  
  61.         private static string GetAstralSign(DateTime birthday)
  62.         {
  63.             int month = birthday.Month;
  64.             int day = birthday.Day;
  65.             switch (month)
  66.             {
  67.                 case 1:
  68.                     if (day <= 19)
  69.                     {
  70.                         return "Capricorn";
  71.                     }
  72.  
  73.                     return "Aquarius";
  74.  
  75.                 case 2:
  76.                     if (day <= 18)
  77.                     {
  78.                         return "Aquarius";
  79.                     }
  80.  
  81.                     return "Pisces";
  82.                 case 3:
  83.                     if (day <= 20)
  84.                     {
  85.                         return "Pisces";
  86.                     }
  87.  
  88.                     return "Aries";
  89.                 case 4:
  90.                     if (day <= 19)
  91.                     {
  92.                         return "Aries";
  93.                     }
  94.  
  95.                     return "Taurus";
  96.                 case 5:
  97.                     if (day <= 20)
  98.                     {
  99.                         return "Taurus";
  100.                     }
  101.  
  102.                     return "Gemini";
  103.                 case 6:
  104.                     if (day <= 20)
  105.                     {
  106.                         return "Gemini";
  107.                     }
  108.  
  109.                     return "Cancer";
  110.                 case 7:
  111.                     if (day <= 22)
  112.                     {
  113.                         return "Cancer";
  114.                     }
  115.  
  116.                     return "Leo";
  117.                 case 8:
  118.                     if (day <= 22)
  119.                     {
  120.                         return "Leo";
  121.                     }
  122.  
  123.                     return "Virgo";
  124.                 case 9:
  125.                     if (day <= 22)
  126.                     {
  127.                         return "Virgo";
  128.                     }
  129.  
  130.                     return "Libra";
  131.                 case 10:
  132.                     if (day <= 22)
  133.                     {
  134.                         return "Libra";
  135.                     }
  136.  
  137.                     return "Scorpio";
  138.                 case 11:
  139.                     if (day <= 21)
  140.                     {
  141.                         return "Scorpio";
  142.                     }
  143.  
  144.                     return "Sagittarius";
  145.                 case 12:
  146.                     if (day <= 21)
  147.                     {
  148.                         return "Sagittarius";
  149.                     }
  150.  
  151.                     return "Capricorn";
  152.             }
  153.  
  154.             return string.Empty;
  155.         }
  156.     }
Advertisement
Add Comment
Please, Sign In to add comment