Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
suppose I have to accept an mathematical expression in a string format from the user, like e.g. 2*5 or 9/5+9 or 91-10+82/3 and so on. It can be of any length. It works if I have single length operand like 3+9-5*4/2 by some change in my below code it works fine. but fails if i give it more then one length operand like 38+12/2-7+69 or 325-69+584 and so on C#.

What I have tried:

C#
Below code works fine for single length operand.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApplicationExpressionFirstDay
    {
    class Program
    {
        static void Main(string[] args)
        {
            char[] arrOne = new char[100];
            int[] arrTwo = new int[2];
            string opr="+";
            string input;
            int j = 0;
            int i = 0;
            input = Console.ReadLine();
            arrOne = input.ToCharArray();
            for (i = 0; i < arrOne.Length; i++)
            {
                
                    if (arrOne[i] >= 48 && arrOne[i] <= 57)
                    {
                            arrTwo[j] = Convert.ToInt32(char.ConvertFromUtf32(arrOne[i]));
                            j++;

                    }
                    else
                    {
                        opr =char.ConvertFromUtf32(arrOne[i]);
                    }

                if (j == 2){
                    switch (opr)
                    {
                        case "+":
                            arrTwo[0] = arrTwo[0] + arrTwo[1];
                            j = 1;
                            break;
                        case "-":
                            arrTwo[0] = arrTwo[0] - arrTwo[1];
                            j = 1;
                            break;
                        case "*":
                            arrTwo[0] = arrTwo[0] * arrTwo[1];
                            j = 1;
                            break;
                        case "/":
                            arrTwo[0] = arrTwo[0] / arrTwo[1];
                            j = 1;
                            break;
                        default:
                            j = 0;
                            break;
                    }
                }
            }
            Console.WriteLine("Result : " + arrTwo[0]);
            Console.ReadLine();

        }
    }
    }


But this code fails for more than one length operands

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApplicationExpressionFirstDay
    {
    class Program
    {
        static void Main(string[] args)
        {
            char[] arrOne = new char[100];
            int[] arrTwo = new int[2];
            string opr="+";
            string input;
            int j = 0;<pre lang="c#">int concat = 0;
            int i = 0;
            input = Console.ReadLine();
            arrOne = input.ToCharArray();
            for (i = 0; i < arrOne.Length; i++)
            {
                
                    if (arrOne[i] >= 48 && arrOne[i] <= 57)
                    {
                        if (concat == 0)
                        {
                            arrTwo[j] = Convert.ToInt32(char.ConvertFromUtf32(arrOne[i]));
                            j++;
                            concat++;
                        }
                        else
                        {
                            j = 1;
                            arrTwo[j] = Convert.ToInt32(Convert.ToString(arrTwo[1])+char.ConvertFromUtf32(arrOne[i]));
                            j++;
                            concat = 0;
                        }

                    }
                    else
                    {
                        opr =char.ConvertFromUtf32(arrOne[i]);
                        concat = 0;
                    }

                if (j == 2&&concat==0){
                    switch (opr)
                    {
                        case "+":
                            arrTwo[0] = arrTwo[0] + arrTwo[1];
                            j = 1;
                            break;
                        case "-":
                            arrTwo[0] = arrTwo[0] - arrTwo[1];
                            j = 1;
                            break;
                        case "*":
                            arrTwo[0] = arrTwo[0] * arrTwo[1];
                            j = 1;
                            break;
                        case "/":
                            arrTwo[0] = arrTwo[0] / arrTwo[1];
                            j = 1;
                            break;
                        default:
                            j = 0;
                            break;
                    }
                }
            }
            Console.WriteLine("Result : " + arrTwo[0]);
            Console.ReadLine();

        }
    }
    }
Posted
Updated 4-Nov-16 18:39pm
v7
Comments
Karthik_Mahalingam 3-Nov-16 0:35am    
check this
http://www.codeproject.com/Articles/17853/Implementing-an-Excel-like-formula-engine
Richard MacCutchan 4-Nov-16 6:50am    
You have not explained where the problem occurs in your code. You need to process each pair of numbers in turn, depending on the priority of the operator.
Akhilesh Chauhan 4-Nov-16 6:53am    
I just simply want to evaluate my expression from first element No need for priority. thanks
Richard MacCutchan 4-Nov-16 6:55am    
That does not alter what you need to do.
Richard MacCutchan 4-Nov-16 6:53am    
Your input parsing does not look very good. You need to extract each number and each operand into separate strings before converting them into integers. Once you have done that you can perform the actual calculations.

Google is your friend: Be nice and visit him often. He can answer questions a lot more quickly than posting them here...

A very quick search would have found you loads of examples. c# evaluate expression - Google Search[^] gives you 88,000 examples ...

Here's a good example: State of the Art Expression Evaluation[^]

In future, please try to do at least basic research yourself, and not waste your time or ours.
 
Share this answer
 
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplicationExpressionUsingWhileLoop
{
class Program
{
    static void Main(string[] args)
    {
        char[] arrOne = new char[100];
        int[] arrTwo = new int[2];
        string opr = " ";
        string input;
        int j = 0;
        int i = 0;
        input = Console.ReadLine();
        arrOne = input.ToCharArray();
        for (i = 0; i < arrOne.Length; i++)
        {

            while(arrOne[i] >= 48 && arrOne[i] <= 57)
            {
                arrTwo[j] = Convert.ToInt32(Convert.ToString(arrTwo[j]) + char.ConvertFromUtf32(arrOne[i]));
                if (i == (arrOne.Length)-1)
                {
                    break;
                }
                else
                {
                    i++;
                }
            }
                j++;
                if (j == 2)
                {
                    switch (opr)
                    {
                        case "+":
                            arrTwo[0] = arrTwo[0] + arrTwo[1];
                            j = 1;
                            arrTwo[1] = 0;
                            break;
                        case "-":
                            arrTwo[0] = arrTwo[0] - arrTwo[1];
                            j = 1;
                            arrTwo[1] = 0;
                            break;
                        case "*":
                            arrTwo[0] = arrTwo[0] * arrTwo[1];
                            j = 1;
                            arrTwo[1] = 0;
                            break;
                        case "/":
                            arrTwo[0] = arrTwo[0] / arrTwo[1];
                            j = 1;
                            arrTwo[1] = 0;
                            break;
                        default:
                            j = 0;
                            break;
                    }
                }
                opr = char.ConvertFromUtf32(arrOne[i]);
        }
        Console.WriteLine("Result : " + arrTwo[0]);
        Console.ReadLine();

    }
}
}
 
Share this answer
 
v3
Comments
Philippe Mori 5-Nov-16 11:06am    
Update you question instead...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900