Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.81 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 string code { get; set; }
  29.         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
  30.  
  31.         protected Converter(int value) // Tu jest ciekawostka. Jak będziesz dziedziczyć klasę, to klasa bazowa Converter wymusi na tobie wygenerowanie konstruktowa
  32.         {
  33.             this.value = value;
  34.         }
  35.  
  36.         ~Converter()
  37.         {
  38.             this.code = "";
  39.             this.value = 0;
  40.         }
  41.  
  42.         // Tu musisz odpowiedzieć mi na pytanie. Co powiesz na utworzenie delegata, który miałby za zadanie początkowe zmanipulowanie wartością wejściową
  43.         // Powiedzmy: wprowadzasz wartość w konsoli, ale klient chce podwoić wartość. Bo tak chce
  44.         // Daj mi znać o tym
  45.         public string convert() {
  46.             return converterDefinition();
  47.         }
  48.  
  49.         protected abstract string converterDefinition();
  50.     }
  51.  
  52.  
  53.  
  54.     public class BinConverter : Converter
  55.     {
  56.         public BinConverter(int value) : base(value) {}
  57.  
  58.         protected override String converterDefinition()
  59.         {
  60.             while (value > 0)
  61.             {
  62.                 int y = value % 2;
  63.                 code += y;
  64.                 value /= 2;
  65.             }
  66.             return StringReverser.reverse(code);
  67.         }
  68.     }
  69.  
  70.     public class OctConverter : Converter
  71.     {
  72.         public OctConverter(int value) : base(value) {}
  73.  
  74.         protected override String converterDefinition()
  75.         {
  76.             while (value > 0)
  77.             {
  78.                 int y = value % 8;
  79.                 code += y;
  80.                 value /= 8;
  81.             }
  82.             return StringReverser.reverse(code);
  83.         }
  84.     }
  85.  
  86.     public class HexConverter : Converter
  87.     {
  88.         // Tu dałem na private. Nie potrzeba tego upubliczniać. Nigdzie tego nie wykorzystujesz
  89.         private static String[] hex =
  90.         { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
  91.  
  92.         public HexConverter(int value) : base(value) {}
  93.  
  94.         protected override String converterDefinition()
  95.         {
  96.             while (value > 0)
  97.             {
  98.                 int y = value % 16;
  99.                 code += hex[y];
  100.                 value /= 16;
  101.             }
  102.             return StringReverser.reverse(code);
  103.         }
  104.     }
  105.  
  106.     public class Program
  107.     {
  108.         public static void Main(string[] args)
  109.         {
  110.             int x = int.Parse(Console.ReadLine());
  111.             Console.WriteLine("Number: "+x);
  112.  
  113.             Converter bin = new BinConverter(x);
  114.             Converter oct = new OctConverter(x);
  115.             Converter hex = new HexConverter(x);
  116.            
  117.             eqSign(12);
  118.             Console.WriteLine("Bin : " + bin.convert());
  119.             Console.WriteLine("Oct : " + oct.convert());
  120.             Console.WriteLine("Hex : " + hex.convert());
  121.             eqSign(12);
  122.         }
  123.        
  124.         static void eqSign(int x)
  125.         {
  126.             while (x != 0)
  127.             {
  128.                 Console.Write("=");
  129.                 x--;
  130.             }
  131.             Console.WriteLine();
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement