2017-04-18 8 views
-1

いくつかの動的式があり、それらは文字列形式です。どのように式の前後に文字列を区切り、その中の各サブ式にいくつかの演算を実行する方法は?私は、それぞれのサブ式の値と最後の10進数の値が必要です。式の前後で文字列をどのように区切るかわからない。文字列の前後で文字列を分割し、それを式に変換する方法は?

// 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; 

Iはreplacings後に得た最終的な発現が 4 * 12 + 8 + 5月16日+ 20 * 12 + 8 + 16分の13

しかし、どのように値を分割することです下記のように及び得る正確な小数値:

=(4 * 12 + 8)

B =(5/16)(20 * 12 + 8 + 13/16)

d = a + b + c;

+0

(*改正*:

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); 

結果: '' string text = "4'-8 \" x5/16 \ "x20'-8 13/16"; ')のコードであり、' '4 * 12 + 8 + 5/16 + 20 * 12 + 8 +13/16 "を結果として得た。 –

+0

なぜ '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を試すことができ、簡単な式のため:私はあなたを実行してきました

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

wt.textbox = aにa、b、cの値を代入する方法はありますか? het.textbox = b; lngt.textbox = c; –

+1

@ madhu kumar:このようなもの: 'wt.textbox.Text = items [0]; het.textbox.Text = items [1]; lngt.textbox.Text = items [2]; ';最後の小数点以下の値を割り当てたい場合は、次のようにします。 '(DataTable dt = new DataTable()){wt.textbox.Text = dt.Compute(items [0]、null).ToString(); ...} ' –

+0

非常にドミトリーBychenkoありがとうございます。 –

関連する問題