TC++PL 5장 연습문제

2009년 8월 25일

1.

  1. // declaration.cc
  2. // 5.9.1 다음의 선언문을 순서대로 작성해 보자. 문자에 대한 포인터, 10개
  3. // 정수의 배열, 10개 정수의 배열의 참조자, 문자열의 배열에 대한 포인터,
  4. // 문자에 대한 포인터에 대한 포인터, 상수 정수, 상수 정수에 대한 포인터,
  5. // 정수에 대한 상수 포인터, 그리고 각각의 객체를 초기화하자.
  6.  
  7. int main()
  8. {
  9.  char *str; // 문자에 대한 포인터
  10.  int ia[10]; // 10개 정수의 배열
  11.  // 10개 정수의 배열의 참조자는 불가능
  12.  char *stra[10]; // 문자열의 배열에 대한 포인터
  13.  char **strb; // 문자에 대한 포인터에 대한 포인터
  14.  const int i = 7; // 상수 정수
  15.  const int* j = &i; // 상수 정수에 대한 포인터
  16.  int k = 8;
  17.  int *const m = &k; // 정수에 대한 상수 포인터
  18. }

2.

  1. // alignment.cc
  2. // 4.9.2 여러분이 사용하는 구현환경에서는 포인터 타입 char*, int*,
  3. // void*에 대해 사용상의 어떤 제약이 있는지 조사해 보자. 예를 들어,
  4. // int*가 홀수 주소값을 가질 수 있는지?
  5. // 힌트: 메모리 경계배열
  6.  
  7. #include<iostream>
  8.  
  9. using std::cout;
  10. using std::endl;
  11.  
  12. int main()
  13. {
  14.  int a = 10;
  15.  int* b = &a;
  16.  
  17.  cout << "address of a = " << b << endl;
  18.  
  19.  b = b + 1;
  20.  
  21.  cout << "new address of a = " << b << endl;
  22. }

실행 결과는

hoppangbook:chap05 hoppang$ ./alignment
address of a = 0xbffff928
new address of a = 0xbffff92c

3.

  1. // typedef.cc
  2. // 4.9.3 typedef를 써서 다음의 타입에 대한 동의어를 정의해 보자.
  3. // unsigned char, const unsigned char, 정수에 대한 포인터, char에 대한
  4. // 포인터, char의 배열에 대한 포인터, int에 대한 포인터가 7개 모인 배열,
  5. // int에 대한 포인터 7개의 배열에 대한 포인터, int에 대한 포인터 7개의
  6. // 배열이 8개 모인 배열.
  7.  
  8. #include<cstring>
  9. #include<cstdio>
  10.  
  11. typedef unsigned char uchar; // unsigned char
  12. typedef const uchar cuchar; // const unsigned char
  13. typedef int* pint; // 정수에 대한 포인터
  14. typedef char* pchar; // char에 대한 포인터
  15. typedef pchar ppchar[5]; // char의 배열에 대한 포인터(크기 지정)
  16. typedef pchar* dpchar; // char의 배열에 대한 포인터(더블 포인터)
  17. typedef pint ppint[7]; // int에 대한 포인터가 7개 모인 배열
  18. typedef ppint* pppint; // int에 대한 포인터 7개의 배열에 대한 포인터
  19. typedef ppint appint[8]; // int에 대한 포인터 7개의 배열이 8개 모인 배열
  20.  
  21. int main()
  22. {
  23.  // char의 배열에 대한 포인터(5개짜리) 사용예
  24.  ppchar a;
  25.  for(int i=0; i<5; i++)
  26.  {
  27.   a[i] = new char[6];
  28.   strcpy(a[i], "hello");
  29.   printf("%s\n", a[i]);
  30.  }
  31. }

4.

  1. // swap.cc
  2. // 5.9.4 두 정수를 스왑(swap)하는 (값을 맞바꾸는) 함수를 하나 작성해 보자.
  3. // 인자 타입으로는 int* 를 사용하자. 다 만들었으면 이번엔 인자 타입으로
  4. // int&를 사용하는 swap 함수를 만들어 보자.
  5.  
  6. #include<iostream>
  7.  
  8. using std::cout;
  9. using std::endl;
  10.  
  11. // int * 형태를 인자로 받는 스왑 함수
  12. void swap_by_pointer(int* a, int* b)
  13. {
  14.  int t;
  15.  t = *a;
  16.  *a = *b;
  17.  *b = t;
  18. }
  19.  
  20. // int & 형태를 인자로 받는 스왑 함수
  21. void swap_by_reference(int &a, int &b)
  22. {
  23.  int t;
  24.  t = a;
  25.  a = b;
  26.  b = t;
  27. }
  28.  
  29. int main()
  30. {
  31.  int a = 10;
  32.  int b = 20;
  33.  
  34.  cout << "original a, b is " << a << ", " << b << endl;
  35.  
  36.  swap_by_pointer(&a, &b);
  37.  
  38.  cout << "after swap_by_pointer: " << a << ", " << b << endl;
  39.  
  40.  swap_by_reference(a, b);
  41.  
  42.  cout << "after swap_by_reference: " << a << ", " << b << endl;
  43.  
  44.  return 0;
  45. }

5.

  1. // strsize.cc
  2. // 5.9.5 다음의 예제에 나온 배열 str의 크기는 얼마인가?
  3. // char str[] = "a short string";
  4. // "a short string"의 길이는 몇일까?
  5.  
  6. #include<cstring>
  7. #include<cstdio>
  8.  
  9. int main()
  10. {
  11.  char str[] = "a short string";
  12.  
  13.  printf("sizeof(str) = %d\n", sizeof(str));
  14.  printf("strlen(str) = %d\n", strlen(str));
  15. }

실행 결과:

hoppangbook:chap05 hoppang$ ./strsize
sizeof(str) = 15
strlen(str) = 14

6.
생략

7.

  1. // calendar.cc
  2. // 5.9.7 1년에 속한 각 달의 이름과 각 달의 날수를 담은 테이블을 하나
  3. // 정의하고, 이 테이블을 출력해 보자. 이것을 두 번 반복한다. 처음엔 달
  4. // 이름에 대해 char 배열을, 날수에 대해 숫자 배열을 사용하고, 다음엔
  5. // 달 이름과 날수를 묶은 구조체의 배열을 사용하자.
  6.  
  7. #include<iostream>
  8.  
  9. using std::cout;
  10. using std::endl;
  11.  
  12. char* month[] = {"1월", "2월", "3월", "4월", "5월", "6월",
  13.      "7월", "8월", "9월", "10월", "11월", "12월" };
  14. int dayofmonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  15.  
  16. struct Calendar
  17. {
  18.  char* month;
  19.  int day;
  20. };
  21.  
  22. int main()
  23. {
  24.  Calendar cal[12];
  25.  
  26.  // 첫 번째
  27.  for(int i=0; i<12; i++)
  28.  {
  29.   cout << month[i] << "은 " << dayofmonth[i] << "일" << endl;
  30.   // Calendar 배열에 첫 번째 데이터를 복사
  31.   cal[i].month = new char[5];
  32.   strcpy(cal[i].month, month[i]);
  33.   cal[i].day = dayofmonth[i];
  34.  }
  35.  
  36.  cout << "두 번째: " << endl;
  37.  for(int i=0; i<12; i++)
  38.  {
  39.   cout << cal[i].month << "은 " << cal[i].day << "일" << endl;
  40.  }
  41.  
  42.  return 0;
  43. }

8.
하는 일은 같지만 만들어내는 (기계어) 코드는 서로 다르다.
Mac OS X 10.5.8 / gcc 4.0.1 기준

9. 생략

10.

  1. // month.cc
  2. // 5.9.10. 달의 이름을 담은 문자열의 배열을 하나 정의하자.
  3. // 각 문자열을 출력해 보고 이들을 출력해 주는 함수에 배열을 넘겨줘 보자.
  4.  
  5. #include<iostream>
  6.  
  7. using std::cout;
  8. using std::endl;
  9.  
  10. char* month[] = {"1월", "2월", "3월", "4월", "5월", "6월",
  11.      "7월", "8월", "9월", "10월", "11월", "12월" };
  12.  
  13. void print_month(char **m, int length)
  14. {
  15.  for(int i=0; i<length; i++)
  16.   cout << m[i] << endl;
  17. }
  18.  
  19. int main()
  20. {
  21.  print_month(month, 12);
  22. }

