본문 바로가기
솔루션모음/쉽게 풀어 쓴 C언어 Express 실습문제

[쉽게 풀어 쓴 C언어 Express] 15장 프로그래밍 programming 솔루션 답지

by 이얏호이야호 2020. 4. 11.

쉽게 풀어 쓴 C언어 express 솔루션입니다.

1.

double power(int x, int y) {

 double result = 1.0;

 int i;

 for (i = 0;i < y; i++) {

 printf("result=%f\n", result);

 //①

 result *= x;

 }

 return result;

}

(a)

#ifdef DEBUG

printf("result=%f\n", result);

//①

#endif

(b)

#if DEBUG==2

printf("result=%f\n", result);

//①

#endif

(c)

#if (DEBUG==2 && LEVEL==3)

printf("result=%f\n", result);

//①

#endif

(d)

printf("%d: result=%f\n", __LINE__, result);

//①

(e)

#if POWER_TYPE==0

int power(int x, int y) {

 int result = 1.0;

 # else

 double power(int x, int y) {

 double result = 1.0;

 #endif

 ... #if POWER_TYPE==0

 printf("result=%d\n", result);

 //①

 # else

 printf("result=%f\n", result);

 //①

 #endif

 ...

 (f)

 #if 0

 printf("result=%f\n", result);

 //①

 # else

 

 

2.

#include <stdio.h>

 #define MIN(x,y) (x)<(y)?(x):(y)

 #define GET_MIN(x,y,z) (MIN(x, y))<(z)?(MIN(x, y)):(z)

 int main(void) {

 int x, y, z, result;

 printf("3개의 정수를 입력하시오: ");

 scanf("%d %d %d", &x, &y, &z);

 result = GET_MIN(x, y, z);

 printf("최소값은 %d입니다. ", result);

 return 0;

 }

 

 

3.

#define ARRAY_INIT(array, size, value) {

 int i;

 for (i=0;i<size;i++)\

 array[i]=(value);

 }

 int main(void) {

 int a[10];

 ARRAY_INIT(a, 10, 0);

 return 0;

 }

 

4.

 #include <stdio.h>

 #define VOLUME(r, h) (3.141592*r*r*h)

 int main(void) {

 double r, h, volume;

 printf("원기둥의 반지름을 입력하시오: ");

 scanf("%lf", &r);

 printf("원기둥의 높이를 입력하시오: ");

 scanf("%lf", &h);

 volume = VOLUME(r, h);

 printf("원기둥의 부피: %f ", volume);

 return 0;

 }

 

 

5.

#include <stdio.h>

 #define IS_SPACE(c) ((c)==' ' || (c)=='\t' || (c)=='\n')

 int main(void) {

 char str[100];

 int i, count;

 printf("문자열을 입력하시오: ");

 gets(str);

 i=0;

 count=0;

 while( str[i] != NULL ) {

 if( IS_SPACE(str[i]) )

 count++;

 i++;

 }

 printf("공백문자의 개수: %d \n", count);

 return 0;

 }

 

 

6.

#include <stdio.h>

 #define SET_BIT(n, pos) ((n) |= (1 << (pos)))

 #define GET_BIT(n, pos) ((n) & (1 << (pos)))

 #define CLR_BIT(n, pos) ((n) &= (~(1) << (pos)))

 int main(void) {

 int n;

 n = 0x808081;

 printf("GET_BIT(): %d \n", GET_BIT(n, 0));

 SET_BIT(n, 2);

 printf("%#x \n", n);

 return 0;

 }

 

 

7. (힌트)에 오타가 있습니다.

for (i=0;i<32;i++)

 if( GET_BIT(n, i) )

 printf("1"); else

 printf("0");

 ->

 for (i=31;i>=0;i--)

 if( GET_BIT(n, i) )

 printf("1"); else

 printf("0");

 #include <stdio.h>

 #define GET_BIT(n, pos) ((n) & (1 << (pos)))

 void display_bit(int value) {

 int i;

 for (i=31;i>=0;i--)

 if( GET_BIT(value, i) )

 printf("1"); else

 printf("0");

 printf("\n");

 }

 int main(void) {

 int n, orientation, distance;

 printf("정수값을 입력하시오: ");

 scanf("%d", &n);

 display_bit(n);

 return 0;

 }

 

 

8.

 #include <stdio.h>

 #define GET_BIT(n, pos) ((n) & (1 << (pos)))

 void display_bit(int value) {

 int i;

 for (i=31;i>=0;i--)

 if( GET_BIT(value, i) )

 printf("1"); else

 printf("0");

 printf("\n");

 }

 int main(void) {

 int n, orientation, distance;

 printf("정수값을 입력하시오: ");

 scanf("%d", &n);

 printf("왼쪽 이동은 0, 오른쪽 이동은 1을 입력하시오: ");

 scanf("%d", &orientation);

 printf("이동시킬 거리: ");

 scanf("%d", &distance);

 printf("이동 전: ");

 display_bit(n) ;

 printf("이동 후: ");

 if( orientation == 0 )

 display_bit(n << distance); else

 display_bit(n >> distance);

 }

 

 

 

 

9.

 #include <stdio.h>

 int main(void) {

 char s[100];

 int i;

 printf("문자열을 입력하시오: ");

 scanf("%s", s);

 for (i=0;i<strlen(s);i++) {

 s[i] = s[i] ^ 0x20;

 }

 printf("결과 문자열: %s\n", s);

 return 0;

 }

 

10.

#include <stdio.h>

 void encode(char *src, char *key, char *result);

 void decode(char *src, char *key, char *result);

 void main(void) {

 char *key = "12345678";

 char *data = "I am a boy";

 char result1[100];

 char result2[100];

 encode(data, key, result1);

 printf("%s가 %s로 엔코딩됨\n", data, result1);

 decode(result1, key, result2);

 printf("%s가 %s로 디코딩됨\n", result1, result2);

 }

 void encode(char *src, char *key, char *result) {

 while(*src) *result++ = *src++ ^ *key++;

 *result = '\0';

 }

 void decode(char *src, char *key, char *result) {

 encode(src, key, result);

 }

 I am a boy가 xRY W Zoy로 엔코딩됨

 xRY W Zoy가 I am a boy로 디코딩됨

 계속하려면 아무 키나 누르십시오 . . .

 

 

 

 

 

 

 

 

 

 

더 많은  쉽게 풀어 쓴 C언어EXPRESS 이론 솔루션

 https://chuinggun.tistory.com/category/%EC%86%94%EB%A3%A8%EC%85%98%EB%AA%A8%EC%9D%8C/%EC%89%BD%EA%B2%8C%20%ED%92%80%EC%96%B4%20%EC%93%B4%20C%EC%96%B8%EC%96%B4%20Express%20%EC%9D%B4%EB%A1%A0

더 많은 쉽게 풀어 쓴 C언어EXPRESS 프로그래밍 솔루션 : 

https://chuinggun.tistory.com/category/%EC%86%94%EB%A3%A8%EC%85%98%EB%AA%A8%EC%9D%8C/%EC%89%BD%EA%B2%8C%20%ED%92%80%EC%96%B4%20%EC%93%B4%20C%EC%96%B8%EC%96%B4%20Express%20%EC%8B%A4%EC%8A%B5%EB%AC%B8%EC%A0%9C

댓글