2017-04-18 21 views
-1

有一些动态表达式,它们是字符串格式。那么如何在表达式前后分离字符串并对其中的每个子表达式执行一些操作?我需要每个子表达式的值分别和最终的十进制数。不知道如何在表达式前后分隔字符串。如何拆分字符串前后的字符串并将其转换为表达式?

// the textfile contains 4'-8"x5/16"x20'-8 13/16 
string text = System.IO.File.ReadAllText(@"D:arith.txt"); 

// 4*12+8+5/16+20*12+8+13/16 
string path = text 
    .Replace("'", "*12") 
    .Replace("-", "+") 
    .Replace("x", "+") 
    .Replace(" ", "+") 
    .Replace(@"""", ""); 

System.Console.WriteLine("The original string: '{0}'", text); 
System.Console.WriteLine("The final string: '{0}'", path); 

txtCalculate.Text = path; 

最终的表达我replacings后得到的是 4 * 12 + 8 + 5/16 + 20 * 12 + 8 + 13/16-

但是如何分割值如下面并获得精确的十进制值:

A =(4 * 12 + 8)

b =(5/16)

c =(20 * 12 + 8 + 13/16)

d = a + b + c;

+0

我执行你的(*修正*: 'string text =“4'-8 \”x5/16 \“x20'-8 13/16”;')code and I've got''4 * 12 + 8 + 5/16 + 20 * 12 + 8 +13/16“'作为结果。 –

+0

当b = 8 + 5/16时,为什么a = 4 * 12 + 8和b = 5/16而不是a = 4 * 12? –

+0

@DmitryBychenko你是对的字符串文本表达。我需要分开这些值并显示为不同的十进制值。从表达式考虑它的宽度,厚度和长度。 –

回答

2

我建议第一Split才把Replace

string text = "4'-8\"x5/16\"x20'-8 13/16"; 

    string[] items = text 
    .Split('x') 
    .Select(item => item 
     .Replace("'", "*12") 
     .Replace("-", "+") 
     .Replace("x", "+") 
     .Replace(" ", "+") 
     .Replace(@"""", "")) 
    .ToArray(); 

测试:

Console.Write(string.Join(Environment.NewLine, items)); 

结果:

4*12+8 
    5/16 
    20*12+8+13/16 

如果你希望将结果格式化了成公式abc ...(因为你已经把它的问题):

string[] items = text 
    .Split('x') 
    .Select(item => item 
     .Replace("'", "*12") 
     .Replace("-", "+") 
     .Replace("x", "+") 
     .Replace(" ", "+") 
     .Replace(@"""", "")) 
    .Select((v, i) => $"{(char)('a' + i)} = ({v})") 
    .ToArray(); 

    string formula = 
    string.Join(Environment.NewLine, items) + Environment.NewLine + 
    $"{(char) ('a' + items.Length)} = " + 
    string.Join(" + ", Enumerable.Range('a', items.Length).Select(c => $"{(char) (c)}")); 

    Console.Write(formula); 

结果:

a = (4*12+8) 
b = (5/16) 
c = (20*12+8+13/16) 
d = a + b + c 

最后,你所提到的

分别各子表达式和最终十进制数

和简单的公式,你可以尝试DataTable.Compute

string formula; 

using (DataTable dt = new DataTable()) { 
    string[] items = text 
    .Split('x') 
    .Select(item => item 
     .Replace("'", "*12") 
     .Replace("-", "+") 
     .Replace("x", "+") 
     .Replace(" ", "+") 
     .Replace(@"""", "")) 
    .ToArray(); 

    formula = 
    string.Join(Environment.NewLine, items 
     .Select((v, i) => $"{(char) ('a' + i)} = ({v}) = {dt.Compute(v, null)}")) + 
    Environment.NewLine + 
    $"{(char) ('a' + items.Length)} = " + 
    string.Join(" + ", Enumerable.Range('a', items.Length).Select(c => $"{(char) (c)}")) + 
    $" = {items.Sum(item => Convert.ToDecimal(dt.Compute(item, null)))}"; 
} 

Console.Write(formula); 

结果:

a = (4*12+8) = 56 
b = (5/16) = 0.3125 
c = (20*12+8+13/16) = 248.8125 
d = a + b + c = 305.1250 
+0

我如何分配a,b,c值到wt.textbox = a; het.textbox = B; lngt.textbox = C; –

+1

@madhu kumar:类似这样的:'wt.textbox.Text = items [0]; het.textbox.Text = items [1]; lngt.textbox.Text = items [2];';如果你想分配最后一个十进制值:'using(DataTable dt = new DataTable()){wt.textbox.Text = dt.Compute(items [0],null).ToString(); ...}' –

+0

非常感谢你德米特里Bychenko。 –

相关问题