11.

  1. // input.cc
  2. // 5.9.11 단어들을 순서대로 입력받는다. 입력의 끝은 Quit라는 단어로
  3. // 판단하게 하자. 입력된 순서대로 각 단어를 출력하자. 단어는 두 번 출력하지
  4. // 않는다. 이 프로그램을 수정해서 출력하기 전에 모든 단어들을 정렬하도록
  5. // 하자.
  6.  
  7. #include<iostream>
  8. #include<list>
  9. #include<string>
  10.  
  11. using std::list;
  12. using std::string;
  13.  
  14. using std::cout;
  15. using std::cin;
  16. using std::endl;
  17.  
  18. void print_word(const string &word)
  19. { cout << word << endl; }
  20.  
  21. int main()
  22. {
  23.  string word;
  24.  list<string> words;
  25.  list<string> unique_words;
  26.  
  27.  cout << "input any words:\n";
  28.  while(1)
  29.  {
  30.   cin >> word;
  31.   if(word == "quit" || word == "Quit") break;
  32.   words.push_back(word);
  33.  }
  34.  
  35.  unique_copy(words.begin(), words.end(), back_inserter(unique_words));
  36.  cout << "Your input: === " << endl;
  37.  for_each(unique_words.begin(), unique_words.end(), print_word);
  38.  
  39.  unique_words.sort();
  40.  cout << "Sorted: === " << endl;
  41.  for_each(unique_words.begin(), unique_words.end(), print_word);
  42. }

12.

  1. // countword.cc
  2. // 5.9.12 어떤 string 안에 들어 있는 글자쌍의 출현 빈도를 세는 함수를
  3. // 하나 만들고, char의 배열(C 방식의 문자열)에 대해 동일하게 동작하는
  4. // 함수를 하나 더 만들자.
  5. // 이를테면, 글자쌍 "ab"는 "xabaacbaxabb"에 두 번 나타난다.
  6.  
  7. #include<iostream>
  8. #include<string>
  9.  
  10. using std::string;
  11. using std::cout;
  12. using std::endl;
  13.  
  14. int count_word(const string& s, const string& to_find)
  15. {
  16.  int n = 0;
  17.  int i = s.find(to_find, 0);
  18.  while(i < s.length()) {
  19.   ++n;
  20.   i = s.find(to_find, i+1);
  21.  }
  22.  return n;
  23. }
  24.  
  25. int count_word_cstr(const char* s, const char* to_find)
  26. {
  27.  int n = 0;
  28.  char *p = strstr(s, to_find);
  29.  while(p)
  30.  {
  31.   ++n;
  32.   p = strstr(p+1, to_find);
  33.  }
  34.  return n;
  35. }
  36.  
  37. int main()
  38. {
  39.  string str = "feel the fury of the forest fawn!";
  40.  string tofind = "f";
  41.  
  42.  char *cstr = "it is a good day to die";
  43.  char *tof_c = "i";
  44.  
  45.  int i = count_word(str, tofind);
  46.  int j = count_word_cstr(cstr, tof_c);
  47.  
  48.  cout << "count_word: " << i << endl;
  49.  cout << "count_word_cstr: " << j << endl;
  50. }

13.

  1. // date.cc
  2. // 5.9.13 날짜 정보를 유지하는 구조체인 Date를 정의하자. 입력된 정보로부터
  3. // Date 객체를 읽고, Date 객체를 출력하고, 날짜정보를 주어 Date를
  4. // 초기화하는 함수를 만들어 보자.
  5.  
  6. #include<iostream>
  7.  
  8. using std::cout;
  9. using std::endl;
  10. using std::cin;
  11.  
  12. struct Date
  13. {
  14.  Date() {}
  15.  Date(int y, int m, int d)
  16.   { read(y, m, d); }
  17.  void read(int y, int m, int d);
  18.  void inline show()
  19.   { cout << year << "년 " << month << "월 " << day << "일" << endl; }
  20.  
  21.  int year;
  22.  int month;
  23.  int day;
  24. };
  25.  
  26. void Date::read(int y, int m, int d)
  27. {
  28.  year = y;
  29.  month = m;
  30.  day = d;
  31. }
  32.  
  33. int main()
  34. {
  35.  Date date;
  36.  int y, m, d;
  37.  cout << "연도를 입력하세요:" << endl;
  38.  cin >> y;
  39.  cout << "월을 입력하세요:" << endl;
  40.  cin >> m;
  41.  cout << "날짜를 입력하세요:" << endl;
  42.  cin >> d;
  43.  
  44.  date.read(y, m, d);
  45.  cout << "입력한 날짜는 ";
  46.  date.show();
  47. }
글쓴이: admin 카테고리: TC++PL 태그: