본문 바로가기
IT창고/C

C 게시판..

by 창구창고 2007. 1. 22.

📑 목차

    반응형

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

    // debug
    #define _ck(x) printf("ck%d\n",x);

    #define _LINE 25
    #define _COL  80
    #define CMD_MAX 50
    #define PER_MAX 100
    #define PAGE_LF(x) for(int z=1;z<=x;z++) printf("\n")
    #define BAR printf("###############################################################################\n")

    #define TITLE_NO 50
    #define BODY_NO 100
    #define USER_NO 50

    struct PERSONtag {
     char title[TITLE_NO];
     char body[BODY_NO];
     char user[USER_NO];
     int line;
    } person[PER_MAX], *pPER;
    int per_no=0;

    char back(char *);
    char input(char *);
    void view();
    void write();
    void read(int);
    void readFile();
    void writeFile();
    void remove(int);
    int search_(int,char *);
    int strsearch(char *str1,char *str2);

    void main(void)
    {
     char cmd[CMD_MAX], *pCMD=cmd;
     char inKey;
     

     readFile();
     while(inKey!='x' && inKey!='X') {
      switch (inKey=back(cmd)) {
       case 'p':
       case 'P':
        view();
        break;
       case 'w':
       case 'W':
        write();
        break;
       case 'm':
       case 'M':
        while (!isdigit(*pCMD) && *pCMD!=0) pCMD++;
        remove(*pCMD-'0');
       case 's':
       case 'S':
        pCMD++;
        if (*pCMD=='t' || *pCMD=='T') {
         pCMD = strchr(pCMD,' ');
         if(search_(1,++pCMD))
          return;
        }
        else if (*pCMD=='u' || *pCMD=='U') {
         pCMD = strchr(pCMD,' ');
         if(search_(2,++pCMD))
          return;
        }
       default :
        if (isdigit(inKey)) {
         if (inKey<=(per_no+'0'))
          read(inKey-'1');
        } else if (inKey!='x' && inKey!='X')
         printf("It's not command\n");
        break;
      }
     }
    }

    char back(char *in)
    {
     view();
     return input(in);
    }

    char input(char *in)
    {
     *in = 0;
     while (*in==10 || *in==0) {
      printf("[input command] ");
      gets(in);
     };

     return *in;
    }

    void view()
    {
     printf("LIST ##########################################################################\n");
     printf("NO%20s %20s\n","title","user");
     if (per_no>0) {
      for (int i=0;i<per_no;i++)
       printf("%d %20s %20s\n",i+1,person[i].title,person[i].user);
     }
     PAGE_LF(_LINE-(per_no+5)); // 4 + command line(1)
     BAR;
     printf("Help(H|h) Write(W|w) Read(NO) reMove(M|m) Search(S|s) View(V|v) eXit(X|x)\n");
    }

    void write()
    {
     char *bd = person[per_no].body;

     printf("Write####\n");
     printf("title : "); scanf("%s",person[per_no].title);
     printf("user : "); scanf("%s",person[per_no].user);
     printf("text : \n");
     person[per_no].line = 0;
     while ((*bd=getchar())!=24) {
      if (*bd==10)
       person[per_no].line++;
      bd++;
     }
     per_no++;
     *bd = 0;
     writeFile();
    }

    void read(int idx)
    {
     BAR;
     printf("title : %s\n",person[idx].title);
     printf("user : %s\n",person[idx].user);
     printf("text : %s\n",person[idx].body);
     PAGE_LF(_LINE-(person[idx].line+3+3)); // 2 + command line(1)
     BAR;
        getchar();
    }

    void remove(int idx)
    {
     if (per_no!=0) per_no--;
     
     if (per_no!=idx) {
      for (int i=idx-1;i<per_no;i++) {
       strcpy(person[i].title,person[i+1].title);
       strcpy(person[i].user,person[i+1].user);
       strcpy(person[i].body,person[i+1].body);
       person[i].line = person[i+1].line;
      }
     }
     writeFile();
    }

    void readFile()
    {
     FILE *fp;

     if (!(fp=fopen("d:\\odete\\study\\board.txt","r")))
      return;

     per_no = fgetc(fp);
     fread((PERSONtag *)person,sizeof(person),1,fp);
     fclose(fp);
    }

    void writeFile()
    {
     FILE *fp;

     if (!(fp=fopen("d:\\odete\\study\\board.txt","w")))
      return;

     fputc(per_no,fp);
     fwrite((PERSONtag *)person,sizeof(person),1,fp);
     fclose(fp);
    }

    int search_(int sel,char *key)
    {
     int sh[PER_MAX];
     char cmd[CMD_MAX];
     int shi=0;
     int i;
     char inKey=0;

     if (sel==1) {
      for (i=0;i<per_no;i++) {
       if (!strsearch(person[i].title,key)) {
        sh[shi] = i;
        shi++;
       }
      }
     } else {
      for (i=0;i<per_no;i++) {
       if (!strsearch(person[i].user,key)) {
        sh[shi] = i;
        shi++;
       }
      }
     }

     while (inKey!='p' && inKey!='P') {
      printf("SEARCH ########################################################################\n");
      printf("NO%20s %20s\n","title","user");
      if (shi>0) {
       for (i=0;i<shi;i++)
        printf("%d %20s %20s\n",sh[i],person[sh[i]].title,person[sh[i]].user);
      }
      PAGE_LF(_LINE-(shi+5)); // 4 + command line(1)
      BAR;
      printf("Help(H|h) Read(NO) Up(P|p) eXit(X|x) \n");
      if (isdigit(inKey=input(cmd))) {
       if (inKey<=(per_no+'0'))
        read(inKey-'1');
      } else if (inKey=='x' || inKey=='X')
       return 1;
      else
       printf("It's not command\n");
     }
     return 0;
    }

    int strsearch(char *str1,char *str2)
    {
     int len1, len2;
     int flag=0;
     
     len1 = strlen(str1);
     len2 = strlen(str2);
     if (len1<len2)
      return 1;

     for(int i=0,j=0;i<len1 && j<len2;i++) {
      if (flag==0) {
       if (*str1++==*str2) {
        flag = 1;
        j++; str2++;
        if (j==len2) return 0;
       }
      } else if (flag==1) {
       if (*str1++==*str2++)
        j++;
        if (j==len2) return 0;
       else flag = 0;
      } 
     }
     return 1;
    }

    반응형

    "이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다."