본문 바로가기
IT/프로그래밍

C언어 본색 파트3 Chapter 5 연습문제 솔루션

by nutrient 2021. 5. 31.
728x90
728x170

 

1번

#include "part3_ch5_prob1_myheader.h"

void main () {

	double a = 10, b = 3;

	printf("add: %lf\n", add(10, 3));
	printf("sub: %lf\n", sub(10, 3));
	printf("mul: %lf\n", mul(10, 3));
	printf("div: %lf\n", div(10, 3));


}

 

myheader

#include "part3_ch5_prob1_myheader.h"

double add (double a, double b) {
	return a+b;
}

double sub (double a, double b) {
	return a-b;
}

double mul (double a, double b) {
	return a*b;
}

double div (double a, double b) {
	return a/b;
}

 

myheader.h

#include <stdio.h>

double add (double a, double b);
double sub (double a, double b);
double mul (double a, double b);
double div (double a, double b);

 

2번

#include <stdio.h>
#define ADD(x, y) x+y

void main () {
	printf("x + y는 %d 입니다.\n", ADD(3, 7));
}

 

 

#include "part3_ch5_prob3_score.h"

void main () {

	int arr[3] = {0};
	
	printf("국어, 영어 수학 점수를 입력하세요 : ");
	scanf("%d %d %d", arr, arr+1, arr+2);

	score(arr, 3);

}

 

 

#include "part3_ch5_prob3_score.h"

void score(int *arr, int size){
	int i = 0;
	double sum = 0, avg = 0;
	char grade;

	for (;i < size; i++) 
		sum += arr[i];
	
	avg = sum/size;

	if (avg > 90) grade = 'A';
	else if (avg > 80) grade = 'B';
	else if (avg > 70) grade = 'C';
	else if (avg > 60) grade = 'D';
	else grade = 'E';

	printf("평균 : %.2lf 학점 : %c\n", avg, grade);
}

 

 

#include <stdio.h>

void score(int *arr, int size);

 

 

#include "part3_ch5_prob4_point.h"

void main () {

	POINT a, b, *c, *d;
	
	a.x = 10;
	a.y = 15;
	b.x = 5;
	b.y = 10;

	printf("a의 x좌표: %d, y좌표: %d\n", a.x, a.y);
	printf("b의 x좌표: %d, y좌표: %d\n", b.x, b.y);

	c = add(&a, &b);
	d = subtract(&a, &b);

	printf("c(a+b)의 x좌표: %d, y좌표: %d\n", c->x, c->y);
	printf("d(a-b)의 x좌표: %d, y좌표: %d\n", d->x, d->y);
}

 

 

#include "part3_ch5_prob4_point.h"

POINT *add (POINT *a, POINT *b) {
	POINT* c = (POINT*) malloc(sizeof(POINT));
	c->x = a->x + b->x;
	c->y = a->y + b->y;
	return c;
}

POINT *subtract (POINT *a, POINT *b) {
	POINT* c = (POINT*) malloc(sizeof(POINT));
	c->x = a->x - b->x;
	c->y = a->y - b->y;
	return c;
}

 

 

#include <stdio.h>
#include <stdlib.h>

struct point
{
	int x;
	int y;
};

typedef struct point POINT;
POINT *add(POINT *a, POINT *b);
POINT *subtract(POINT *a, POINT *b);
728x90
그리드형

댓글