使用的LINQ和正則表達式,像這樣嘗試:
string input1 = "bike2car5ship86plan3";
string input2 = "car382bike50ship92yoyo2";
var inputs1 = Regex
.Matches(input1, "([a-zA-Z]+)([0-9]+)")
.OfType<Match>()
.ToDictionary(match => match.Groups[1].Value,
match => int.Parse(match.Groups[2].Value));
var inputs2 = Regex
.Matches(input2, "([a-zA-Z]+)([0-9]+)")
.OfType<Match>()
.ToDictionary(match => match.Groups[1].Value,
match => int.Parse(match.Groups[2].Value));
TextInfo textInfo = CultureInfo.CurrentCulture.TextInfo;
var data = inputs1
.Where(pair => inputs2.ContainsKey(pair.Key))
.OrderBy(pair => pair.Key)
.Select(pair => new {
Name = textInfo.ToTitleCase(pair.Key),
Quantity = pair.Value,
Price = inputs2[pair.Key],
Total = pair.Value * inputs2[pair.Key], })
.ToArray();
string table = string.Join(Environment.NewLine, data
.Select(item => string.Format("{0,-5} {1,3} x {2,3} = {3,4}",
item.Name, item.Quantity, item.Price, item.Total)));
string result = string.Join(Environment.NewLine,
table,
new string('-', 25),
string.Format("Total = {0}", data.Sum(pair => pair.Total)));
Console.Write(result);
結果:
Bike 2 x 50 = 100
Car 5 x 382 = 1910
Ship 86 x 92 = 7912
-------------------------
Total = 9922
您嘗試了什麼以及您的問題在哪裏? –
爲什麼'汽車5×38 = 190',而不是'汽車5×382 = 1910'? –
「我試過Matches和LinQ」請發佈這些嘗試,我們可以從那裏開始 –