Advertisement
eniodordan

[OOP] LV6 - Zadaci + Analiza

Jan 9th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.07 KB | None | 0 0
  1. // Zadaci
  2. // 1. zadatak
  3. namespace LV6
  4. {
  5.     public partial class form_trokut : Form
  6.     {
  7.         double a, b, c;
  8.         public form_trokut()
  9.         {
  10.             InitializeComponent();
  11.         }
  12.  
  13.         double povrsina() {
  14.             double s = (a + b + c) / 2;
  15.             return System.Math.Round(System.Math.Sqrt(s*(s - a)*(s - b)*(s - c)), 2);
  16.         }
  17.  
  18.         double opseg() {
  19.             return System.Math.Round((a + b + c), 2);
  20.         }
  21.  
  22.         bool pravokutan()
  23.         {
  24.             if (c == System.Math.Sqrt(a * a + b * b))
  25.             {
  26.                 return true;
  27.             }
  28.             return false;
  29.         }
  30.  
  31.         private void button_izracunaj_Click(object sender, EventArgs e)
  32.         {
  33.             if (!double.TryParse(textBox_a.Text, out a)) {
  34.                 MessageBox.Show("Pogrešan unos stranice a!", "Pogreška!");
  35.             }
  36.             else if(!double.TryParse(textBox_b.Text, out b)) {
  37.                 MessageBox.Show("Pogrešan unos stranice b!", "Pogreška!");
  38.             }
  39.             else if(!double.TryParse(textBox_c.Text, out c)) {
  40.                 MessageBox.Show("Pogrešan unos stranice c!", "Pogreška!");
  41.             }
  42.             else if (c >= a + b || b >= a + c || a >= b + c) {
  43.                 MessageBox.Show("Trokut nije moguć!", "Pogreška!");
  44.             }
  45.             else
  46.             {
  47.                 label_povrsina.Text = ("Površina: " + povrsina()).ToString();
  48.                 label_opseg.Text = ("Opseg: " + opseg()).ToString();
  49.             }
  50.         }
  51.  
  52.         private void button_pravokutan_Click(object sender, EventArgs e)
  53.         {
  54.             if (!double.TryParse(textBox_a.Text, out a))
  55.             {
  56.                 MessageBox.Show("Pogrešan unos stranice a!", "Pogreška!");
  57.             }
  58.             else if (!double.TryParse(textBox_b.Text, out b))
  59.             {
  60.                 MessageBox.Show("Pogrešan unos stranice b!", "Pogreška!");
  61.             }
  62.             else if (!double.TryParse(textBox_c.Text, out c))
  63.             {
  64.                 MessageBox.Show("Pogrešan unos stranice c!", "Pogreška!");
  65.             }
  66.             else if (c >= a + b || b >= a + c || a >= b + c)
  67.             {
  68.                 MessageBox.Show("Trokut nije moguć!", "Pogreška!");
  69.             }
  70.             else
  71.             {
  72.                 if (pravokutan())
  73.                 {
  74.                     label_pravokutan.ForeColor = System.Drawing.Color.Green;
  75.                     label_pravokutan.Text = "Pravokutan: DA";
  76.                 }
  77.                 else
  78.                 {
  79.                     label_pravokutan.ForeColor = System.Drawing.Color.Red;
  80.                     label_pravokutan.Text = "Pravokutan: NE";
  81.                 }
  82.             }
  83.         }
  84.  
  85.         private void button_izlaz_Click(object sender, EventArgs e)
  86.         {
  87.             Application.Exit();
  88.         }
  89.     }
  90. }
  91.  
  92. // 2. zadatak
  93. namespace LV6___Zadatak_2
  94. {
  95.     public partial class form_imenik : Form
  96.     {
  97.         public form_imenik()
  98.         {
  99.             InitializeComponent();
  100.         }
  101.  
  102.         class Kontakt
  103.         {
  104.             #region data_members
  105.             private string ime;
  106.             public string prezime;
  107.             private string broj_telefona;
  108.             #endregion
  109.  
  110.             #region public_methods
  111.             public Kontakt()
  112.             {
  113.                 ime = ""; prezime = ""; broj_telefona = "";
  114.             }
  115.             public Kontakt(string i, string p, string b)
  116.             {
  117.                 ime = i; prezime = p; broj_telefona = b;
  118.             }
  119.             public override string ToString()
  120.             {
  121.                 return ime + "\t" + prezime + "\t" + broj_telefona;
  122.             }
  123.             #endregion
  124.         }
  125.  
  126.         List<Kontakt> listKontakt = new List<Kontakt>();
  127.         string path = "C:\\Users\\Student\\Documents\\Visual Studio 2015\\Projects\\LV6 - Zadatak 2\\LV6 - Zadatak 2\\imenik.txt";
  128.  
  129.         private void form_imenik_Load(object sender, EventArgs e)
  130.         {
  131.             using (System.IO.StreamReader reader = new System.IO.StreamReader(@path))
  132.             {
  133.                 string line;
  134.                 while ((line = reader.ReadLine()) != null)
  135.                 {
  136.                     string[] parts = line.Split('\t');
  137.                     Kontakt K = new Kontakt(parts[0], parts[1], parts[2]);
  138.                     listKontakt.Add(K);
  139.                 }
  140.                 listBox_popis.DataSource = null;
  141.                 listBox_popis.DataSource = listKontakt;
  142.             }
  143.  
  144.         }
  145.  
  146.         private void button_dodaj_Click(object sender, EventArgs e)
  147.         {
  148.             bool error = false;
  149.  
  150.             if (textBox_ime.Text == "" || textBox_prezime.Text == "" || textBox_broj.Text == "")
  151.             {
  152.                 error = true;
  153.                 MessageBox.Show("Neko od polja ostalo je prazno.", "Pogreška!");
  154.             }
  155.             else if (!Regex.IsMatch(textBox_broj.Text, @"^[0-9]+$"))
  156.             {
  157.                 error = true;
  158.                 MessageBox.Show("Broj telefona je neispravan!", "Pogreška!");
  159.             }
  160.             else
  161.             {
  162.                 error = false;
  163.             }
  164.  
  165.             if (!error)
  166.             {
  167.                 Kontakt K = new Kontakt(textBox_ime.Text, textBox_prezime.Text, textBox_broj.Text);
  168.                 listKontakt.Add(K);
  169.                 listBox_popis.DataSource = null;
  170.                 listBox_popis.DataSource = listKontakt;
  171.                 textBox_ime.Text = String.Empty;
  172.                 textBox_prezime.Text = String.Empty;
  173.                 textBox_broj.Text = String.Empty;
  174.             }
  175.         }
  176.  
  177.         private void button_izlaz_Click(object sender, EventArgs e)
  178.         {
  179.             using (System.IO.StreamWriter write = new System.IO.StreamWriter(@path))
  180.             {
  181.                 foreach (Kontakt K in listKontakt)
  182.                 {
  183.                     {
  184.                         write.WriteLine(K.ToString());
  185.                     }
  186.                 }
  187.             }
  188.             Application.Exit();
  189.         }
  190.  
  191.         private void button_pretraga_Click(object sender, EventArgs e)
  192.         {
  193.             bool state = false;
  194.  
  195.             if (textBox_pretraga.Text == "")
  196.             {
  197.                 MessageBox.Show("Niste unjeli prezime!", "Pogreška!");
  198.             }
  199.             else
  200.             {
  201.                 foreach (Kontakt K in listKontakt)
  202.                 {
  203.                     if (K.prezime.Contains(textBox_pretraga.Text))
  204.                     {
  205.                         state = true;
  206.                         break;
  207.                     }
  208.                     else
  209.                     {
  210.                         state = false;
  211.                     }
  212.                 }
  213.  
  214.                 if (state)
  215.                 {
  216.                     MessageBox.Show("Kontakt postoji!", "Pretraga!");
  217.                 }
  218.                 else
  219.                 {
  220.                     MessageBox.Show("Kontakt ne postoji!", "Pretraga!");
  221.                 }
  222.             }
  223.         }
  224.     }
  225. }
  226.  
  227. // Analiza
  228. // 1. Napravite aplikaciju znanstveni kalkulator koja će imati funkcionalnost
  229. // znanstvenog kalkulatora, odnosno implementirati osnovne (+,-,*,/) i barem 5
  230. // naprednih (sin, cos, log, sqrt...) operacija.
  231.  
  232. namespace LV6___Analiza_1
  233. {
  234.     public partial class form_kalkulator : Form
  235.     {
  236.         double FirstNumber;
  237.         string Operation;
  238.  
  239.         public form_kalkulator()
  240.         {
  241.             InitializeComponent();
  242.         }
  243.  
  244.         private void button1_Click(object sender, EventArgs e)
  245.         {
  246.             if (textBox_display.Text == "0" && textBox_display.Text != null)
  247.             {
  248.                 textBox_display.Text = "1";
  249.             }
  250.             else
  251.             {
  252.                 textBox_display.Text = textBox_display.Text + "1";
  253.             }
  254.         }
  255.  
  256.         private void button2_Click(object sender, EventArgs e)
  257.         {
  258.             if (textBox_display.Text == "0" && textBox_display.Text != null)
  259.             {
  260.                 textBox_display.Text = "2";
  261.             }
  262.             else
  263.             {
  264.                 textBox_display.Text = textBox_display.Text + "2";
  265.             }
  266.         }
  267.  
  268.         private void button3_Click(object sender, EventArgs e)
  269.         {
  270.             if (textBox_display.Text == "0" && textBox_display.Text != null)
  271.             {
  272.                 textBox_display.Text = "3";
  273.             }
  274.             else
  275.             {
  276.                 textBox_display.Text = textBox_display.Text + "3";
  277.             }
  278.         }
  279.  
  280.         private void button4_Click(object sender, EventArgs e)
  281.         {
  282.             if (textBox_display.Text == "0" && textBox_display.Text != null)
  283.             {
  284.                 textBox_display.Text = "4";
  285.             }
  286.             else
  287.             {
  288.                 textBox_display.Text = textBox_display.Text + "4";
  289.             }
  290.         }
  291.  
  292.         private void button5_Click(object sender, EventArgs e)
  293.         {
  294.             if (textBox_display.Text == "0" && textBox_display.Text != null)
  295.             {
  296.                 textBox_display.Text = "5";
  297.             }
  298.             else
  299.             {
  300.                 textBox_display.Text = textBox_display.Text + "5";
  301.             }
  302.         }
  303.  
  304.         private void button6_Click(object sender, EventArgs e)
  305.         {
  306.             if (textBox_display.Text == "0" && textBox_display.Text != null)
  307.             {
  308.                 textBox_display.Text = "6";
  309.             }
  310.             else
  311.             {
  312.                 textBox_display.Text = textBox_display.Text + "6";
  313.             }
  314.         }
  315.  
  316.         private void button7_Click(object sender, EventArgs e)
  317.         {
  318.             if (textBox_display.Text == "0" && textBox_display.Text != null)
  319.             {
  320.                 textBox_display.Text = "7";
  321.             }
  322.             else
  323.             {
  324.                 textBox_display.Text = textBox_display.Text + "7";
  325.             }
  326.         }
  327.  
  328.         private void button8_Click(object sender, EventArgs e)
  329.         {
  330.             if (textBox_display.Text == "0" && textBox_display.Text != null)
  331.             {
  332.                 textBox_display.Text = "8";
  333.             }
  334.             else
  335.             {
  336.                 textBox_display.Text = textBox_display.Text + "8";
  337.             }
  338.         }
  339.  
  340.         private void button9_Click(object sender, EventArgs e)
  341.         {
  342.             if (textBox_display.Text == "" && textBox_display.Text != null)
  343.             {
  344.                 textBox_display.Text = "9";
  345.             }
  346.             else
  347.             {
  348.                 textBox_display.Text = textBox_display.Text + "9";
  349.             }
  350.         }
  351.  
  352.         private void button0_Click(object sender, EventArgs e)
  353.         {
  354.             textBox_display.Text = textBox_display.Text + "0";
  355.         }
  356.  
  357.         private void button_dot_Click(object sender, EventArgs e)
  358.         {
  359.             textBox_display.Text = textBox_display.Text + ".";
  360.         }
  361.  
  362.         private void button_off_Click(object sender, EventArgs e)
  363.         {
  364.             Application.Exit();
  365.         }
  366.  
  367.         private void button_ce_Click(object sender, EventArgs e)
  368.         {
  369.             textBox_display.Text = "0";
  370.         }
  371.  
  372.         private void button_add_Click(object sender, EventArgs e)
  373.         {
  374.             FirstNumber = Convert.ToDouble(textBox_display.Text);
  375.             textBox_display.Text = "0";
  376.             Operation = "+";
  377.         }
  378.  
  379.         private void button_sub_Click(object sender, EventArgs e)
  380.         {
  381.             FirstNumber = Convert.ToDouble(textBox_display.Text);
  382.             textBox_display.Text = "0";
  383.             Operation = "-";
  384.         }
  385.  
  386.         private void button_mul_Click(object sender, EventArgs e)
  387.         {
  388.             FirstNumber = Convert.ToDouble(textBox_display.Text);
  389.             textBox_display.Text = "0";
  390.             Operation = "*";
  391.         }
  392.  
  393.         private void button_div_Click(object sender, EventArgs e)
  394.         {
  395.             FirstNumber = Convert.ToDouble(textBox_display.Text);
  396.             textBox_display.Text = "0";
  397.             Operation = "/";
  398.         }
  399.  
  400.         private void button_equal_Click(object sender, EventArgs e)
  401.         {
  402.             double SecondNumber;
  403.             double Result;
  404.  
  405.             SecondNumber = Convert.ToDouble(textBox_display.Text);
  406.  
  407.             if (Operation == "+")
  408.             {
  409.                 Result = (FirstNumber + SecondNumber);
  410.                 textBox_display.Text = Convert.ToString(Result);
  411.                 FirstNumber = Result;
  412.             }
  413.             if (Operation == "-")
  414.             {
  415.                 Result = (FirstNumber - SecondNumber);
  416.                 textBox_display.Text = Convert.ToString(Result);
  417.                 FirstNumber = Result;
  418.             }
  419.             if (Operation == "*")
  420.             {
  421.                 Result = (FirstNumber * SecondNumber);
  422.                 textBox_display.Text = Convert.ToString(Result);
  423.                 FirstNumber = Result;
  424.             }
  425.             if (Operation == "/")
  426.             {
  427.                 if (SecondNumber == 0)
  428.                 {
  429.                     textBox_display.Text = "Cannot divide by zero";
  430.  
  431.                 }
  432.                 else
  433.                 {
  434.                     Result = (FirstNumber / SecondNumber);
  435.                     textBox_display.Text = Convert.ToString(Result);
  436.                     FirstNumber = Result;
  437.                 }
  438.             }
  439.         }
  440.  
  441.         private void button_sqrt_Click(object sender, EventArgs e)
  442.         {
  443.             FirstNumber = Convert.ToDouble(textBox_display.Text);
  444.             double Result = 0;
  445.             Result = Math.Sqrt(FirstNumber);
  446.             textBox_display.Text = Result.ToString();
  447.         }
  448.  
  449.         private void button_log_Click(object sender, EventArgs e)
  450.         {
  451.             FirstNumber = Convert.ToDouble(textBox_display.Text);
  452.             double Result;
  453.             Result = Math.Log(FirstNumber);
  454.             textBox_display.Text = Result.ToString();
  455.         }
  456.  
  457.         private void button_sin_Click(object sender, EventArgs e)
  458.         {
  459.             FirstNumber = Convert.ToDouble(textBox_display.Text);
  460.             double Result = 0;
  461.             Result = Math.Sin(FirstNumber);
  462.             textBox_display.Text = Result.ToString();
  463.         }
  464.  
  465.         private void button_cos_Click(object sender, EventArgs e)
  466.         {
  467.             FirstNumber = Convert.ToDouble(textBox_display.Text);
  468.             double Result = 0;
  469.             Result = Math.Cos(FirstNumber);
  470.             textBox_display.Text = Result.ToString();
  471.         }
  472.  
  473.         private void button_tan_Click(object sender, EventArgs e)
  474.         {
  475.             FirstNumber = Convert.ToDouble(textBox_display.Text);
  476.             double Result = 0;
  477.             Result = Math.Tan(FirstNumber);
  478.             textBox_display.Text = Result.ToString();
  479.         }
  480.     }
  481. }
  482.  
  483. // 2. Napravite jednostavnu igru vješala. Pojmovi se učitavaju u listu iz datoteke, i u
  484. // svakoj partiji se odabire nasumični pojam iz liste. Omogućiti svu
  485. // funkcionalnost koju biste očekivali od takve igre. Nije nužno crtati vješala,
  486. // dovoljno je na labeli ispisati koliko je pokušaja za odabir slova preostalo.
  487.  
  488. namespace LV6___Analiza_2
  489. {
  490.     public partial class form_vjesalo : Form
  491.     {
  492.         public form_vjesalo()
  493.         {
  494.             InitializeComponent();
  495.         }
  496.  
  497.         int hit = 0;
  498.         int pokusaji = 8;
  499.         string pojam;
  500.         static Random rnd = new Random();
  501.  
  502.         List<Label> labels = new List<Label>();
  503.         List<string> pojmovi = new List<string>();
  504.         string path = "C:\\Users\\Đordan\\Documents\\Visual Studio 2013\\Projects\\LV6 - Analiza 2\\LV6 - Analiza 2\\pojmovi.txt";
  505.  
  506.         private void Form1_Load(object sender, EventArgs e)
  507.         {
  508.             using (System.IO.StreamReader reader = new System.IO.StreamReader(@path))
  509.             {
  510.                 string line;
  511.                 while ((line = reader.ReadLine()) != null)
  512.                 {
  513.                     pojmovi.Add(line);
  514.                 }
  515.             }
  516.  
  517.             int r = rnd.Next(pojmovi.Count);
  518.             pojam = (string)pojmovi[r];
  519.  
  520.             labels = new List<Label>();
  521.             int startX = 80;
  522.             foreach (char c in pojam)
  523.             {
  524.                 Label lbl = new Label();
  525.                 lbl.Text = "_";
  526.                 lbl.Font = new Font(lbl.Font.Name, 20, lbl.Font.Style);
  527.                 lbl.Location = new Point(startX, 90);
  528.                 lbl.Tag = c.ToString();
  529.                 lbl.AutoSize = true;
  530.                 this.Controls.Add(lbl);
  531.                 labels.Add(lbl);
  532.                 startX = lbl.Right;
  533.             }
  534.         }
  535.  
  536.         private void button_pokusaj_Click(object sender, EventArgs e)
  537.         {
  538.             if (textBox_pokusaj.Text.Length == 0 || textBox_pokusaj.Text.Length > 1)
  539.             {
  540.                 textBox_pokusaj.Text = "";
  541.                 MessageBox.Show("Pogrešan unos!", "Greška!");
  542.             }
  543.             else
  544.             {
  545.                 if (pojam.Contains(textBox_pokusaj.Text))
  546.                 {
  547.                     for (int i = 0; i < pojam.Length; i++)
  548.                     {
  549.                         if (pojam.IndexOf(textBox_pokusaj.Text) == pojam.IndexOf(pojam[i]))
  550.                         {
  551.                             if (labels[i].Text == "_")
  552.                             {
  553.                                 labels[i].Text = textBox_pokusaj.Text;
  554.                                 hit++;
  555.                             }
  556.                             else
  557.                             {
  558.                                 MessageBox.Show("Slovo je već unešeno!", "Greška!");
  559.                                 break;
  560.                             }
  561.                         }
  562.                     }
  563.                     textBox_pokusaj.Text = "";
  564.                     if (hit == pojam.Length)
  565.                     {
  566.                         MessageBox.Show("Pobjedio si!", "Pobjeda!");
  567.                         Application.Exit();
  568.                     }
  569.                 }
  570.                 else
  571.                 {
  572.                     if (pokusaji == 1)
  573.                     {
  574.                         textBox_pokusaj.Text = "";
  575.                         pokusaji--;
  576.                         label_pokusaji.Text = "Broj preostalih pokusaja: " + pokusaji;
  577.                         MessageBox.Show("Izgubio si!", "Poraz!");
  578.                         Application.Exit();
  579.                     }
  580.                     else
  581.                     {
  582.                         textBox_pokusaj.Text = "";
  583.                         pokusaji--;
  584.                         label_pokusaji.Text = "Broj preostalih pokusaja: " + pokusaji;
  585.                     }
  586.                 }
  587.             }
  588.         }
  589.     }
  590. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement