본문 바로가기

언어/C++

공 게임 !

#include <iostream>
#include <string>
using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
class ArrayUtility{
	
	public :
		static int*concat (int s1[], int s2[], int size){
			int *s3=new int [size];
			int j=0;
			for(int i=0; i<(size/2); i++){
				s3[j]=s1[i];
				j++;
			}
			for(int i=0; i<(size/2); i++){
				s3[j]=s2[i];
				j++;
			}		
			return s3;	
		}
	
		static int* remove(int s1[], int s2[], int size, int& retSize){
			
			
		// NULL 값으로 다른 값 비교해주기  
			
			for(int i=0; i<size; i++){
				for(int j=0; j<size; j++){
					if(s1[i]== s2[j]){
						s1[i]=NULL;
					}
				}
			}
			
		// 다른 값이면 RETSIZE RKQT 증가 해주기  
			
			for(int i=0; i<size; i++){
				if(s1[i]!=NULL) retSize++;
			}
			
		// retsize 만큼의 s3 배열 만들어주기 
		// s3 배열에 s1에 null 아닌 값 배열 원소들 넣어주기  
			int *s3= new int[retSize];
			int j=0;
			 
			for(int i=0; i<size; i++){
				if(s1[i]!=NULL){
					s3[j]=s1[i];
					j++;
				}
			}
			
			if (retSize ==0 ) return NULL;
			else return s3;
			
			
		}
	
};

int main(int argc, char** argv) {
	
	int x[5];
	int y[5];
	int num=0;
	int size;
	
	cout << "정수를 5개 입력하라 배열 x에 삽입한다 >> ";
	for (int i=0; i<5; i++){
		cin >> x[i];
	} 
	
	cout << "정수를 5개 입력하라. 배열 y에 삽입한다 >> ";
	for (int i=0; i<5; i++){
		cin >> y[i];
	}
		
	size=sizeof(x)/4 + sizeof(y)/4;
	cout << "합친 정수 배열을 출력한다.";
	
	int *s=ArrayUtility :: concat(x,y,size);
	
	for(int i=0; i<size; i++){
		cout <<s[i] << ' ';
	}
		
	size= sizeof(x)/4;
	cout << "배열 x[]에서 y[]를 뺀 결과를 출력한다.";
	s=ArrayUtility :: remove( x, y, size, num);
	
	for(int i=0; i<num; i++){
		cout << s[i] << ' ';
	}		
		
	return 0;
}