'Nicely print Calculator input along with the result
How can I output the number, the operator, and the result at once. After the user input
double answer;
WriteLine("Enter your first number");
double num1 = Convert.ToDouble(Console.ReadLine());
string sign;
WriteLine("Enter any of the following operators (+,-,*,/ )");
sign = Console.ReadLine();
WriteLine("Enter your second number");
double num2 = Convert.ToDouble(Console.ReadLine());
switch (sign)
{
case "+":
answer = num1 + num2;
WriteLine(answer);
break;
case "-":
answer = num1 - num2;
WriteLine(answer);
break;
}
Ideally output would look like 12 + 34 = 46, but any variations (including RPN - 12 34 + 46 would do).
Since this is homework for one of the first lessons, please don't push the envelope with suggested answers - while some input validation would be nice avoid lambda expressions and generics as we have not officially studied those.
Solution 1:[1]
Here you go, for fun, here's how I would do this:
var calculations = new Dictionary<string, Func<double, double, double>>()
{
{ "+", (x, y) => x + y },
{ "-", (x, y) => x - y },
{ "*", (x, y) => x * y },
{ "/", (x, y) => x / y },
};
double EnterNumber(string message)
{
while (true)
{
Console.WriteLine(message);
if (double.TryParse(Console.ReadLine(), out double value))
{
return value;
}
}
}
string EnterOperator(string message)
{
string sign = null;
while (true)
{
Console.WriteLine(message);
if ("+-*/".Contains(sign = Console.ReadLine()))
{
return sign;
}
}
}
double num1 = EnterNumber("Enter your first number");
string sign = EnterOperator("Enter any of the following operators (+,-,*,/ )");
double num2 = EnterNumber("Enter your second number");
Console.WriteLine($"{num1} {sign} {num2} = {calculations[sign](num1, num2)}");
Here's a slightly more refactored version to eliminate as much repetition as possible.
var calculations = new Dictionary<string, Func<double, double, double>>()
{
{ "+", (x, y) => x + y },
{ "-", (x, y) => x - y },
{ "*", (x, y) => x * y },
{ "/", (x, y) => x / y },
};
T EnterValue<T>(string message, Func<string, (bool success, T value)> tryParse)
{
while (true)
{
Console.WriteLine(message);
var output = tryParse(Console.ReadLine());
if (output.success)
{
return output.value;
}
}
}
double EnterNumber(string message) => EnterValue<double>(message, x =>
double.TryParse(x, out double value) ? (true, value) : (false, default));
string EnterOperator(string message) => EnterValue<string>(message, x =>
("+-*/".Contains(x), x));
double num1 = EnterNumber("Enter your first number");
string sign = EnterOperator("Enter any of the following operators (+,-,*,/ )");
double num2 = EnterNumber("Enter your second number");
Console.WriteLine($"{num1} {sign} {num2} = {calculations[sign](num1, num2)}");
Solution 2:[2]
Here it goes:-
switch (sign) {
case "+":
answer = num1 + num2;
WriteLine(num1+" "+num2+" + "+answer);
break;
case "-":
answer = num1 - num2;
WriteLine(num1+" "+num2+" - "+answer);
break;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Anukul Rawat |
