搜档网
当前位置:搜档网 › C#练习题及答案

C#练习题及答案

泛型堆栈类

1、创建C#控制台应用程序
2、新建一个泛型的堆栈类CzStack。堆栈是指这样一种数据结构:它管理着一个集合,每次只能删除或访问集合中的第一个元素,新加入集合的元素总是放在集合的最前面。这里T表示堆栈的元素类型。
3、为CzStack定义一个T[]类型的字段_list,表示堆栈中的元素集合:以及一个uint类型的字段_current,表示堆栈的顶部元素位置(即当前堆栈大小,初始值为0,表示堆栈为空。)
4、定义CzStack的构造函数,通过它可以创建指定容量的堆栈对象。
5、定义CzStack的公有属性,通过Size属性返回堆栈的容量,通过Current返回当前大小,通过IsEmpty判断堆栈是否为空、IsFull判断堆栈是否已满。
6、为CzStack定义一个Push方法,用于向堆栈顶部加入元素,加入成功返回true,否则返回false。
7、类似的,为CzStack定义一个Top属性和一个Pop方法,其中属性直接返回堆栈顶部元素,而Pop方法弹出并返回堆栈当前的顶部元素。
8、在程序主方法中创建元素为int类型的堆栈对象,编写代码来实现整数的入栈和出栈,并编译运行程序来检测其效果。
9、类似的,在程序方法中再创建double类型的堆栈对象,编写代码来实现整数的入栈和出栈,并编译运行程序来检测其效果。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace 反转
{
static class Program
{
///


/// 应用程序的主入口点。
///

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}

public class UseerString
{
static public string Reverse(string s)
{
string s1 = "";
char[] temp = s.ToCharArray();
for (int i = temp.Length - 1; i >= 0; i--)
{
s1 = s1 + temp[i];
}
return s1;
}
static public bool IsPalindrome(string s)
{
char[] temp = s.ToCharArray();
for (int i = 0; i < temp.Length; i++)
{
if (temp[i] != temp[temp.Length - i-1])
{
return false;
}
}
return true;
}
}
}



using System;
using System.Collections.Generic;
using https://www.sodocs.net/doc/4b9391292.html,ponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 反转
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_C

lick(object sender, EventArgs e)
{
textBox2.Text = UseerString.Reverse(textBox1.Text);
}

private void button2_Click(object sender, EventArgs e)
{
if (UseerString.IsPalindrome(textBox1.Text))
{
MessageBox.Show("是回文数!");
}
else
{
MessageBox.Show("不是回文数!");
}
}
}
}









using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MachengDong
{
class MaChengDong
{
static void Main(string[] args)
{
CzStack t1 = new CzStack(6);
for (int i = 0; i < 6; i++)
{
t1.Push(i + 1);
}
Console.WriteLine("堆栈容量:{0}\n堆栈大小:{1}\n是否满:{2}\n顶部元素:{3}\n当前顶部元素:{4}",
t1.Size, t1.Current, t1.IsFull, t1.Top, t1.Pop());

CzStack t2 = new CzStack(6);
for (int i = 0; i < 5; i++)
{
t2.Push(i * 1.1);
}
Console.WriteLine("堆栈容量:{0}\n堆栈大小:{1}\n是否满:{2}\n顶部元素:{3}\n当前顶部元素:{4}",
t2.Size, t2.Current, t2.IsFull, t2.Top, t2.Pop());
Console.ReadLine();
}
}

class CzStack
{
T[] _list;
uint _current = 0;

public CzStack(uint length)
{
_list = new T[length];
}

public int Size
{
get
{
return _list.Length;
}
}
public uint Current
{
get
{
return _current;
}
}
public bool IsEmpty
{
get
{
if (_current == 0)
{
return true;
}
return false;
}
}
public bool IsFull
{
get
{
if (_current == _list.Length)
{
return true;
}
return false;
}
}

public bool Push(T l)
{
if (!IsFull)
{
_list[_current] = l;
_current++;
return true;
}
else
{
return false;
}
}

public T Top
{
get
{
return _list[_current - 1];
}
}

public T Pop()
{
_list[--_current ] = default(T);
return _list[_current - 1];
}
}
}


相关主题