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");
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!!!");
Console.WriteLine("Game over...");
Console.ReadKey();
2.
using System.ComponentModel;
using System.Data;
using System.Drawing;
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...";
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.
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);
button1.Dispose();
4.Znajdywanie cyfr rozwinięcia dwójkowego liczby z instrukcją warunkową
namespace Cyfry_liczby
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";
label1.Text = label1.Text + "0";
p = p / 2;
5.Znajdowanie cyfr ułamka w aplikacji konsolowej
namespace Cyfry_ułamka
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
namespace Cyfry_liczby_całkowitej
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);
7.
namespace Powitanie
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) =>
...
dj_bacha