Turbo-C
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
터보-C 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
Lua 게시판
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C/C++ Q/A
[5416] 터보씨를 이용해서 구현 하는 건데요..이거..해석 좀..해주세요..;;
[] 1484 읽음    2005-10-23 15:11
#include <stdio.h>

FILE *ip;      
int written[5];

int frozen[5]; 

int buffer[5]; 
int run;      

/*---------------------------------------------------
    This routine initializes the buffer[] & written[].
---------------------------------------------------*/
void initialize()
{
    int i;

    for (i=0; i<5; i++)
        written[i] = 1;
    i=0;
    do {
        fscanf(ip, "%d", &buffer[i]);
        written[i] = 0;
        i++;
    } while (!feof(ip) && i != 5);
}

/*----------------------------------------------------------------
    This routine checks if there are any unfrozen record in buffer.
----------------------------------------------------------------*/
int check_frozen()
{
    int i;

    for (i=0; i<5; i++)
        if (frozen[i] == 0) return 0;

    return 1;
}

/*---------------------------------------------------------------------
    This routine finds the index of buffer which has the smallest value.
    That value should not be frozen.
---------------------------------------------------------------------*/
int find_smallest()
{
    int i, small;

    for (i=0; i<5; i++)
        if (frozen[i] == 0) {
            small = i;
            break;
        }

    for (i=0; i<5; i++)
        if (buffer[small] > buffer[i] && frozen[i] == 0)
            small = i;

    return small;
}

/*------------------------------------------
    This routine makes RUNs for a given file.
------------------------------------------*/
void make_run()
{
    int i, small, last_key;

    run = 1;

    printf("\nThe result RUNS of a given file\n");
    printf("by using Replacement Selection method.\n\n");

   
    while (!feof(ip)) {
        for (i=0; i<5; i++)
            if (!written[i]) frozen[i] = 0;
        printf("RUN %d : ", run++);
       
        while (!check_frozen()) {
            small = find_smallest();
            printf("%3d ", buffer[small]);
            last_key = buffer[small];
            written[small] = 1;
            frozen[small] = 1;
            if (!feof(ip)) {
                fscanf(ip, "%d", &buffer[small]);
                written[small] = 0;
                if (buffer[small] > last_key)
                    frozen[small] = 0;
            }
        }
        printf("\n");
    }
}

/*------------------------------------------
    This routine outputs unwritten records in ascending key order.
    This RUN is the last RUN.
------------------------------------------*/
void last_run()
{
    int i, j, idx, min, tv;
    int temp[5];

    printf("RUN %d : ", run);

    idx = 0;
    for (i=0; i<5; i++)
        if (!written[i])
            temp[idx++] = buffer[i];

    /* selection sort method */
    for (i=0; i<idx-1; i++) {
        min = i;
        for (j=i+1; j<idx; j++)
            if (temp[j] < temp[min])
                min = j;
        tv = temp[i];
        temp[i] = temp[min];
        temp[min] = tv;
    }

    for (i=0; i<idx; i++)
        printf("%3d ", temp[i]);

    printf("\n\n");
}

main()
{
    ip = fopen("d:\\입력파일.c", "r");
    initialize();
    make_run();  
    last_run();  
    fclose(ip);
}

이게 대체선택입니다..파이 처리하는건데..이론으로는 이해 하겠는데..역시 c언어가 들어가면..난감해지네요..
부탁드립니다..

+ -

관련 글 리스트
5416 터보씨를 이용해서 구현 하는 건데요..이거..해석 좀..해주세요..;; 1484 2005/10/23
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.