943,607 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1747
  • C# RSS
May 14th, 2008
0

how to compute a string in c#???

Expand Post »
IN C#:::
i have a string
string abc="40+40*4.2%+100"
now how can i get the result????
Reputation Points: 10
Solved Threads: 0
Light Poster
u4umang2001 is offline Offline
28 posts
since Nov 2007
May 14th, 2008
0

Re: 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????
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.

c# Syntax (Toggle Plain Text)
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. double abc = 40 + 40 * 4.2 % +100;
  6. Console.WriteLine(abc);
  7. Console.ReadLine();
  8. }
  9. }

Answer is 108.

Regards,
PK
Reputation Points: 51
Solved Threads: 24
Junior Poster
puneetkay is offline Offline
122 posts
since Nov 2007
May 15th, 2008
0

Re: 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????
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 :-)
Reputation Points: 10
Solved Threads: 1
Newbie Poster
corwing is offline Offline
10 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: OLeDb Connection fail
Next Thread in C# Forum Timeline: Static Cast





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC