954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to compute a string in c#???

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

u4umang2001
Light Poster
28 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 
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:)

puneetkay
Junior Poster
122 posts since Nov 2007
Reputation Points: 51
Solved Threads: 24
 
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 :-)

corwing
Newbie Poster
10 posts since Apr 2008
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You