public static int GetValueOfUnitsInString(string metalsAssignment, Dictionary <string, int> units) { IEnumerable <string> matchQuery; int result = 0; int lastDecimal = 0; foreach (var unit in units.Keys) { if (metalsAssignment.Contains(unit)) { string[] source = metalsAssignment.Split(' '); matchQuery = from word in source where word == unit select word; if (matchQuery.Count() > 0) { foreach (var match in matchQuery) { int val = 0; bool canGetValue = units.TryGetValue(match, out val); lastDecimal = val; result = CalculatorHelper.ValuePrecedence(result, val, lastDecimal); } } } } return(result); }
public Dictionary <string, int> CalculateValue(List <string> parsedInput, Dictionary <string, int> units) { string[] valueQuestioninputs; int lastDecimal = 0; Dictionary <string, int> results = new Dictionary <string, int>(); foreach (var value in parsedInput) { int result = 0; if (value.Contains(Symbols.assignment) && value.ToLower().Contains(Symbols.question) && value.ToLower().Contains(Symbols.howMuch)) { var val = value.Substring(value.IndexOf(Symbols.assignment) + 3); valueQuestioninputs = val.Split(' '); int outVal = 0; foreach (var match in valueQuestioninputs) { bool canGetValue = units.TryGetValue(match, out outVal); lastDecimal = outVal; result = CalculatorHelper.ValuePrecedence(result, outVal, lastDecimal); } } results.Add(value, result); } return(results); }
public Dictionary <string, double> CalculateCredits(List <string> parsedInput, Dictionary <string, double> metalToCreditMap, Dictionary <string, int> units) { int creditResult = 0; int lastDecimal = 0; string[] creditQuestionInput; Dictionary <string, double> results = new Dictionary <string, double>(); foreach (var value in parsedInput) { if (value.Contains(Symbols.assignment) && value.ToLower().Contains(Symbols.question) && value.ToLower().Contains(Symbols.credits) && value.ToLower().Contains(Symbols.howMany)) { var creditsString = value.Substring(value.IndexOf(Symbols.assignment) + 3); creditQuestionInput = creditsString.Split(' '); double outDbl = 0; int outVal = 0; double res = 0; creditResult = 0; foreach (var match in creditQuestionInput) { bool test = metalToCreditMap.TryGetValue(match.ToLower(), out outDbl); bool unitTest = units.TryGetValue(match.ToLower(), out outVal); lastDecimal = outVal; creditResult = CalculatorHelper.ValuePrecedence(creditResult, outVal, lastDecimal); if (test) { res = creditResult * outDbl; } } results.Add(value, res); } } return(results); }