Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace _03.Cooking
- {
- class Program
- {
- static List<double> inputQuantity = new List<double>();
- static List<string> inputMeasure = new List<string>();
- static List<string> inputType = new List<string>();
- static List<double> usedQuantity = new List<double>();
- static List<string> usedMeasure = new List<string>();
- static List<string> usedType = new List<string>();
- static string[] tableMeasure = { "mls", "tsps", "tbsps", "fl ozs", "cups", "pts", "qts", "ls", "gals" };
- static int[] tableUnits = { 1, 5, 15, 30, 240, 480, 960, 1000, 3840 };
- static List<double> outputQty = new List<double>();
- static List<string> outputMeasure = new List<string>();
- static List<string> outputType = new List<string>();
- static void FillInputData(short n)
- {
- for (int i = 0; i < n; i++)
- {
- string line = Console.ReadLine();
- int start = 0;
- int indexNext = line.IndexOf(':', start);
- double qty = double.Parse(line.Substring(0, indexNext - start));
- inputQuantity.Add(qty);
- start = indexNext + 1;
- indexNext = line.IndexOf(':', start);
- string measure = line.Substring(start, indexNext - start);
- inputMeasure.Add(measure);
- start = indexNext + 1;
- indexNext = line.IndexOf(':', start);
- string type = line.Substring(start);
- inputType.Add(type);
- }
- }
- static void FillUsedUnitsData(short m)
- {
- for (int i = 0; i < m; i++)
- {
- string line = Console.ReadLine();
- int start = 0;
- int indexNext = line.IndexOf(':', start);
- double qty = double.Parse(line.Substring(0, indexNext - start));
- usedQuantity.Add(qty);
- start = indexNext + 1;
- indexNext = line.IndexOf(':', start);
- string measure = line.Substring(start, indexNext - start);
- usedMeasure.Add(measure);
- start = indexNext + 1;
- indexNext = line.IndexOf(':', start);
- string type = line.Substring(start);
- usedType.Add(type);
- }
- }
- static double CheckQuantityDifference(string type, string measure, double quantity, string searchType, string searchMeasure, double searchQty)
- {
- double inputInMls = ConvertToBaseUnits(quantity, measure);
- double usedInMls = ConvertToBaseUnits(searchQty, searchMeasure);
- double difference = ConvertbaseToOriginal(inputInMls - usedInMls, measure);
- return difference;
- }
- static double ConvertbaseToOriginal(double quantity, string measure)
- {
- double converted = 0;
- switch (measure)
- {
- case "mls":
- case "milliliters": converted = quantity;
- break;
- case "teaspoons":
- case "tsps": converted = quantity / 3;
- break;
- case "tablespoons":
- case "tbsps": converted = quantity / 15;
- break;
- case "fluid ounces":
- case "fl ozs": converted = quantity / 30;
- break;
- case "cups": converted = quantity / 240;
- break;
- case "pints":
- case "pts": converted = quantity / 480;
- break;
- case "quarts":
- case "qts": converted = quantity / 960;
- break;
- case "liters":
- case "ls": converted = quantity / 1000;
- break;
- case "gallons":
- case "gals": converted = quantity / 3840;
- break;
- default:
- break;
- }
- return converted;
- }
- static double ConvertToBaseUnits(double quantity, string measure)
- {
- double quantityInMls = 0;
- switch (measure)
- {
- case "mls":
- case "milliliters": quantityInMls = quantity;
- break;
- case "teaspoons":
- case "tsps": quantityInMls = quantity * 3;
- break;
- case "tablespoons":
- case "tbsps": quantityInMls = quantity * 15;
- break;
- case "fluid ounces":
- case "fl ozs":quantityInMls = quantity * 30;
- break;
- case "cups": quantityInMls = quantity * 240;
- break;
- case "pints":
- case "pts": quantityInMls = quantity * 480;
- break;
- case "quarts":
- case "qts": quantityInMls = quantity * 960;
- break;
- case "liters":
- case "ls": quantityInMls = quantity * 1000;
- break;
- case "gallons":
- case "gals": quantityInMls = quantity * 3840;
- break;
- default:
- break;
- }
- return quantityInMls;
- }
- static void CheckIfAlreadyFound(double quantity, string measure, string type)
- {
- bool found = false;
- for (int i = 0; i < outputType.Count; i++)
- {
- if (outputType[i].ToLower() == type.ToLower())
- {
- found = true;
- double addQuantityInMls = ConvertToBaseUnits(quantity, measure);
- outputQty[i] += ConvertbaseToOriginal(addQuantityInMls, outputMeasure[i]);
- }
- }
- if (found == false)
- {
- outputQty.Add(quantity);
- outputMeasure.Add(measure);
- outputType.Add(type);
- }
- }
- static void Main(string[] args)
- {
- short n = short.Parse(Console.ReadLine());
- FillInputData(n);
- short m = short.Parse(Console.ReadLine());
- FillUsedUnitsData(m);
- //Console.WriteLine();
- //for (int i = 0; i < n; i++)
- //{
- // Console.Write(inputQuantity[i] + " ");
- // Console.Write(inputMeasure[i] + " ");
- // Console.WriteLine(inputType[i]);
- //}
- //for (int i = 0; i < m; i++)
- //{
- // Console.Write(usedQuantity[i] + " ");
- // Console.Write(usedMeasure[i] + " ");
- // Console.WriteLine(usedType[i]);
- //}
- for (int i = 0; i < n; i++)
- {
- string type = inputType[i].ToLower();
- string measure = inputMeasure[i];
- double quantity = inputQuantity[i];
- bool match = false;
- double difference = 0;
- for (int j = 0; j < m; j++)
- {
- if (type == usedType[j].ToLower())
- {
- match = true;
- string searchType = usedType[j].ToLower();
- string searchMeasure = usedMeasure[j];
- double searchQty = usedQuantity[j];
- difference += CheckQuantityDifference(type, measure, quantity, searchType, searchMeasure, searchQty);
- }
- }
- if (match == true && difference <= 0) //The exact or more quantities needed for the recipy were used
- {
- CheckIfAlreadyFound(difference, measure, inputType[i]);
- }
- if (match == false) //No used ingredients can be found
- {
- CheckIfAlreadyFound(quantity, measure, inputType[i]);
- }
- if (match == true && difference > 0) //The required was more than the used
- {
- CheckIfAlreadyFound(difference, measure, inputType[i]);
- }
- }
- for (int i = 0; i < outputQty.Count; i++)
- {
- if (outputQty[i] >= 0)
- {
- Console.Write("{0:F2}:{1}:{2}", outputQty[i], outputMeasure[i], outputType[i]);
- Console.WriteLine();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement