past/CIE


(1) Using Array..




/**************************************************************************************
 * 001.c                                                                              *
 * Auther  : Jay Kim [김  재  희, 20023119]                                           *
 * Purpose : Queue Using Array Index.                                                 *
 * Usage   :                                                                          *
 **************************************************************************************/
/* Index,counter implementation of the FIFO */
#include <stdio.h>

#define FifoSize 10                  /* Number of 8 bit data in the Fifo */

unsigned char PutI;                  /* Index of where to put next */
unsigned char GetI;                  /* Index of where to get next */
unsigned char Size;                  /* Number currently in the FIFO */
                               /* FIFO is empty if Size=0 */
                               /* FIFO is full  if Size=FifoSize */
char Fifo[FifoSize];                 /* The statically allocated data */

void InitFifo(void)
{
  PutI=GetI=Size=0;                  /* Empty when Size==0 */
}

int PutFifo (char data)
{
  unsigned char SaveSP;
  if (Size == FifoSize )
  {
    return(0);                       /* Failed, fifo was full */
  }
  else
  {
    Size++;
    Fifo[PutI++]=data;               /*  put data into fifo */
    if (PutI == FifoSize)
    {
      PutI = 0;         /* Wrap */
    }
    return(-1);                      /* Successful */
  }
}

int GetFifo (char *datapt)
{
    if(GetI==PutI)
    {
      return(0);
    }

    *datapt=Fifo[GetI];
    Fifo[GetI]=0;
    GetI++;
    Size--;
    return(1);
}

int DispFifo(void)
{
  unsigned int i;
 
  for(i=0; i<FifoSize; i++)
  {
    printf("Array Index: %4d,  Data: %4d \n", i, Fifo[i]);
  }
  printf("PutI: %2d, GetI: %2d, Size: %2d \n", PutI, GetI, Size);
}
 
int main(void)
{
  unsigned char Input;                // 사용자로부터 입력받은 데이터
  unsigned char PutGet;               // Put or Get 선택.
  int result_put, result_get;         // 함수의 결과 반환값.

  printf("Testing FIFO using Array. \n");
  printf("    Key press x: exit\n");
  printf("    Key press d: display the Fifo\n");

  InitFifo();                // 초기화 함수 call.

  while(1)
  {
    printf("Put(p) or Get(g): ");
    scanf("%c", &PutGet);
   
    if(PutGet=='p')
    {
      printf("Data [0~255]: ");
      scanf("%d", &Input);
      result_put=PutFifo(Input);
      if(result_put==0)
      {
        printf("Error.\n");
      }
    } 
    else if(PutGet=='g')
    {
      result_get=GetFifo(&Input);
      if(result_get!=0)
      {
        printf("Data : %d\n", Input);
      }
      else
      {
        printf("Error.\n");
      }
    }
    else if (PutGet=='d')
    {
      DispFifo();
    } 
    else if (PutGet=='x')
    {
      break;
    }
  }
 
  system("PAUSE"); 
  return 0;
}


(2) Using Linked List..




/**************************************************************************************
 * 002.c                                                                              *
 * Auther  : Jay Kim [김  재  희, 20023119]                                           *
 * Purpose : Queue Using Linked List.                                                 *
 * Usage   :                                                                          *
 **************************************************************************************/
struct Node
{
  unsigned short data;      // 16 bit information
  struct Node *next;        // pointer to the next
};

typedef struct Node NodeType;
typedef NodeType *NodePtr;

NodePtr PutPt;              /* Place to put */
NodePtr GetPt;              /* Place to get */

#define NULL 0
#include <stdio.h>
#include <stdlib.h>

void Fifo_Init(void)
{
  GetPt = NULL;
  PutPt = NULL;
}

int PutFifo (unsigned short data)
{
  NodePtr pt;
  pt=malloc(sizeof(NodeType));
  if(pt==0) return (0); /* Full */

  if(pt!=NULL)
    {   
         pt->data=data;
         pt->next=NULL;
         if(GetPt==NULL)
         GetPt=pt;
         else
         {
             PutPt=GetPt;
             while(PutPt->next!=0)
             {
                 PutPt=PutPt->next;
             }
             PutPt->next=pt;
        
         }
         return(1);
    }
    return(0);   
}

int GetFifo (unsigned short *datapt)
{
  NodePtr pt;
  if(GetPt==0)
  {
    return(0); /*empty*/
  }
 
  *datapt = GetPt->data;
  pt = GetPt;
  GetPt = GetPt->next;
 
  if(GetPt==NULL)
  {
    PutPt = NULL;
  }
  free(pt);
  return(1);
}

void DispFifo(void)
{
  NodePtr pt;
  pt = GetPt;

  while(pt!=0){
    printf("data: %d \n", pt->data);
    pt=pt->next;
  }
}
 
int main(int argc, char *argv[]){
  unsigned short Input;
  unsigned char PutGet;
  int result_put, result_get;         // 함수의 결과 반환값.

  printf("Testing FIFO using Linked List. \n");
  printf("    Key press x: exit\n");

  while (1)
  {
    printf("Put(p) or Get(g): ");
    scanf("%c", &PutGet);
   
    if(PutGet == 'p')
    {
      printf("Data [0~255]: ");
      scanf("%d", &Input);
     
      result_put=PutFifo(Input);
     
      if(result_put==0)
      {
        printf("Error.\n");
      }
    } 
    else if(PutGet == 'g')
    {
      result_get=GetFifo(&Input);
      if(result_get!=0)
      {
        printf("Data : %d\n", Input);
      }
      else
      {
        printf("Error.\n");
      }
    }
    else if (PutGet == 'd')
    {
      DispFifo();
    } 
    else if (PutGet == 'x')
    {
      break;
    }
  }
  system("PAUSE"); 
  return 0;
}



(3) Using Pointer..




/**************************************************************************************
 * 003.c                                                                              *
 * Auther  : Jay Kim [김  재  희, 20023119]                                           *
 * Purpose : Queue Using Pointer.                                                 *
 * Usage   :                                                                          *
 **************************************************************************************/

#include <stdio.h>

#define FifoSize 10                  /* Number of 8 bit data in the Fifo */

unsigned char Size;                  /* Number currently in the FIFO */
char *PutPtr;
char *GetPtr;
unsigned short PutI,GetI,Index;
                               /* FIFO is empty if Size=0 */
                               /* FIFO is full  if Size=FifoSize */
char Fifo[FifoSize];                 /* The statically allocated data */

void InitFifo(void)
{
  PutPtr=&Fifo[0];
  GetPtr=&Fifo[0];
  Size=0;                  /* Empty when Size==0 */
  PutI=0;
  GetI=0;
  Index=0;
}

int PutFifo (char data)
{
  if (Size == FifoSize )
  {
    return(0);                       /* Failed, fifo was full */
  }
  else
  {
    Fifo[Index++]=data;               /*  put data into fifo */
    Size++;
    PutPtr++;
    PutI++;

    if (Index==FifoSize)
    {
      PutPtr = &Fifo[0];         /* Wrap */
      Index=0;
    }
    return(-1);                      /* Successful */
  }
}

int GetFifo (char *datapt)
{
    if(Size==0)
    {
      return(0);
    }

    *datapt=*GetPtr;
    *GetPtr=0;
    GetPtr=GetPtr+1;
    GetI++;
    Size--;
   
    if(GetPtr==&Fifo[9])
    {
      GetPtr=&Fifo[0];
    }
   
    return(1);
}

int DispFifo(void)
{
  unsigned int i;
 
  for(i=0; i<FifoSize; i++)
  {
    printf("Array Index: %4d,  Data: %4d \n", i, Fifo[i]);
  }
  printf("PutI: %2d, GetI: %2d, Size: %2d \n", PutI, GetI, Size);
}
 
int main(void)
{
  unsigned char Input;                // 사용자로부터 입력받은 데이터
  unsigned char PutGet;               // Put or Get 선택.
  int result_put, result_get;         // 함수의 결과 반환값.

  printf("Testing FIFO using Pointer!!!!. \n");
  printf("    Key press x: exit\n");
  printf("    Key press d: display the Fifo\n");

  InitFifo();                // 초기화 함수 call.

  while(1)
  {
    printf("Put(p) or Get(g): ");
    scanf("%c", &PutGet);
   
    if(PutGet=='p')
    {
      printf("Data [0~255]: ");
      scanf("%d", &Input);
      result_put=PutFifo(Input);
      if(result_put==0)
      {
        printf("Error.\n");
      }
    } 
    else if(PutGet=='g')
    {
      result_get=GetFifo(&Input);
      if(result_get!=0)
      {
        printf("Data : %d\n", Input);
      }
      else
      {
        printf("Error.\n");
      }
    }
    else if (PutGet=='d')
    {
      DispFifo();
    } 
    else if (PutGet=='x')
    {
      break;
    }
  }
 
  system("PAUSE"); 
  return 0;
}


'past > CIE' 카테고리의 다른 글

OOP Step2. 【 Banking System v2.0】  (0) 2009.08.27
심심해서 만들어 본 드라이버..  (0) 2009.08.20
Mouse 만들기.. 【 1 】  (0) 2009.08.18
마지막 컴퓨터 언어 레포트..  (0) 2008.12.13
Assignment LLL(Linear Linked List)  (0) 2008.12.09
연습.13-2  (0) 2008.12.01
【 VHDL 】11월 12일 수업 과제..  (0) 2008.11.13
연습 12-2.  (0) 2008.11.10
0 0