Problem in C#

Posts 1–4 of 4 · Page 1 of 1
Problem in C#
i have a problem in C#
Im working/learning in consoleapplications and im trying to make a more advanced calculator.

Instead of type first the tal1 then method and last tal2 i wanna make it more advanced so you just can type like : 5+2 and get the answer...or 12*512

heres my progress.
Code:
string method = null;
            string all = null;

            int nr1 = 0;
            int nr2 = 0;

            Console.WriteLine("Welcome to Hasses calculator.");
            Console.WriteLine("Type a mathcalc like 6 + 2, 10 / 2, 1000 - 500, 8 * 9");
            all = Console.ReadLine();

            if (all.Contains("+")){method = "+";}
            if (all.Contains("-")) { method = "-"; }
            if (all.Contains("*")) { method = "*"; }
            if (all.Contains("/")) { method = "/"; }

            //Here will the get the first number, then the secound should be read into tal1 & tal2.

            Console.WriteLine("Your calculation is {0}", all);
            Console.WriteLine("Method is : {0}", method);
            Console.ReadLine();
i made it read nearly right but i got one big problem and that was i cant convert a Array into string/int else i think i would solve this
you can simply use:

Code:
all.Substring(...)
or
Code:
all.Split(...)
you will need an index for it.. (position) you can get that with

Code:
all.IndexOf("+"); //Example
to convert a string to int, it's simple, use:

Code:
Convert.ToInt32(string);
hope that helps

edit:

a simple example.

lets imagine you typed 544-133

Code:
int tal1 = Convert.ToInt32(all.Substring(0, all.IndexOf("-")));
//You should get in tal1, the number 544.
btw you have more ways to do this, etc..
Well, idk how advanced with this your getting. If you're talking about:
15 + 12

That easy to parse(though messy). If you want to parse

15 + 32 * 14^12 (or something like that, lol)

It's going to be more difficult. You need to create a token parsing module. I wrote one for JScript, I cant share unfortunately as the project is currently closed-source.

For the latter, just use split(though careful as these are regex expression and some need to be escaped).

I.e

consider the method:

String::Split(String deliminators, int count, StringSplitOptions options);

deliminators is an array of characters which will trigger a split.
count is the maximum number of splits that can occur
and StringSplitOptions are optional flags for the method.

Consider I have

Code:
String sBuffer = "15 +  32";
String[] sArray = sBuffer.Split(" ", 3, StringSplitOptions.RemoveEmptyEntries);
sArray[0] is equal to "15"
sArray[1] is equal to "+"
sArray[2] is equal to "32"
Quote Originally Posted by radnomguywfq3 View Post
Well, idk how advanced with this your getting. If you're talking about:
15 + 12

That easy to parse(though messy). If you want to parse

15 + 32 * 14^12 (or something like that, lol)

It's going to be more difficult. You need to create a token parsing module. I wrote one for JScript, I cant share unfortunately as the project is currently closed-source.

For the latter, just use split(though careful as these are regex expression and some need to be escaped).

I.e

consider the method:

String::Split(String deliminators, int count, StringSplitOptions options);

deliminators is an array of characters which will trigger a split.
count is the maximum number of splits that can occur
and StringSplitOptions are optional flags for the method.

Consider I have

Code:
String sBuffer = "15 +  32";
String[] sArray = sBuffer.Split(" ", 3, StringSplitOptions.RemoveEmptyEntries);
sArray[0] is equal to "15"
sArray[1] is equal to "+"
sArray[2] is equal to "32"
i have done that but i del it bocouse i coulden´t convert the arrayitem to a string int ex. tal1.

like above tal1 = convert.tostring(something i think sArray[]); somethign like that im not that experienced
Posts 1–4 of 4 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?