how to compute a string in c#???

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 28
Reputation: u4umang2001 is an unknown quantity at this point 
Solved Threads: 0
u4umang2001's Avatar
u4umang2001 u4umang2001 is offline Offline
Light Poster

how to compute a string in c#???

 
0
  #1
May 14th, 2008
IN C#:::
i have a string
string abc="40+40*4.2%+100"
now how can i get the result????
Thanks & Regards
Umang
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 121
Reputation: puneetkay is on a distinguished road 
Solved Threads: 23
puneetkay's Avatar
puneetkay puneetkay is offline Offline
Junior Poster

Re: how to compute a string in c#???

 
0
  #2
May 14th, 2008
Originally Posted by u4umang2001 View Post
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.

  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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 10
Reputation: corwing is an unknown quantity at this point 
Solved Threads: 0
corwing corwing is offline Offline
Newbie Poster

Re: how to compute a string in c#???

 
0
  #3
May 15th, 2008
Originally Posted by u4umang2001 View Post
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 :-)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC