Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace ConverterApplication
- {
- public class StringReverser // odwracanie ciągu można zrealizować w innej klasie
- {
- public static String reverse(String x)
- {
- string code = "";
- for (int i = x.Length; i > 0; i--)
- {
- char ch = x[i - 1];
- code += ch;
- }
- return code;
- }
- }
- public interface IConverter // Publiczny interfejs? Proszę bardzo
- {
- string convert();
- }
- public abstract class Converter : IConverter
- {
- 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
- protected Converter(int value) // Tu jest ciekawostka. Jak będziesz dziedziczyć klasę, to klasa bazowa Converter wymusi na tobie wygenerowanie konstruktowa
- {
- this.value = value;
- }
- // Tu musisz odpowiedzieć mi na pytanie. Co powiesz na utworzenie delegata, który miałby za zadanie początkowe zmanipulowanie wartością wejściową
- // Powiedzmy: wprowadzasz wartość w konsoli, ale klient chce podwoić wartość. Bo tak chce
- // Daj mi znać o tym
- public string convert() {
- return converterDefinition();
- }
- protected abstract string converterDefinition();
- }
- public class BinConverter : Converter
- {
- public BinConverter(int value) : base(value) {}
- protected override String converterDefinition()
- {
- while (value > 0)
- {
- int y = value % 2;
- code += y;
- value /= 2;
- }
- return StringReverser.reverse(code);
- }
- }
- public class OctConverter : Converter
- {
- public OctConverter(int value) : base(value) {}
- protected override String converterDefinition()
- {
- while (value > 0)
- {
- int y = value % 8;
- code += y;
- value /= 8;
- }
- return StringReverser.reverse(code);
- }
- }
- public class HexConverter : Converter
- {
- // Tu dałem na private. Nie potrzeba tego upubliczniać. Nigdzie tego nie wykorzystujesz
- private static String[] hex =
- { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
- public HexConverter(int value) : base(value) {}
- protected override String converterDefinition()
- {
- while (value > 0)
- {
- int y = value % 16;
- code += hex[y];
- value /= 16;
- }
- return StringReverser.reverse(code);
- }
- }
- public class Program
- {
- public static void Main(string[] args)
- {
- int x = int.Parse(Console.ReadLine());
- Console.WriteLine("Number: "+x);
- Converter bin = new BinConverter(x);
- Converter oct = new OctConverter(x);
- Converter hex = new HexConverter(x);
- eqSign(12);
- Console.WriteLine("Bin : " + bin.convert());
- Console.WriteLine("Oct : " + oct.convert());
- Console.WriteLine("Hex : " + hex.convert());
- eqSign(12);
- }
- static void eqSign(int x)
- {
- while (x != 0)
- {
- Console.Write("=");
- x--;
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement