TC++PL 5장 연습문제
2009년 8월 25일
1.
-
// declaration.cc
-
// 5.9.1 다음의 선언문을 순서대로 작성해 보자. 문자에 대한 포인터, 10개
-
// 정수의 배열, 10개 정수의 배열의 참조자, 문자열의 배열에 대한 포인터,
-
// 문자에 대한 포인터에 대한 포인터, 상수 정수, 상수 정수에 대한 포인터,
-
// 정수에 대한 상수 포인터, 그리고 각각의 객체를 초기화하자.
-
-
int main()
-
{
-
char *str; // 문자에 대한 포인터
-
int ia[10]; // 10개 정수의 배열
-
// 10개 정수의 배열의 참조자는 불가능
-
char *stra[10]; // 문자열의 배열에 대한 포인터
-
char **strb; // 문자에 대한 포인터에 대한 포인터
-
const int i = 7; // 상수 정수
-
const int* j = &i; // 상수 정수에 대한 포인터
-
int k = 8;
-
int *const m = &k; // 정수에 대한 상수 포인터
-
}
2.
-
// alignment.cc
-
// 4.9.2 여러분이 사용하는 구현환경에서는 포인터 타입 char*, int*,
-
// void*에 대해 사용상의 어떤 제약이 있는지 조사해 보자. 예를 들어,
-
// int*가 홀수 주소값을 가질 수 있는지?
-
// 힌트: 메모리 경계배열
-
-
#include<iostream>
-
-
using std::cout;
-
using std::endl;
-
-
int main()
-
{
-
int a = 10;
-
int* b = &a;
-
-
cout << "address of a = " << b << endl;
-
-
b = b + 1;
-
-
cout << "new address of a = " << b << endl;
-
}
실행 결과는
hoppangbook:chap05 hoppang$ ./alignment
address of a = 0xbffff928
new address of a = 0xbffff92c
3.
-
// typedef.cc
-
// 4.9.3 typedef를 써서 다음의 타입에 대한 동의어를 정의해 보자.
-
// unsigned char, const unsigned char, 정수에 대한 포인터, char에 대한
-
// 포인터, char의 배열에 대한 포인터, int에 대한 포인터가 7개 모인 배열,
-
// int에 대한 포인터 7개의 배열에 대한 포인터, int에 대한 포인터 7개의
-
// 배열이 8개 모인 배열.
-
-
#include<cstring>
-
#include<cstdio>
-
-
typedef unsigned char uchar; // unsigned char
-
typedef const uchar cuchar; // const unsigned char
-
typedef int* pint; // 정수에 대한 포인터
-
typedef char* pchar; // char에 대한 포인터
-
typedef pchar ppchar[5]; // char의 배열에 대한 포인터(크기 지정)
-
typedef pchar* dpchar; // char의 배열에 대한 포인터(더블 포인터)
-
typedef pint ppint[7]; // int에 대한 포인터가 7개 모인 배열
-
typedef ppint* pppint; // int에 대한 포인터 7개의 배열에 대한 포인터
-
typedef ppint appint[8]; // int에 대한 포인터 7개의 배열이 8개 모인 배열
-
-
int main()
-
{
-
// char의 배열에 대한 포인터(5개짜리) 사용예
-
ppchar a;
-
for(int i=0; i<5; i++)
-
{
-
a[i] = new char[6];
-
strcpy(a[i], "hello");
-
printf("%s\n", a[i]);
-
}
-
}
4.
-
// swap.cc
-
// 5.9.4 두 정수를 스왑(swap)하는 (값을 맞바꾸는) 함수를 하나 작성해 보자.
-
// 인자 타입으로는 int* 를 사용하자. 다 만들었으면 이번엔 인자 타입으로
-
// int&를 사용하는 swap 함수를 만들어 보자.
-
-
#include<iostream>
-
-
using std::cout;
-
using std::endl;
-
-
// int * 형태를 인자로 받는 스왑 함수
-
void swap_by_pointer(int* a, int* b)
-
{
-
int t;
-
t = *a;
-
*a = *b;
-
*b = t;
-
}
-
-
// int & 형태를 인자로 받는 스왑 함수
-
void swap_by_reference(int &a, int &b)
-
{
-
int t;
-
t = a;
-
a = b;
-
b = t;
-
}
-
-
int main()
-
{
-
int a = 10;
-
int b = 20;
-
-
cout << "original a, b is " << a << ", " << b << endl;
-
-
swap_by_pointer(&a, &b);
-
-
cout << "after swap_by_pointer: " << a << ", " << b << endl;
-
-
swap_by_reference(a, b);
-
-
cout << "after swap_by_reference: " << a << ", " << b << endl;
-
-
return 0;
-
}
5.
-
// strsize.cc
-
// 5.9.5 다음의 예제에 나온 배열 str의 크기는 얼마인가?
-
// char str[] = "a short string";
-
// "a short string"의 길이는 몇일까?
-
-
#include<cstring>
-
#include<cstdio>
-
-
int main()
-
{
-
char str[] = "a short string";
-
-
printf("sizeof(str) = %d\n", sizeof(str));
-
printf("strlen(str) = %d\n", strlen(str));
-
}
실행 결과:
hoppangbook:chap05 hoppang$ ./strsize
sizeof(str) = 15
strlen(str) = 14
6.
생략
7.
-
// calendar.cc
-
// 5.9.7 1년에 속한 각 달의 이름과 각 달의 날수를 담은 테이블을 하나
-
// 정의하고, 이 테이블을 출력해 보자. 이것을 두 번 반복한다. 처음엔 달
-
// 이름에 대해 char 배열을, 날수에 대해 숫자 배열을 사용하고, 다음엔
-
// 달 이름과 날수를 묶은 구조체의 배열을 사용하자.
-
-
#include<iostream>
-
-
using std::cout;
-
using std::endl;
-
-
char* month[] = {"1월", "2월", "3월", "4월", "5월", "6월",
-
"7월", "8월", "9월", "10월", "11월", "12월" };
-
int dayofmonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
-
-
struct Calendar
-
{
-
char* month;
-
int day;
-
};
-
-
int main()
-
{
-
Calendar cal[12];
-
-
// 첫 번째
-
for(int i=0; i<12; i++)
-
{
-
cout << month[i] << "은 " << dayofmonth[i] << "일" << endl;
-
// Calendar 배열에 첫 번째 데이터를 복사
-
cal[i].month = new char[5];
-
strcpy(cal[i].month, month[i]);
-
cal[i].day = dayofmonth[i];
-
}
-
-
cout << "두 번째: " << endl;
-
for(int i=0; i<12; i++)
-
{
-
cout << cal[i].month << "은 " << cal[i].day << "일" << endl;
-
}
-
-
return 0;
-
}
8.
하는 일은 같지만 만들어내는 (기계어) 코드는 서로 다르다.
Mac OS X 10.5.8 / gcc 4.0.1 기준
9. 생략
10.
-
// month.cc
-
// 5.9.10. 달의 이름을 담은 문자열의 배열을 하나 정의하자.
-
// 각 문자열을 출력해 보고 이들을 출력해 주는 함수에 배열을 넘겨줘 보자.
-
-
#include<iostream>
-
-
using std::cout;
-
using std::endl;
-
-
char* month[] = {"1월", "2월", "3월", "4월", "5월", "6월",
-
"7월", "8월", "9월", "10월", "11월", "12월" };
-
-
void print_month(char **m, int length)
-
{
-
for(int i=0; i<length; i++)
-
cout << m[i] << endl;
-
}
-
-
int main()
-
{
-
print_month(month, 12);
-
}
11.
-
// input.cc
-
// 5.9.11 단어들을 순서대로 입력받는다. 입력의 끝은 Quit라는 단어로
-
// 판단하게 하자. 입력된 순서대로 각 단어를 출력하자. 단어는 두 번 출력하지
-
// 않는다. 이 프로그램을 수정해서 출력하기 전에 모든 단어들을 정렬하도록
-
// 하자.
-
-
#include<iostream>
-
#include<list>
-
#include<string>
-
-
using std::list;
-
using std::string;
-
-
using std::cout;
-
using std::cin;
-
using std::endl;
-
-
void print_word(const string &word)
-
{ cout << word << endl; }
-
-
int main()
-
{
-
string word;
-
list<string> words;
-
list<string> unique_words;
-
-
cout << "input any words:\n";
-
while(1)
-
{
-
cin >> word;
-
if(word == "quit" || word == "Quit") break;
-
words.push_back(word);
-
}
-
-
unique_copy(words.begin(), words.end(), back_inserter(unique_words));
-
cout << "Your input: === " << endl;
-
for_each(unique_words.begin(), unique_words.end(), print_word);
-
-
unique_words.sort();
-
cout << "Sorted: === " << endl;
-
for_each(unique_words.begin(), unique_words.end(), print_word);
-
}
12.
-
// countword.cc
-
// 5.9.12 어떤 string 안에 들어 있는 글자쌍의 출현 빈도를 세는 함수를
-
// 하나 만들고, char의 배열(C 방식의 문자열)에 대해 동일하게 동작하는
-
// 함수를 하나 더 만들자.
-
// 이를테면, 글자쌍 "ab"는 "xabaacbaxabb"에 두 번 나타난다.
-
-
#include<iostream>
-
#include<string>
-
-
using std::string;
-
using std::cout;
-
using std::endl;
-
-
int count_word(const string& s, const string& to_find)
-
{
-
int n = 0;
-
int i = s.find(to_find, 0);
-
while(i < s.length()) {
-
++n;
-
i = s.find(to_find, i+1);
-
}
-
return n;
-
}
-
-
int count_word_cstr(const char* s, const char* to_find)
-
{
-
int n = 0;
-
char *p = strstr(s, to_find);
-
while(p)
-
{
-
++n;
-
p = strstr(p+1, to_find);
-
}
-
return n;
-
}
-
-
int main()
-
{
-
string str = "feel the fury of the forest fawn!";
-
string tofind = "f";
-
-
char *cstr = "it is a good day to die";
-
char *tof_c = "i";
-
-
int i = count_word(str, tofind);
-
int j = count_word_cstr(cstr, tof_c);
-
-
cout << "count_word: " << i << endl;
-
cout << "count_word_cstr: " << j << endl;
-
}
13.
-
// date.cc
-
// 5.9.13 날짜 정보를 유지하는 구조체인 Date를 정의하자. 입력된 정보로부터
-
// Date 객체를 읽고, Date 객체를 출력하고, 날짜정보를 주어 Date를
-
// 초기화하는 함수를 만들어 보자.
-
-
#include<iostream>
-
-
using std::cout;
-
using std::endl;
-
using std::cin;
-
-
struct Date
-
{
-
Date() {}
-
Date(int y, int m, int d)
-
{ read(y, m, d); }
-
void read(int y, int m, int d);
-
void inline show()
-
{ cout << year << "년 " << month << "월 " << day << "일" << endl; }
-
-
int year;
-
int month;
-
int day;
-
};
-
-
void Date::read(int y, int m, int d)
-
{
-
year = y;
-
month = m;
-
day = d;
-
}
-
-
int main()
-
{
-
Date date;
-
int y, m, d;
-
cout << "연도를 입력하세요:" << endl;
-
cin >> y;
-
cout << "월을 입력하세요:" << endl;
-
cin >> m;
-
cout << "날짜를 입력하세요:" << endl;
-
cin >> d;
-
-
date.read(y, m, d);
-
cout << "입력한 날짜는 ";
-
date.show();
-
}