| | |
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 ado.net algorithm array barchart basic bitmap box broadcast buttons c# check checkbox client combobox contorl control conversion csharp custom database datagrid datagridview dataset datetime degrees deployment development disabled displayingopenforms draganddrop drawing editing editor encryption enum event excel file form format forms function gdi+ httpwebrequest i18n image imageprocessing index input install java label list listbox mandelbrot math mathematics mouseclick mysql operator oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting richtextbox rows server setup sleep socket sql statistics stream string table text textbox thread time timer update user usercontrol validation visualstudio webbrowser windows winforms wpf xml





