Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Second___Easy
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.Write("Write function (+ - * / % ^): ");
- string operation = Console.ReadLine();
- Console.Write("Write FIRST value (use prefix \"h\" for hexadecimal or \"b\" for binary number): ");
- string firstValue = Console.ReadLine();
- Console.WriteLine("Write SECOND value (use prefix \"h\" for hexadecimal or \"b\" for binary number): ");
- string secondValue = Console.ReadLine();
- int firstNumber = ParseNumber(firstValue);
- int secondNumber = ParseNumber(secondValue);
- int result;
- switch (operation)
- {
- case "+":
- result = firstNumber + secondNumber;
- break;
- case "-":
- result = firstNumber - secondNumber;
- break;
- case "*":
- result = firstNumber * secondNumber;
- break;
- case "/":
- result = firstNumber / secondNumber;
- break;
- default:
- Console.WriteLine("Unknown operation");
- return;
- }
- Console.WriteLine();
- Console.WriteLine();
- string[] firstOutput = GetAllFormats(firstNumber);
- string[] secondOutput = GetAllFormats(secondNumber);
- string[] resultOutput = GetAllFormats(result);
- Console.WriteLine("In decimal");
- Console.WriteLine("{0} {1} {2} = {3}", firstOutput[0], operation, secondOutput[0], resultOutput[0]);
- Console.WriteLine();
- Console.WriteLine("In binary");
- Console.WriteLine("{0} {1} {2} = {3}", firstOutput[1], operation, secondOutput[1], resultOutput[1]);
- Console.WriteLine();
- Console.WriteLine("In hexadecimal");
- Console.WriteLine("{0} {1} {2} = {3}", firstOutput[2], operation, secondOutput[2], resultOutput[2]);
- Console.WriteLine();
- Console.ReadKey();
- }
- private static string[] GetAllFormats(int result)
- {
- string[] retVal = new string[3];
- retVal[0] = result.ToString();
- retVal[1] = Convert.ToString(result, 2);
- retVal[2] = Convert.ToString(result, 16);
- return retVal;
- }
- private static int ParseNumber(string value)
- {
- if (value[0] == 'b' || value[0] == 'B')
- {
- value = value.Substring(1);
- return Convert.ToInt32(value, 2);
- }
- if (value[0] == 'h' || value[0] == 'H')
- {
- value = value.Substring(1);
- return Convert.ToInt32(value, 16);
- }
- return int.Parse(value);
- }
- }
- }
Add Comment
Please, Sign In to add comment