Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.66 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConverterApplication
  4. {
  5.  
  6.     public class StringReverser // odwracanie ciągu można zrealizować w innej klasie
  7.     {
  8.         public static String reverse(String x)
  9.         {
  10.             string code = "";
  11.             for (int i = x.Length; i > 0; i--)
  12.             {
  13.                 char ch = x[i - 1];
  14.                 code += ch;
  15.             }
  16.             return code;
  17.         }
  18.     }
  19.  
  20.     public interface IConverter // Publiczny interfejs? Proszę bardzo
  21.     {
  22.         string convert();
  23.     }
  24.  
  25.  
  26.     public abstract class Converter : IConverter
  27.     {
  28.         protected int value { get; set; } // dodałem nowe pole, które przechowuje wartość wejściową. Jest chronione, bo nikt, prócz klas dziedziczących nie może widzieć jej wartości
  29.  
  30.         protected Converter(int value) // Tu jest ciekawostka. Jak będziesz dziedziczyć klasę, to klasa bazowa Converter wymusi na tobie wygenerowanie konstruktowa
  31.         {
  32.             this.value = value;
  33.         }
  34.  
  35.         // Tu musisz odpowiedzieć mi na pytanie. Co powiesz na utworzenie delegata, który miałby za zadanie początkowe zmanipulowanie wartością wejściową
  36.         // Powiedzmy: wprowadzasz wartość w konsoli, ale klient chce podwoić wartość. Bo tak chce
  37.         // Daj mi znać o tym
  38.         public string convert() {
  39.             return converterDefinition();
  40.         }
  41.  
  42.         protected abstract string converterDefinition();
  43.     }
  44.  
  45.  
  46.  
  47.     public class BinConverter : Converter
  48.     {
  49.         public BinConverter(int value) : base(value) {}
  50.  
  51.         protected override String converterDefinition()
  52.         {
  53.             while (value > 0)
  54.             {
  55.                 int y = value % 2;
  56.                 code += y;
  57.                 value /= 2;
  58.             }
  59.             return StringReverser.reverse(code);
  60.         }
  61.     }
  62.  
  63.     public class OctConverter : Converter
  64.     {
  65.         public OctConverter(int value) : base(value) {}
  66.  
  67.         protected override String converterDefinition()
  68.         {
  69.             while (value > 0)
  70.             {
  71.                 int y = value % 8;
  72.                 code += y;
  73.                 value /= 8;
  74.             }
  75.             return StringReverser.reverse(code);
  76.         }
  77.     }
  78.  
  79.     public class HexConverter : Converter
  80.     {
  81.         // Tu dałem na private. Nie potrzeba tego upubliczniać. Nigdzie tego nie wykorzystujesz
  82.         private static String[] hex =
  83.         { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
  84.  
  85.         public HexConverter(int value) : base(value) {}
  86.  
  87.         protected override String converterDefinition()
  88.         {
  89.             while (value > 0)
  90.             {
  91.                 int y = value % 16;
  92.                 code += hex[y];
  93.                 value /= 16;
  94.             }
  95.             return StringReverser.reverse(code);
  96.         }
  97.     }
  98.  
  99.     public class Program
  100.     {
  101.         public static void Main(string[] args)
  102.         {
  103.             int x = int.Parse(Console.ReadLine());
  104.             Console.WriteLine("Number: "+x);
  105.  
  106.             Converter bin = new BinConverter(x);
  107.             Converter oct = new OctConverter(x);
  108.             Converter hex = new HexConverter(x);
  109.            
  110.             eqSign(12);
  111.             Console.WriteLine("Bin : " + bin.convert());
  112.             Console.WriteLine("Oct : " + oct.convert());
  113.             Console.WriteLine("Hex : " + hex.convert());
  114.             eqSign(12);
  115.         }
  116.        
  117.         static void eqSign(int x)
  118.         {
  119.             while (x != 0)
  120.             {
  121.                 Console.Write("=");
  122.                 x--;
  123.             }
  124.             Console.WriteLine();
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement