prog.egzam.docx

(22 KB) Pobierz

1.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Zgadywanka

{

    class Program

    {

        static void Main(string[] args)

        {

            var kostka = new Random();

            var kod = kostka.Next(1, 7);

            var próby = 3;

 

            var Czytaj = new Func<Int32>(() =>

                {

                    do

                    {

                        Console.Write("Podaj liczbę od 1 do 6: ");

                        try

                        {

                            var wartość = Convert.ToInt32(Console.ReadLine());

                            if (wartość >= 1 && wartość <= 6)

                            {

                                próby = próby - 1;

                                return wartość;

                            }

                            else

                            {

                                Console.WriteLine("Niepoprawna wartość!");

                            }

                        }

                        catch (Exception)

                        {

 

                            Console.WriteLine("Musisz podać liczbę!");

                        }

                    } while (true);

                }

            );

            var liczba = Czytaj();

            while (liczba != kod && próby > 0)

            {

                if (liczba > kod)

                    Console.WriteLine("Liczba jest za duża");

                else

                    Console.WriteLine("Liczba jest za mała");

                Console.WriteLine("Pozostało Ci {0} prób", próby);

                liczba = Czytaj();

 

            }

            if (liczba == kod)

            {

                Console.WriteLine("Gratulacje!!!");

            }

            else

            {

                Console.WriteLine("Game over...");

            }

            Console.ReadKey();

        }

    }

}

 

 

 

 

 

 

2.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace Inteligentny_przycisk

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            if (button1.Text == "Kliknij mnie!" || button1.Text == "O, jak dobrze.")

            {

                button1.Text = "Chcę jeszcze...";

            }

            else

            {

                button1.Text = "O, jak dobrze.";

            }

 

            // to samo ze słownikiem

 

            var d = new Dictionary<String, String> {

                {"Kliknij mnie!", "Chcę jeszcze..."},

                {"O, jak dobrze.", "Chcę jeszcze..."},

                {"Chcę jeszcze...", "O, jak dobrze."}

            };

 

            button1.Text = d[button1.Text];

        }

    }

}

3.

private void button1_Click(object sender, EventArgs e)

        {

            var inteligencja = new Dictionary<String, Func<Double, String>> {

                {"Kliknij mnie!", p => p < 1.0 ? "Chcę jeszcze..." : ""},

                {"O, jak dobrze.", p => p < 0.9 ? "Chcę jeszcze..." : "Chyba już starczy?"},

                {"Chcę jeszcze...", p => p < 0.9 ? "O, jak dobrze." : "Chyba już starczy?"},

                {"Chyba już starczy?", p => p < 0.5 ? "Chyba już starczy?" : "Już dosyć!!!"}

            };

 

            var pamiec = button1.Text;

            var emocje = new Random().NextDouble();

 

            if (inteligencja.ContainsKey(pamiec))

            {

                button1.Text = inteligencja[pamiec](emocje);

            }

            else

            {

                button1.Dispose();

            }

        }

 

 

4.Znajdywanie cyfr rozwinięcia dwójkowego liczby z instrukcją warunkową

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace Cyfry_liczby

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)

        {

            label1.Text = "0";

            var p = Int32.MaxValue/2+1; // Math.Pow(2, 30)

            var x = numericUpDown1.Value;

            foreach (var item in Enumerable.Range(1, 31))

            {

                if (x >= p)

                {

                    x = x - p;

                    label1.Text = label1.Text + "1";

                }

                else

                {

                    label1.Text = label1.Text + "0";

                }

                p = p / 2;

            }

        }

    }

}

 

5.Znajdowanie cyfr ułamka w aplikacji konsolowej

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Cyfry_ułamka

{

    class Program

    {

        static void Main(string[] args)

        {

            var x = 0.4;

            Console.Write("0.");

            foreach (var item in Enumerable.Range(1, 53))

                  {

                x = 2 * x;

                var y = Math.Floor(x); // żeby dwa razy nie wołać

                Console.Write(y);

                x = x - y;

                  }

            Console.WriteLine();

            Console.ReadKey(true);

        }

    }

}

6.Znajdowanie cyfr liczby całkowitej w aplikacji konsolowej

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Cyfry_liczby_całkowitej

{

    class Program

    {

        static void Main(string[] args)

        {

            var x = Int32.MaxValue; // (((1 << 30) - 1) << 1) + 1;

 

            var r = "";

            foreach (var item in Enumerable.Range(1, 10))

            {

                r = x % 10 + r;

                x = x / 10;

            }

            Console.WriteLine("{0} = {1}", r, Math.Pow(2, 31) - 1);

            Console.ReadKey(true);

        }

    }

}

 

7.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

using System.Windows.Forms;

 

namespace Powitanie

{

    class Program

    {

        static void Main(string[] args)

        {

            var okno = new Form();

            okno.Text = "Jak masz na imię?";

            var pole = new TextBox();

            pole.Dock = DockStyle.Fill;

            pole.Multiline = true;

            okno.Controls.Add(pole);

            var przycisk = new Button();

            przycisk.Text = "Kliknij mnie!";

            przycisk.Dock = DockStyle.Bottom;

            przycisk.Click += (o, e) =>

...

Zgłoś jeśli naruszono regulamin