473,326 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

C# Parsing help wanted

In C# is there a way to build a string for example: string d="1 + 2";
and have C# convert this to the sum and display "3" or some such thing. It seems like there should be something for this. This is killing me, please help.
Sep 17 '07 #1
4 1068
shweta123
692 Expert 512MB
Hi,

I think , it is not possible to display the sum using the string "1+2" , instead of this, you can split the string and write

Dim d As String = Val(Convert.ToInt32("1")) + Val(Convert.ToInt32("2"))



In C# is there a way to build a string for example: string d="1 + 2";
and have C# convert this to the sum and display "3" or some such thing. It seems like there should be something for this. This is killing me, please help.
Sep 17 '07 #2
I'm sorry, perhaps you misunderstood. I want to know if C# can do the mathmatical conversion and spit out the sum. If it sees this code:

string a = "1";
string b = "+";
string c = "2";

MessageBox.Show(a+b+c);
it should display this: 1+2
Now I want to find the mathematic sum of this string. Assuming that the values for the variables can be chosen by the user with a button press; how do I do have C# solve this string mathematically? Eventually I would like for the user to be able to input a math equation and have the program solve it.

Hi,

I think , it is not possible to display the sum using the string "1+2" , instead of this, you can split the string and write

Dim d As String = Val(Convert.ToInt32("1")) + Val(Convert.ToInt32("2"))
Sep 18 '07 #3
Plater
7,872 Expert 4TB
There is nothing that will do that for you, however you could do it yourself.
If you truely will be given the equation broken down like this:
Expand|Select|Wrap|Line Numbers
  1. string a = "1";
  2. string b = "+";
  3. string c = "2";
  4.  
It is a simple matter to just parse on it.
Expand|Select|Wrap|Line Numbers
  1. int result=0;
  2. if (b=="+")
  3. {
  4.    result=int.Parse(a) + int.Parse(b);
  5. }
  6. //repeat for other functions
  7.  
if you really want a "calculator" type implementation, many people have already done it and a simple web search will turn up some for you.
Sep 18 '07 #4
Thank you for your help. You didn't misunderstand me then. I misunderstood you, sorry. I was hoping there was a simpler method. I already wrote a calculator program. The other ones I found seemed more complicated than they needed to be. Thank you for your help.

There is nothing that will do that for you, however you could do it yourself.
If you truely will be given the equation broken down like this:
Expand|Select|Wrap|Line Numbers
  1. string a = "1";
  2. string b = "+";
  3. string c = "2";
  4.  
It is a simple matter to just parse on it.
Expand|Select|Wrap|Line Numbers
  1. int result=0;
  2. if (b=="+")
  3. {
  4.    result=int.Parse(a) + int.Parse(b);
  5. }
  6. //repeat for other functions
  7.  
if you really want a "calculator" type implementation, many people have already done it and a simple web search will turn up some for you.
Sep 18 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed...
3
by: Gregor Horvath | last post by:
Hi, given the dynamic nature of python I assume that there is an elegant solution for my problem, but I did not manage to find it. I have a file that contains for example on line: when...
4
by: Erik Moore | last post by:
I am both producing and parsing an xml document that needs to be validated against a schema. I wanted some consumers of the document to have the option of not performing a validation, so I left the...
6
by: Ulrich Vollenbruch | last post by:
Hi all! since I'am used to work with matlab for a long time and now have to work with c/c++, I have again some problems with the usage of strings, pointers and arrays. So please excuse my basic...
2
by: Anthony Boudouvas | last post by:
Hi to all, i have a very simple XML file that i present to a user and i will allow him/her to manualy edit it and send it back to a listening server. What is the best -simple- way to parse it...
9
by: ankitdesai | last post by:
I would like to parse a couple of tables within an individual player's SHTML page. For example, I would like to get the "Actual Pitching Statistics" and the "Translated Pitching Statistics"...
3
by: aspineux | last post by:
My goal is to write a parser for these imaginary string from the SMTP protocol, regarding RFC 821 and 1869. I'm a little flexible with the BNF from these RFC :-) Any comment ? tests= def...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
0
by: savj14 | last post by:
I have been driving myself crazy the past few days trying to figure this out. I have tried different Parsing Scripts and have read and searched various things trying to find a solution. I am...
0
by: HalfCoded | last post by:
hi everyone, I am kind of stuck and therefore would really appreciate some clues: I actually have to run a script which has to compare two elements from two different files which are a blast...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.