AlgorithMage Ω(n) Θ(n×p)
Switching to C# and curious if there's anything I could improve on in this very basic math script.
c#
class Program
{
static void Main()
{
MathClass.Print(2,5,"subtract");
}
}
class MathClass
{
public static void Print(int x, int y, string type)
{
switch (type.ToLower())
{
case "add":
Console.WriteLine($"Result of adding {x} and {y} is {Add(x,y)}");
break;
case "subtract":
Console.WriteLine($"Result of subtracting {x} from {y} is {Subtract(x,y)}");
break;
case "multiply":
Console.WriteLine($"Result of multiplying {x} and {y} is {Multiply(x,y)}");
break;
case "divide":
Console.WriteLine($"Result of dividing {x} and {y} is {Divide(x,y)}");
break;
default:
Console.WriteLine("No matching operation found.");
break;
}
}
private static int Add(int x, int y)
{
return x + y;
}
private static int Subtract(int x, int y)
{
return x - y;
}
private static int Multiply(int x, int y)
{
return x * y;
}
private static int Divide(int x, int y)
{
return x / y;
}
}
c#
class Program
{
static void Main()
{
MathClass.Print(2,5,"subtract");
}
}
class MathClass
{
public static void Print(int x, int y, string type)
{
switch (type.ToLower())
{
case "add":
Console.WriteLine($"Result of adding {x} and {y} is {Add(x,y)}");
break;
case "subtract":
Console.WriteLine($"Result of subtracting {x} from {y} is {Subtract(x,y)}");
break;
case "multiply":
Console.WriteLine($"Result of multiplying {x} and {y} is {Multiply(x,y)}");
break;
case "divide":
Console.WriteLine($"Result of dividing {x} and {y} is {Divide(x,y)}");
break;
default:
Console.WriteLine("No matching operation found.");
break;
}
}
private static int Add(int x, int y)
{
return x + y;
}
private static int Subtract(int x, int y)
{
return x - y;
}
private static int Multiply(int x, int y)
{
return x * y;
}
private static int Divide(int x, int y)
{
return x / y;
}
}
20 replies