Долго выполняется программа и зависает - C#
Формулировка задачи:
Программа на C# долго выполняется и зависает при вводе eps
вот код:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
if (textBox2.Text.Length == 0 && textBox3.Text.Length == 0)
{
button1.Enabled = false;
}
else
button1.Enabled = true;
}
private void label2_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
var eps = Convert.ToDouble(textBox3.Text);
var x = Convert.ToDouble(textBox2.Text);
Sum(x, eps);
Sin(x);
}
private void texBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (textBox2.Text.Length == 0)
button1.Enabled = false;
else
button1.Enabled = true;
}
private void texBox3_KeyPress(object sender, KeyPressEventArgs e)
{
if (textBox3.Text.Length == 0)
button1.Enabled = false;
else
button1.Enabled = true;
}
public double Sum(double x, double eps)
{
//double epsilon = 1e-6;
double sum = 0;
int n = 1;
double numerator = Math.Pow(-1 * x, 2);
double Factorial = ((2 * n) + 2) * ((2 * n) + 3);
double coefNumerator = -x * x;
double lastSummand;
do
{
lastSummand = numerator / Factorial;
sum += lastSummand;
numerator *= coefNumerator;
n += 2;
}
while (Math.Abs(lastSummand) > eps);
label8.Text = " " + sum;
label7.Text = " " + n;
return sum;
}
public double Sin(double x)
{
double sin = Math.Sin(x);
label6.Text = " " + sin;
return sin;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
button1.Enabled = (!String.IsNullOrEmpty(textBox2.Text) && !String.IsNullOrEmpty(textBox3.Text));
}
}
}Решение задачи: «Долго выполняется программа и зависает»
textual
Листинг программы
do { lastSummand = numerator / Factorial; sum += lastSummand; numerator *= coefNumerator; n += 2; } while (Math.Abs(lastSummand) > eps);