1. 다음은 색의 3요소인 red,green, blue로 색을 추상화한 Color 클래스를 선언하고 활용하는 코드이다. 빈칸을 채워라. red, green, blue는 0~255의 값만 가진다.

#include <iostream>
using namespace std;

class Color {
	int red, green, blue;
public:
	Color() { red = green = blue = 0; }
	Color(int r, int g, int b) { red = r; green = g; blue = b; }
	void setColor(int r, int g, int b) { red = r; green = g; blue = b; }
	void show() { cout << red << ' ' << green << ' ' << blue << endl; }
};

int main() {
	Color screenColor(255, 0, 0);
	Color* p;
	p = &screenColor;
	p->show(); // cout << p->show() (X)
	Color colors[3];
	p = colors;

	p[0].setColor(255, 0, 0);
	p[1].setColor(0, 255, 0);
	p[2].setColor(0, 0, 255);

	for (int i = 0; i < 3; i++) {
		p[i].show();
	}
}

2. 정수 공간 5개를 배열로 동적 할당받고, 정수를 5개 입력받아 평균을 구하고 출력한 뒤 배열을 소멸시키도록 main() 함수를 작성하라.

#include <iostream>
using namespace std;

int main() {
	int* p = new int[5];
	int sum = 0;
	
	cout << "정수 5개 입력>>";
	for (int i = 0; i < 5; i++) {
		cin >> p[i];
	}

	for (int i = 0; i < 5; i++) {
		sum += p[i];
	}
	
	cout << "평균 " << (float)sum / 5 << endl;
	
	delete[] p;

}

3. string 클래스를 이용하여 빈칸을 포함하는 문자열을 입력받고 문자열에서 'a'가 몇개 있는지 출력하는 프로그램을 작성해보자.

 

(1) 문자열에서 'a'를 찾기 위해  string 클래스의 멤버 at()나 []를 이용하여 작성하라.

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

int main() {
	string s;
	int count = 0;

	cout << "문자열 입력>>";
	getline(cin, s, '\n');

	for (int i = 0; i < s.length(); i++) { // s.length() 대신 s.size() 가능
		if (s[i] == 'a') // s[i] 대신 s.at(i) 가능
			count++;
	}

	cout << "문자 a는 " << count << "개 있습니다." << endl;

}

 

(2) 문자열에서 'a'를 찾기 위해 string 클래스의 find() 멤버 함수를 이용하여 작성하라. text.find('a',index);는 text 문자열의 index 위치부터 'a'를 찾아 문자열 내 인덱스를 리턴한다. 

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

int main() {
	string s;
	int count = 0, index = 0;

	cout << "문자열 입력>>";
	getline(cin, s, '\n');
	
	index = s.find('a');
	while (index != -1) {
		count++;
		index = s.find('a', index + 1);
	}

	cout << "문자 a는 " << count << "개 있습니다." << endl;

}

4.

 

다음과 같은 Sample 클래스가 있다.

class Sample {
	int* p;
	int size;
public:
	Sample(int n) {
		size = n;
		p = new int[n];
	}
	void read();
	void write();
	int big();
	~Sample();
};

 

다음 main() 함수가 실행되도록 Sample 클래스를 완성하라.

int main() {
	Sample s(10);
	s.read();
	s.write();
	cout << "가장 큰 수는 " << s.big() << endl;
}

 

 

void Sample::read() {
	for (int i = 0; i < size; i++) {
		cin >> p[i];
	}
}

void Sample::write() {
	for (int i = 0; i < size; i++) {
		cout << p[i] << ' ';
	}
	cout << endl;
}

int Sample::big() {
	int max = p[0];
	for (int i = 1; i < size; i++) {
		if (max < p[i])
			max = p[i];
	}
	return max;
}

Sample::~Sample() {
	delete[] p;
}

 

 

전체 코드 

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

class Sample {
	int* p;
	int size;
public:
	Sample(int n) {
		size = n;
		p = new int[n];
	}
	void read();
	void write();
	int big();
	~Sample();
};

void Sample::read() {
	for (int i = 0; i < size; i++) {
		cin >> p[i];
	}
}

void Sample::write() {
	for (int i = 0; i < size; i++) {
		cout << p[i] << ' ';
	}
	cout << endl;
}

int Sample::big() {
	int max = p[0];
	for (int i = 1; i < size; i++) {
		if (max < p[i])
			max = p[i];
	}
	return max;
}

Sample::~Sample() {
	delete[] p;
}

int main() {
	Sample s(10);
	s.read();
	s.write();
	cout << "가장 큰 수는 " << s.big() << endl;
}

5. string 클래스를 이용하여 사용자가 입력한 영문 한 줄을 입력받고 글자 하나만 랜덤하게 수정하여 출력하는 프로그램을 작성하라. 

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

int main() {
	srand((unsigned)time(NULL));
	string s;
	int index;
	cout << "아래에 한 줄을 입력하세요.(exit를 입력하면 종료합니다)" << endl;
	while (true) {
		cout << ">>";
		getline(cin, s, '\n');
		if (s.compare("exit") == 0) // s == "exit" 도 가능 
			break;

		index = rand() % s.length();
		cout << s[index] << endl;
	}

}

6. string 클래스를 이용하여 사용자가 입력한 영문 한 줄을 문자열로입력받고 거꾸로 출력하는 프로그램을 작성하라.

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

int main() {
	string s;
	cout << "아래에 한줄을 입력하세요.(exit를 입력하면 종료합니다.)" << endl;
	while (true) {
		cout << ">>";
		getline(cin, s, '\n');
		if (s == "exit") break;

		for (int i = (s.length() - 1); i >= 0; i--) {
			cout << s[i];
		}
        
		cout << endl;
	}
}

 


