Created
July 11, 2019 04:15
-
-
Save danielplawgo/0fc030c7af1ebac0b25c8fd7dcd48702 to your computer and use it in GitHub Desktop.
dotnet try test
This file contains hidden or 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
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
new OperationDemo().Run(); | |
} | |
} | |
public class Expression | |
{ | |
public string Operator { get; set; } | |
public int Arg1 { get; set; } | |
public int Arg2 { get; set; } | |
public override string ToString() | |
{ | |
return $"{Arg1} {Operator} {Arg2}"; | |
} | |
} | |
class OperationDemo | |
{ | |
public void Run() | |
{ | |
var expression = new Expression() | |
{ | |
Arg1 = 10, | |
Arg2 = 5, | |
Operator = "/" | |
}; | |
CSharp1(expression); | |
} | |
void CSharp1(Expression ex) | |
{ | |
int result = 0; | |
switch (ex.Operator) | |
{ | |
case "+": | |
result = ex.Arg1 + ex.Arg2; | |
break; | |
case "/": | |
if (ex.Arg2 == 0) | |
{ | |
throw new ArgumentException("Arg2 cannot be 0"); | |
} | |
result = ex.Arg1 / ex.Arg2; | |
break; | |
default: | |
throw new InvalidOperationException(); | |
} | |
Console.WriteLine($"{ex} = {result}"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment