Advertisement
stanevplamen

02.09.04.03.Cooking

Jul 12th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class Program
  5. {
  6.     static double[] firstQuantities;
  7.     static string[] firstMeasurement;
  8.     static string[] firstProduct;
  9.  
  10.     static double[] secondQuantities;
  11.     static string[] secondMeasurement;
  12.     static string[] secondProduct;
  13.  
  14.     static void Main()
  15.     {
  16.         AddDictionary();
  17.         int n = int.Parse(Console.ReadLine());                                      
  18.         string[] inputLinesFirst = new string[n];
  19.         for (int i = 0; i < n; i++)
  20.         {
  21.             inputLinesFirst[i] = Console.ReadLine();
  22.         }
  23.         //inputLinesFirst[0] = "12:ls:water";            
  24.         //inputLinesFirst[1] = "12000:mls:Water";      
  25.  
  26.         int m = int.Parse(Console.ReadLine());                                    
  27.         string[] inputLinesSecond = new string[m];
  28.         for (int i = 0; i < m; i++)
  29.         {
  30.             inputLinesSecond[i] = Console.ReadLine();
  31.         }
  32.         //inputLinesSecond[0] = "12:cups:coffee";      
  33.  
  34.         firstQuantities = new double[n];
  35.         firstMeasurement = new string[n];
  36.         firstProduct = new string[n];
  37.  
  38.         // parsing the first input
  39.         for (int i = 0; i < inputLinesFirst.Length; i++)
  40.         {
  41.             string[] parceOneStr = inputLinesFirst[i].Split(':');
  42.             firstQuantities[i] = double.Parse(parceOneStr[0].Trim());
  43.             firstMeasurement[i] = parceOneStr[1].Trim();
  44.             firstProduct[i] = parceOneStr[2].Trim();
  45.         }
  46.  
  47.  
  48.         secondQuantities = new double[m];
  49.         secondMeasurement = new string[m];
  50.         secondProduct = new string[m];
  51.  
  52.         // parsing the second input
  53.         for (int i = 0; i < inputLinesSecond.Length; i++)
  54.         {
  55.             string[] parceTwoStr = inputLinesSecond[i].Split(':');
  56.             secondQuantities[i] = double.Parse(parceTwoStr[0].Trim());
  57.             secondMeasurement[i] = parceTwoStr[1].Trim();
  58.             secondProduct[i] = parceTwoStr[2].Trim();
  59.         }
  60.         CompareTwoInputs();
  61.         Print();
  62.     }
  63.  
  64.     private static void Print()
  65.     {
  66.         for (int i = 0; i < firstMeasurement.Length; i++)
  67.         {
  68.             if (firstMeasurement[i] != null && firstQuantities[i] > 0)
  69.             {
  70.                 Console.WriteLine("{0:F2}:{1}:{2}", firstQuantities[i], firstMeasurement[i], firstProduct[i]);
  71.             }
  72.         }
  73.     }
  74.  
  75.     private static void CompareTwoInputs()
  76.     {
  77.         for (int i = 0; i < firstProduct.Length; i++)
  78.         {
  79.             for (int j = i + 1; j < firstProduct.Length; j++)
  80.             {
  81.                 if (firstProduct[i] != null && firstProduct[j] != null && firstProduct[i].ToLower() == firstProduct[j].ToLower())
  82.                 {
  83.                     // quantity i = i + j
  84.                     firstQuantities[i] = CompareProduct(firstQuantities[i], firstQuantities[j],
  85.                         firstMeasurement[i], firstMeasurement[j]);
  86.                     firstQuantities[j] = 0.0;
  87.                     firstMeasurement[j] = null;
  88.                     firstProduct[j] = null;
  89.  
  90.                 }
  91.             }
  92.         }
  93.  
  94.         for (int i = 0; i < secondProduct.Length; i++)
  95.         {
  96.             for (int j = i + 1; j < secondProduct.Length; j++)
  97.             {
  98.                 if (secondProduct[i] != null && secondProduct[j] != null &&  secondProduct[i].ToLower() == secondProduct[j].ToLower())
  99.                 {
  100.                     secondQuantities[i] = CompareProduct(secondQuantities[i], secondQuantities[j],
  101.                         secondMeasurement[i], secondMeasurement[j]);
  102.                     secondQuantities[j] = 0.0;
  103.                     secondMeasurement[j] = null;
  104.                     secondProduct[j] = null;
  105.  
  106.                 }
  107.             }
  108.         }
  109.  
  110.  
  111.         for (int i = 0; i < firstProduct.Length; i++)
  112.         {
  113.             for (int j = 0; j < secondProduct.Length; j++)
  114.             {
  115.                 if (firstProduct[i] != null &&  secondProduct[j] != null && firstProduct[i].ToLower() == secondProduct[j].ToLower())
  116.                 {
  117.                     firstQuantities[i] = CompareDiffProduct(firstQuantities[i], secondQuantities[j],
  118.                         firstMeasurement[i], secondMeasurement[j]);
  119.                     secondQuantities[j] = 0.0;
  120.                     secondMeasurement[j] = null;
  121.                     secondProduct[j] = null;
  122.                     break;
  123.                 }
  124.             }
  125.         }
  126.  
  127.     }
  128.  
  129.     private static double CompareDiffProduct(double toSum1, double toSum2, string measure1, string measure2)
  130.     {
  131.         int firstMesNumber = CheckNumber(measure1);
  132.         int secondMesNumber = CheckNumber(measure2);
  133.         if (firstMesNumber != 9 && secondMesNumber != 9)
  134.         {
  135.             double addedMes = toSum1 - ((globalValues[firstMesNumber] / globalValues[secondMesNumber]) * toSum2);
  136.             return addedMes;
  137.         }
  138.         else
  139.         {
  140.             return toSum1;
  141.         }
  142.     }
  143.  
  144.     private static double CompareProduct(double toSum1, double toSum2, string measure1, string measure2)
  145.     {
  146.         int firstMesNumber = CheckNumber(measure1);
  147.         int secondMesNumber = CheckNumber(measure2);
  148.         if (firstMesNumber != 9 && secondMesNumber != 9)
  149.         {
  150.             double addedMes = toSum1 + ((globalValues[firstMesNumber] / globalValues[secondMesNumber]) * toSum2);
  151.             return addedMes;
  152.         }
  153.         else
  154.         {
  155.             return toSum1;
  156.         }
  157.  
  158.     }
  159.  
  160.     private static int CheckNumber(string measure)
  161.     {
  162.         switch (measure)
  163.         {
  164.             case "cups":
  165.                 return 0;
  166.             case "tsps":
  167.                 return 1;
  168.             case "tbsps":
  169.                 return 2;
  170.             case "pts":
  171.                 return 3;
  172.             case "qts":
  173.                 return 4;
  174.             case "fl ozs":
  175.                 return 5;
  176.             case "gals":
  177.                 return 6;
  178.             case "mls":
  179.                 return 7;
  180.             case "ls":
  181.                 return 8;
  182.             case "teaspoons":
  183.                 return 1;
  184.             case "tablespoons":
  185.                 return 2;
  186.             case "pints":
  187.                 return 3;
  188.             case "quarts":
  189.                 return 4;
  190.             case "fluid ounces":
  191.                 return 5;
  192.             case "gallons":
  193.                 return 6;
  194.             case "milliliters":
  195.                 return 7;
  196.             case "liters":
  197.                 return 8;
  198.             default:
  199.                 return 9;
  200.         }
  201.     }
  202.  
  203.     static List<double> globalValues = new List<double>();
  204.  
  205.     static void AddDictionary()
  206.     {
  207.         globalValues.Add(1.0);              // "cup"
  208.         globalValues.Add(48.0);             // "tsps",
  209.         globalValues.Add(48.0/3);           // "tbsps",
  210.         globalValues.Add(0.5);              // pts",
  211.         globalValues.Add(0.25);             // "qts",
  212.         globalValues.Add(8.0);              // "fl ozs",
  213.         globalValues.Add(0.25 / 4);         // gals
  214.         globalValues.Add(48.0 * 5);         // mls",
  215.         globalValues.Add(48.0 * 5 / 1000);  // "ls",
  216.     }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement