仅供参考:
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Formula
{
public partial class Form1 : Form
{
public bool X = false;
public bool N = false;
public Form1()
{
InitializeComponent();
bt_calculate.Enabled = false;
}
private int doMutiN(int n)
{
int total = 1;
for (int i = n; i >= 1; i--)
{
total *= i;
}
return total;
}
private void bt_calculate_Click(object sender, EventArgs e)
{
int n = Convert.ToInt16( textBox_N.Text);
double x = Convert.ToSingle(textBox_X.Text);
double result = 1 + x - Math.Pow(x, 2) / doMutiN(2) + Math.Pow(x, 3) / doMutiN(3) + Math.Pow(-1, n + 1) * Math.Pow(x, n) / doMutiN(n);
textBox_result.Text =
result.ToString("f8");
}
private void doCheck_X_N()
{
if (X & N)
{
bt_calculate.Enabled = true;
}
}
private void textBox_N_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
{
e.Handled = true;
}
if(textBox_N.Text!="")
{
N = true;
}
doCheck_X_N();
}
private void textBox_X_KeyPress_1(object sender, KeyPressEventArgs e)
{
if (!char.IsDigit(e.KeyChar) && !char.IsPunctuation(e.KeyChar) && !char.IsControl(e.KeyChar))
{
e.Handled = true;
}
if (textBox_X.Text != "")
{
X = true;
}
doCheck_X_N();
}
}
}