Jam's story
[C++ programming ] 실습문제 6장 본문
1번
#include <iostream>
using namespace std;
int add(int a[], int size, int b[]=NULL) {
int sum = 0;
for (int i = 0; i < size; i++) {
sum += a[i];}
if (b == NULL) return sum;
for (int i = 0; i < size; i++)
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;
return 0;
}
2번
#include <iostream>
using namespace std;
class Person {
int id;
double weight;
string name;
public:
Person(int a = 1, string name = "grace", double b = 20.5) {
this->name = name;
this->id = a;
this->weight = b;
}
void show() {
cout << id << ' ' << weight << ' ' << name << endl;
}
};
int main()
{
Person grace, ashely(2, "Ashley"), helen(3, "Helen", 32.5);
grace.show();
ashely.show();
helen.show();
}
3번
#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;
}
#include <iostream>
using namespace std;
int big(int a, int b, int c = 100) {
int bigNum = 0;
//bigNum으로 a랑 b 둘중에 큰 값을 설정
if (a > b) bigNum = a;
else bigNum = b;
if (bigNum < c) return bigNum;
else return c;
}
int main()
{
int x = big(3, 5);
int y = big(300, 60);
int z = big(30, 60, 50);
cout << x << ' ' << y << ' ' << z << endl;
return 0;
}
4번
초기화는 클래스 밖에서 해야한다
#include <iostream>
using namespace std;
class MyVector {
int* mem;
int size;
public:
MyVector(int n, int val);
void show();
~MyVector() { delete[] mem; }
};
MyVector::MyVector(int n=100, int val=0) {
mem = new int[n];
size = n;
for (int i = 0; i < size; i++) mem[i] =val;
}
void MyVector::show(){
for (int i = 0; i < size; i++) {
cout << mem[i] << ' ';
}
}
int main()
{
MyVector a;
MyVector b(10, 7);
a.show();
b.show();
}
5번
static 함수를 어떻게 선언해야하는지, 몰랐었다.
클래스 밖에서 static을 빼고
#include <iostream>
using namespace std;
class ArrayUtility {
public:
static void intToDouble(int source[], double dest[], int size);
static void doubleToInt(double source[], int dest[], int size);
};
void ArrayUtility::intToDouble(int source[], double dest[], int size) {
for (int i = 0; i < size; i++) {
dest[i] = (double)source[i];
}
}
void ArrayUtility::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번
if (j = size - 1) tmp[cnt++] = s1[i]; 이부분이 처음에 이해가 가지 않았다.
return 값을 제대로 해줘야 한다.
#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* s3 = new int[size*2];
for (int i = 0; i < size; i++) {
s3[i] = s1[i];
s3[i + size] = s2[i];
}
cout << "합친 정수 배열을 출력한다.";
for (int i = 0; i < size * 2; i++) {
cout << s3[i] << ' ';
}
return s3;
}
int* ArrayUtility2::remove(int s1[], int s2[], int size, int& retSize) {
int cnt = 0; //retSize
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;
//j가 마지막수까지 온거면 ,중복된거 없으니 tmp에 그 s1[i]를 추가해
if (j = size - 1) tmp[cnt++] = s1[i];
}
}
if (cnt == 0) return NULL;
retSize = cnt;
int* res = new int[retSize];
cout << "배열 x[]에서 y[]를 뺀 결과를 출력한다. 개수는 " << retSize << endl;
for (int i = 0; i < retSize; i++) {
res[i] = tmp[i];
cout << res[i] << ' ';
}
return res;
}
int main()
{
int x[5], y[5], retSize;
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];
}
int* z = ArrayUtility2::concat(x, y, 5);
int* w = ArrayUtility2::remove(x, y, 5, retSize);
}
7번
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
class Random {
public:
static void seed() { srand((unsigned)time(0)); }
static int nextInt(int min, int max );
static char nextAlphabet();
static double nextDouble();
};
int Random::nextInt(int min = 0, int max = 32767) {
return rand()%(max-min)+min;
}
char Random::nextAlphabet() {
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() << ' ';
}
}
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()) {//"태그가 있다면"
for (int i = 0; i < index; i++) {
if (log[i][0].compare(tag)==0) {
cout << log[i][0] << ": " << log[i][1] << endl;
}
}
}
else {
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();
return 0;
}
9번
#include <iostream>
#include <string>
using namespace std;
class Board {
public:
static string* log;
static int index;
static void add(string message);
static void print();
};
string* Board::log = new string[100];
int Board::index = 0;
void Board::add(string message){
log[index] = message;
index++;
}
void Board::print() {
cout << "********게시판 입니다*********";
for (int i = 0; i < index; i++) {
cout << i << ":" << log[i] << endl;
}
}
int main() {
Board::add("중간고사는 감독 없는 자율 시험입니다.");
Board::add("코딩 라운지 많이 이용해 주세요.");
Board::print();
Board::add("진소린 학생이 경진대회 입상하였습니다. 축하해주세요");
Board::print();
return 0;
}
'2021-2학기 > C++' 카테고리의 다른 글
c++기본 (0) | 2021.10.26 |
---|---|
c4996 에러 해결방법 (0) | 2021.10.24 |
2장 틀렸던 문제 다시풀어보기 (0) | 2021.10.24 |
[명품 c++ programming] 5장 open challenge (0) | 2021.10.10 |
[C++] 함수의 참조, 복사 생성자 (0) | 2021.10.10 |
Comments