728x90
1.
(1)
#include <iostream>
using namespace std;
int add(int a[], int n) {
int sum = 0;
for (int i = 0; i < n; i++) sum += a[i];
return sum;
}
int add(int a[], int n, int b[]) {
int sum = 0;
for (int i = 0; i < n; i++) sum += a[i] + b[i];
return sum;
}
int main() {
int a[] = {1, 2, 3, 4, 5};
int b[] = {6, 7, 8, 9, 10};
int c = add(a, 5);
int d = add(a, 5, b);
cout << c << endl;
cout << d << endl;
}
(2)
#include <iostream>
using namespace std;
int add(int a[], int n, int b[] = nullptr) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i];
if (b != nullptr) sum += b[i];
}
return sum;
}
int main() {
int a[] = {1, 2, 3, 4, 5};
int b[] = {6, 7, 8, 9, 10};
int c = add(a, 5);
int d = add(a, 5, b);
cout << c << endl;
cout << d << endl;
}
2.
(1)
#include <iostream>
#include <string>
using namespace std;
class Person {
int id;
double weight;
string name;
public:
Person() {
id = 1;
name = "Grace";
weight = 20.5;
}
Person(int id, string name) {
this->id = id;
this->name = name;
weight = 20.5;
}
Person(int id, string name, double weight) {
this->id = id;
this->name = name;
this->weight = weight;
}
void show() { cout << id << ' ' << weight << ' ' << name << endl; }
};
int main() {
Person grace, ashley(2, "Ashley"), helen(3, "Helen", 32.5);
grace.show();
ashley.show();
helen.show();
}
(2)
#include <iostream>
#include <string>
using namespace std;
class Person {
int id;
double weight;
string name;
public:
Person(int id = 1, string name = "Grace", double weight = 20.5) {
this->id = id;
this->name = name;
this->weight = weight;
}
void show() { cout << id << ' ' << weight << ' ' << name << endl; }
};
int main() {
Person grace, ashley(2, "Ashley"), helen(3, "Helen", 32.5);
grace.show();
ashley.show();
helen.show();
}
3.
(1)
#include <iostream>
using namespace std;
int big(int a, int b) {
if (a < b) a = b;
if (a > 100) return 100;
else return a;
}
int big(int a, int b, int max) {
if (a < b) a = b;
if (a > max) return max;
else return a;
}
int main() {
int x = big(3, 5);
int y = big(300, 60);
int z = big(30, 60, 50);
cout << x << ' ' << y << ' ' << z << endl;
}
(2)
#include <iostream>
using namespace std;
int big(int a, int b, int max = 100) {
if (a < b) a = b;
if (a > max) return max;
else return a;
}
int main() {
int x = big(3, 5);
int y = big(300, 60);
int z = big(30, 60, 50);
cout << x << ' ' << y << ' ' << z << endl;
}
4.
#include <iostream>
using namespace std;
class MyVector {
int *mem;
int size;
public:
MyVector(int n = 100, int val = 0);
~MyVector() { delete[] mem; }
void show() {
for (int i = 0; i < size; i++) cout << mem[i];
cout << endl;
}
};
MyVector::MyVector(int n, int val) {
mem = new int[n];
size = n;
for (int i = 0; i < size; i++) mem[i] = val;
}
int main() {
MyVector a;
MyVector b(50, 1);
a.show();
b.show();
}
5.
#include <iostream>
using namespace std;
class ArrayUtility {
public:
static void intToDouble(int source[], double dest[], int size) {
for (int i = 0; i < size; i++) dest[i] = (double)source[i];
}
static void doubleToInt(double source[], int dest[], int size) {
for (int i = 0; i < size; i++) dest[i] = (int)source[i];
}
};
int main() {
int x[] = {1, 2, 3, 4, 5};
double y[5];
double z[] = {9.9, 8.8, 7.7, 6.6, 5.6};
ArrayUtility::intToDouble(x, y, 5);
for (int i = 0; i < 5; i++) cout << y[i] << ' ';
cout << endl;
ArrayUtility::doubleToInt(z, x, 5);
for (int i = 0; i < 5; i++) cout << x[i] << ' ';
cout << endl;
}
6.
#include <iostream>
using namespace std;
class ArrayUtility2 {
public:
static int *concat(int s1[], int s2[], int size) {
int *arr = new int[size * 2];
int n = 0;
for (int i = 0; i < size; i++, n++) arr[n] = s1[i];
for (int i = 0; i < size; i++, n++) arr[n] = s2[i];
return arr;
}
static int *remove(int s1[], int s2[], int size, int &retSize) {
int *arr = new int[size];
bool t;
for (int i = 0; i < size; i++)
{
t = true;
for (int j = 0; j < size; j++)
{
if (s1[i] == s2[j])
{
t = false;
break;
}
}
if (t == true) {
arr[retSize++] = s1[i];
}
}
return arr;
}
};
int main()
{
cout << "정수를 5 개 입력하라. 배열 x에 삽입한다>>";
int x[5];
for (int i = 0; i < 5; i++) cin >> x[i];
cout << "정수를 5 개 입력하라. 배열 y에 삽입한다>>";
int y[5];
for (int i = 0; i < 5; i++) cin >> y[i];
cout << "합친 정수 배열을 출력한다" << endl;
int *arr = ArrayUtility2::concat(x, y, 5);
for (int i = 0; i < 10; i++) cout << arr[i] << ' ';
cout << endl;
delete[] arr;
int size = 0;
arr = ArrayUtility2::remove(x, y, 5, size);
cout << "배열 x[]에서 y[]를 뺀 결과를 출력한다. 개수는 " << size << endl;
for (int i = 0; i < size; i++) cout << arr[i] << ' ';
cout << endl;
delete[] arr;
}
7.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class Random {
public:
static void seed() { srand((unsigned)time(0)); }
static int nextInt(int min = 0, int max = 32767) {
return (rand() % (max - min + 1)) + min;
}
static char nextAlphabet() {
int r = rand() % 52;
if (r < 26) return 'a' + r;
else return 'A' + r - 26;
}
static double nextDouble() {
return rand() / 32767.;
}
};
int main()
{
Random::seed();
cout << "1에서 100까지 랜덤한 정수 10개를 출력합니다" << endl;
for (int i = 0; i < 10; i++) cout << Random::nextInt(1, 100) << ' ';
cout << endl;
cout << "알파벳을 랜덤하게 10개를 출력합니다" << endl;
for (int i = 0; i < 10; i++) cout << Random::nextAlphabet() << ' ';
cout << endl;
cout << "랜덤한 실수를 10개를 출력합니다" << endl;
for (int i = 0; i < 5; i++) cout << Random::nextDouble() << ' ';
cout << endl;
for (int i = 0; i < 5; i++) cout << Random::nextDouble() << ' ';
cout << endl;
}
8.
#include <iostream>
#include <string>
using namespace std;
class Trace {
public:
static int max, num;
static string trace[100][2];
static void put(string tag, string ifo) {
if (max > num) {
trace[num][0] = tag;
trace[num++][1] = ifo;
}
}
static void print(string tag = "all") {
if (tag == "all") {
cout << "----- 모든 Trace 정보를 출력합니다. -----" << endl;
for (int i = 0; i < num; i++) {
cout << trace[i][0] << ":" << trace[i][1] << endl;
}
}
else {
cout << "----- " << tag << "태그의 Trace 정보를 출력합니다. -----" << endl;
for (int i = 0; i < num; i++) {
if (trace[i][0] == tag) {
cout << trace[i][0] << ":" << trace[i][1] << endl;
}
}
}
}
};
int Trace::max = 100;
int Trace::num = 0;
string Trace::trace[100][2] = {};
void f() {
int a, b, c;
cout << "두 개의 정수를 입력하세요>>";
cin >> a >> b;
Trace::put("f()", "정수를 입력 받았음");
c = a + b;
Trace::put("f()", "합 계산");
cout << "합은 " << c << endl;
}
int main() {
Trace::put("main()", "프로그램 시작합니다");
f();
Trace::put("main()", "종료");
Trace::print("f()");
Trace::print();
}
9.
#include <iostream>
#include <string>
using namespace std;
class Board {
public:
static string text[100];
static int num;
static void add(string t) { text[num++] = t; }
static void print() {
cout << "************* 게시판입니다. *************" << endl;
for (int i = 0; i < num; i++) {
cout << i << ": " << text[i] << endl;
}
cout << endl;
}
};
string Board::text[100] = {};
int Board::num = 0;
int main()
{
Board::add("중간고사는 감독 없는 자율 시험입니다.");
Board::add("코딩 라운지 많이 이용해 주세요.");
Board::print();
Board::add("진소린 학생이 경진대회 입상하였습니다. 축하해주세요");
Board::print();
}
728x90
'C++ > 명품 C++ Programming' 카테고리의 다른 글
명품 C++ 7장 연습문제 - 실습 문제 (0) | 2022.01.26 |
---|---|
명품 C++ 7장 연습문제 - 이론 문제 (0) | 2022.01.24 |
명품 C++ 6장 연습문제 - 이론 문제 (0) | 2022.01.12 |
명품 C++ 5장 연습문제 - 실습 문제 (0) | 2022.01.10 |
명품 C++ 5장 연습문제 - 이론 문제 (5) | 2022.01.08 |
댓글