명품 C++ 프로그래밍 6장 연습문제

Written by 박민영 on Mar 8th, 2022 Views Report Post

6-1

(1)

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

int add(int a[], int b)
{
	int n=0;
	for (int i = 0; i < b; i++)
		n += a[i];
	return n;
}

int add(int a[], int b, int c[])
{
	return add(a, b) + add(c, b);
}

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>
#include <string>
using namespace std;

int add(int x[], int size, int y[] = NULL)
{
	int s = 0;
	for (int i = 0; i < size; i++)
		s += x[i];
	if (y == NULL)
		return s;
	for (int i = 0; i < size; i++)
		s += y[i];
	return s;
}

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;
}

6-2

(1)

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

class Person {
	int id;
	double weight;
	string name;
public:
	void show() { cout << id << ' ' << weight << ' ' << name << endl; }
	Person() { this->id = 1; this->weight = 20.5; this->name = "Grace";}
	Person(int id, string name) { this->id = id; this->weight = 20.5; this->name = "Ashley"; }
	Person(int id, string name,double weight) { this->id = id; this->weight = weight; this->name = name; }

};

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:
	void show() { cout << id << ' ' << weight << ' ' << name << endl; }
	Person(int id = 1,string name="Grace",double weight=20.5) { this->id = id; this->weight = weight; this->name = name; }
};

int main() {
	Person grace, ashley(2, "Ashley"), helen(3, "Helen", 32.5);
	grace.show();
	ashley.show();
	helen.show();

6-3

(1)

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

int big(int a, int b)
{
	return(((a > b) ? a : b) > 100) ? 100 : (a > b ? a : b);
}

int big(int a, int b, int c)
{
	return(((a > b) ? a : b) > c) ? c : (a > b ? a : b);
}



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>
#include <string>
using namespace std;



int big(int a, int b, int c=100)
{
	return(((a > b) ? a : b) > c) ? c : (a > b ? a : b);
}



int main() {
	int x = big(3, 5);
	int y = big(300, 60);
	int z = big(30, 60, 50);
	cout << x << ' ' << y << ' ' << z << endl;
}

6-4

6-5

#include <iostream>
#include <string>
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.5 };

	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-6

#include <iostream>
#include <string>
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* p = new int[size];
	int ind = 0;
	cout << "합친 정수 배열을 출력한다." << endl;
	for (int i = 0; i < size/2; i++)
	{
		p[ind] = s1[i];
		cout << p[ind] << ' ';
		ind++;
	}
	for (int i = 0; i < size / 2; i++)
	{
		p[ind] = s2[i];
		cout << p[ind] << ' ';
		ind++;
	}
	cout << endl;
	return p;
}
int* ArrayUtility2::remove(int s1[], int s2[], int size, int& retSize)
{
	int cnt = 0;
	int* tmp = new int[size];

	for (int i = 0; i < size; i++) {
		for (int j = 0; j < size; j++) {
			if (s1[i] == s2[j])break;
			if (j == size - 1)tmp[cnt++] = s1[i];
		}
	}
	if (cnt == 0)return NULL;

	retSize = cnt;
	cout << "배열 x[]에서 y[]를 뺀 결과를 출력한다. 개수는 " << retSize << endl;
	int* ret = new int[retSize];
	for (int i = 0; i < retSize; i++) {
		ret[i] = tmp[i];
		cout << ret[i] << ' ';
	}
	cout << endl;
	return ret;
}

int main()
{
	int x[5], y[5], retSize;
	int* z, * w;
	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];
	}
	z=ArrayUtility2::concat(x, y, 10);
	w=ArrayUtility2::remove(x, y, 5, retSize);
}

6-7

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

class Random {
public:
	static void seed() { srand((unsigned)time(0)); }
	static int nextInt(int min = 0,int max = 32767);
	static char nextAlphabet();
	static double nextDouble();
};

int Random::nextInt(int min, int max) {
	return rand() % (max - min) + min;
}
char Random::nextAlphabet() {
	if (rand() % 2 == 0)return rand() % 26 + 'a';
	else return rand() % 26 + 'A';
}
double Random::nextDouble() {
	return (double)rand() / RAND_MAX;
}


int main() {
	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 < 10; i++) {
		cout << Random::nextDouble() << ' ';
	}
	cout << endl;
}

6-8

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

class Trace {
public:
	static string log[100][2];
	static int index;
	static void put(string tag, string str);
		static void print(string tag="");
};

string Trace::log[100][2] = { "" };
int Trace::index = 0;
void Trace::put(string tag, string str) {
	log[index][0] = tag;
	log[index][1] = str;
	index++;
}
void Trace::print(string tag) {
	if (!tag.empty()) {
		cout << "----- " << tag << "태그의 Trace 정보를 축력합니다. -----" << endl;
		for (int i = 0; i < index; i++) {
			if (log[i][0].compare(tag) == 0) {
				cout << log[i][0] << ":" << log[i][1] << endl;
			}
		}
	}
	else {
		cout << tag << "----- 모든 Trace 정보를 출력합니다. -----" << endl;
		for (int i = 0; i < index; i++) {
			cout << log[i][0] << ":" << log[i][1] << endl;
		}
	}
}

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();
}

6-9

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

class Board {
public:
	static string *notices;
		static int index;
	static void add(string str);
	static void print();
};

string Board::* notices = NULL;
int Board::index = 0;
void Board::add(string str) {
	notices[index++] = str;
}
void Board::print() {
	cout << "************* " << "게시판입니다. " << "*************" << endl;
	for (int i = 0; i < index; i++) {
		cout << i << ":" <<notices[i] << endl;
	}
	cout << endl;
}

Comments (0)