-
September 26th, 2013, 09:03 AM
#1
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
-
September 26th, 2013, 06:16 PM
#2
Re: Calculator
What problem are you having?
-
September 27th, 2013, 12:07 AM
#3
-
September 27th, 2013, 02:37 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|