//************定义类的成员以及函数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace stack_push_pop
{
class Stack
{
private int[] arr;
private int sp;
private int count;
public Stack()
{
arr = new int[10];
count = 10;
sp = 0;
}
public Stack(int Length)
{
arr = new int[Length];
count = Length;
sp = 0;
}
public void push(int element)
{
if (sp > count)
{
MessageBox.Show("the satck is full");
}
else { arr[sp] = element; sp++; }
}
public int pop()
{
if (sp < 0)
{
MessageBox.Show("the stack is enpty");
return (0);
}
else { sp--; return (arr[sp]); }
}
}
static class Program
{
/// <summary>
/// 应®|用®?程¨¬序¨°的Ì?主¡Â入¨?口¨²点Ì?。¡ê
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
//**************窗体以及各个控件的 属性以及事件
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace stack_push_pop
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "cin the length of stack";
textBox1.Text = "10";
button1.Text = "push into the stack";
button2.Text = "refesh";
}
private void button1_Click(object sender, EventArgs e)
{
int k, m;
string str;
str = textBox1.Text;
m = Int32.Parse(str);// str 类型转化成int 类型的方法
Stack s1 = new Stack(m);
Random r1 = new Random();
for (int i = 0; i < m; i++)
{
k = r1.Next(10, 20);
s1.push(k);//调用实例化对象s1的函数push 形参为随机数k
label2.Text += k + " ";
}
for (int i = 0; i < m; i++)
{
k = s1.pop();//调用实例化对象s1的函数pop 没有参数只是返回一个int 类型的arr[sp]
label3.Text += k + " ";
}
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "";
label2.Text = "";
label3.Text = "";
}
}
}
收获:
1 定义类的成员以及函数
2 数据结构 栈的理解
3 字符类型到int 类型的转换
4 一定范围 的随机数的产生
浙公网安备 33010602011771号