标签:
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 Exiperiment61
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//将光标设于输入文本框中
txtline.Select();
}
private void txtline_KeyPress(object sender, KeyPressEventArgs e)
{
//非数字键放弃该输入
//if(!char.IsDigit(e.KeyChar) && (int)e.KeyChar!=8)
//{
// e.Handled=true;
// return;
//}
//非数字键,放弃该输入 (2)
if( (int)e.KeyChar <48 || (int)e.KeyChar>58 )
{
if((int)e.KeyChar!=8)
{
e.Handled=true ;
return ;
}
}
}
private void btnpain_Click(object sender, EventArgs e)
{
int i, j;
int iinpuutline; //输入行数
int itemp = 0; //保存杨辉三角的临时变量
int icurline;//当前行数的阶乘,从0开始
int iposition;//行的第几个位置的阶乘
string strtemp; //用于临时编辑的输出字符
//未输入检查
if (txtline.Text.Trim() == "")
{
MessageBox.Show("请输入行数!",
"杨辉三角提示信息",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
txtline.Select();
return;
}
//输入值超过规定值检查
iinpuutline = int.Parse(txtline.Text);//数据类型转换
iinpuutline = Convert.ToInt32(txtline.Text );//与上一句比较
if (iinpuutline < 1 || iinpuutline > 8)
{
MessageBox.Show("输入越界!","杨辉三角提示信息",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
txtline .Select ();
return;
}
int ibackline;
//初始化临时存放字符串
strtemp ="";
for(i=0;i<iinpuutline ;i++)
{
for (j=iinpuutline ;j>=i;j--)
strtemp =strtemp +string .Format ("{0,2}",""); //输出前面的空格
for(j=0;j<=i;j++)
{
icurline =fun(i);//当前行数的阶乘,从0开始
ibackline=fun(i-j); //行数-列数位置所构成的阶乘
iposition =fun(j);//行的第几个位置的阶乘
itemp =icurline /(ibackline*iposition );//具体位置杨辉三角的值
strtemp =strtemp +string .Format("{0,4}",itemp);
if(i==j)
strtemp=strtemp +"\n";
}
}
txtresult .Text=strtemp;
}
int fun(int n)
{
if (n == 0 || n == 1)
return 1;
else
return n * fun(n - 1);
}
private void timer1_Tick(object sender, EventArgs e)
{
}
}
}
标签:
原文地址:http://www.cnblogs.com/-slient/p/5116783.html