Lab1.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace WindowsFormsApp6
  11. {
  12. public partial class Lab1 : Form
  13. {
  14. public static bool Active;
  15. public Lab1()
  16. {
  17. InitializeComponent();
  18. }
  19. private void Lab1_Load(object sender, EventArgs e)
  20. {
  21. Active = true;
  22. }
  23. private void Lab1_FormClosed(object sender, FormClosedEventArgs e)
  24. {
  25. Active = false;
  26. }
  27. private void btnCalculate_Click(object sender, EventArgs ef)
  28. {
  29. try
  30. {
  31. MessageBox.Show($"Ответ: {Calculate(int.Parse(tbInputK.Text))}", "Ответ");
  32. } catch
  33. {
  34. MessageBox.Show($"Не верно заполнено поле", "Ошибка");
  35. }
  36. }
  37. static double Calculate(int num)
  38. {
  39. int k = num;
  40. double Sum = 0;
  41. double e = 0.001;
  42. while (true)
  43. {
  44. double term = Math.Pow(-1, k + 1) / (2 * k - 1);
  45. Sum += term;
  46. if (Math.Abs(term) < e)
  47. {
  48. break;
  49. }
  50. k++;
  51. }
  52. double piOver4 = Sum;
  53. double pi = piOver4 * 4;
  54. double roundedpiOver4 = Math.Round(piOver4, 5);
  55. return roundedpiOver4;
  56. }
  57. public string MessagePull()
  58. {
  59. string message = $"Lab1 | Число K: {tbInputK.Text}";
  60. return message;
  61. }
  62. }
  63. }