2

I have winform project in c# that makes math operations. The string comes like "=B10+B4*(B12-B8)". And B10 represents "3", B4 represents "10" B12 represent "6" and B8 represent "2". I want to convert this string to "=3+10*(6-2)". So math operation can be happened. For solution I created three list. I succeded to make operatorList and numberRepresentList as seen below. My problem is combining this two list with a specific order.

List<string> operatorList= new List<string>();
List<string> numberRepresentList = new List<string>();
List<string> mergedList= new List<string>();


operatorList={"=","+","*","(","-",")"}
numberRepresentList =={"3","10","6","2"}

How can i combine them with a specific order so the result will shown as:

mergedList={"=","3","+","10","*","(","6","-","2",")"}

I have searched in forms but i can not find similar one. So i created one. Thanx in advance.

Edited: Thanx to @vivek nuna. I made a list name partOfString

 comesFromUser="=B10+B4*(B12-B8)"
 partofString={"B10","B4","B12","B8"}
 numberRepresentList =={"3","10","6","2"}
    
    for (int i = 0; i < partOfString.Count; i++)
        {
         comesFromUser = comesFromUser.Replace(partOfString[i], numberRepresentList[i]);
        }
Gokhan
  • 453
  • 4
  • 10
  • 1
    You need a parser/lexer, a bit beyond the scope of [so] – Charlieface Feb 21 '21 at 10:36
  • @Charlieface Why is it beyond the scope of SO? If he can formulate a code problem, surely that's on-topic for SO? As long as it's reasonably scoped to a concrete question. – Xerillio Feb 21 '21 at 10:46
  • @Gokhan why did you choose to split the input string into two separate lists? Doing that you are losing the information about where the *tokens* are positioned – Xerillio Feb 21 '21 at 10:50
  • @Xerillio. Thanx for your attention.I have to split string. Because, for example in B10: "B" represent datagridview Column Name. "10" represent row index number. So B10 is a dataGridView Cell that it's value is "3". I'm trying to make excel operations. – Gokhan Feb 21 '21 at 11:23

1 Answers1

4

There could be many ways to solve this problem.

The simplest approach, you can do this.

string output = input.Replace("B10", "3").Replace("B4", "10").Replace("B12", "6").Replace("B8", "2");
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197