유니티3D 프로그래밍

C# 2주차 3일 수업 과제 : Stack (21.03.17) 본문

C#/수업과제

C# 2주차 3일 수업 과제 : Stack (21.03.17)

tjdgus9955 2021. 3. 17. 17:47

Stack

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

namespace Study01
{
    class App
    {
        public App()
        {
            Console.WriteLine("App");

            Stack<int> num = new Stack<int>();

            for(int i = 0; i < 10; i++)
            {
                num.Push(i);
            }

            foreach(int count in num)
            {
                Console.Write("{0} ",count);
            }
            Console.WriteLine();
            int num1 = num.Pop();

            foreach (int count in num)
            {
                Console.Write("{0} ", count);
            }
        }
    }
}

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

namespace Study01
{
    class App
    {
        public App()
        {
            Console.WriteLine("App");

            Stack<Item> items = new Stack<Item>();

            Item item = new Item("체력포션1");
            Item item1 = new Item("체력포션2");
            Item item2 = new Item("체력포션3");
            Item item3 = new Item("체력포션4");
            Item item4 = new Item("체력포션5");

            items.Push(item);
            items.Push(item1);
            items.Push(item2);
            items.Push(item3);
            items.Push(item4);

            foreach (Item count in items)
            {
                Console.WriteLine("{0} ", count.GetName());
            }
            Console.WriteLine();
            items.Pop();

            foreach (Item count in items)
            {
                Console.WriteLine("{0} ", count.GetName());
            }
            Console.WriteLine();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study01
{
    public class Item
    {
        private string name;
        public Item(string name)
        {
            this.name = name;
        }

        public string GetName()
        {
            return this.name;
        }
    }
}

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

namespace Study01
{
    class App
    {
        public App()
        {
            Console.WriteLine("App");

            Inventory inven = new Inventory();

            Item item = new Item("체력포션");
            Item item1 = new Item("마력포션");
            Item item2 = new Item("표창");
            Item item3 = new Item("폭탄");

            inven.SetItemInventory(item);
            inven.SetItemInventory(item1);
            inven.SetItemInventory(item2);
            inven.SetItemInventory(item3);

            //인벤토리 내의 아이템의 이름 불러오기
            Console.WriteLine(inven.GetItem(0).GetName());
            Console.WriteLine(inven.GetItem(1).GetName());
            Console.WriteLine(inven.GetItem(2).GetName());
            Console.WriteLine(inven.GetItem(3).GetName());


            //인벤토리 내의 아이템 사용하기
            inven.GetItem(0).Use();
            inven.GetItem(1).Use();
            inven.GetItem(2).Use();
            inven.GetItem(3).Use();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study01
{
    public class Inventory
    {
        List<Item> item = new List<Item>();
        public Inventory()
        {
            
        }
        
        //인벤토리에 아이템 추가
        public void SetItemInventory(Item item)
        {
            this.item.Add(item);
        }

        //인벤토리에 있는 아이템 불러오기
        public Item GetItem(int index)
        {
            return item[index];
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study01
{
    public class Item : Inventory, IUse
    {
        private string name;
        public Item(string name)
        {
            this.name = name;
        }

        public string GetName()
        {
            return this.name;
        }

        public void Use()
        {
            Console.WriteLine("{0} 아이템을 사용합니다.", this.name);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study01
{
    interface IUse
    {
        void Use();
    }
}

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

namespace Study01
{
    class App
    {
        public App()
        {
            Console.WriteLine("App");

            Item item = new Item("장검");
            Item item1 = new Item("단검");

            //인벤토리에 아이템 추가
            Inventory inventory = new Inventory(item);
            
            //인벤토리의 아이템 출력
            Console.WriteLine(inventory.ItemName.GetName());

            //인벤토리에 아이템 변경
            inventory.ItemName = item1;

            //인벤토리의 아이템 출력
            Console.WriteLine(inventory.ItemName.GetName());
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study01
{
    public class Inventory
    {
        private Item itemName;

        public Item ItemName
        {
            get
            {
                return itemName;
            }
            set
            {
                itemName = value;
            }
        }

        public Inventory(Item itemName)
        {
            this.itemName = itemName;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Study01
{
    public class Item 
    {
        private string name;
        public Item(string name)
        {
            this.name = name;
        }

        public string GetName()
        {
            return this.name;
        }
    }
}