| | |
how to compute a string in c#???
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
•
•
IN C#:::
i have a string
string abc="40+40*4.2%+100"
now how can i get the result????
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)
class Program { static void Main(string[] args) { double abc = 40 + 40 * 4.2 % +100; Console.WriteLine(abc); Console.ReadLine(); } }
Answer is 108.
Regards,
PK
•
•
Join Date: Apr 2008
Posts: 10
Reputation:
Solved Threads: 0
•
•
•
•
IN C#:::
i have a string
string abc="40+40*4.2%+100"
now how can i get the result????
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 :-)
![]() |
Similar Threads
- how do i compute for the time?? (Java)
- permutations of STRING Vs First n natural numbers (C++)
- Problem with string variable in void prnt function (C++)
- help on using StringTokenizer to read and compute multiple lines from text (Java)
- Function that returns void (C++)
Other Threads in the C# Forum
- Previous Thread: OLeDb Connection fail
- Next Thread: Static Cast
| Thread Tools | Search this Thread |
.net access algorithm array barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom cyclethruopenforms data database datagrid datagridview dataset date/time datetime degrees development dll draganddrop drawing encryption enum event excel file finalyearproject form format forms function gdi+ getoutlookcontactusinfcsvfile globalization httpwebrequest image index input install installer java label list listbox mandelbrot math mono mouseclick mysql operator panel path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox save server silverlight sleep socket sql sql-server statistics stream string table text textbox thread time timer timespan update usercontrol users validate validation visualstudio webbrowser wia windows winforms wpf xml





