Advertisement
Guest User

MAD CV1

a guest
Sep 21st, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace MAD___CV1
  8. {
  9.     class Program
  10.     {
  11.         private const string FILE_DESTINATION = @"C:\Users\jakubdolnicek\source\repos\MAD - CV1\MAD - CV1\results.txt";
  12.  
  13.         private static int allIterationsCount = 0;
  14.  
  15.         //outlook == sunny && humidity == high && play == no
  16.         private static int firstRuleFounds = 0;
  17.         private static int firstRuleFoundsCorrect = 0;
  18.  
  19.         //outlook == rainy && windy == true && play == no
  20.         private static int secondRuleFounds = 0;
  21.         private static int secondRuleFoundsCorrect = 0;
  22.  
  23.         //outlook == overcast && play == yes
  24.         private static int thirdRuleFounds = 0;
  25.         private static int thirdRuleFoundsCorrect = 0;
  26.  
  27.         //humidity == normal && play == yes
  28.         private static int fourthRuleFounds = 0;
  29.         private static int fourthRuleFoundsCorrect = 0;
  30.  
  31.         //none of above play == yes
  32.         private static int fifthRuleFounds = 0;
  33.         private static int fifthRuleFoundsCorrect = 0;
  34.  
  35.         enum OutlookOptions
  36.         {
  37.             sunny,
  38.             overcast,
  39.             rainy,
  40.             none
  41.         }
  42.  
  43.         enum TemperatureOptions
  44.         {
  45.             hot,
  46.             mild,
  47.             cool,
  48.             none
  49.         }
  50.  
  51.         enum HumidityOptions
  52.         {
  53.             high,
  54.             normal,
  55.             none
  56.         }
  57.  
  58.         enum WindyOptions
  59.         {
  60.             TRUE,
  61.             FALSE,
  62.             none
  63.         }
  64.  
  65.         enum PlayOptions
  66.         {
  67.             yes,
  68.             no
  69.         }
  70.  
  71.         static List<string> resultLines = new List<string>();
  72.  
  73.         static bool CheckFirstRule(OutlookOptions outlook, HumidityOptions humidity, PlayOptions play)
  74.         {
  75.             if (outlook == OutlookOptions.sunny && humidity == HumidityOptions.high)
  76.             {
  77.                 Program.firstRuleFounds++;
  78.  
  79.                 if (play == PlayOptions.no)
  80.                 {
  81.                     Program.firstRuleFoundsCorrect++;
  82.                 }
  83.  
  84.                 return true;
  85.             }
  86.  
  87.             return false;
  88.         }
  89.  
  90.         static bool CheckSecondRule(OutlookOptions outlook, WindyOptions windy, PlayOptions play)
  91.         {
  92.             if (outlook == OutlookOptions.rainy && windy == WindyOptions.TRUE)
  93.             {
  94.                 Program.secondRuleFounds++;
  95.  
  96.                 if (play == PlayOptions.no)
  97.                 {
  98.                     Program.secondRuleFoundsCorrect++;
  99.                 }
  100.  
  101.                 return true;
  102.             }
  103.  
  104.             return false;
  105.         }
  106.  
  107.         static bool CheckThirdRule(OutlookOptions outlook, PlayOptions play)
  108.         {
  109.             if (outlook == OutlookOptions.overcast)
  110.             {
  111.                 Program.thirdRuleFounds++;
  112.                 if (play == PlayOptions.yes)
  113.                 {
  114.                     Program.thirdRuleFoundsCorrect++;
  115.                 }
  116.  
  117.                 return true;
  118.             }
  119.  
  120.             return false;
  121.         }
  122.  
  123.         static bool CheckFourthRule(HumidityOptions humidity, PlayOptions play)
  124.         {
  125.             if (humidity == HumidityOptions.normal)
  126.             {
  127.                 Program.fourthRuleFounds++;
  128.                 if (play == PlayOptions.yes)
  129.                 {
  130.                     Program.fourthRuleFoundsCorrect++;
  131.                 }
  132.  
  133.                 return true;
  134.             }
  135.  
  136.             return false;
  137.         }
  138.  
  139.         static void CheckFifthRule(PlayOptions play)
  140.         {
  141.             Program.fifthRuleFounds++;
  142.             if (play == PlayOptions.yes)
  143.             {
  144.                 Program.fifthRuleFoundsCorrect++;
  145.             }
  146.         }
  147.  
  148.         static double Support(int r, int n)
  149.         {
  150.             return (double)r / n;
  151.         }
  152.  
  153.         static double Confidence(int Cmax, int r)
  154.         {
  155.             return (double)Cmax / r;
  156.         }
  157.  
  158.         static double toProcents(double value)
  159.         {
  160.             return value * 100;
  161.         }
  162.  
  163.         static void calcResults(int ruleFounds, int ruleFoundsCorrect, string ruleName)
  164.         {
  165.             resultLines.Add(
  166.                 ruleName
  167.                 + " support: " + toProcents(Support(ruleFounds, allIterationsCount)).ToString()
  168.                 + "% confidence: " + toProcents(Confidence(ruleFoundsCorrect, ruleFounds)).ToString() + "%"
  169.             );
  170.         }
  171.  
  172.         static void printResults()
  173.         {
  174.             System.IO.File.WriteAllLines(FILE_DESTINATION, resultLines.ToArray());
  175.         }
  176.  
  177.         public static void Main(string[] args)
  178.         {
  179.             foreach(OutlookOptions outlook in Enum.GetValues(typeof(OutlookOptions)))
  180.             {
  181.                 foreach (TemperatureOptions temperature in Enum.GetValues(typeof(TemperatureOptions)))
  182.                 {
  183.                     foreach (HumidityOptions humidity in Enum.GetValues(typeof(HumidityOptions)))
  184.                     {
  185.                         foreach (WindyOptions windy in Enum.GetValues(typeof(WindyOptions)))
  186.                         {
  187.                             foreach (PlayOptions play in Enum.GetValues(typeof(PlayOptions)))
  188.                             {
  189.                                 allIterationsCount++;
  190.                                 bool somethingMatch = false;
  191.  
  192.                                 somethingMatch = CheckFirstRule(outlook, humidity, play) ?  true : false;
  193.                                 somethingMatch = CheckSecondRule(outlook, windy, play) || somethingMatch ? true : false;
  194.                                 somethingMatch = CheckThirdRule(outlook, play) || somethingMatch ? true : false;
  195.                                 somethingMatch = CheckFourthRule(humidity, play) || somethingMatch ? true : false;
  196.  
  197.                                 if (!somethingMatch)
  198.                                 {
  199.                                     CheckFifthRule(play);
  200.                                 }
  201.                             }
  202.                         }
  203.                     }
  204.                 }
  205.             }
  206.  
  207.             calcResults(Program.firstRuleFounds, Program.firstRuleFoundsCorrect, "outlook == sunny && humidity == high && play == no");
  208.             calcResults(Program.secondRuleFounds, Program.secondRuleFoundsCorrect, "outlook == rainy && windy == true && play == no");
  209.             calcResults(Program.thirdRuleFounds, Program.thirdRuleFoundsCorrect, "outlook == overcast && play == yes");
  210.             calcResults(Program.fourthRuleFounds, Program.fourthRuleFoundsCorrect, "humidity == normal && play == yes");
  211.             calcResults(Program.fifthRuleFounds, Program.fifthRuleFoundsCorrect, "none of above play == yes");
  212.             printResults();
  213.         }
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement