Zk 12.2.2009 (yoyo check it out :)

Cílem semináře je seznámit posluchače s jazykem C#, prostředím platformy .NET a tvorbou aplikací s grafickým uživatelským rozhraním. Předpokládají se znalosti objektově orientovaného programování.
Uživatelský avatar
Santhos
Matfyz(ák|ačka) level I
Příspěvky: 24
Registrován: 8. 1. 2007 11:34

Zk 12.2.2009 (yoyo check it out :)

Příspěvek od Santhos »

Hey,

sup fellows? Lets rock:
4120, 4096, 4104,...?

Kód: Vybrat vše

(24 | 4096) & (~(1 << 4))
4104
a) nezkompiluje se
b) 1,2
c) , (tedy pouze znak carka)
d) runtime error recpectively Exception

Kód: Vybrat vše

class Plus {
    public static void AddPlus() {
        Prg.AddOp("+",1);
    }
}

class Divide {
    public static void AddDivide() {
        Prg.AddOp("/",2);
    }
}

class Prg {
    static Dictionary<string, int> d=new Dictionary<string, int>();

    public static void AddOp(string s, int i) {
        d.Add(s,i);
    }

    static void Main() {                       
        Console.WriteLine(string.Format("{0},{1}",d["+"],d["-"]));
        new Plus();
        new Divide();
    }
}
d)
4B, 8B, 32B, 64B, 2^32, 2^64B, platform dependent? // B = Byte

Kód: Vybrat vše

size of long
8B
cannot be compiled, 32, 25... dunno actually

Kód: Vybrat vše

delegate int F();
... a very nice code
?
System.Object, object, decimal, some class, System.Int64

Kód: Vybrat vše

value types
decimal, System.Int64
What can interface I1 enforce?

Kód: Vybrat vše

a) operator *
b) public method
c) private method
d) internal method
e) interface I2
f) indexer []
g) static property
b), f) ... more?
Write 2 of the main reasons why to use immutable classes:

1) Immutable classes are thread safe
2) They can be used as hash values in hash tables such as Dictionary

http://codebetter.com/blogs/patricksmac ... -them.aspx
Implement enque in priority queue made of doubly-linked list. Make it safe to use in any context (besides thread safe, exceptions).

Kód: Vybrat vše

class Queue<T, P>
        {
            _____Node
            {
                public Node prev, next;
                public T item;
                public P prio;
                public Node(T i, P p) { item = t; prio = p; }
            }

            public void Enqueue(T item, P prio)
            {
                Node n = new Node(item, prio);


            }

            public T Dequeue()
            {
                 ...  // count on correct implementation
            }
        }
Answer:

Kód: Vybrat vše

class Queue<T, P> where P:IComparable<P>
        {
            class Node
            {
                public Node prev, next;
                public T item;
                public P prio;
                public Node(T i, P p) { item = t; prio = p; }
            }

            Node first = null;

            public void Enqueue(T item, P prio)
            {
                Node n = new Node(item, prio);
                lock (this)
                {
                    if (first == null)
                        first = n;
                    else
                    {
                        Node cur = first;
                        Node prev = null;
                        while (cur != null && prio.CompareTo(cur.prio) <= 0)
                        {
                            prev = cur;
                            cur = cur.next;
                        }
                        prev.next = n;
                        if (cur != null)
                            cur.prev = n;
                    }
                }
            }

            public T Dequeue()
            {
                 ...
            }

!NO WARRANTY GUARANTEED!
Tempest
Matfyz(ák|ačka) level I
Příspěvky: 3
Registrován: 28. 5. 2008 17:05
Typ studia: Informatika Bc.

Re: Zk 12.2.2009 (yoyo check it out :)

Příspěvek od Tempest »

Kód: Vybrat vše

 public void Enqueue(T item, P prio)
            {
                Node n = new Node(item, prio);
                lock (this)
                {
                    if (first == null)
                        first = n;
                    else
                    {
                        Node cur = first;
                        Node prev = null;
                        while (cur != null && prio.CompareTo(cur.prio) <= 0)
                        {
                            prev = cur;
                            cur = cur.next;
                        }
                        prev.next = n;
                        if (cur != null)
                            cur.prev = n;
                    }
                }
            }
Jen pro pořádek, to lock(this) je špatně, už to tu párkrát bylo (např. http://forum.matfyz.info/viewtopic.php?f=421&t=5004).
Jonáš
Matfyz(ák|ačka) level I
Příspěvky: 15
Registrován: 19. 1. 2010 10:57
Typ studia: Informatika Mgr.

Re: Zk 12.2.2009 (yoyo check it out :)

Příspěvek od Jonáš »

What can interface I1 enforce?

Kód: Vybrat vše
a) operator *
b) public method
c) private method
d) internal method
e) interface I2
f) indexer []
g) static property

b), f) ... more?
Není správně i e) interface I2?
Pokud I1 dědí od I2, třída implementující I1 musí implementovat i vše co delkaruj I2.
Je v nějakých případech rozdíl, jestli třída A imlementuje I1 který dědí od I2 nebo A implementuje přímo I1 a I2?
Odpovědět

Zpět na „NPRG035 Jazyk C# a platforma .NET“