#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언어가 들어가면..난감해지네요..
부탁드립니다..
|