7. 다음과 같이 원을 추상화한 Circle 클래스가 있다. Circle 클래스와 main() 함수를 작성하고 3개의 Circle 객체를 가진 배열을 선언하고, 반지름 값을 입력받고 면적이 100보다 큰 원의 개수를 출력하는 프로그램을 완성하라. Circle 클래스도 완성하라.

class Circle {
	int radius;
public:
	void setRadius(int radius);
	double getArea();
};

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

class Circle {
	int radius;
public:
	void setRadius(int radius);
	double getArea();
};

void Circle::setRadius(int radius) {
	this->radius = radius;
}

double Circle::getArea() {
	return 3.14 * radius * radius;
}

int main() {
	Circle arr[3];
	int radius, count = 0;

	for (int i = 0; i < 3; i++) {
		cout << "원 " << i+1 << "의 반지름 >> ";
		cin >> radius;
		arr[i].setRadius(radius);
	}

	for (int i = 0; i < 3; i++) {
		if (arr[i].getArea() > 100)
			count++;
	}
	
	cout << "면적이 100보다 큰 원은 " << count << "개 입니다." << endl;

}

8. 실습 문제 7의 문제를 수정해보자. 사용자로부터 다음과 같이 원의 개수를 입력바독, 원의 개수만큼 반지름을 입력받는 방식으로 수정하라. 원의 개수에 따라 동적으로 배열을 할당받아야 한다.

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

class Circle {
	int radius;
public:
	void setRadius(int radius);
	double getArea();
};

void Circle::setRadius(int radius) {
	this->radius = radius;
}

double Circle::getArea() {
	return 3.14 * radius * radius;
}

int main() {
	Circle* p;
	int radius, count = 0, n;

	cout << "원의 개수 >> ";
	cin >> n;
	p = new Circle[n];

	for (int i = 0; i < n; i++) {
		cout << "원 " << i+1 << "의 반지름 >> ";
		cin >> radius;
		p[i].setRadius(radius);
	}

	for (int i = 0; i < n; i++) {
		if (p[i].getArea() > 100)
			count++;
	}
	
	cout << "면적이 100보다 큰 원은 " << count << "개 입니다." << endl;

}

9. 다음과 같은 Person 클래스가 있다. Person 클래스와 main() 함수를 작성하여, 3개의 Person 객체를 가지는 배열을 선언하고, 다음과 같이 키보드에서 이름과 전화번호를 입력받아 출력하고 검색하는 프로그램을 완성하라.

class Person {
	string name;
	string tel;
public:
	Person();
	string getName() { return name; }
	string getTel() { return tel; }
	void set(string name, string tel);
};

 

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

class Person {
	string name;
	string tel;
public:
	Person();
	string getName() { return name; }
	string getTel() { return tel; }
	void set(string name, string tel);
};

Person::Person() {

}

void Person::set(string name, string tel) {
	this->name = name;
	this->tel = tel;
}


int main() {
	Person arr[3];
	string name, tel;

	cout << "이름과 전화 번호를 입력해 주세요" << endl;
	for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) {
		cout << "사람 " << i + 1 << ">> ";
		cin >> name >> tel;
		arr[i].set(name, tel);
	}
	cout << "모든 사람의 이름은 ";
	for (int i = 0; i < 3; i++) {
		cout << arr[i].getName() << ' ';
	}
	cout << endl;
	cout << "전화번호 검색합니다. 이름을 입력하세요>>";
	cin >> name;

	for (int i = 0; i < 3; i++) {
		if (name == arr[i].getName()) {
			cout << "전화번호는 " << arr[i].getTel() << endl;
			break;
		}
	}
}

10. 다음에서 Person은 사람을, Family는 가족을 추상화한 클래스로서 완성되지 않은클래스이다.

 다음에서 Person은 사람을, Family는 가족을 추상화한 클래스로서 완성되지 않은 클래스이다.

class Person {
	string name;
public:
	Person(string name) { this->name = name; }
	string getName() { return name; }
};

class Family {
	Person* p;
	int size;
public:
	Family(string name, int size);
	void show();
	~Family();
};

 

다음 main()이 작동하도록 Person과 Family 클래스에필요한 멤버들을 추가하고 코드를 완성하라.

int main() {
	Family* simpson = new Family("Simson", 3);
	simpson->setName(0, "Mr. Simpson");
	simpson->setName(1, "Mrs. Simpson");
	simpson->setName(2, "Bart Simpson");
	simpson->show();
	delete simpson;
}

 

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

class Person {
	string name;
public:
	Person(){}
	Person(string name) { this->name = name; }
	string getName() { return name; }
	void setName(string name);
};

class Family {
	Person* p;
	int size;
	string name;
public:
	Family(string name, int size);
	void show();
	void setName(int index, string name);
	~Family();
};


void Person::setName(string name) {
	this->name = name;
}

Family::Family(string name, int size) {
	this->name = name;
	this->size = size;
	p = new Person[size];
}

void Family::show() {
	cout << name << "가족은 다음과 같이" << size << "명 입니다." << endl;
	for (int i = 0; i < size; i++) {
		cout << p[i].getName() << '\t';
	}
}

void Family::setName(int index, string name) {
	p[index].setName(name);
}

Family::~Family() {
	delete[] p;
}


int main() {
	Family* simpson = new Family("Simson", 3);
	simpson->setName(0, "Mr. Simpson");
	simpson->setName(1, "Mrs. Simpson");
	simpson->setName(2, "Bart Simpson");
	simpson->show();
	delete simpson;
}

11. 다음은 커피자판기로 작동하는 프로그램을 만들기 위해 필요한 두 클래스이다.

 

+ Recent posts