Advertisement
Alexander7337

BasicMath

Jun 27th, 2016
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.48 KB | None | 0 0
  1. using System;
  2.  
  3. namespace StaticMembersExercises
  4. {
  5.     public class Program
  6.     {
  7.         public static void Main()
  8.         {
  9.             string line = Console.ReadLine().Trim();
  10.             while (!line.Equals("End"))
  11.             {
  12.                 string[] calculationInfo = line.Split(new char[] { }, StringSplitOptions.RemoveEmptyEntries);
  13.                 string calculation = calculationInfo[0];
  14.                 decimal num1 = decimal.Parse(calculationInfo[1]);
  15.                 decimal num2 = decimal.Parse(calculationInfo[2]);
  16.  
  17.                 decimal? result = null;
  18.                 if (calculation == "Sum")
  19.                 {
  20.                     result = MathUtil.Sum(num1, num2);
  21.                 }
  22.                 else if (calculation == "Multiply")
  23.                 {
  24.                     result = MathUtil.Multiply(num1, num2);
  25.                 }
  26.                 else if (calculation == "Percentage")
  27.                 {
  28.                     result = MathUtil.Percentage(num1, num2);
  29.                 }
  30.                 else if (calculation == "Divide")
  31.                 {
  32.                     if (num2 > 0 || num2 < 0)
  33.                     {
  34.                         result = MathUtil.Divide(num1, num2);
  35.                     }
  36.                     else
  37.                     {
  38.                         Console.WriteLine("Infinity");
  39.                     }
  40.                 }
  41.                 else if (calculation == "Subtract")
  42.                 {
  43.                     result = MathUtil.Subtract(num1, num2);
  44.                 }
  45.                
  46.                 if (result != null)
  47.                 {
  48.                     Console.WriteLine($"{result:F2}");
  49.                 }
  50.                
  51.                 line = Console.ReadLine();
  52.             }
  53.         }
  54.     }
  55.  
  56.     public class MathUtil
  57.     {
  58.         public static decimal Sum(decimal x, decimal y)
  59.         {
  60.             return x + y;
  61.         }
  62.  
  63.         public static decimal Subtract(decimal x, decimal y)
  64.         {
  65.             return x - y;
  66.         }
  67.  
  68.         public static decimal Multiply(decimal x, decimal y)
  69.         {
  70.             return x * y;
  71.         }
  72.  
  73.         public static decimal Divide(decimal x, decimal y)
  74.         {
  75.             //if (y > 0 || y < 0)
  76.             //{
  77.                 return x / y;
  78.             //}
  79.             //return 0;
  80.         }
  81.  
  82.         public static decimal Percentage(decimal totalNumber, decimal percent)
  83.         {
  84.             return totalNumber * percent / 100;
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement