C#프로그래밍
배열 연습 Inventory(정렬 안함, 정렬함 + 아이템 개수 스택)
EasyCastleT
2023. 7. 24. 16:54
정렬 안 함
아이템
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnDotnet
{
internal class Item
{
public string Name { get; set; } //속성
//생성자
public Item(string name)
{
this.Name = name;
// Console.WriteLine("{0}이 생성되었습니다",this.Name);
}
}
}
인벤토리
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnDotnet
{
internal class Inventory
{
//멤버변수
private int capacity;
private Item[] items;
private Item searchItem;
private int num = 0;
//생성자
public Inventory(int capacity)
{
this.capacity = capacity;
items = new Item[this.capacity];
}
//멤버 함수
//아이템을 배열에 저장하는 함수
public void AddItem(Item item)
{
items[num] = item;
num++;
if(this.capacity < num) { num = 0; }
}
//인벤토리 현재 상태를 보여주는 함수
public void PrintAllItems()
{
for(int i=0; i<items.Length; i++)
{
if (items[i] != null)
{
Console.WriteLine("{0}", items[i].Name);
}
else
{
Console.WriteLine("[ {0} ]", null);
}
}
Console.WriteLine("--------------------------");
}
//이름으로 아이템을 인벤토리에서 빼는 함수
public Item GetItemByName(string name)
{
for(int i=0; i < items.Length; i++)
{
if (items[i] != null)
{
if (items[i].Name == name)
{
searchItem = items[i];
items[i] = null;
}
}
}
return searchItem;
}
}
}
실행
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnDotnet
{
internal class App
{
//생성자
public App()
{
Inventory inven = new Inventory(5);
inven.AddItem(new Item("장검"));
inven.AddItem(new Item("단검"));
inven.AddItem(new Item("활"));
inven.PrintAllItems();
string searchName = "장검";
Item item = inven.GetItemByName(searchName);
if (item != null)
{
Console.WriteLine("{0}을 꺼냈습니다.",item.Name);
Console.WriteLine("--------------------------");
}
else
{
Console.WriteLine("{0}을 찾을수 없습니다.",searchName);
Console.WriteLine("--------------------------");
}
inven.PrintAllItems();
}
}
}
정렬함
인벤토리만 차이 있음
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnDotnet
{
internal class Inventory
{
//멤버변수
private int capacity;
private Item[] items;
private Item searchItem;
private int num = 0;
//생성자
public Inventory(int capacity)
{
this.capacity = capacity;
items = new Item[this.capacity];
}
//멤버 함수
//아이템을 배열에 저장하는 함수
public void AddItem(Item item)
{
items[num] = item;
num++;
if(this.capacity < num) { num = 0; }
}
//인벤토리 현재 상태를 보여주는 함수
public void PrintAllItems()
{
for(int i=0; i<items.Length; i++)
{
if (items[i] != null)
{
Console.WriteLine("{0}", items[i].Name);
}
else
{
Console.WriteLine("[ {0} ]", null);
}
}
Console.WriteLine("--------------------------");
}
//이름으로 아이템을 인벤토리에서 빼는 함수
public Item GetItemByName(string name)
{
for(int i=0; i < items.Length; i++)
{
if (items[i] != null)
{
if (items[i].Name == name)
{
searchItem = items[i];
items[i] = null;
}
}
}
//인벤토리 정렬 하는 부분
//모든 요소들을 하나 앞으로 보낸다.
//하나씩 넣었을 때만 기능
for(int i=1; i < items.Length; i++)
{
items[i - 1] = items[i];
}
return searchItem;
}
}
}
스택
아이템
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnDotnet
{
internal class Item
{
//멤버 변수
public int itemCount = 0;
public string Name { get; set; } //속성
//생성자
public Item(string name)
{
this.Name = name;
// Console.WriteLine("{0}이 생성되었습니다",this.Name);
}
}
}
인벤토리
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace LearnDotnet
{
internal class Inventory
{
//멤버변수
private int capacity;
private Item[] items;
private Item searchItem;
private int num = 0;
private int itemCount = 0;
//생성자
public Inventory(int capacity)
{
this.capacity = capacity;
items = new Item[this.capacity];
}
//멤버 함수
//아이템을 배열에 저장하는 함수
public void AddItem(Item item)
{
for (int i = 0; i < items.Length; i++)
{
if (this.items[i] == null)
{
this.items[i] = item;
this.items[i].itemCount++;
// Console.WriteLine("{0} 추가", this.items[i].Name);
break;
}
else if (this.items[i].Name == item.Name)
{
this.items[i].itemCount++;
// Console.WriteLine("{0} 추가", this.items[i].Name);
break;
}
}
}
//인벤토리 현재 상태를 보여주는 함수
public void PrintAllItems()
{
for (int i = 0; i < items.Length; i++)
{
if (items[i] != null)
{
Console.WriteLine("{0}X{1}", this.items[i].Name, this.items[i].itemCount);
}
else
{
Console.WriteLine("[ {0} ]", null);
}
}
Console.WriteLine("--------------------------");
}
//이름으로 아이템을 인벤토리에서 빼는 함수
public Item GetItemByName(string name)
{
int emptyNumber = 0;
for (int i = 0; i < items.Length; i++)
{
if (items[i] != null)
{
if (items[i].Name == name)
{
emptyNumber = i;
searchItem = items[i];
items[i].itemCount--;
if (this.items[i].itemCount == 0)
{
this.items[i] = null;
}
}
}
}
//앞으로 정렬
Item[] newItems = new Item[this.capacity];
int itemPointer = 0;
for (int i = 0; i < this.items.Length; i++)
{
if (items[i] != null)
{
newItems[itemPointer] = items[i];
itemPointer++;
}
}
items = newItems;
return searchItem;
}
}
}
실행
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnDotnet
{
internal class App
{
//생성자
public App()
{
Inventory inven = new Inventory(5);
inven.AddItem(new Item("장검"));
inven.AddItem(new Item("장검"));
inven.AddItem(new Item("장검"));
inven.AddItem(new Item("단검"));
inven.AddItem(new Item("활"));
inven.PrintAllItems();
string searchName = "장검";
Item item = inven.GetItemByName(searchName);
if (item != null)
{
Console.WriteLine("{0}을 꺼냈습니다.",item.Name);
Console.WriteLine("--------------------------");
}
else
{
Console.WriteLine("{0}을 찾을수 없습니다.",searchName);
Console.WriteLine("--------------------------");
}
inven.PrintAllItems();
}
}
}