Hello everyone
I see there is duplication in my cases
Could you help me to optimize it?
- using System;
- namespace _3_??
- {
- public delegate int BinaryOp(int x, int y);
- public class SimpleMath
- {
- public static int Add(int x, int y)
- { return x + y; }
- public static int Subtract(int x, int y)
- { return x - y; }
- }
- class Program
- {
- static void Main(string[] args)
- {
- BinaryOp b = new BinaryOp(SimpleMath.Add);
- BinaryOp w = new BinaryOp(SimpleMath.Subtract);
- string operationRes;
- Console.WriteLine("What operations you wonna checkup? (addition/subtraction, write a/s to choose): ");
- operationRes = Console.ReadLine();
- Console.WriteLine("Insert first number");
- int x = (Convert.ToInt16(Console.ReadLine()));
- Console.WriteLine("Insert second number");
- int y = (Convert.ToInt16(Console.ReadLine()));
- switch (operationRes)
- {
- case "a":
- Console.WriteLine("Insert sum of x and y");
- int userresult = (Convert.ToInt16(Console.ReadLine()));
- int pcresult = b(x, y);
- if (userresult > pcresult)
- Console.WriteLine("Something wrong with your calculations, man..." + "\n" + "Your result should be less...");
- else if (userresult < pcresult)
- Console.WriteLine("Something wrong with your calculations, man..." + "\n" + "Your result should be more...");
- else
- Console.WriteLine("Y've inserted right result!");
- break;
- case "s":
- Console.WriteLine("Insert substraction of x and y");
- int userresult2 = (Convert.ToInt16(Console.ReadLine()));
- int pcresult2 = w(x, y);
- if (userresult2 > pcresult2)
- Console.WriteLine("Something wrong with your calculations, man..." + "\n" + "Your result should be less...");
- else if (userresult2 < pcresult2)
- Console.WriteLine("Something wrong with your calculations, man..." + "\n" + "Your result should be more...");
- else
- Console.WriteLine("Y've inserted right result!");
- break;
- default:
- break;
- }
- }
- }
- }