kunio.lol
Home
Studenci
Mama
Narzędzia
▼
Notatnik
Skróć link
Chmura
Studia
▼
Magisterskie
Semestr 1
▶
IoT: Lab 3
Inżynierskie
Semestr 7
▶
Oceny 7sem
Paczka
MiSSE: Lab1
MiSSE: Lab2
MiSSE: Lab3
MiSSE: Lab5
Semestr 3
▶
ISD: Filmy
EE: Filmy
AKiSO: Kalkulator
AKiSO: Lab 5
PT: Wejściówka
PT: Lab 6
Semestr 2
▶
OiS: Test 6
OiS: Test 12
Zaloguj
Notatnik
Wszystkie zmiany są zapisywane automatycznie...
// ######################################################### // calcPI.cs (Zwiększony czas obliczeń) // ######################################################### static double CalcPiLeibnitz(CancellationToken ct) { double piVal = 0; int n = 1; double error = 1e-15; // Zmienione, żeby liczyło dłużej int count = int.MaxValue; // Zmienione na ponad 2 miliardy while (Math.Abs(Math.PI - piVal) > error) { if (n % 1000 == 0) ct.ThrowIfCancellationRequested(); piVal += 4 * (n % 2 == 0 ? -1.0 : 1.0) / (2 * n - 1); if (n >= count) break; n++; } return piVal; } static double CalcPiEulerTrig(CancellationToken ct) { double piValTemp = 0, piVal = 0; int n = 1; double error = 1e-15; int count = int.MaxValue; while (Math.Abs(Math.PI - piVal) > error) { if (n % 1000 == 0) ct.ThrowIfCancellationRequested(); piValTemp += Math.Pow(n, -2); piVal = Math.Sqrt(piValTemp * 6); if (n >= count) break; n++; } return piVal; } static double CalcPiWallis(CancellationToken ct) { double piVal = 2; int n = 1; double error = 1e-15; int count = int.MaxValue; while (Math.Abs(Math.PI - piVal) > error) { if (n % 1000 == 0) ct.ThrowIfCancellationRequested(); if (n % 2 == 0) piVal *= (double)n / (n + 1); else piVal *= (double)(n + 1) / n; if (n >= count) break; n++; } return piVal; } // ######################################################### // PRZYCISKI (Naprawione tworzenie tokena) // ######################################################### private async void button1_Click(object sender, EventArgs e) { button1.Enabled = false; // WSPÓLNY TOKEN: Tworzy nowy tylko gdy jest null lub został już anulowany if (cts == null || cts.IsCancellationRequested) cts = new CancellationTokenSource(); try { double wynik = await Task.Run(() => CalcPiLeibnitz(cts.Token)); textBox1.Text = wynik.ToString(); } catch (OperationCanceledException) { textBox1.Text = "OperationCanceledException!"; } button1.Enabled = true; } private async void button2_Click(object sender, EventArgs e) { button2.Enabled = false; if (cts == null || cts.IsCancellationRequested) cts = new CancellationTokenSource(); try { double wynik = await Task.Run(() => CalcPiEulerTrig(cts.Token)); textBox2.Text = wynik.ToString(); } catch (OperationCanceledException) { textBox2.Text = "OperationCanceledException!"; } button2.Enabled = true; } private async void button3_Click(object sender, EventArgs e) { button3.Enabled = false; if (cts == null || cts.IsCancellationRequested) cts = new CancellationTokenSource(); try { double wynik = await Task.Run(() => CalcPiWallis(cts.Token)); textBox3.Text = wynik.ToString(); } catch (OperationCanceledException) { textBox3.Text = "OperationCanceledException!"; } button3.Enabled = true; } private void button4_Click(object sender, EventArgs e) { if (cts != null) { cts.Cancel(); } }