01.
더보기
#include <stdio.h>
int main(void)
{
struct Employee {
char name[20];
int salary;
};
struct Employee Employee[] = { {"홍길동",2000000},{"임걱정",3000000},"김영철",2500000 };
//안쪽 중괄호는 구조체의 개수만큼 해줘야함, 그룹만 잘 묶으면 뒤는 중괄호 생략 가능
for (int i = 0; i < 3; i++) {
printf("직원 이름: %s 월급여: %d\n", Employee[i].name, Employee[i].salary);
}
return 0;
}
02.
더보기
#include <stdio.h>
int main(void) {
struct fraction {
int numerator;
int denominator;
};
struct fraction a = { 4,5 };
struct fraction b = { 3,7 };
char* var[] = { "a","b" };
printf("%s = %d/%d\n", var[0], a.numerator, a.denominator);
printf("%s = %d/%d\n", var[1], b.numerator, b.denominator);
return 0;
}
03.
더보기
#include <stdio.h>
int main(void) {
struct fraction {
int numerator;
int denominator;
};
struct fraction a = { 4,5 };
struct fraction b = { 3,7 };
struct fraction c;
char* var[] = { "a","b","c" };
printf("%s = %d/%d\n", var[0], a.numerator, a.denominator);
printf("%s = %d/%d\n", var[1], b.numerator, b.denominator);
c.numerator = a.numerator * b.numerator;
c.denominator = a.denominator * b.denominator;
printf("%s = %s*%s = %d/%d\n", var[2], var[0], var[1], c.numerator, c.denominator);
return 0;
}
04.
더보기
#include <stdio.h>
int main(void)
{
typedef struct home_address {
int st_num;
char* street;
char city[20];
}address;
address add1 = { 223,"선릉로","서울시" };
address add2 = { .city = "서울시",.st_num = 567,.street = "경인로" };
//초기화 때는 포인터, 배열 둘 다 "문자열" 형식으로 대입 가능함 컴파일러가 초기화때는 알아서 대입해줌
//add1.street = "선릉로"; //포인터니까 문자열 상수의 주소를 저장 가능함
//add1.city[0] = 'a';
//add1.city[1] = 'b'; //한글은 2바이트씩 먹으므로 이렇게 하면 ab울시 ~ 가 출력됨
//add.city = "adfsfsf"; //배열에 문자열 상수 주소를 넣어주는건 오류가 발생
printf("%s %s %d\n", add1.city, add1.street, add1.st_num);
printf("%s %s %d\n", add2.city, add2.street, add2.st_num);
return 0;
}
05.
더보기
#include <stdio.h>
int main(void)
{
typedef struct home_address {
int st_num;
char* street;
char city[20];
}address;
struct number {
int first;
int middle;
int last;
};
struct person {
address address;
char* name;
struct number number;
};
//이 챕터에서는 그냥 썼지만,
//char* 문자열은 동적으로 할당해 줄 필요가 있기 때문에 주의가 필요하다.
struct person people[] = { {{123,"시청길","춘천시"},"홍길동",{11,1111,1111}},
{{456,"경인로","서울시"},"조명호",{17,3245,3278}},
{{789,"영통로","수원시"},"최윤호",{11,2222,2456}} };
char* head[] = { "주 소","이 름","전화번호" };
printf("%11s %15s %15s\n\n", head[0], head[1], head[2]);
for (int i = 0; i < (int)(sizeof(people) / sizeof(people[0])); i++) {
printf("%s %s %d %10s \t %03d-%d-%d\n", people[i].address.city, people[i].address.street, people[i].address.st_num, people[i].name, people[i].number.first, people[i].number.middle, people[i].number.last);
}
return 0;
}
06.
더보기
#include <stdio.h>
int main(void)
{
struct student {
char name[10];
int st_num;
double avg_grade;
char dept[20];
char goal[15];
};
struct student alumni[] = { {"김형근",52029,4.2,"전자상거래","웹디자이너"},
{"배태준",52030,4.3,"전자상거래","군인"},
{"김보름",52031,4.3,"전자상거래","웹디자이너"},
{"서정순",52032,3.5,"전자상거래","웹마스터"},
{"최종욱",52033,3.7,"전자상거래","DB관리자"} };
char* head[] = { "이 름","학 번","학 점"," 학 과","진 로" };
printf("\n%10s %12s %12s %13s %15s\n\n", head[0], head[1], head[2], head[3], head[4]);
for (int i = 0; i < (sizeof(alumni) / sizeof(alumni[0])); i++) {
printf("%10s\t%07d %10.1lf %15s %15s\n", alumni[i].name, alumni[i].st_num, alumni[i].avg_grade, alumni[i].dept, alumni[i].goal);
}
return 0;
}
07.
더보기
#include <stdio.h>
#include <string.h>
int main(void)
{
typedef struct home_address {
char city[20];
char* town;
char village[20];
}address;
struct number {
int first;
int middle;
int last;
};
struct person {
address address;
char* name;
struct number number;
};
struct professor {
struct person person;
char* subjt[2];
char dept[10];
};
struct professor myprf[5] = { { {{"서울시","구로구","고척동"} , "홍길동" , {11,1111,1111}},{"객체지향", "분석설계"},"전산과" },
{ {{"경기도","안양시","비산동"} , "최윤호" , {11,2222,2456}},{"C 언어", "자바입문"},"전산과" },
{ {{"\0","인천광역시","간석동"} , "박종학" , {11,3333,2456}},{"컴퓨터개론", "프로그래밍"},"전산과" },
{ {{"서울시","강남구","도곡동"} , "이종석" , {16,3467,4389}},{"영어회화", "국어"},"교양과" },
{ {{"경기도","일산시","화곡동"} , "신용현" , {17,2222,2456}},{"기초수학", "이산수학"},"교양과" } };
char* head[] = { "이 름","전화번호","주 소","학 과","담당","과목" };
printf("%7s %10s %17s %15s %12s %7s\n\n", head[0], head[1], head[2], head[3], head[4], head[5]);
for (int i = 0; i<(int)(sizeof(myprf) / sizeof(myprf[0])); i++)
printf("%7s %03d-%d-%d %2s %s %s %9s %11s %11s\n", myprf[i].person.name, myprf[i].person.number.first, myprf[i].person.number.middle, myprf[i].person.number.last, myprf[i].person.address.city, myprf[i].person.address.town, myprf[i].person.address.village, myprf[i].dept, myprf[i].subjt[0], myprf[i].subjt[1]);
return 0;
};
08.
더보기
#include <stdio.h>
int main(void)
{
typedef struct home_address {
char city[20];
char* town;
char village[20];
}address;
struct number {
int first;
int middle;
int last;
};
struct person {
address address;
char* name;
struct number number;
};
struct professor {
struct person person;
char* subjt[2];
char dept[10];
};
struct student {
struct person person;
char* st_num;
double avg_grade;
int dept;
int goal;
struct professor professor;
};
char* wannabe[] = { "공무원", "회사원","프로게이머","웹마스터","웹디자이너","군인","DB관리자" };
char* deparment[] = { "전자상거래","전산정보과","비즈니스과","비서과" };
char* head[] = { "이 름","전화번호","주 소","학 번","평점","학과","진로","지도교수" };
struct student member[10] = { { {{"","서울시","도곡동"},"김영국",{11,4567,9834}},"9952203",4.2,0,0,{.person.name = "최윤호"} },
{ {{"","서울시","도화동"},"강미연",{11,4567,9834}},"9952123",3.5,1,1,{.person.name = "홍길동"} },
{ {{"","서울시","고척동"},"송은혜",{11,4567,9834}},"9952108",3.2,2,1,{.person.name = "박종학"} },
{ {{"","서울시","마곡동"},"박보현",{11,4567,9834}},"9952115",3.0,0,2,{.person.name = "이종석"} },
{ {{"","서울시","화곡동"},"김미경",{11,4567,9834}},"0052028",4.1,0,3,{.person.name = "신용현"} },
{ {{"","서울시","대치동"},"김형근",{11,4567,9834}},"0052029",4.2,2,4,{.person.name = "박종학"} },
{ {{"","서울시","마장동"},"배태준",{11,4567,9834}},"0052030",4.3,0,5,{.person.name = "홍길동"} },
{ {{"","서울시","부평동"},"김보름",{11,4567,9834}},"0052031",4.3,0,4,{.person.name = "최윤호"} },
{ {{"","서울시","도곡동"},"서정순",{11,4567,9834}},"0052032",3.5,2,3,{.person.name = "이종석"} },
{ {{"","서울시","도곡동"},"최종욱",{11,4567,9834}},"0052033",3.7,3,6,{.person.name = "박종학"} } };
printf("%6s %12s %12s %10s %s %8s %12s %9s\n\n", head[0], head[1], head[2], head[3], head[4], head[5], head[6], head[7]);
for (int i = 0; i < (sizeof(member) / sizeof(member[0])); i++) {
printf("%7s %03d-%d-%d %7s%s %s %.2lf %11s %11s %7s\n",member[i].person.name, member[i].person.number.first, member[i].person.number.middle, member[i].person.number.last,
member[i].person.address.town,member[i].person.address.village,member[i].st_num,member[i].avg_grade,deparment[member[i].dept],
wannabe[member[i].goal],member[i].professor.person.name);
}
}