实 验 集合的使用

来源:互联网 发布:江西seo 编辑:程序博客网 时间:2024/06/06 14:11

教学目标:

1、  熟练掌握ArrayList的使用

2、  熟练掌握HashTable的使用

实验内容:

 1、使用ArrayList定义MyStack实现栈的结构。

要求如下:

(1)返回栈顶元素的方法Peek

(2)入栈方法Push

(3)出栈方法Pop

(4)是否包含某个元素Contains

(5)清空栈Clear

(6)三个构造函数,无参数,有参数(根据Capacity容量来实例化),有参数(从指定集合复制元素)

(7)栈的元素个数(属性)Count

(8)栈的容量(属性)Capacity

(9)在main函数中测试

参考代码如下:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Collections;namespace ConsoleApplication1{    class myStack    {        ArrayList myList;        public myStack()        {            myList = new ArrayList(10);        }        public myStack(int capacity)        {            myList = new ArrayList(capacity);        }        public myStack(ArrayList fromList)        {            myList = new ArrayList(fromList.Count);            myList = (ArrayList)fromList.Clone();        }        public int peek()        {            return (int)myList[myList.Count - 1];        }        public void push(int num)        {            myList.Add(num);        }        public int pop()        {            int returnValue = (int)myList[myList.Count - 1];            myList.RemoveAt(myList.Count - 1);            return returnValue;        }        public bool contains(int num)        {            return myList.Contains(num);        }        public void clear()        {            int count = myList.Count;            for (int i = 0; i < count; i++)                myList.RemoveAt(0);        }        public int count        {            get { return myList.Count; }        }        public int capacity        {            get { return myList.Capacity; }        }    }    class Program    {        static void Main(string[] args)        {            myStack stack1 = new myStack();            stack1.push(23);            stack1.push(40);            Console.WriteLine(stack1.peek());            Console.WriteLine(stack1.pop());            stack1.push(34);            Console.WriteLine(stack1.pop());            Console.WriteLine(stack1.pop());            Console.ReadLine();        }    }}

2、(1)编写一个电脑信息类:包括电脑的型号、价格和出厂日期。程序应根据用户输入的型号来显示相应的电脑信息。

(2)供应商类:使用Hashtable对象存储电脑信息

        可以添加电脑信息

        根据型号查看电脑信息:输入一个型号,程序检查判断集合中是否存在该型号的电脑,如果存在则显示具体信息,不存在给出提示信息

        可以通过输入型号,删除某型号的电脑

        按型号对电脑信息进行排序

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Collections;namespace ConsoleApplication3{    class Computer    {        public string type;        public double price;        public DateTime fromFactory;        public Computer(string type, double price, DateTime time)        {            this.type = type;            this.price = price;            this.fromFactory = time;        }    }    class Supplier    {        public Hashtable computerHT=new Hashtable(100);        public Supplier()        { }        public Supplier(Hashtable computers)        {            computerHT = computers;        }        public void add(Computer cmp)        {            computerHT.Add(cmp.type, cmp);        }        public void search(string vartype)        {            bool flag=false;            foreach (DictionaryEntry de in computerHT)            {                if (de.Key == vartype)                {                    flag = true;                    Computer cmp = (Computer)de.Value;                    Console.WriteLine("type:{0},price:{1}, Time:{2}", cmp.type, cmp.price, cmp.fromFactory);                }            }            if (flag == false)                Console.WriteLine("no found");        }        public void delete(string vartype)        {            computerHT.Remove(vartype);        }        public void sort()        {            ArrayList newList = new ArrayList(computerHT.Keys);            newList.Sort();            for (int i = 0; i < newList.Count; i++)            {                Computer cmp = (Computer)computerHT[newList[i]];                Console.WriteLine("type:{0},price:{1}, Time:{2}",newList[i],cmp.price, cmp.fromFactory);            }        }    }        class Program    {        static void Main(string[] args)        {                        Computer com1=new Computer("zxf1", 2300.0, DateTime.Now);            Computer com2 = new Computer("zx2", 3300.0, DateTime.Now);            Computer com3 = new Computer("za2", 3300.0, DateTime.Now);            Supplier sp1 = new Supplier();            sp1.add(com1);            sp1.add(com2);            sp1.add(com3);            sp1.sort();            Console.ReadLine();        }    }}

选做:

1、用C#模拟实现扑克牌发牌、排序程序。

(1)52张扑克牌,四种花色(红桃、黑桃、方块和梅花),随机发牌给四个人。

(2)最后将四个人的扑克牌包括花色打印在控制台上。

其中:

  花色和点数用枚举类型实现

  每张扑克牌用结构实现

提示:可以用ArrayList初始化52张扑克牌,然后从这个链表中随机取牌发给四个玩家,直到链表为空为止。

参考程序:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Collections;namespace joke{    class Program    {        enum HuaSe : int        {             FangPian, HeiTao, HongTao, MeiHua        }        struct Joke        {            public int point;            public HuaSe hs;            public Joke(int point, HuaSe hs)            {                this.point = point;                this.hs = hs;            }        }        static void Main(string[] args)        {            ArrayList list = new ArrayList();            for (int i = 0; i < 13; i++)            {                for (int j = 0; j < 4; j++)                {                    HuaSe hh=HuaSe.FangPian;                    switch (j)                    {                        case 0: hh = HuaSe.FangPian; break;                        case 1: hh = HuaSe.HeiTao; break;                        case 2: hh = HuaSe.HongTao; break;                        case 3: hh = HuaSe.MeiHua; break;                    }                    Joke jk = new Joke(i + 1, hh);                    list.Add(jk);                }            }                      Random r = new Random();            int mm = 0;            while (list.Count != 0)            {                int nn = 0;                Console.WriteLine("第{0}个人的牌",mm+1);                while (nn < 13)                {                    int index = r.Next(list.Count - 1);                    Joke jj = (Joke)list[index];                    Console.Write("{0}-{1}\t",jj.hs,jj.point);                    list.RemoveAt(index);                    nn++;                }                nn = 0; mm++;                Console.WriteLine();            }            Console.ReadLine();        }    }}


0 0
原创粉丝点击