'프로그래밍/C언어'에 해당하는 글 15건

#include <stdio.h>

#include <stdlib.h>

#include <string.h>


void reverse(char * pStr);

 

void main()

{

  char str[100];

  printf("Input string : ");

  gets(str);

  reverse(str);

  printf("Reverse sting : %s\n", str);

  system("PAUSE");

}

 

void reverse(char * pStr)

{

  int nLen = strlen(pStr);

  int nLoop = 0;

  char * tmp = NULL;

  for(nLoop=0; nLoop<nLen/2; nLoop++)

  {

    tmp = *(pStr+nLoop);

    *(pStr+nLoop) = *(pStr+nLen-1-nLoop);

    *(pStr+nLen-1-nLoop) = tmp;

  }

}

저작자 표시
신고

WRITTEN BY
빅터크룸
머 먹고 사냐.....

받은 트랙백이 없고 , 댓글이 없습니다.
secret



저작자 표시
신고

WRITTEN BY
빅터크룸
머 먹고 사냐.....

받은 트랙백이 없고 , 댓글이 없습니다.
secret

출력 모습

1단

2단  3단

4단  5단  6단

7단  8단  9단  10단

 

소스


#include stdio.h>
void main()
{
   int nLoop_1;
   int nLoop_2;
   int nLoop_3;
   for(nLoop_1=1 ; nLoop_1<=7; nLoop_1+=nLoop_3)
   {
      for(nLoop_2=1 ; nLoop_2<=9; nLoop_2++)
      {
         for(nLoop_3=0 ; nLoop_3<=(nLoop_1-nLoop_3); nLoop_3++)
         {
            printf("%2d * %1d = %2d\t", (nLoop_3+nLoop_1), nLoop_2, (nLoop_3+nLoop_1) * nLoop_2);
         }
         printf("\n");
      }
      printf("\n");
  }
}

저작자 표시
신고

WRITTEN BY
빅터크룸
머 먹고 사냐.....

받은 트랙백이 없고 , 댓글이 없습니다.
secret

#include stdio.h>
#include stdlib.h>

#define CLS   system("cls")
#define PAUSE system("pause")

#define SAFE_FREE(a) if(a){free(a); a=NULL;}
#define MALLOC       (Node *)malloc(sizeof(Node))

#define ALL 0
#define ONE 1

typedef struct _Node
{
   int           m_nData;
   struct _Node *m_pNext;
}Node;

void LinkedList_DisplayNode (Node *pHead, int nCase);
void LinkedList_AddNode (Node **ppHead, int nData);
void LinkedList_InsertNode (Node **ppHead, int nData, int nPosition);
void LinkedList_RemoveNode(Node **ppHead, int nData);
void LinkedList_RemoveAllNode(Node **ppHead);
void LinkedList_SearchNode(Node *pHead, int nData);

int main(void)
{
   int   nSelect = 0;
   int   nTemp = 0;
   int   nPosition = 0;
   Node *pHead = NULL;

   while(1)
   {
      CLS;
      printf("\n   __________________________________\n");
      printf("   |________ Linked List TEST _______|\n");
      printf("   | Select     >>>> 1. Add          |\n");
      printf("   |            >>>> 2. Insert       |\n");
      printf("   |            >>>> 3. Search       |\n");
      printf("   |            >>>> 4. Remove       |\n");
      printf("   |            >>>> 5. Remove All   |\n");
      printf("   |            >>>> 6. Print        |\n");
      printf("   |            >>>> 0. Exit         |\n");
      printf("   |_________________________________|\n");
      printf("   Enter a select Number : ");
      scanf("%d", &nSelect);

      if(nSelect == 0)
      {
         CLS;
         printf("\n   ______________________________\n");
         printf("       Exit Linked List TEST \n");
         printf("   ______________________________\n");
         LinkedList_RemoveAllNode(&pHead);
         return;
      }
      else if(nSelect == 1)
      {
         CLS;
         printf("\n   ______________________________\n");
         printf("      Add Linked List TEST\n");
         printf("   ______________________________\n");
         printf("   Enter a input Data : ");
         scanf("%d", &nTemp);
         LinkedList_AddNode(&pHead, nTemp);
         PAUSE;
      }
      else if(nSelect == 2)
      {
         CLS;
         printf("\n   ______________________________\n");
         printf("      Insert Linked List TEST\n");
         printf("   ______________________________\n");
         printf("   Enter a input Data : ");
         scanf("%d", &nTemp);
         printf("   Enter a taget Node : ");
         scanf("%d", &nPosition);
         LinkedList_InsertNode(&pHead, nTemp, nPosition);
         PAUSE;
      }
      else if(nSelect == 3)
      {
         CLS;
         printf("\n   ______________________________\n");
         printf("      Search Linked List TEST\n");
         printf("   ______________________________\n");
         printf("   Enter a search Data : ");
         scanf("%d", &nTemp);
         LinkedList_SearchNode(pHead, nTemp);
         PAUSE;
      }
      else if(nSelect == 4)
      {
         CLS;
         printf("\n   ______________________________\n");
         printf("      Remove Linked List TEST\n");
         printf("   ______________________________\n");
         printf("   Enter a remove Data : ");
         scanf("%d", &nTemp);
         LinkedList_RemoveNode(&pHead, nTemp);
         PAUSE;
      }
      else if(nSelect == 5)
      {
         CLS;
         printf("\n   ______________________________\n");
         printf("      Remove All Linked List TEST\n");
         printf("   ______________________________\n");
         LinkedList_RemoveAllNode(&pHead);
         printf("   Remove All SUCCESS!!\n");
         PAUSE;
      }
      else if(nSelect == 6)
      {
         CLS;
         printf("\n   ______________________________\n");
         printf("      Display Linked List TEST\n");
         printf("   ______________________________\n");
         LinkedList_DisplayNode(pHead, ALL);
         PAUSE;
      }
   }
   return;
}

void LinkedList_DisplayNode (Node *pHead, int nCase)
{
   Node *pTemp = NULL;

   pTemp = pHead;

   switch(nCase)
   {
      case 0: // 전체 노드 출력
         while(pTemp != NULL)
         {
            printf("\n   ______________________________\n");
            printf("   LinkedList Data : %d\n", pTemp->m_nData);
            printf("   LinkedList Next : %x\n", pTemp->m_pNext);
            pTemp = pTemp->m_pNext;
         }
         printf("   ______________________________\n");
         break;
      case 1: // 한 노드만 출력
         printf("\n   ______________________________\n");
         printf("   LinkedList Data : %d\n", pTemp->m_nData);
         printf("   LinkedList Next : %x\n", pTemp->m_pNext);
         printf("   ______________________________\n");
         break;
   }
}

void LinkedList_AddNode (Node ** ppHead, int nData)
{
   Node *pTemp = NULL;
   Node *pSearch = NULL;

   pTemp = MALLOC;
   
   if(pTemp == NULL)
   {
      printf("   Malloc is failed!!\n");
      return;
   }
   pTemp->m_nData = nData;
   pTemp->m_pNext = NULL;

   //노드가 생성되지 않은 경우
   if(*ppHead == NULL)
   {
      *ppHead = pTemp;
      LinkedList_DisplayNode(*ppHead, ONE);
   }
   //노드가 1 이상 존재하는 경우
   else
   {
      pSearch = *ppHead;
      //마지막 노드 검색
      while(pSearch->m_pNext != NULL)
      {
         pSearch = pSearch->m_pNext;
      }
      //마지막 노드 다음에 노드 추가
      pSearch->m_pNext = pTemp;
      //추가 된 노드 정보 출력
      pSearch = pSearch->m_pNext;
      LinkedList_DisplayNode(pSearch, ONE);
   }
}

void LinkedList_InsertNode (Node **ppHead, int nData, int nPosition)
{
   Node *pTemp = NULL;
   Node *pTarget = NULL;

   pTemp = MALLOC;
   
   if(pTemp == NULL)
   {
      printf("   Malloc is failed!!\n");
      return;
   }
   
   pTemp->m_nData = nData;
   pTemp->m_pNext = NULL;

   if(*ppHead == NULL)
   {
      *ppHead = pTemp;
      LinkedList_DisplayNode(*ppHead, ONE);
   }
   else
   {
      pTarget = *ppHead;

      //노드 검색
      while(pTarget != NULL && (pTarget->m_nData != nPosition))
      {
         pTarget = pTarget->m_pNext;
      }
     
      //노드를 찾은 경우
      if(pTarget != NULL)
      {
         //해당 노드 뒤에 노드 추가
         pTemp->m_pNext = pTarget->m_pNext;
         pTarget->m_pNext = pTemp;
         LinkedList_DisplayNode(pTemp, ONE);
      }
      //노드를 찾지 못한 경우
      else
      {
         printf("\n   Not Find!!\n");
         SAFE_FREE(pTarget);
         SAFE_FREE(pTemp);
      }
   }
}

void LinkedList_RemoveNode(Node **ppHead, int nData)
{
   Node *pTemp = NULL;
   Node *pTarget = NULL;

   pTemp = *ppHead;
   pTarget = *ppHead;

   //노드 검색
   while(pTemp != NULL && (pTemp->m_nData != nData))
   {
      pTarget = pTemp;
      pTemp = pTemp->m_pNext;
   }

   //노드를 찾은 경우
   if(pTemp != NULL)
   {
      if(pTemp == pTarget)
      {
         printf("\n   Remove object is Head!!\n");
         printf("\n   Remove SUCCESS!!\n");
         *ppHead = (*ppHead)->m_pNext;
         SAFE_FREE(pTemp);
      }
      else
      {
         pTarget->m_pNext = pTemp->m_pNext;
         SAFE_FREE(pTemp);
         printf("\n   Remove SUCCESS!!\n");
      }
   }
   //노드를 찾지 못한 경우
   else
   {
      printf("\n   Not Find!!\n");
      SAFE_FREE(pTemp);
   }
}

void LinkedList_RemoveAllNode(Node **ppHead)
{
   Node *pTemp = NULL;
   Node *pDelete = NULL;

   pTemp = *ppHead;

   while(pTemp != NULL)
   {
      pDelete = pTemp;
      pTemp = pTemp->m_pNext;
      SAFE_FREE(pDelete);
   }

   (*ppHead) = NULL;
}

void LinkedList_SearchNode(Node *pHead, int nData)
{
   Node *pTemp = NULL;

   pTemp = pHead;

   //노드 검색
   while(pTemp != NULL && (pTemp->m_nData != nData))
   {
      pTemp = pTemp->m_pNext;
   }

   //노드를 찾은 경우
   if(pTemp != NULL)
   {
      printf("\n   Find SUCCESS!!\n");
      LinkedList_DisplayNode(pTemp, ONE);
   }
   //노드를 찾지 못한 경우
   else
   {
      printf("\n   Not Find!!\n");
   }
}

저작자 표시
신고

WRITTEN BY
빅터크룸
머 먹고 사냐.....

받은 트랙백이 없고 , 댓글이 없습니다.
secret

#include 

#define CLS    system("cls")
#define PAUSE  system("pause")

#define RELEASEIF(a)    if(a){free(a); a=NULL;}
#define MALLOC(x)          x=(Node *)malloc(sizeof(Node));if(x == NULL){printf("   Malloc is failed!!\n");return;}

#define ALL       0
#define ONE       1

#define FORWARD   1
#define REVERSE   2

#define FIRST     1
#define LAST      2
#define SELECT    3

typedef struct _Node
{
  int              m_nData;
  struct _Node    *m_pNext;
  struct _Node    *m_pPrev;
}Node;

