Question

Im a C# beginner here,Im taking string from textbox and applying following algo,but the breaking of operands and operator doesnt work..

    string cal = "225+699"; 
        char[] all= cal.ToCharArray();
        int i=0,j=0;
        while (all[i] != '+' || all[i] != '-' || all[i] != '/' || all[i] != 'x')
        {

            j++;

        } // first operator check 
        char op = all[j + 1]; // operator save
        char[] part1 = new char[j];

        for (i = 0; i <= j; i++)
        {
            part1[i] = all[i];
        }
        string inp = Convert.ToString(part1);
        float a = float.Parse(inp); // first operand
        int l=cal.Length-(j+1);
        char[] part2 = new char[l];
        int c = 0;
        for (i = j + 1; i <= cal.Length; i++)// starts after operator
        {
            part2[c] = all[i];
            c++;

        }

        string h = Convert.ToString(part2);
        float b = float.Parse(h); // other operand 
Was it helpful?

Solution

Prerequisite: learn the basics of the language. For example that you can use an indexer on a string or how to use lists instead of arrays. Learn about how logic operators work, your while condition for example is always true or throws an exception, it also seems out of place as there is no nested loop here but it looks like it belongs in one. Bottom line is - you're going in too deep and too fast.

If you're a beginner and looking to learn parsing... go back to the books and read some theory, find out what Reverse Polish Notation is and how to use it

If you're looking to solve the parsing problem in order to solve some other task look at FLEE, it can do the parsing for you and will likely be more useful from a practical standpoint (although theoretical knowledge has its merits and shouldn't be dismissed)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top