cal.c

파일 입출력이 포인터형식이라는 것을 까먹어 애를 먹었다. 파일의 위치를 기억하고 있는 포인터!!

생각보다 소스가 복잡하네.?!?!

----------------------------------

#include<stdio.h>

int main(void)

{

int i, j, count, even=0;

int height, wide, repeat;

FILE* fp=fopen("input.txt", "rt");

FILE* fo=fopen("output.txt", "wt");


if(fp == NULL)

{

printf(" 파일이 없습니다");

return 1;

}

fscanf(fp, "%d %d", &height, &wide);


if( height%2 == 0)

even = 1;

count =1;

repeat = height/2;


for(i=0; i<repeat; i++)

{

for(j=0;j<wide; j++)

{

fprintf(fo, "%d",count);

if(j == wide-1)

fprintf(fo, "\n");

else

fprintf(fo, " ");

count++;

}


count += wide-1;


for(j=0;j<wide; j++)

{

fprintf(fo, "%d",count);

if(j == wide-1)

fprintf(fo, "\n");

else

fprintf(fo, " ");

count--;

}


count += wide+1;

}

if(even == 0)//홀수이면

{

for(j=0;j<wide; j++)

{

fprintf(fo, "%d",count);

if(j == wide-1)

fprintf(fo, "\n");

else

fprintf(fo, " ");

count++;

}

}


fclose(fp);


return 0;

}




+ Recent posts