Hi,

Can anyone please tell me how to solve this calculator code with using cases?
I really don't know how to conclude it. It doesn't matter if code is long cuz of cases.

Code:

``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Calculator
{
class Program
{
static void Main(string[] args)
{
float result;

try
{
Console.Write("Enter the first number: ");

Console.Write("Enter the operation (+), (-), (*) ali (/): ");

if (operation1.Equals('+') || operation1.Equals('-') || operation1.Equals('*') || operation1.Equals('/'))
{
Console.Write("Enter the second number: ");

Console.Write("Enter the operation (+), (-), (*) ali (/): ");

if (operation2.Equals('+') || operation2.Equals('-') || operation2.Equals('*') || operation2.Equals('/'))
{
Console.Write("Enter the third number: ");

switch (operation1)
{
case '-':
result = a - b - c;
break;
case '*':
result = a * b * c;
break;
case '+':
result = a + b + c;
break;
case '/':
result = a / b / c;
break;

default:
result = 0;
break;
}
Console.WriteLine("Result: " + Math.Round(result));
}
else
{
Console.WriteLine("Input error!");
}
}
else
{
Console.WriteLine("Input error!");
}
}
catch
{
Console.WriteLine("Input error!");
}
}
}
}``````

Edited by danuz: forget using [CODE]

2
Contributors
9
Replies
15
Views
7 Years
Discussion Span
Last Post by danuz

1

Edited by james6754: n/a

Yes I see. I fixed that with just one operators declaration.

``````using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Calculator
{
class Program
{
static void Main(string[] args)
{
float result;

try
{
Console.Write("Enter the first number: ");

Console.Write("Enter the operation (+), (-), (*) ali (/): ");

if (operation.Equals('+') || operation.Equals('-') || operation.Equals('*') || operation.Equals('/'))
{
Console.Write("Enter the second number: ");

Console.Write("Enter the operation (+), (-), (*) ali (/): ");

if (operation.Equals('+') || operation.Equals('-') || operation.Equals('*') || operation.Equals('/'))
{
Console.Write("Enter the third number: ");

switch (operation)
{
case '-':
result = a - b - c;
break;
case '*':
result = a * b * c;
break;
case '+':
result = a + b + c;
break;
case '/':
result = a / b / c;
break;

default:
result = 0;
break;
}
Console.WriteLine("Result: " + Math.Round(result));
}
else
{
Console.WriteLine("Input error!");
}
}
else
{
Console.WriteLine("Input error!");
}
}
catch
{
Console.WriteLine("Input error!");
}
}
}
}``````

Is it a good way to solve that code with all possible switches ?
Example:

``````switch (operation)
{
case '-':
result = a + b - c;
break;
case '*':
result = a / b * c;
break;
case '+':
result = a - b + c;
break;
case '/':
result = a * b / c;
break;

default:
result = 0;
break;
}
switch (operation)
{
case '-':
result = a / b - c;
break;
case '*':
result = a - b * c;
break;
case '+':
result = a * b + c;
break;
case '/':
result = a - b / c;
break;

default:
result = 0;
break;
}``````

Edited by danuz: nn

Can you give more info about what you are trying to achieve, you are inputting in three numbers, what calculation are you wanting to perform on those numbers?

well you are using "operation1" in a switch statement and the other numbers and operations you enter are not being considered...what are you wanting to achieve with the program?

I just want to make a simple calculator including 3 numbers and 2 operators between them. Basic operators (+), (-), (*) and (/).

is it required that you use switch statements?

No, but it needs to be a simple code. Switch statements are not so complicated.

I've started learning C# about 5 months ago and I don't know a lot of it. So I need to start with the basicaliest syntaxes.

Edited by danuz: n/a

now this is a simple way..there are better ways but hopefully will give you a nice simple understanding...

``````class Program
{
static void Main(string[] args)
{
int num1 = 0;
int num2 = 0;
int num3 = 0;

string op1 = null;
string op2 = null;
string op3 = null;

int result = 0;//will hold final calculation

Console.WriteLine("enter first num");

Console.Write("enter op you want to perform on number");

Console.WriteLine("enter second num");

Console.Write("enter op you want to perform on number");

Console.WriteLine("enter third number");

Console.Write("enter op you want to perform on number");
}``````

now when you type your numbers and operations they will be stored in the variables that are declared at the top of the code....

you can then use your if statements to perform actions on these variables..for example

``````if (op1 == "-")
{
result = num1 - num2;
}``````

you could then print the result to the console screen as so...

``Console.WriteLine("your result is: {0}",result);``