void LinkedList_DisplayNode    (Node *pHead, int nCase);
void LinkedList_AddNode        (Node **ppHead, Node **ppTail, int nData, int nPosition);
void LinkedList_InsertNode     (Node **ppHead, Node ** ppTail, int nData, int nPosition);
void LinkedList_RemoveNode     (Node **ppHead, Node **ppTail, int nData, int nPosition);
void LinkedList_RemoveAllNode  (Node **ppHead);
void LinkedList_SearchNode     (Node *pHead, Node *pTail, int nData, int nPosition);

int main(void)
{
   int    nSelect = 0;
   int    nTemp = 0;
   int    nPosition = 0;
   Node  *pHead = NULL;
   Node  *pTail = NULL;

   while(1)
   {
      CLS;
      printf("\n   __________________________________\n");
      printf("   |________ Linked List TEST _______|\n");
      printf("   | Select     >>>> 1. Add          |\n");
      printf("   |            >>>> 2. Insert       |\n");
      printf("   |            >>>> 3. Search       |\n");
      printf("   |            >>>> 4. Remove       |\n");
      printf("   |            >>>> 5. Remove All   |\n");
      printf("   |            >>>> 6. Print        |\n");
      printf("   |            >>>> 0. Exit         |\n");
      printf("   |_________________________________|\n");
      printf("   Enter a select Number : ");
      scanf("%d", &nSelect);

      if(nSelect == 0)
      {
         CLS;
         printf("\n   ______________________________\n");
         printf("       Exit Linked List TEST \n");
         printf("   ______________________________\n");
         LinkedList_RemoveAllNode(&pHead);
         return;
      }
      else if(nSelect == 1)
      {
         CLS;
         printf("\n   ______________________________\n");
         printf("      Add Linked List TEST\n");
         printf("   ______________________________\n");
         printf("   Enter a input Data : ");
         scanf("%d", &nTemp);
         printf("   First = 1 | Last = 2 :");
         scanf("%d", &nPosition);
         LinkedList_AddNode(&pHead, &pTail, nTemp, nPosition);
         PAUSE;
      }
      else if(nSelect == 2)
      {
         CLS;
         printf("\n   ______________________________\n");
         printf("      Insert Linked List TEST\n");
         printf("   ______________________________\n");
         printf("   Enter a input Data : ");
         scanf("%d", &nTemp);
         printf("   Enter a taget Node : ");
         scanf("%d", &nPosition);
         LinkedList_InsertNode(&pHead, &pTail, nTemp, nPosition);
         PAUSE;
      }
    else if(nSelect == 3)
    {
         CLS;
         printf("\n   ______________________________\n");
         printf("      Search Linked List TEST\n");
         printf("   ______________________________\n");
         printf("   Enter a search Data : ");
         scanf("%d", &nTemp);
         printf("   Forward = 1 | Reverse = 2 :");
         scanf("%d", &nPosition);
         LinkedList_SearchNode(pHead, pTail, nTemp, nPosition);
         PAUSE;
      }
      else if(nSelect == 4)
      {
         CLS;
         printf("\n   ______________________________\n");
         printf("      Remove Linked List TEST\n");
         printf("   ______________________________\n");
         printf("   First = 1 | Last = 2 | Select = 3 :");
         scanf("%d", &nPosition);

         if (nPosition == 1 || nPosition == 2)
         {
            LinkedList_RemoveNode(&pHead, &pTail, nTemp, nPosition);
         }
         else if (nPosition == 3)
         {
            printf("   Enter a remove Data : ");
            scanf("%d", &nTemp);
            LinkedList_RemoveNode(&pHead, &pTail, nTemp, nPosition);
         }
         else
         {
            printf("   Error Input Number!!\n");
         }
         PAUSE;
      }
      else if(nSelect == 5)
      {
         CLS;
         printf("\n   ______________________________\n");
         printf("      Remove All Linked List TEST\n");
         printf("   ______________________________\n");
         LinkedList_RemoveAllNode(&pHead);
         printf("   Remove All SUCCESS!!\n");
         PAUSE;
      }
      else if(nSelect == 6)
      {
         CLS;
         printf("\n   ______________________________\n");
         printf("      Display Linked List TEST\n");
         printf("   ______________________________\n");
         LinkedList_DisplayNode(pHead, ALL);
         PAUSE;
      }
      else
      {
         CLS;
         printf("\n   ______________________________\n");
         printf("   Error input Number!!\n");
         printf("   ______________________________\n");
         PAUSE;
      }
   }
   return;
}

void LinkedList_DisplayNode (Node *pHead, int nCase)
{
   Node *pTemp = NULL;

   pTemp = pHead;

   switch(nCase)
   {
      case  0: // 전체 노드 출력
      while(pTemp != NULL)
      {
         printf("\n   ______________________________\n");
         printf("   LinkedList Data : %d\n", pTemp->m_nData);
         printf("   LinkedList Next : %x\n", pTemp->m_pNext);
         printf("   LinkedList Prev : %x\n", pTemp->m_pPrev);
         pTemp = pTemp->m_pNext;
      }
      printf("   ______________________________\n");
      break;
      case  1: // 한 노드만 출력
         printf("\n   ______________________________\n");
         printf("   LinkedList Data : %d\n", pTemp->m_nData);
         printf("   LinkedList Next : %x\n", pTemp->m_pNext);
         printf("   LinkedList Prev : %x\n", pTemp->m_pPrev);
         printf("   ______________________________\n");
         break;
   }
}

void LinkedList_AddNode (Node **ppHead, Node **ppTail, int nData, int nPosition)
{
   Node *pTemp = NULL;

   MALLOC(pTemp);

   pTemp->m_nData = nData;
   pTemp->m_pNext = NULL;
   pTemp->m_pPrev= NULL;
   if(*ppHead == NULL)
   {
      *ppHead = pTemp;
      *ppTail = pTemp;
      LinkedList_DisplayNode(*ppHead, ONE);
   }
   else
   {
      if (nPosition == FIRST)
      {
         if(*ppHead)
         {
            (*ppHead)->m_pPrev = pTemp;
            pTemp->m_pNext = (*ppHead);
            (*ppHead) = pTemp;
         }
         LinkedList_DisplayNode(*ppHead, ONE);
      }
      else if (nPosition == LAST)
      {
         if(*ppTail)
         {
            (*ppTail)->m_pNext = pTemp;
            pTemp->m_pPrev = (*ppTail);
            (*ppTail) = pTemp;
         }
         LinkedList_DisplayNode(*ppTail, ONE);
      }
      else
      {
         printf("   Error Input Number!!\n");
      }
   }
}

void LinkedList_InsertNode (Node **ppHead, Node ** ppTail, int nData, int nPosition)
{
   Node  * pNode  = NULL;
   Node  * pTemp  = NULL;
   Node  * pList  = NULL;
   Node  * pPrev  = NULL;

   MALLOC(pNode);

   pNode->m_nData = nData;
   pNode->m_pPrev = NULL;
   pNode->m_pNext = NULL;

   pList = *ppHead;
   if( NULL==pList )
   {
      *ppHead = pNode;
       LinkedList_DisplayNode(*ppHead, ONE);
       return;
   }
   else
   {
      while( pList )
      {
         if( pList->m_nData==nPosition )
         {
            break;
         }
         pPrev = pList->m_pPrev;
         pList = pList->m_pNext;
      }
      if( pList )
      {
         pTemp = pList->m_pNext;
         pList->m_pNext = pNode;
         pNode->m_pNext = pTemp;
         pNode->m_pPrev = pList;
         if( pTemp )
         {
            pTemp->m_pPrev = pNode;
         }
         LinkedList_DisplayNode(pNode, ONE);
         return;
      }
      else if( pPrev )
      {
         pPrev->m_pNext = pNode;
         pNode->m_pPrev = pPrev;
         LinkedList_DisplayNode(pNode, ONE);
         return;
      }
      else
      {
         return;
      }
      if( pNode->m_pPrev==*ppTail )
      {
         *ppTail = pNode;
          LinkedList_DisplayNode(*ppTail, ONE);
          return;
      }
   }
}

void LinkedList_RemoveNode(Node **ppHead, Node **ppTail, int nData, int nPosition)
{
   Node *pTemp = NULL;

   if (nPosition == FIRST)
   {
      if(*ppHead == *ppTail)
      {
         RELEASEIF(*ppHead);
      }
      else
      {
         if(!*ppTail)
         {
            return;
         }
         if(*ppHead == NULL)
         {
            return;
         }
         pTemp = (*ppHead)->m_pNext;
         pTemp->m_pPrev = NULL;
         *ppHead = pTemp;
      }
      printf("\n   Remove SUCCESS!!\n");
   }
   else if (nPosition == LAST)
   {
      if(*ppHead == *ppTail)
      {
         RELEASEIF(*ppHead);
      }
      else
      {
         pTemp = *ppHead;

         while(pTemp != NULL && (pTemp->m_nData != (*ppTail)->m_nData))
         {
            pTemp = pTemp->m_pNext;
         }

         if(!*ppTail)
         {
            return;
         }

         if(*ppTail == NULL)
         {
            return;
         }

         if(pTemp == *ppTail)
         {
            pTemp = (*ppTail)->m_pPrev;
            pTemp->m_pNext = NULL;
            RELEASEIF( *ppTail );
            *ppTail = pTemp;
         }
      }
      printf("\n   Remove SUCCESS!!\n");
   }
   else if (nPosition == SELECT)
   {
      Node *pPrev = NULL;
      Node *pNext = NULL;

      pTemp = *ppHead;

      while(pTemp != NULL && (pTemp->m_nData != nData))
      {
         pTemp = pTemp->m_pNext;
      }
      if(pTemp != NULL)
      {
         pPrev = pTemp->m_pPrev;
         pNext = pTemp->m_pNext;

         if(pPrev)
         {
            pPrev->m_pNext = pNext;
         }
         else
         {
            *ppHead = pNext;
         }

         if(pNext)
         {
            pNext->m_pPrev = pPrev;
         }
         else
         {
            *ppTail = pPrev;
         }
         RELEASEIF(pTemp);
         printf("\n   Remove SUCCESS!!\n");
      }
   }
   else
   {
      printf("   Error Input Number!!\n");
   }
}

void LinkedList_RemoveAllNode(Node **ppHead)
{
   Node *pTemp = NULL;
   Node *pDelete = NULL;

   pTemp = *ppHead;

   while(pTemp != NULL)
   {
      pDelete = pTemp;
      pTemp = pTemp->m_pNext;
      RELEASEIF(pDelete);
   }
   (*ppHead) = NULL;
}

void LinkedList_SearchNode(Node *pHead, Node *pTail, int nData, int nPosition)
{
   Node *pTemp = NULL;

   if(nPosition == FORWARD)
   {
      pTemp = pHead;

      while(pTemp != NULL && (pTemp->m_nData != nData))
      {
         pTemp = pTemp->m_pNext;
      }
   }
   else if(nPosition == REVERSE)
   {
      pTemp = pTail;

      while(pTemp != pHead && (pTemp->m_nData != nData))
      {
         pTemp = pTemp->m_pPrev;
      }
   }
   else
   {
      printf("   Error Input Number!!\n");
   }
  
   if(pTemp != NULL && pTemp->m_nData == nData)
   {
      printf("\n   Find SUCCESS!!\n");
      LinkedList_DisplayNode(pTemp, ONE);
   }
   else
   {
   printf("\n   Not Find!!\n");
   }
}

저작자 표시
신고

WRITTEN BY
빅터크룸
머 먹고 사냐.....

받은 트랙백이 없고 , 댓글이 없습니다.
secret