1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #include <iostream> using namespace std; class ArrayUtility2 { public: static int* concat(int s1[], int s2[], int size); static int* remove(int s1[], int s2[], int size, int& retSize); }; int* ArrayUtility2::concat(int s1[], int s2[], int size) { int *buf = new int[size * 2]; for (int i = 0; i < size * 2; i++) { i < size ? buf[i] = s1[i] : buf[i] = s2[i - size]; } return buf; } int* ArrayUtility2::remove(int s1[], int s2[], int size, int& retSize) { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (s1[i] == s2[j]) s1[i] = NULL; } } for (int i = 0; i < size; i++) if (s1[i] != NULL) retSize++; if (retSize == 0) return NULL; int *buf = new int[retSize]; int n = 0; for (int i = 0; i < size; i++) { if (s1[i] != NULL) { buf[n] = s1[i]; n++; } } return buf; } int main() { int x[5], y[5]; int *z; int retSize = 0; 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]; cout << "합친 정수 배열을 출력한다" << endl; z = ArrayUtility2::concat(x, y, 5); for (int i = 0; i < 10; i++) { cout << z[i] << ' '; } cout << endl; z = ArrayUtility2::remove(x, y, 5, retSize); cout << "배열 x[]에서 y[]를 뺀 결과를 출력한다. 개수는 " << retSize << endl; if (retSize == NULL) cout << "두 배열이 같습니다." << endl; else { for (int i = 0; i < retSize; i++) { cout << z[i] << ' '; } cout << endl; } } | cs |
'Programming > 명품 C++ Programming' 카테고리의 다른 글
| 명품 C++ Programming 실습문제 6장 8번 (0) | 2018.06.16 |
|---|---|
| 명품 C++ Programming 실습문제 6장 7번 (0) | 2018.05.26 |
| 명품 C++ Programming 실습문제 6장 5번 (0) | 2018.05.10 |
| 명품 C++ Programming 실습문제 6장 4번 (0) | 2018.05.10 |
| 명품 C++ Programming 실습문제 6장 3번 (0) | 2018.05.10 |