Guest User

Reddit project 2e

a guest
Feb 23rd, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Second___Easy
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Console.Write("Write function (+ - * / % ^): ");
  13.             string operation = Console.ReadLine();
  14.             Console.Write("Write FIRST value (use prefix \"h\" for hexadecimal or \"b\" for binary number): ");
  15.             string firstValue = Console.ReadLine();
  16.             Console.WriteLine("Write SECOND value (use prefix \"h\" for hexadecimal or \"b\" for binary number): ");
  17.             string secondValue = Console.ReadLine();
  18.  
  19.             int firstNumber = ParseNumber(firstValue);
  20.             int secondNumber = ParseNumber(secondValue);
  21.  
  22.             int result;
  23.             switch (operation)
  24.             {
  25.                 case "+":
  26.                     result = firstNumber + secondNumber;
  27.                     break;
  28.                 case "-":
  29.                     result = firstNumber - secondNumber;
  30.                     break;
  31.                 case "*":
  32.                     result = firstNumber * secondNumber;
  33.                     break;
  34.                 case "/":
  35.                     result = firstNumber / secondNumber;
  36.                     break;
  37.                 default:
  38.                     Console.WriteLine("Unknown operation");
  39.                     return;
  40.             }
  41.  
  42.             Console.WriteLine();
  43.             Console.WriteLine();
  44.  
  45.             string[] firstOutput = GetAllFormats(firstNumber);
  46.             string[] secondOutput = GetAllFormats(secondNumber);
  47.             string[] resultOutput = GetAllFormats(result);
  48.  
  49.             Console.WriteLine("In decimal");
  50.             Console.WriteLine("{0} {1} {2} = {3}", firstOutput[0], operation, secondOutput[0], resultOutput[0]);
  51.             Console.WriteLine();
  52.             Console.WriteLine("In binary");
  53.             Console.WriteLine("{0} {1} {2} = {3}", firstOutput[1], operation, secondOutput[1], resultOutput[1]);
  54.             Console.WriteLine();
  55.             Console.WriteLine("In hexadecimal");
  56.             Console.WriteLine("{0} {1} {2} = {3}", firstOutput[2], operation, secondOutput[2], resultOutput[2]);
  57.             Console.WriteLine();
  58.  
  59.             Console.ReadKey();
  60.         }
  61.  
  62.         private static string[] GetAllFormats(int result)
  63.         {
  64.             string[] retVal = new string[3];
  65.             retVal[0] = result.ToString();
  66.             retVal[1] = Convert.ToString(result, 2);
  67.             retVal[2] = Convert.ToString(result, 16);
  68.             return retVal;
  69.         }
  70.  
  71.         private static int ParseNumber(string value)
  72.         {
  73.             if (value[0] == 'b' || value[0] == 'B')
  74.             {
  75.                 value = value.Substring(1);
  76.                 return Convert.ToInt32(value, 2);
  77.             }
  78.  
  79.             if (value[0] == 'h' || value[0] == 'H')
  80.             {
  81.                 value = value.Substring(1);
  82.                 return Convert.ToInt32(value, 16);
  83.             }
  84.  
  85.             return int.Parse(value);
  86.         }
  87.     }
  88. }
Add Comment
Please, Sign In to add comment