구현 코드:

 

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

#define OUT_FILE_NAME "output.txt"

#define INPUT_FILE_NAME "input.txt"

 

void ReadInput(int *len, int *type);

void WriteOutput(char *text);

void RemoveOutput();

 

void ProcType1(int len);

void ProcType2(int len);

void ProcType3(int len);

 

 

int main()

{

          int len =0, type=0;

 

          void (*ProcTypeFn[4])(int);

          ProcTypeFn[1] = ProcType1;

          ProcTypeFn[2] = ProcType2;

          ProcTypeFn[3] = ProcType3;    

 

          RemoveOutput();

          ReadInput(&len, &type);

 

          ProcTypeFn[type](len);

         

          return 0;

}

 

void ProcType1(int len)

{

          //한변의 길이 len 정사각형을 아래와 같이 출력

          //1 1 1 1 1

          //2 2 2 2 2

          //3 3 3 3 3

          //4 4 4 4 4

          //5 5 5 5 5

          int i=0, j=0;

          char text[20] = {0};

          for(i=1;i<=len;i++)

          {

                    for(j=1;j<=len;j++)

                    {

                               sprintf(text, "%d ", i);

                               WriteOutput(text);

                    }

                    WriteOutput("\n");

          }

}

 

void ProcType2(int len)

{

          //한변의 길이 len 정사각형을 아래와 같이 출력

          //1 2 3 4 5

          //5 4 3 2 1

          //1 2 3 4 5

          //5 4 3 2 1

          //1 2 3 4 5

          int i=0, j=0;

          int direction = 1;

          char text[20] = {0};

          for(i=1;i<=len;i++)

          {

                    if(direction)

                    {

                               for(j=1;j<=len;j++)

                               {

                                         sprintf(text, "%d ", j);

                                         WriteOutput(text);

                               }

                    }

                    else

                    {

                               for(j=len;j>0;j--)

                               {

                                         sprintf(text, "%d ", j);

                                         WriteOutput(text);

                               }

                    }

                   

                    direction = direction==1 ? 0 : 1;

 

                    WriteOutput("\n");

          }

}

void ProcType3(int len)

{

          //한변의 길이 len 정사각형을 아래와 같이 출력

          //1 2 3 4 5

          //2 4 6 8 10

          //3 6 9 12 15

          //4 8 12 16 20

          //5 10 1 20 25

          int i=0, j=0;

          int num = 0;

          char text[20] = {0};

 

          for(i=1;i<=len;i++)

          {

                    num = i;

                    for(j=1;j<=len;j++)

                    {

                               sprintf(text, "%d ", num);

                               WriteOutput(text);

                               num = num + i;

                    }

                    WriteOutput("\n");

          }

}

 

void RemoveOutput()

{

          unlink(OUT_FILE_NAME);

}

 

void WriteOutput(char *text)

{

          FILE* fs;

          char *res=0;

          fs=fopen(OUT_FILE_NAME,"a");

          if(fs)

          {

                    while(*text != '\0')

                    {

                               fwrite(text, 1, 1, fs);

                               text++;

                    }

                   

                    fclose(fs);

          }

}

void ReadInput(int *len, int *type)

{

          FILE* fs;

          char data[100]={0};

          char *res=0;

          fs=fopen(INPUT_FILE_NAME,"r");

          if(fs)

          {

                    fread(data, sizeof(char),5,fs);

                    *len = atoi(strtok(data, " "));

                    *type = atoi(strtok(0, " "));

                    fclose(fs);

          }

}

 

+ Recent posts