쉽게 풀어쓴 C언어 EXPRESS(개정 3판) - Chapter 15-2

5.


#include <stdio.h>

#include <math.h>


struct vector {


double x;

double y;

}

;


struct vector vector_add(struct vector v1, struct vector v2) {


struct vector r;


r.x = v1.x + v2.x;

r.y = v1.y + v2.y;


return r;

}


void vector_print(struct vector v) {


printf("(%f, %f)\n", v.x, v.y);

}


int main(void) {


struct vector v1 = {

1.0, 2.0

}

;


struct vector v2 = {

2.0, 3.0

}

;


struct vector v3;


v3 = vector_add(v1, v2);

vector_print(v3);


return 0;

}


6.


#include <stdio.h>

#include <string.h>


struct email {


char title[100];

char receiver[50];

char sender[50];

char content[1000];

char date[100];

int priority;

}

;


int main(void) {


struct email e;


strcpy(e.title, "안부 메일");

strcpy(e.receiver, "chulsoo@hankuk.ac.kr");

strcpy(e.sender, "hsh@hankuk.ac.kr");

strcpy(e.content, "안녕하십니까? 별일 없으신지요?");

strcpy(e.date, "2010/9/1");


e.priority = 1;


return 0;

}


7.


#include <stdio.h>

#include <math.h>


struct food {


char name[100];

int calories;

}

;


int calc_total_calroies(struct food array[], int size);


int main(void) {


struct food food_array[3] = { {

"hambuger", 900

}


, {

"bulgogi", 500

}


, {

"sushi", 700

}

}

;


int total = calc_total_calroies(food_array, 3);


printf("%d\n", total);


return 0;

}


int calc_total_calroies(struct food array[], int size) {


int i, total = 0;


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

total += array[i].calories;

}


return total;

}


8.


#include <stdio.h>

#include <string.h>



struct employee {


int number;

char name[20];

int age;

char tel[20];

}

;


int main(void) {


struct employee e[10] = { {

1, "홍길동1", 20, "111-1111"

}


, {

2, "홍길동2", 25, "111-1112"

}


, {

3, "홍길동3", 60, "111-1113"

}


, {

4, "홍길동4", 40, "111-1114"

}


, {

5, "홍길동5", 50, "111-1115"

}


, {

6, "홍길동6", 45, "111-1116"

}


, {

7, "홍길동7", 32, "111-1117"

}


, {

8, "홍길동8", 23, "111-1118"

}


, {

9, "홍길동9", 29, "111-1119"

}


, {

10, "홍길동10", 62, "111-1120"

}

}

;


int i;


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

if (e[i].age >= 20 && e[i].age <= 30)

printf("%s\n", e[i].name);


return 0;

}

댓글

이 블로그의 인기 게시물

쉽게 풀어쓴 C언어 EXPRESS(개정 3판) - Chapter 10-3

쉽게 풀어쓴 C언어 EXPRESS(개정 3판) - Chapter 16-1

쉽게 풀어쓴 C언어 EXPRESS(개정 3판) - Chapter 13-2