728x90
********************************* Generic 안 썼을때 막 코드 *********************************
1. Stack 스택
제한적으로 접근할 수 있는 나열 구조
여러가지 아이템을 먹었을 때
먼저 먹은 아이템부터 사용하고 싶을때 ex) 카트라이더
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CStackInt
{
int emptyStack;
int[] items;
int top;
int size;
public CStackInt(int size)
{
this.size = size;
items = new int[size];
emptyStack = -1;
top = emptyStack;
}
public void Push(int item)
{
items[++top] = item;
}
public int Pop()
{
return items[top--];
}
public bool IsFull()
{
return (top + 1) == size;
}
public bool IsEmpty()
{
return top == emptyStack;
}
}
public class NoneGenericStack : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
2. int 스택 생성
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CStackInt
{
int emptyStack;
int[] items;
int top;
int size;
public CStackInt(int size)
{
this.size = size;
items = new int[size];
emptyStack = -1;
top = emptyStack;
}
public void Push(int item)
{
items[++top] = item;
}
public int Pop()
{
return items[top--];
}
public bool IsFull()
{
return (top + 1) == size;
}
public bool IsEmpty()
{
return top == emptyStack;
}
}
public class NoneGenericStack : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
int size = 5;
CStackInt istack = new CStackInt(size);
for (int i = 0; i<7; i++)
{
if(!istack.IsFull())
{
istack.Push(i);
}
else
{
Debug.Log("스택이 꽉 찼습니다.!!!");
}
}
}
// Update is called once per frame
void Update()
{
}
}
3. 스택 뽑기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CStackInt
{
int emptyStack;
int[] items;
int top;
int size;
public CStackInt(int size)
{
this.size = size;
items = new int[size];
emptyStack = -1;
top = emptyStack;
}
public void Push(int item)
{
items[++top] = item;
}
public int Pop()
{
return items[top--];
}
public bool IsFull()
{
return (top + 1) == size;
}
public bool IsEmpty()
{
return top == emptyStack;
}
}
public class NoneGenericStack : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
int size = 5;
CStackInt istack = new CStackInt(size);
for (int i = 0; i<7; i++)
{
if(!istack.IsFull())
{
istack.Push(i);
}
else
{
Debug.Log("스택이 꽉 찼습니다.!!!");
}
}
while (!istack.IsEmpty())
{
Debug.Log(istack.Pop());
}
}
// Update is called once per frame
void Update()
{
}
}
3. float
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CStackInt
{
int emptyStack;
int[] items;
int top;
int size;
public CStackInt(int size)
{
this.size = size;
items = new int[size];
emptyStack = -1;
top = emptyStack;
}
public void Push(int item)
{
items[++top] = item;
}
public int Pop()
{
return items[top--];
}
public bool IsFull()
{
return (top + 1) == size;
}
public bool IsEmpty()
{
return top == emptyStack;
}
}
//float
public class CStackFloat
{
int emptyStack;
float[] items;
int top;
int size;
public CStackFloat(int size)
{
this.size = size;
items = new float[size];
emptyStack = -1;
top = emptyStack;
}
public void Push(float item)
{
items[++top] = item;
}
public float Pop()
{
return items[top--];
}
public bool IsFull()
{
return (top + 1) == size;
}
public bool IsEmpty()
{
return top == emptyStack;
}
}
public class NoneGenericStack : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
int size = 5;
//IntStackTest(size);
FloatStackTest(size);
}
void FloatStackTest(int size)
{
CStackFloat fstack = new CStackFloat(size);
for (int i = 0; i < 7; i++)
{
if (!fstack.IsFull())
{
float data = Random.Range(1f, 30f);
Debug.Log("istack Push : " + i);
fstack.Push(i);
}
else
{
Debug.Log("스택이 꽉 찼습니다.!!!");
}
}
while (!fstack.IsEmpty())
{
Debug.Log(fstack.Pop());
}
}
void IntStackTest(int size)
{
CStackInt istack = new CStackInt(size);
for (int i = 0; i < 7; i++)
{
if (!istack.IsFull())
{
float data = Random.Range(1f, 30f);
Debug.Log("istack Push : " + i);
istack.Push(i);
}
else
{
Debug.Log("스택이 꽉 찼습니다.!!!");
}
}
while (!istack.IsEmpty())
{
Debug.Log(istack.Pop());
}
}
}
4. string
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CStackInt
{
int emptyStack;
int[] items;
int top;
int size;
public CStackInt(int size)
{
this.size = size;
items = new int[size];
emptyStack = -1;
top = emptyStack;
}
public void Push(int item)
{
items[++top] = item;
}
public int Pop()
{
return items[top--];
}
public bool IsFull()
{
return (top + 1) == size;
}
public bool IsEmpty()
{
return top == emptyStack;
}
}
//float
public class CStackFloat
{
int emptyStack;
float[] items;
int top;
int size;
public CStackFloat(int size)
{
this.size = size;
items = new float[size];
emptyStack = -1;
top = emptyStack;
}
public void Push(float item)
{
items[++top] = item;
}
public float Pop()
{
return items[top--];
}
public bool IsFull()
{
return (top + 1) == size;
}
public bool IsEmpty()
{
return top == emptyStack;
}
}
//string
public class CStackString
{
int emptyStack;
string[] items;
int top;
int size;
public CStackString(int size)
{
this.size = size;
items = new string[size];
emptyStack = -1;
top = emptyStack;
}
public void Push(string item)
{
items[++top] = item;
}
public string Pop()
{
return items[top--];
}
public bool IsFull()
{
return (top + 1) == size;
}
public bool IsEmpty()
{
return top == emptyStack;
}
}
public class NoneGenericStack : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
int size = 5;
IntStackTest(size);
FloatStackTest(size);
StringStackTest(size);
}
void StringStackTest(int size)
{
CStackString sstack = new CStackString(size);
for (int i = 0; i < 7; i++)
{
if (!sstack.IsFull())
{
Debug.Log("istack Push : " + "숫자" + (i + 1));
sstack.Push("숫자" + (i + 1));
}
else
{
Debug.Log("스택이 꽉 찼습니다.!!!");
}
}
while (!sstack.IsEmpty())
{
Debug.Log(sstack.Pop());
}
}
void FloatStackTest(int size)
{
CStackFloat fstack = new CStackFloat(size);
for (int i = 0; i < 7; i++)
{
if (!fstack.IsFull())
{
float data = Random.Range(1f, 30f);
Debug.Log("fstack Push : " + data);
fstack.Push(data);
}
else
{
Debug.Log("스택이 꽉 찼습니다.!!!");
}
}
while (!fstack.IsEmpty())
{
Debug.Log(fstack.Pop());
}
}
void IntStackTest(int size)
{
CStackInt istack = new CStackInt(size);
for (int i = 0; i < 7; i++)
{
if (!istack.IsFull())
{
Debug.Log("istack Push : " + i);
istack.Push(i);
}
else
{
Debug.Log("스택이 꽉 찼습니다.!!!");
}
}
while (!istack.IsEmpty())
{
Debug.Log(istack.Pop());
}
}
}
********************************* Generic 코드 *********************************
아이템 타입이 달라도 <T> 클래스 하나로 사용 가능!!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//제네릭 메소드
public class CStack<T>
{
int emptyStack;
T[] items;
int top;
int size;
public CStack(int size)
{
this.size = size;
items = new T[size];
emptyStack = -1;
top = emptyStack;
}
public void Push(T item)
{
items[++top] = item;
}
public T Pop()
{
return items[top--];
}
public bool IsFull()
{
return (top + 1) == size;
}
public bool IsEmpty()
{
return top == emptyStack;
}
}
public class GenericStack : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
int size = 5;
IntStackTest(size);
FloatStackTest(size);
StringStackTest(size);
}
void StringStackTest(int size)
{
CStack<string> sstack = new CStack<string>(size);
for (int i = 0; i < 7; i++)
{
if (!sstack.IsFull())
{
Debug.Log("istack Push : " + "숫자" + (i + 1));
sstack.Push("숫자" + (i + 1));
}
else
{
Debug.Log("스택이 꽉 찼습니다.!!!");
}
}
while (!sstack.IsEmpty())
{
Debug.Log(sstack.Pop());
}
}
void FloatStackTest(int size)
{
CStack<float> fstack = new CStack<float>(size);
for (int i = 0; i < 7; i++)
{
if (!fstack.IsFull())
{
float data = Random.Range(1f, 30f);
Debug.Log("fstack Push : " + data);
fstack.Push(data);
}
else
{
Debug.Log("스택이 꽉 찼습니다.!!!");
}
}
while (!fstack.IsEmpty())
{
Debug.Log(fstack.Pop());
}
}
void IntStackTest(int size)
{
CStack<int> istack = new CStack<int>(size);
for (int i = 0; i < 7; i++)
{
if (!istack.IsFull())
{
Debug.Log("istack Push : " + i);
istack.Push(i);
}
else
{
Debug.Log("스택이 꽉 찼습니다.!!!");
}
}
while (!istack.IsEmpty())
{
Debug.Log(istack.Pop());
}
}
}
'Unity > C# 문법' 카테고리의 다른 글
[유니티] Mathf (0) | 2021.05.06 |
---|---|
[유니티] 3D 게임 기본문법 (0) | 2021.05.04 |
[유니티] 객체 배열/업캐스팅 (0) | 2021.04.27 |
[유니티] 상속 문법(부모/자식 class) (0) | 2021.04.27 |
[유니티] 데이터 은닉(캡슐화)+생성자 (0) | 2021.04.27 |
댓글