쉽게 풀어쓴 C언어 EXPRESS(개정 3판) - Chapter 13-2
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;
}
9.
#include <stdio.h>
#include <math.h>
struct contact {
char name[100];
char home_phone[100];
char cell_phone[100];
}
;
int main(void) {
struct contact list[5];
int i;
char name[100];
for (i = 0; i < 5; i++) {
printf("이름을 입력하시오:");
scanf("%s", list[i].name);
printf("집전화번호를 입력하시오:");
scanf("%s", list[i].home_phone);
printf("휴대폰번호를 입력하시오:");
scanf("%s", list[i].cell_phone);
}
printf("검색할 이름을 입력하시오:");
scanf("%s", name);
for (i = 0; i < 5; i++) {
if (strcmp(name, list[i].name) == 0) {
printf("집전화번호: %s\n", list[i].home_phone);
printf("휴대폰번호: %s\n", list[i].cell_phone);
return 0;
}
}
printf("검색이 실패하였슴\n");
return 0;
}
10.
#include <stdio.h>
#include <math.h>
struct card {
int value;
char suit;
}
;
int main(void) {
struct card cards[52];
int i;
for (i = 0; i < 13; i++) {
cards[i].value = i % 13 + 1;
cards[i].suit = 'c';
}
for (i = 0; i < 13; i++) {
cards[i + 13].value = i % 13 + 1;
cards[i + 13].suit = 'd';
}
for (i = 0; i < 13; i++) {
cards[i + 26].value = i % 13 + 1;
cards[i + 26].suit = 'h';
}
for (i = 0; i < 13; i++) {
cards[i + 39].value = i % 13 + 1;
cards[i + 39].suit = 's';
}
for (i = 0; i < 52; i++) {
printf("%d:%c ", cards[i].value, cards[i].suit);
}
return 0;
}
댓글
댓글 쓰기