Advertisement
amarek

OOP LV6 - Analiza

Nov 18th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.12 KB | None | 0 0
  1. // 1. Napravite aplikaciju znanstveni kalkulator koja će imati funkcionalnost
  2. // znanstvenog kalkulatora, odnosno implementirati osnovne (+,-,*,/) i barem 5
  3. // naprednih (sin, cos, log, sqrt...) operacija.
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14.  
  15. namespace LV6___Analiza_1
  16. {
  17.     public partial class form_kalkulator : Form
  18.     {
  19.         double FirstNumber;
  20.         string Operation;
  21.  
  22.         public form_kalkulator()
  23.         {
  24.             InitializeComponent();
  25.         }
  26.  
  27.         private void button1_Click(object sender, EventArgs e)
  28.         {
  29.             if (textBox_display.Text == "0" && textBox_display.Text != null)
  30.             {
  31.                 textBox_display.Text = "1";
  32.             }
  33.             else
  34.             {
  35.                 textBox_display.Text = textBox_display.Text + "1";
  36.             }
  37.         }
  38.  
  39.         private void button2_Click(object sender, EventArgs e)
  40.         {
  41.             if (textBox_display.Text == "0" && textBox_display.Text != null)
  42.             {
  43.                 textBox_display.Text = "2";
  44.             }
  45.             else
  46.             {
  47.                 textBox_display.Text = textBox_display.Text + "2";
  48.             }
  49.         }
  50.  
  51.         private void button3_Click(object sender, EventArgs e)
  52.         {
  53.             if (textBox_display.Text == "0" && textBox_display.Text != null)
  54.             {
  55.                 textBox_display.Text = "3";
  56.             }
  57.             else
  58.             {
  59.                 textBox_display.Text = textBox_display.Text + "3";
  60.             }
  61.         }
  62.  
  63.         private void button4_Click(object sender, EventArgs e)
  64.         {
  65.             if (textBox_display.Text == "0" && textBox_display.Text != null)
  66.             {
  67.                 textBox_display.Text = "4";
  68.             }
  69.             else
  70.             {
  71.                 textBox_display.Text = textBox_display.Text + "4";
  72.             }
  73.         }
  74.  
  75.         private void button5_Click(object sender, EventArgs e)
  76.         {
  77.             if (textBox_display.Text == "0" && textBox_display.Text != null)
  78.             {
  79.                 textBox_display.Text = "5";
  80.             }
  81.             else
  82.             {
  83.                 textBox_display.Text = textBox_display.Text + "5";
  84.             }
  85.         }
  86.  
  87.         private void button6_Click(object sender, EventArgs e)
  88.         {
  89.             if (textBox_display.Text == "0" && textBox_display.Text != null)
  90.             {
  91.                 textBox_display.Text = "6";
  92.             }
  93.             else
  94.             {
  95.                 textBox_display.Text = textBox_display.Text + "6";
  96.             }
  97.         }
  98.  
  99.         private void button7_Click(object sender, EventArgs e)
  100.         {
  101.             if (textBox_display.Text == "0" && textBox_display.Text != null)
  102.             {
  103.                 textBox_display.Text = "7";
  104.             }
  105.             else
  106.             {
  107.                 textBox_display.Text = textBox_display.Text + "7";
  108.             }
  109.         }
  110.  
  111.         private void button8_Click(object sender, EventArgs e)
  112.         {
  113.             if (textBox_display.Text == "0" && textBox_display.Text != null)
  114.             {
  115.                 textBox_display.Text = "8";
  116.             }
  117.             else
  118.             {
  119.                 textBox_display.Text = textBox_display.Text + "8";
  120.             }
  121.         }
  122.  
  123.         private void button9_Click(object sender, EventArgs e)
  124.         {
  125.             if (textBox_display.Text == "" && textBox_display.Text != null)
  126.             {
  127.                 textBox_display.Text = "9";
  128.             }
  129.             else
  130.             {
  131.                 textBox_display.Text = textBox_display.Text + "9";
  132.             }
  133.         }
  134.  
  135.         private void button0_Click(object sender, EventArgs e)
  136.         {
  137.             textBox_display.Text = textBox_display.Text + "0";
  138.         }
  139.  
  140.         private void button_dot_Click(object sender, EventArgs e)
  141.         {
  142.             textBox_display.Text = textBox_display.Text + ".";
  143.         }
  144.  
  145.         private void button_off_Click(object sender, EventArgs e)
  146.         {
  147.             Application.Exit();
  148.         }
  149.  
  150.         private void button_ce_Click(object sender, EventArgs e)
  151.         {
  152.             textBox_display.Text = "0";
  153.         }
  154.  
  155.         private void button_add_Click(object sender, EventArgs e)
  156.         {
  157.             FirstNumber = Convert.ToDouble(textBox_display.Text);
  158.             textBox_display.Text = "0";
  159.             Operation = "+";
  160.         }
  161.  
  162.         private void button_sub_Click(object sender, EventArgs e)
  163.         {
  164.             FirstNumber = Convert.ToDouble(textBox_display.Text);
  165.             textBox_display.Text = "0";
  166.             Operation = "-";
  167.         }
  168.  
  169.         private void button_mul_Click(object sender, EventArgs e)
  170.         {
  171.             FirstNumber = Convert.ToDouble(textBox_display.Text);
  172.             textBox_display.Text = "0";
  173.             Operation = "*";
  174.         }
  175.  
  176.         private void button_div_Click(object sender, EventArgs e)
  177.         {
  178.             FirstNumber = Convert.ToDouble(textBox_display.Text);
  179.             textBox_display.Text = "0";
  180.             Operation = "/";
  181.         }
  182.  
  183.         private void button_equal_Click(object sender, EventArgs e)
  184.         {
  185.             double SecondNumber;
  186.             double Result;
  187.  
  188.             SecondNumber = Convert.ToDouble(textBox_display.Text);
  189.  
  190.             if (Operation == "+")
  191.             {
  192.                 Result = (FirstNumber + SecondNumber);
  193.                 textBox_display.Text = Convert.ToString(Result);
  194.                 FirstNumber = Result;
  195.             }
  196.             if (Operation == "-")
  197.             {
  198.                 Result = (FirstNumber - SecondNumber);
  199.                 textBox_display.Text = Convert.ToString(Result);
  200.                 FirstNumber = Result;
  201.             }
  202.             if (Operation == "*")
  203.             {
  204.                 Result = (FirstNumber * SecondNumber);
  205.                 textBox_display.Text = Convert.ToString(Result);
  206.                 FirstNumber = Result;
  207.             }
  208.             if (Operation == "/")
  209.             {
  210.                 if (SecondNumber == 0)
  211.                 {
  212.                     textBox_display.Text = "Cannot divide by zero";
  213.  
  214.                 }
  215.                 else
  216.                 {
  217.                     Result = (FirstNumber / SecondNumber);
  218.                     textBox_display.Text = Convert.ToString(Result);
  219.                     FirstNumber = Result;
  220.                 }
  221.             }
  222.         }
  223.  
  224.         private void button_sqrt_Click(object sender, EventArgs e)
  225.         {
  226.             FirstNumber = Convert.ToDouble(textBox_display.Text);
  227.             double Result = 0;
  228.             Result = Math.Sqrt(FirstNumber);
  229.             textBox_display.Text = Result.ToString();
  230.         }
  231.  
  232.         private void button_log_Click(object sender, EventArgs e)
  233.         {
  234.             FirstNumber = Convert.ToDouble(textBox_display.Text);
  235.             double Result;
  236.             Result = Math.Log(FirstNumber);
  237.             textBox_display.Text = Result.ToString();
  238.         }
  239.  
  240.         private void button_sin_Click(object sender, EventArgs e)
  241.         {
  242.             FirstNumber = Convert.ToDouble(textBox_display.Text);
  243.             double Result = 0;
  244.             Result = Math.Sin(FirstNumber);
  245.             textBox_display.Text = Result.ToString();
  246.         }
  247.  
  248.         private void button_cos_Click(object sender, EventArgs e)
  249.         {
  250.             FirstNumber = Convert.ToDouble(textBox_display.Text);
  251.             double Result = 0;
  252.             Result = Math.Cos(FirstNumber);
  253.             textBox_display.Text = Result.ToString();
  254.         }
  255.  
  256.         private void button_tan_Click(object sender, EventArgs e)
  257.         {
  258.             FirstNumber = Convert.ToDouble(textBox_display.Text);
  259.             double Result = 0;
  260.             Result = Math.Tan(FirstNumber);
  261.             textBox_display.Text = Result.ToString();
  262.         }
  263.     }
  264. }
  265.  
  266. // 2. Napravite jednostavnu igru vješala. Pojmovi se učitavaju u listu iz datoteke, i u
  267. // svakoj partiji se odabire nasumični pojam iz liste. Omogućiti svu
  268. // funkcionalnost koju biste očekivali od takve igre. Nije nužno crtati vješala,
  269. // dovoljno je na labeli ispisati koliko je pokušaja za odabir slova preostalo.
  270.  
  271. using System;
  272. using System.Collections.Generic;
  273. using System.ComponentModel;
  274. using System.Data;
  275. using System.Drawing;
  276. using System.Linq;
  277. using System.Text;
  278. using System.Threading.Tasks;
  279. using System.Windows.Forms;
  280.  
  281. namespace LV6___Analiza_2
  282. {
  283.     public partial class form_vjesalo : Form
  284.     {
  285.         public form_vjesalo()
  286.         {
  287.             InitializeComponent();
  288.         }
  289.  
  290.         int hit = 0;
  291.         int pokusaji = 8;
  292.         string pojam;
  293.         static Random rnd = new Random();
  294.  
  295.         List<Label> labels = new List<Label>();
  296.         List<string> pojmovi = new List<string>();
  297.         string path = "C:\\Users\\Đordan\\Documents\\Visual Studio 2013\\Projects\\LV6 - Analiza 2\\LV6 - Analiza 2\\pojmovi.txt";
  298.  
  299.         private void Form1_Load(object sender, EventArgs e)
  300.         {
  301.             using (System.IO.StreamReader reader = new System.IO.StreamReader(@path))
  302.             {
  303.                 string line;
  304.                 while ((line = reader.ReadLine()) != null)
  305.                 {
  306.                     pojmovi.Add(line);
  307.                 }
  308.             }
  309.  
  310.             int r = rnd.Next(pojmovi.Count);
  311.             pojam = (string)pojmovi[r];
  312.  
  313.             labels = new List<Label>();
  314.             int startX = 80;
  315.             foreach (char c in pojam)
  316.             {
  317.                 Label lbl = new Label();
  318.                 lbl.Text = "_";
  319.                 lbl.Font = new Font(lbl.Font.Name, 20, lbl.Font.Style);
  320.                 lbl.Location = new Point(startX, 90);
  321.                 lbl.Tag = c.ToString();
  322.                 lbl.AutoSize = true;
  323.                 this.Controls.Add(lbl);
  324.                 labels.Add(lbl);
  325.                 startX = lbl.Right;
  326.             }
  327.         }
  328.  
  329.         private void button_pokusaj_Click(object sender, EventArgs e)
  330.         {
  331.             if (textBox_pokusaj.Text.Length == 0 || textBox_pokusaj.Text.Length > 1)
  332.             {
  333.                 textBox_pokusaj.Text = "";
  334.                 MessageBox.Show("Pogrešan unos!", "Greška!");
  335.             }
  336.             else
  337.             {
  338.                 if (pojam.Contains(textBox_pokusaj.Text))
  339.                 {
  340.                     for (int i = 0; i < pojam.Length; i++)
  341.                     {
  342.                         if (pojam.IndexOf(textBox_pokusaj.Text) == pojam.IndexOf(pojam[i]))
  343.                         {
  344.                             if (labels[i].Text == "_")
  345.                             {
  346.                                 labels[i].Text = textBox_pokusaj.Text;
  347.                                 hit++;
  348.                             }
  349.                             else
  350.                             {
  351.                                 MessageBox.Show("Slovo je već unešeno!", "Greška!");
  352.                                 break;
  353.                             }
  354.                         }
  355.                     }
  356.                     textBox_pokusaj.Text = "";
  357.                     if (hit == pojam.Length)
  358.                     {
  359.                         MessageBox.Show("Pobjedio si!", "Pobjeda!");
  360.                         Application.Exit();
  361.                     }
  362.                 }
  363.                 else
  364.                 {
  365.                     if (pokusaji == 1)
  366.                     {
  367.                         textBox_pokusaj.Text = "";
  368.                         pokusaji--;
  369.                         label_pokusaji.Text = "Broj preostalih pokusaja: " + pokusaji;
  370.                         MessageBox.Show("Izgubio si!", "Poraz!");
  371.                         Application.Exit();
  372.                     }
  373.                     else
  374.                     {
  375.                         textBox_pokusaj.Text = "";
  376.                         pokusaji--;
  377.                         label_pokusaji.Text = "Broj preostalih pokusaja: " + pokusaji;
  378.                     }
  379.                 }
  380.             }
  381.         }
  382.     }
  383. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement