관리 메뉴

개발 노트

3/22 3주차(종강 후 공개) 본문

학부 공부 : 21.03.02~06.20/데이터베이스 이론 및 실습

3/22 3주차(종강 후 공개)

hayoung.dev 2021. 3. 22. 12:00

[C언어 구조체]

 

엑셀 테이블

1. 엑셀의 테이블 사원리스트(Emp), 부서리스트(Dept)를 구조체로 작성하여 출력하고 사원리스트와 부서명 합쳐서 출력하기

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

int main()
{
    typedef struct employee {   //구조체(테이블 형식) employee를 EMP로 정의
        int empno;
        char empname[10];
        int deptno;
    } EMP;

    typedef struct dept {
        int deptno;
        char deptname[10];
    } DEPT;

    typedef struct employeedept {
        int deptno;
        int empcnt;
    } EMPDEPT;

    EMP emps[8] = {
        {10, "Kim", 200 },
        {20, "Lee", 100 },
        {30, "Park", 200 },
        {40, "Choi", 300 },
        {50, "Kwon", 300 },
        {60, "Shim", 300 },
        {70, "Hwang", 200 },
        {80, "Jenny", 300 },
    };

    DEPT depts[3] = {
        {100, "Marketing" },
        {200, "Planning" },
        {300, "Financing" }
    };

    EMPDEPT *empdept;   // EMPDEPT포인터    

    int i, j;

    printf("\n* Get All Emps...\n");
    for (i = 0; i < sizeof(emps) / sizeof(EMP); i++) {  // emps 사이즈(8)/구조체EMP사이즈(1)= 8 =레코드개수
        printf("%d %s %d\n", emps[i].empno, emps[i].empname, emps[i].deptno);
    }

    printf("\n* Get All Depts...\n");
    for (i = 0; i < sizeof(depts) / sizeof(DEPT); i++) {
        printf("%d %s\n", depts[i].deptno, depts[i].deptname);
    }

    printf("\n* Get All Emps with Deptname...\n");
    for (i = 0; i < sizeof(emps) / sizeof(EMP); i++) {  //EMP
        for (j = 0; j < sizeof(depts) / sizeof(DEPT); j++) {    //DEPT
            if (emps[i].deptno == depts[j].deptno)
                printf("%d %s %s\n", emps[i].empno, emps[i].empname, depts[j].deptname);
        }
    }
    return 0;
}

 

출력 결과

 

 

2. 부서별 인원 수(EmpDept)를 구조체 포인터의 동적인 메모리 할당을 사용하여 구하고 엑셀과 같이 출력, 인원 수가 가장 많은 부서 번호를 구하여 출력, 인원수가 가장 많은 부서의 사원 리스트(부서명 포함)를 구하여 출력

 

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

int main()
{
    typedef struct employee {   //구조체(테이블 형식) employee를 EMP로 정의
        int empno;
        char empname[10];
        int deptno;
    } EMP;

    typedef struct dept {
        int deptno;
        char deptname[10];
    } DEPT;

    typedef struct employeedept {
        int deptno;
        int empcnt;
    } EMPDEPT;

    EMP emps[8] = {
        {10, "Kim", 200 },
        {20, "Lee", 100 },
        {30, "Park", 200 },
        {40, "Choi", 300 },
        {50, "Kwon", 300 },
        {60, "Shim", 300 },
        {70, "Hwang", 200 },
        {80, "Jenny", 300 },
    };

    DEPT depts[3] = {
        {100, "Marketing" },
        {200, "Planning" },
        {300, "Financing" }
    };

    EMPDEPT *empdept;   // EMPDEPT포인터    

    int i, j;

    int found = 0, num = 0;     // 0:False, 1:True

    // 부서번호별 인원수를 구함
    printf("\n* Get the # of emp grouping by deptname...\n");
    for (i = 0; i < sizeof(emps) / sizeof(EMP); i++) {  //8번 반복
        found = 0;  //False, 못 찾음
        for (j = 0; j < num; j++) {  //처음엔 num-0이므로 실행안함
            if (emps[i].deptno == (empdept + j)->deptno) {  //emp의 deptno가 empdept의 deptno에 있다면 
                (empdept + j)->empcnt++;    //empcnt만 증가시킨다.
                found = 1; //found가 true
            }
        }
        if (!found) {   //찾지 못하면
            if (empdept == NULL) {  //아무것도 없으면
                num = 1;    //한 줄 추가
                empdept = (EMPDEPT*)malloc(num * sizeof(EMPDEPT));
                //empdept에 EMPDEPT num 사이즈만큼의 메모리를 받아서 EMPDEPT 메모리로 넣는다.
                //즉 empdept에 저장할 한 줄을 만든다.
            }
            else {
                num++;
                empdept = (EMPDEPT*)realloc(empdept, (int)(num * sizeof(EMPDEPT)));
                //empdept를 확장하여 (num * sizeof(EMPDEPT)만큼 만든다.
                //레코드 넣을 공간을 한 줄 더 만든다.
            }
            (empdept + (num - 1))->deptno = emps[i].deptno; //empdept + (num - 1)위치의 번호에 emps[i].deptno를 기록하고
            (empdept + (num - 1))->empcnt = 1;  //count를 1로 기록
        }
    }
    
    // 부서번호별 인원수를 출력
    for (i = 0; i < num; i++)
        printf("empdept %d %d\n", (empdept + i)->deptno, (empdept + i)->empcnt);

    int deptmax = -1, maxempdept = 0;

    // 인원수가 가장 많은 부서번호를 구하여 출력
    for (i = 0; i < num; i++) {
        if ((empdept + i)->empcnt > deptmax)
            deptmax = (empdept + i)->empcnt;
        maxempdept = (empdept + i)->deptno;
    }
    printf("\ndeptmax %d, maxempdept %d\n\n", deptmax, maxempdept);
    //인원 수가 deptmax, 인원수가 많은 부서 이름이 maxempdept

    // 인원수가 가장 많은 부서의 사원 리스트(부서명포함)를 구하여 출력
    for (i = 0; i < sizeof(emps) / sizeof(EMP); i++) {
        for (j = 0; j < sizeof(depts) / sizeof(DEPT); j++) {
            if (emps[i].deptno == depts[j].deptno && depts[j].deptno == maxempdept)
            //emp의 부서 번호와 dept의 부서 번호가 같을 때 and 부서 번호가 maxempdept일 때
                printf("%d %s %s\n", emps[i].empno, emps[i].empname, depts[j].deptname);
        }
    }

    return 0;
}

출력 결과

 

 

 

전체 c언어 테이블 공부 소스코드

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

int main()
{
    typedef struct employee {   //구조체(테이블 형식) employee를 EMP로 정의
        int empno;
        char empname[10];
        int deptno;
    } EMP;

    typedef struct dept {
        int deptno;
        char deptname[10];
    } DEPT;

    typedef struct employeedept {
        int deptno;
        int empcnt;
    } EMPDEPT;

    EMP emps[8] = {
        {10, "Kim", 200 },
        {20, "Lee", 100 },
        {30, "Park", 200 },
        {40, "Choi", 300 },
        {50, "Kwon", 300 },
        {60, "Shim", 300 },
        {70, "Hwang", 200 },
        {80, "Jenny", 300 },
    };

    DEPT depts[3] = {
        {100, "Marketing" },
        {200, "Planning" },
        {300, "Financing" }
    };

    EMPDEPT *empdept;   // EMPDEPT포인터    

    int i, j;

    printf("\n* Get All Emps...\n");
    for (i = 0; i < sizeof(emps) / sizeof(EMP); i++) {  // emps 사이즈(8)/구조체EMP사이즈(1)= 8 =레코드개수
        printf("%d %s %d\n", emps[i].empno, emps[i].empname, emps[i].deptno);
    }

    printf("\n* Get All Depts...\n");
    for (i = 0; i < sizeof(depts) / sizeof(DEPT); i++) {
        printf("%d %s\n", depts[i].deptno, depts[i].deptname);
    }

    printf("\n* Get All Emps with Deptname...\n");
    for (i = 0; i < sizeof(emps) / sizeof(EMP); i++) {  //EMP
        for (j = 0; j < sizeof(depts) / sizeof(DEPT); j++) {    //DEPT
            if (emps[i].deptno == depts[j].deptno)
                printf("%d %s %s\n", emps[i].empno, emps[i].empname, depts[j].deptname);
        }
    }

    int found = 0, num = 0;     // 0:False, 1:True

    // 부서번호별 인원수를 구함
    printf("\n* Get the # of emp grouping by deptname...\n");
    for (i = 0; i < sizeof(emps) / sizeof(EMP); i++) {  //8번 반복
        found = 0;  //False, 못 찾음
        for (j = 0; j < num; j++) {  //처음엔 num-0이므로 실행안함
            if (emps[i].deptno == (empdept + j)->deptno) {  //emp의 deptno가 empdept의 deptno에 있다면 
                (empdept + j)->empcnt++;    //empcnt만 증가시킨다.
                found = 1; //found가 true
            }
        }
        if (!found) {   //찾지 못하면
            if (empdept == NULL) {  //아무것도 없으면
                num = 1;    //한 줄 추가
                empdept = (EMPDEPT*)malloc(num * sizeof(EMPDEPT));
                //empdept에 EMPDEPT num 사이즈만큼의 메모리를 받아서 EMPDEPT 메모리로 넣는다.
                //즉 empdept에 저장할 한 줄을 만든다.
            }
            else {
                num++;
                empdept = (EMPDEPT*)realloc(empdept, (int)(num * sizeof(EMPDEPT)));
                //empdept를 확장하여 (num * sizeof(EMPDEPT)만큼 만든다.
                //레코드 넣을 공간을 한 줄 더 만든다.
            }
            (empdept + (num - 1))->deptno = emps[i].deptno; //empdept + (num - 1)위치의 번호에 emps[i].deptno를 기록하고
            (empdept + (num - 1))->empcnt = 1;  //count를 1로 기록
        }
    }

    // 부서번호별 인원수를 출력
    for (i = 0; i < num; i++)
        printf("empdept %d %d\n", (empdept + i)->deptno, (empdept + i)->empcnt);


    int deptmax = -1, maxempdept = 0;

    // 인원수가 가장 많은 부서번호를 구하여 출력
    for (i = 0; i < num; i++) {
        if ((empdept + i)->empcnt > deptmax)
            deptmax = (empdept + i)->empcnt;
        maxempdept = (empdept + i)->deptno;
    }
    printf("\ndeptmax %d, maxempdept %d\n\n", deptmax, maxempdept);
    //인원 수가 deptmax, 인원수가 많은 부서 이름이 maxempdept


    // 인원수가 가장 많은 부서의 사원 리스트(부서명포함)를 구하여 출력
    for (i = 0; i < sizeof(emps) / sizeof(EMP); i++) {
        for (j = 0; j < sizeof(depts) / sizeof(DEPT); j++) {
            if (emps[i].deptno == depts[j].deptno && depts[j].deptno == maxempdept)
            //emp의 부서 번호와 dept의 부서 번호가 같을 때 and 부서 번호가 maxempdept일 때
                printf("%d %s %s\n", emps[i].empno, emps[i].empname, depts[j].deptname);
        }
    }

    return 0;
}

 

 

[SQL Plus 실행]

 

1. cmd > sqlplus > 로그인

2. demobld.sql파일을 다운받은 후 

SQL > @"C:\...demobld.sql 파일 설치되어 있는 주소\demobld.sql";

하면 코드파일 실행 됨

 

3. sql developer 실행 > 왼쪽 위 초록색 + 버튼 > Name 설정 후 사용자 이름, 비빌번호 입력 > 테스트, 저장, 접속 순으로 클릭

4. 작업 준비 완료

 

 

**oracle을 설치한 뒤로 oracle을 사용하지 않는 중인데 메모리를 차지하는 경우가 있다.

그럴 때에는

서비스 > oracle로 시작하는 하단 4개 설정 > 자동(지연)을 수동 으로 바꾼다.

대신 sqlplus를 실행할 때에는

ERROR : ORA-12560: TNS:프로토콜 어댑터 오류

가 뜰 수 있다. 

이러한 경우에는 다시 자동(지연)으로 바꿔주거나 시작을 누르면 된다.

 

반응형