본문 바로가기

반응형

C

(149)
[C언어] 구조체 안에서 다른 구조체를 멤버 변수로 가질 수 있다. 구조체 안에서 다른 구조체를 멤버 변수로 가질 수 있다. #include #include // strlen(), strcpy() struct profile{ int age; double height; char* name; // 동적할당을 위한 포인터 변수를 선언한다. }; struct student{ struct profile pf; // 다른 구조체를 멤버로 가진다. int id; double grade; }; int main(){ struct student stu; char tmp_name[10]; // (필수) 동적할당을 위해 임의의 변수를 선언해준다. printf("이름 입력 : "); scanf("%s", tmp_name); stu.pf.name = (char*)malloc(strlen(tmp_..
[C언어] 실습: 구조체 'cracker'로 과자의 가격 및 열량 출력하기 구조체 'cracker'로 과자의 가격 및 열량 출력하기 입력형식 출력형식 빈츠의 가격과 열량을 입력하세요: 3000, 45 가격: 3000원 열량: 45kcal #include typedef struct cracker{ // 구조체 선언 int price; int cal; }Cracker; int main(){ Cracker c1; // 구조체 변수 선언 printf("빈츠의 가격과 열량을 입력하세요 : "); scanf("%d, %d", &c1.price, &c1.cal); printf("가격 : %d 원\n", c1.price); printf("열량 : %d kcal\n", c1.cal); return 0; }
[C언어] 실습: 좌표값(x, y) 출력하기 좌표값(x, y) 출력하기 #include typedef struct point{ int x; int y; } Point; void printp(Point p); // 좌표값을 출력하는 함수 int setX(Point *p); // x의 좌표값을 변경하는 함수 int main(){ Point p1 = {2, 4}; // Point 구조체의 변수 p를 선언하고 초기화해준다. printf("x : %3d, y : %3d\n", p1.x, p1.y); p1.x = 10; p1.y = 20; printp(p1); p1.x = setX(&p1); // p1의 값을 바꾸기 위해 인자에 주소값을 전달한다. printp(p1); return 0; } void printp(Point p){ printf("x : %3d,..
[C언어] 실습: 입력받은 숫자가 배열에 저장되어 있는지 확인하기 입력받은 숫자가 배열에 저장되어 있는지 확인하기 #include int main(){ int arr[5] = {1, 2, 3, 4, 5}; // 크기 5의 배열을 생성하여 초기화한다. int num; int index = -1; // 입력한 값이 arr[]에 있는지 확인하기 위해, index 값은 arr[]에 초기화되지 않은 값으로 저장한다. printf("num : "); scanf("%d", &num); // 값을 입력받아 변수 num에 저장한다. for(int i = 0; i < 5; ++i){ // arr[] 배열의 for 문을 돌며, if(num == arr[i]) // 입력한 num의 값이 arr[] 배열에 저장되어 있으면 index = i; // 해당하는 index의 값을 저장한다. } if..
[C언어] 실습: 이름을 검색하여 해당하는 연락처 출력하기 (구조체) 이름을 검색하여 해당하는 연락처 출력하기 (구조체) #include #include // strcmp() #define STR_SIZE 20 typedef struct book{ char name[STR_SIZE]; char phone[STR_SIZE]; int ringtone; } PhoneBook; // 구조체 struct book을 phoneBook으로 선언한다. int main(){ PhoneBook pb[] = { // 구조체의 변수 pb[]를 생성하고, 초기화 해준다. {"홍길동", "010-1111-1111", 1}, {"장보고", "010-2222-2222", 2}, {"김유신", "010-3333-3333", 3}, {"이순신", "010-4444-4444", 4}, {"유관순", "01..
[C언어] 실습: 구조체 'student'로 3명의 총점 및 평균을 계산하는 성적 처리 프로그램 구현하기 구조체 'student'로 3명의 총점 및 평균을 계산하는 성적 처리 프로그램 구현하기 #include struct student{ int kor, eng, math, tot; double avg; }; int main(){ // 선언문 struct student stu[3]; // 구조체 student의 변수 stu[] 선언 // 입력문, 처리문 for(int i = 0; i < 3; ++i){ printf("[ student %d ] 입력\n", i + 1); printf("국어 : "); scanf("%d", &stu[i].kor); printf("영어 : "); scanf("%d", &stu[i].eng); printf("수학 : "); scanf("%d", &stu[i].math); stu[i]..
[C언어] 구조체 객체 복사 같은 구조체는 객체를 복사할 수 있다. #define _CRT_SECURE_NO_WARNINGS #include #include // strcpy(), strcmp() struct student { // student 구조체 선언 int id; char name[20]; int score; }; int main() { struct student stu1; // 구조체 변수 선언 struct student stu2 = { 1234, "aaa", 88 }; struct student stu3; stu3.id = 4567; strcpy(stu3.name, "ccc"); stu3.score = 90; printf("[ stu3 ]\n"); printf("id : %d\n", stu3.id); printf("na..
[C언어] 실습: 구조체 'car'로 자동차, 속도, 연료 상태 출력하기 구조체 'car'로 자동차, 속도, 연료 상태 출력하기 ​ 조건1. Accel을 밟으면 속도는 10km/s씩 증가하고, 연료는 2%씩 감소 조건2. Break를 밟으면 속도는 10km/s씩 감소 조건3. 자동차 최대 속도는 200km/s #include #define MAX_SPEED 200 // 최고 속도 #define STEP 2 // 연료 소모량 #define FULL 10 // 증감 속도량 struct car { // 구조체 car 선언 char name[20]; // 자동차 int speed; // 현재 속도 int gas; // 현재 연료 }; typedef struct car Car; // 모든 함수에서 사용하기 위해 전역변수로 만들어준다. // struct car == Car void ca..

반응형