User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 425,978 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,648 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 394 | Replies: 2
Reply
Join Date: Nov 2007
Posts: 28
Reputation: u4umang2001 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
u4umang2001's Avatar
u4umang2001 u4umang2001 is offline Offline
Light Poster

how to compute a string in c#???

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Posts: 30
Reputation: puneetkay is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 6
puneetkay's Avatar
puneetkay puneetkay is online now Online
Light Poster

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

  #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  
Join Date: Apr 2008
Location: Western New York
Posts: 10
Reputation: corwing is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
corwing corwing is offline Offline
Newbie Poster

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

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C# Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C# Forum

All times are GMT -4. The time now is 11:23 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC