Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- public class BlindAbacus : IEnumerable
- {
- public override string ToString()
- {
- StringBuilder wynik = new StringBuilder();
- for (int i = iloscrzedow - 1; i >= 0; i--)
- {
- wynik.Append(rzedy[i].ToString());
- wynik.AppendLine();
- }
- return wynik.ToString();
- }
- public class ComputationException : Exception { }
- public class IncorrectRowValueException : Exception { }
- public class SomeoneThereException : Exception { }
- public int podstawa;
- public Row[] rzedy;
- bool[] zajete;
- int iloscrzedow;
- public static BlindAbacus nowyAbakus(int liczba, int podstawa)
- {
- if(liczba<0) throw new ComputationException();
- int ilerzedow = 0;
- int temp = liczba;
- while (temp > 0)
- {
- temp/=podstawa;
- ilerzedow++;
- }
- BlindAbacus wynik = new BlindAbacus(podstawa, ilerzedow);
- for (int i = 0; i < ilerzedow; i++)
- {
- wynik[i] = new Row(podstawa, liczba % podstawa);
- liczba /= podstawa;
- }
- return wynik;
- }
- public BlindAbacus(int x, int y)
- {
- podstawa = x;
- iloscrzedow = y;
- rzedy = new Row[y];
- zajete = new bool[y];
- for (int i = 0; i < y; i++) this[i] = new Row(x);
- }
- public static BlindAbacus operator +(BlindAbacus a, BlindAbacus b)
- {
- if (a.podstawa != b.podstawa) throw new ComputationException();
- return nowyAbakus((int)a + (int)b, a.podstawa);
- }
- public static BlindAbacus operator -(BlindAbacus a, BlindAbacus b)
- {
- if (a.podstawa != b.podstawa) throw new ComputationException();
- return nowyAbakus((int)a - (int)b, a.podstawa);
- }
- public static BlindAbacus operator ++(BlindAbacus a)
- {
- return nowyAbakus((int)a + 1, a.podstawa);
- }
- public static BlindAbacus operator --(BlindAbacus a)
- {
- return nowyAbakus((int)a - 1, a.podstawa);
- }
- public static implicit operator int(BlindAbacus m)
- {
- int wynik = 0;
- int mnozyciel=1;
- for(int i=0; i<m.iloscrzedow; i++)
- {
- wynik += m.rzedy[i].liczba * mnozyciel;
- mnozyciel*=m.podstawa;
- }
- return wynik;
- }
- public Row this[int i]
- {
- get
- {
- return rzedy[i];
- }
- set
- {
- rzedy[i] = value;
- }
- }
- public class Row
- {
- public int podstawa;
- public int liczba;
- public Row(int p, int l)
- {
- podstawa = p;
- liczba = l;
- }
- public Row(int p)
- {
- podstawa = p;
- liczba = 0;
- }
- public void add(int x)
- {
- if ((x + liczba) >= podstawa) throw new IncorrectRowValueException();
- liczba += x;
- }
- public void remove(int x)
- {
- if (x > liczba) throw new IncorrectRowValueException();
- liczba -= x;
- }
- public override string ToString()
- {
- StringBuilder wynik = new StringBuilder();
- for (int i = 0; i < liczba; i++) wynik.Append("I");
- for (int i = 0; i < podstawa-1; i++) wynik.Append("-");
- for (int i = 0; i < podstawa-1-liczba; i++) wynik.Append("I");
- return wynik.ToString();
- }
- }
- public IEnumerator GetEnumerator()
- {
- return new MojEnumerator(rzedy,zajete);
- }
- public class MojEnumerator : IEnumerator
- {
- object[] lista;
- bool[] czyzajete;
- public MojEnumerator(object[] par, bool[] anybodythere)
- {
- lista = par;
- czyzajete=anybodythere;
- }
- int position = -1;
- public bool MoveNext()
- {
- if (position >= 0) czyzajete[position] = false;
- if (position >= lista.Length - 1) return false;
- if (czyzajete[position + 1]) throw new SomeoneThereException();
- position++;
- czyzajete[position] = true;
- return true;
- }
- public object Current
- {
- get
- {
- return lista[position];
- }
- }
- public void Reset() { position = -1; }
- }
- }
- class Program
- {
- public static void Desc(BlindAbacus a)
- {
- Console.WriteLine(a);
- Console.WriteLine("Value: " + (int) a);
- }
- public static void Main()
- {
- BlindAbacus test = new BlindAbacus(7,7);
- for(int i = 0; i <7; i++)
- test[i].add(i);
- Console.WriteLine(test);
- int line = 0;
- foreach(BlindAbacus.Row r in test) {
- Console.WriteLine(++line + "outer :" + r);
- if(line == 5) try {
- foreach(BlindAbacus.Row s in test)
- Console.WriteLine(" inner :" + s);
- }catch(BlindAbacus.SomeoneThereException) {
- Console.WriteLine("Someone there");
- }
- }
- foreach(BlindAbacus.Row r in test)
- Console.WriteLine(" outer :" + r);
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement