IN C#:::
i have a string
string abc="40+40*4.2%+100"
now how can i get the result????

IN C#:::
i have a string
string abc="40+40*4.2%+100"
now how can i get the result????

Hie Umang,

As i know, you cant do manipulations in string type variables.

You will have to use int, float or double type variable for it.

class Program
    {
        static void Main(string[] args)
        {
            double abc = 40 + 40 * 4.2 % +100;
            Console.WriteLine(abc);
            Console.ReadLine();
        }
    }

Answer is 108.

Regards,
PK:)

IN C#:::
i have a string
string abc="40+40*4.2%+100"
now how can i get the result????

I had this as a project in assembler class... it was fun
You'll want to parse the string into smaller parts as you find the operators. Then put each one into a string that you can then convert to an int or double. Then where you find your operator perform the operation. But you have to perform them based upon priority... so for your current string you would
parse it and find the operators
you'll have strings "40", "+", "40", "*", "4.2", "*", ".01", "+", "100"
you can have 2 temp vars(temp operator and temp double) and 2 stacks (operators and numbers) then as you pull them off the stack you check for priority of it versus the next operator then do the operation... store your cumulative answer and keep checking to see if there is more to do and do it
Have fun with the code :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.