6 Replies - 585 Views - Last Post: 15 March 2011 - 03:44 AM Rate Topic: -----

#1 XDAO  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-March 11

Split Method

Posted 09 March 2011 - 04:45 PM

Hello I am working on a calculator for my class and I am have trouble getting the program to accept a single line of input which will contain the entire equation to be executed. Are teacher said to use the split method but I don't understand it at all. So if anyone could give me an example on how to use the split method with two numbers and a operation, or give me another way to do it all together. I don't see my teacher till Friday and I don't want to waste a day not doing anything to my project.

 class Program
    {
        static void Main(string[] args)
        {
            double num1, num2;
            double total = 0;
            char operation;
            string inputstring;
            const char Quit = 'Q';
            

            Console.WriteLine("Enter a number ");
            inputstring = Console.ReadLine();
            num1 = Convert.ToDouble(inputstring);
            
            Console.WriteLine("Enter a operation ");
            inputstring = Console.ReadLine();
            operation = Convert.ToChar(inputstring);
            
            Console.WriteLine("Enter another number ");
            inputstring = Console.ReadLine();
            num2 = Convert.ToDouble(inputstring);

            
            

            while (operation != Quit)
            {
               
                if (operation == '+')
                {
                    total = num1 + num2;
                    
                }


                else
                    if (operation == '-')
                    {
                        total = num1 - num2;
                        
                    }
                    else
                        if (operation == '*')
                        {
                            total = num1 * num2;
                        }
                        else
                            if (operation == '/')
                            {
                                total = num1 / num2;
                                
                            }


                num1 = total;

                
                Console.WriteLine("Enter a operation or " + Quit + " to stop ");
                inputstring = Console.ReadLine();
                operation = Convert.ToChar(inputstring);

                
                Console.WriteLine("Enter another number ");
                inputstring = Console.ReadLine();
                num2 = Convert.ToDouble(inputstring);
               
            }
            Console.WriteLine("Total is {0} ", total); 


Is This A Good Question/Topic? 0
  • +

Replies To: Split Method

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 995
  • View blog
  • Posts: 2,386
  • Joined: 04-October 09

Re: Split Method

Posted 09 March 2011 - 04:50 PM

Since you are entering the numbers and operation in different prompts, I don't see a need for split at all. But if you entered them all in one prompt, with the user placing spaces between each (2 + 3, for example), then split would work for you:
string myString = "2 + 3";
string[] parts = myString.Split(' ');


After this parts[0] would have "2", parts[1] would have "+" and parts[2] would have "3".
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: Split Method

Posted 09 March 2011 - 05:00 PM

View PostXDAO, on 09 March 2011 - 05:45 PM, said:

Hello I am working on a calculator for my class {...}

There's a tutorial for that
Calculator tutorial

Plus
Bulding an application - Part 1
Building an application - Part 2
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: Split Method

Posted 09 March 2011 - 05:19 PM

Learning to find answers is a very import part of coding. We all search and try to find some article or description about what we are about to understand, before we try to actually implement it. It's amazing how much aspirin you can save by reading about something before you go off an try to use it. Not to mention is is a lot faster than formulating a question and waiting for someone else to answer it: Especially if you are like me and code at 0300hrs when most sane people are sleeping.

One of my favorite methods is Google.
In this case doing a search for "MSDN String.Split" gives you very targeted results.
I actually find this better than searching the MSDN through their built-in search feature.

Attached Image

This post has been edited by tlhIn`toq: 09 March 2011 - 05:20 PM

Was This Post Helpful? 0
  • +
  • -

#5 XDAO  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-March 11

Re: Split Method

Posted 14 March 2011 - 05:28 PM

I don't understand how to make the program I have accept a single line of input which will contain the entire equation to be executed. Any tips to do this or any thing I should look up that might help?

class Program
    {
        static void Main(string[] args)
        {
            double num1, num2;
            double total = 0;
            char operation;
            string inputstring;
            const char Quit = 'Q';
            

            Console.WriteLine("Enter a number ");
            inputstring = Console.ReadLine();
            num1 = Convert.ToDouble(inputstring);
            
            Console.WriteLine("Enter a operation ");
            inputstring = Console.ReadLine();
            operation = Convert.ToChar(inputstring);
            
            Console.WriteLine("Enter another number ");
            inputstring = Console.ReadLine();
            num2 = Convert.ToDouble(inputstring);

            while (operation != Quit)
            {
                
                if (operation == '+')
                {
                    total = num1 + num2;
                   
                }
                else
                    if (operation == '-')
                    {
                        total = num1 - num2;
                        
                    }
                    else
                        if (operation == '*')
                        {
                            total = num1 * num2;
                            
                        }
                        else
                            if (operation == '/')
                            {
                                total = num1 / num2;
                                
                            }

                num1 = total;

                Console.WriteLine("\n" + total);


                Console.WriteLine("Enter a operation or " + Quit + " to stop ");
                inputstring = Console.ReadLine();
                operation = Convert.ToChar(inputstring);

                Console.WriteLine("Enter another number ");
                inputstring = Console.ReadLine();
                num2 = Convert.ToDouble(inputstring);

                
            }


Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8374
  • View blog
  • Posts: 31,122
  • Joined: 12-June 08

Re: Split Method

Posted 14 March 2011 - 06:03 PM

It's all about parsing. Input a single line like "5*40=" as a string, right? Then parse that string any way you see fit. The common method would be to check each spot of the character array and decide what to do. Dump them in an array, a queue, or what ever storage medium you feel is needed.

In this case the first spot is a number. I peek to the next index and see that is *not* a number so I push 5 off to an integer variable.

I move to the second and see it's a plus sign. I save that off as an operator.

The next is a number and I peek to see there's a number after it. I take 44 and put that into another integer variable.

The last sign is an equals so it means calculate up what you have.

I take my sign, run it through a select case to see that int_variable1 and int_variable2 should be added and the result outputted.

Clearly this is a super trivial example but you see how this pans out. It gets more interesting with nested parentheses and so forth.
Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,961
  • Joined: 15-July 08

Re: Split Method

Posted 15 March 2011 - 03:44 AM

Duplicate Topics merged. Please, if you're going to get help, take our advice and change your code. This code is virtually identical to the one that you posted before. This shows that you didn't really take our advice.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1