Created
July 28, 2014 00:32
-
-
Save AlbertoMonteiro/e25023dc197a793282c7 to your computer and use it in GitHub Desktop.
Simples calculadora usando scriptcs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum Operações | |
{ | |
Soma = 1, | |
Subtração, | |
Multiplicação, | |
Divisão | |
} | |
var type = typeof(Operações); | |
var todasOperações = Enum.GetNames(type).Select(n => new { Name = n, Value = (int)Enum.Parse(type,n) }); | |
Console.WriteLine("Digite a operação:"); | |
foreach(var a in todasOperações) | |
Console.WriteLine("{0} => {1}", a.Name, a.Value); | |
var operação = ((Operações)int.Parse(Console.ReadLine())); | |
var formato = "O resultado da {0} é {1}"; | |
Tuple<int,int> valores; | |
int valorFinal; | |
Func<Tuple<int,int>> capturaValores = () => | |
{ | |
Console.WriteLine("Digite o primeiro número"); | |
var valor1 = int.Parse(Console.ReadLine()); | |
Console.WriteLine("Digite o segundo número"); | |
var valor2 = int.Parse(Console.ReadLine()); | |
return new Tuple<int,int>(valor1,valor2); | |
}; | |
valores = capturaValores(); | |
switch(operação) | |
{ | |
case Operações.Soma: | |
valorFinal = valores.Item1 + valores.Item2; | |
break; | |
case Operações.Subtração: | |
valorFinal = valores.Item1 - valores.Item2; | |
break; | |
case Operações.Multiplicação: | |
valorFinal = valores.Item1 * valores.Item2; | |
break; | |
case Operações.Divisão: | |
valorFinal = valores.Item1 / valores.Item2; | |
break; | |
} | |
Console.WriteLine(formato, Enum.GetName(type, operação), valorFinal); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment