Calculator
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Calculator

  1. #1
    Join Date
    Mar 2012
    Posts
    19

    Calculator

    Hi,
    I want write a simple calculator without any priority but i have problem with this.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace o
    {
        class Program
        {
            static void Main(string[] args)
            {
                int num;
                char lastOperator = '+';
               
                int sum = 0;
                int Total = 0;
                string str = Console.ReadLine();
       
    
                for (int i = 0; i < str.Length; i++)
                {
                   
                    if (char.IsDigit(str[i]))
                    {
                        num = (int)char.GetNumericValue(str[i]);
                        sum = (sum * 10) + num;
                    }
                    else if (str[i] =='+'  || str[i] == '-' || str[i] == '*') 
                    {
                        lastOperator = str[i];
                        switch (lastOperator)
                        {
                            case '+': Total += sum;
                                break;
                            case '-': Total -= sum; break;
                            case '*': Total *= sum; break;
                                
                        }
                        sum = 0;
                    }
    
    
                    switch (lastOperator)
                    {
                        case '+': Total += sum;
                            break;
                        case '-': Total -= sum; break;
                        case '*': Total *= sum; break;
    
                    }
                }
               
    
                Console.WriteLine(Total);
    
               
            }
        }
    }
    Last edited by Arjay; September 26th, 2013 at 06:16 PM. Reason: Added code tags

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,256

    Re: Calculator

    What problem are you having?

  3. #3
    Join Date
    Mar 2012
    Posts
    19

    Re: Calculator

    123+321 = 123 !!

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,256

    Re: Calculator

    Have you tried setting a breakpoint and step through your code in a debugger?

    Put the cursor on the line that begins with for( and press F9. This will set a breakpoint at that line.

    Next, press F5 to start debugging your program. After you have entered the calculator expression in the console,
    the program will stop at the breakpoint. Press F10 to step over the program line. Hover the mouse over the variables as you single step through the code and you can see the values of the variables. This way you can determine what is going wrong in your program.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center