Advertisement
stak441

Practice Exam (Feb7) - Cooking

Feb 4th, 2013
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _03.Cooking
  7. {
  8.     class Program
  9.     {
  10.         static List<double> inputQuantity = new List<double>();
  11.         static List<string> inputMeasure = new List<string>();
  12.         static List<string> inputType = new List<string>();
  13.         static List<double> usedQuantity = new List<double>();
  14.         static List<string> usedMeasure = new List<string>();
  15.         static List<string> usedType = new List<string>();
  16.         static string[] tableMeasure = { "mls", "tsps", "tbsps", "fl ozs", "cups", "pts", "qts", "ls", "gals" };
  17.         static int[] tableUnits = { 1, 5, 15, 30, 240, 480, 960, 1000, 3840 };
  18.  
  19.         static List<double> outputQty = new List<double>();
  20.         static List<string> outputMeasure = new List<string>();
  21.         static List<string> outputType = new List<string>();
  22.  
  23.         static void FillInputData(short n)
  24.         {
  25.             for (int i = 0; i < n; i++)
  26.             {
  27.                 string line = Console.ReadLine();
  28.                 int start = 0;
  29.                 int indexNext = line.IndexOf(':', start);
  30.                 double qty = double.Parse(line.Substring(0, indexNext - start));
  31.                 inputQuantity.Add(qty);
  32.  
  33.                 start = indexNext + 1;
  34.                 indexNext = line.IndexOf(':', start);
  35.                 string measure = line.Substring(start, indexNext - start);
  36.                 inputMeasure.Add(measure);
  37.  
  38.                 start = indexNext + 1;
  39.                 indexNext = line.IndexOf(':', start);
  40.                 string type = line.Substring(start);
  41.                 inputType.Add(type);
  42.             }
  43.         }
  44.  
  45.         static void FillUsedUnitsData(short m)
  46.         {
  47.             for (int i = 0; i < m; i++)
  48.             {
  49.                 string line = Console.ReadLine();
  50.                 int start = 0;
  51.                 int indexNext = line.IndexOf(':', start);
  52.                 double qty = double.Parse(line.Substring(0, indexNext - start));
  53.                 usedQuantity.Add(qty);
  54.  
  55.                 start = indexNext + 1;
  56.                 indexNext = line.IndexOf(':', start);
  57.                 string measure = line.Substring(start, indexNext - start);
  58.                 usedMeasure.Add(measure);
  59.  
  60.                 start = indexNext + 1;
  61.                 indexNext = line.IndexOf(':', start);
  62.                 string type = line.Substring(start);
  63.                 usedType.Add(type);
  64.             }
  65.         }
  66.  
  67.         static double CheckQuantityDifference(string type, string measure, double quantity, string searchType, string searchMeasure, double searchQty)
  68.         {
  69.             double inputInMls = ConvertToBaseUnits(quantity, measure);
  70.             double usedInMls = ConvertToBaseUnits(searchQty, searchMeasure);
  71.  
  72.             double difference = ConvertbaseToOriginal(inputInMls - usedInMls, measure);
  73.             return difference;
  74.  
  75.         }
  76.  
  77.         static double ConvertbaseToOriginal(double quantity, string measure)
  78.         {
  79.             double converted = 0;
  80.             switch (measure)
  81.             {
  82.                 case "mls":
  83.                 case "milliliters": converted = quantity;
  84.                     break;
  85.                 case "teaspoons":
  86.                 case "tsps": converted = quantity / 3;
  87.                     break;
  88.                 case "tablespoons":
  89.                 case "tbsps": converted = quantity / 15;
  90.                     break;
  91.                 case "fluid ounces":
  92.                 case "fl ozs": converted = quantity / 30;
  93.                     break;
  94.                 case "cups": converted = quantity / 240;
  95.                     break;
  96.                 case "pints":
  97.                 case "pts": converted = quantity / 480;
  98.                     break;
  99.                 case "quarts":
  100.                 case "qts": converted = quantity / 960;
  101.                     break;
  102.                 case "liters":
  103.                 case "ls": converted = quantity / 1000;
  104.                     break;
  105.                 case "gallons":
  106.                 case "gals": converted = quantity / 3840;
  107.                     break;
  108.                 default:
  109.                     break;
  110.             }
  111.             return converted;
  112.         }
  113.  
  114.         static double ConvertToBaseUnits(double quantity, string measure)
  115.         {
  116.             double quantityInMls = 0;
  117.             switch (measure)
  118.             {
  119.                 case "mls":
  120.                 case "milliliters": quantityInMls = quantity;
  121.                     break;
  122.                 case "teaspoons":
  123.                 case "tsps":  quantityInMls = quantity * 3;
  124.                     break;
  125.                 case "tablespoons":
  126.                 case "tbsps": quantityInMls = quantity * 15;
  127.                     break;
  128.                 case "fluid ounces":
  129.                 case "fl ozs":quantityInMls = quantity * 30;
  130.                     break;
  131.                 case "cups":  quantityInMls = quantity * 240;
  132.                     break;
  133.                 case "pints":
  134.                 case "pts":   quantityInMls = quantity * 480;
  135.                     break;
  136.                 case "quarts":
  137.                 case "qts":   quantityInMls = quantity * 960;
  138.                     break;
  139.                 case "liters":
  140.                 case "ls":    quantityInMls = quantity * 1000;
  141.                     break;
  142.                 case "gallons":
  143.                 case "gals":  quantityInMls = quantity * 3840;
  144.                     break;
  145.                 default:
  146.                     break;
  147.             }
  148.             return quantityInMls;
  149.         }
  150.  
  151.         static void CheckIfAlreadyFound(double quantity, string measure, string type)
  152.         {
  153.             bool found = false;
  154.             for (int i = 0; i < outputType.Count; i++)
  155.             {
  156.                 if (outputType[i].ToLower() == type.ToLower())
  157.                 {
  158.                     found = true;
  159.                     double addQuantityInMls = ConvertToBaseUnits(quantity, measure);
  160.                     outputQty[i] += ConvertbaseToOriginal(addQuantityInMls, outputMeasure[i]);
  161.                 }
  162.             }
  163.  
  164.             if (found == false)
  165.             {
  166.                 outputQty.Add(quantity);
  167.                 outputMeasure.Add(measure);
  168.                 outputType.Add(type);
  169.             }
  170.         }
  171.  
  172.         static void Main(string[] args)
  173.         {        
  174.  
  175.             short n = short.Parse(Console.ReadLine());
  176.             FillInputData(n);
  177.             short m = short.Parse(Console.ReadLine());
  178.             FillUsedUnitsData(m);
  179.  
  180.             //Console.WriteLine();
  181.             //for (int i = 0; i < n; i++)
  182.             //{
  183.             //    Console.Write(inputQuantity[i] + " ");
  184.             //    Console.Write(inputMeasure[i] + " ");
  185.             //    Console.WriteLine(inputType[i]);
  186.  
  187.             //}
  188.  
  189.             //for (int i = 0; i < m; i++)
  190.             //{
  191.             //    Console.Write(usedQuantity[i] + " ");
  192.             //    Console.Write(usedMeasure[i] + " ");
  193.             //    Console.WriteLine(usedType[i]);
  194.                
  195.             //}
  196.  
  197.            
  198.  
  199.             for (int i = 0; i < n; i++)
  200.             {
  201.                 string type = inputType[i].ToLower();
  202.                 string measure = inputMeasure[i];
  203.                 double quantity = inputQuantity[i];
  204.                 bool match = false;
  205.                 double difference = 0;
  206.                 for (int j = 0; j < m; j++)
  207.                 {                    
  208.                     if (type == usedType[j].ToLower())
  209.                     {
  210.                         match = true;
  211.                         string searchType = usedType[j].ToLower();
  212.                         string searchMeasure = usedMeasure[j];
  213.                         double searchQty = usedQuantity[j];
  214.                         difference += CheckQuantityDifference(type, measure, quantity, searchType, searchMeasure, searchQty);                        
  215.                     }
  216.                 }
  217.  
  218.                 if (match == true && difference <= 0)           //The exact or more quantities needed for the recipy were used
  219.                 {
  220.                     CheckIfAlreadyFound(difference, measure, inputType[i]);    
  221.                 }
  222.                 if (match == false)          //No used ingredients can be found
  223.                 {
  224.                     CheckIfAlreadyFound(quantity, measure, inputType[i]);
  225.                 }
  226.                 if (match == true && difference > 0)           //The required was more than the used
  227.                 {
  228.                     CheckIfAlreadyFound(difference, measure, inputType[i]);
  229.                 }
  230.             }
  231.  
  232.             for (int i = 0; i < outputQty.Count; i++)
  233.             {                
  234.                 if (outputQty[i] >= 0)
  235.                 {
  236.                     Console.Write("{0:F2}:{1}:{2}", outputQty[i], outputMeasure[i], outputType[i]);
  237.                     Console.WriteLine();
  238.                 }
  239.                
  240.             }
  241.         }
  242.  
  243.        
  244.  
  245.        
  246.  
  247.        
  248.  
  249.        
  250.     }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement