Author: NV Language: c
Description: Not specified Timestamp: 2013-05-31 16:10:58 +0000
View raw paste Reply
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <string.h>
  4. #include <malloc.h>
  5.  
  6. #define MAXSTR 21 //string to write
  7. #define MAXLEN 1000
  8.  
  9.  
  10. typedef struct Item {
  11. char c;
  12. struct Item *next;
  13. } Item;
  14.  
  15. //convert Hex->Bin
  16. struct Item *HexToBin(struct Item *BSpisok, struct Item *HSpisok);
  17. /* Enter string-list */
  18. int getList(struct Item **);
  19. /* Get string-list */
  20. void printList(const char *, struct Item *);
  21. /* Free memory */
  22. struct Item *deleteList(struct Item *);
  23. /* Forming new string-list */
  24. struct Item *SaveInputStr(struct Item *);
  25. struct Item *SaveNewStr(struct Item *);
  26. /* Delete spaces from string-list */
  27. struct Item *delSpace(struct Item *);
  28. /* Admission the word in string-list */
  29. struct Item *skipWord(struct Item *);
  30. /* Delete word from string-list */
  31. struct Item *delWord(struct Item *);
  32. //******************************************************************************
  33. //******************************************************************************
  34. //******************************************************************************
  35.  
  36.  
  37. int getList(struct Item **pptr)
  38. {
  39.   char buf[21], *str;
  40.   struct Item head = {'*', NULL};
  41.   struct Item *last = &head;
  42.   int n, rc = 1;
  43.  
  44. do{
  45.    n = scanf("%20[^\n]", buf);
  46.    if (n < 0)
  47.      {
  48.       deleteList(head.next);
  49.       head.next = NULL;
  50.       rc = 0;
  51.       continue;
  52.      }
  53.    if (n > 0)
  54.      {
  55.       for (str = buf; *str != '\0'; ++str)
  56.         {
  57.          last->next = (struct Item *)malloc(sizeof(struct Item));
  58.          last = last->next;
  59.          last->c = *str;
  60.         }
  61.       last->next = NULL;
  62.      }
  63.      else scanf("%*c");
  64.   } while(n > 0);
  65. *pptr = head.next;
  66. return rc;
  67. }
  68.  
  69.  
  70. void printList(const char *msg, struct Item *ptr)
  71. {
  72.   printf(" %s: \"", msg);
  73.   for (; ptr != NULL; ptr = ptr->next)
  74.     printf("%c", ptr->c);
  75.   printf("\"\n");
  76. }
  77.  
  78.  
  79. struct Item *deleteList(struct Item *ptr)
  80. {
  81. struct Item *tmp = NULL;
  82.   while(ptr != NULL)
  83.     {
  84.       tmp = ptr;
  85.       ptr = ptr->next;
  86.       free(tmp);
  87.     }
  88. return ptr;
  89. }
  90.  
  91.  
  92. struct Item *SaveNewStr(struct Item *p)
  93. {
  94. struct Item  *head = p;
  95. struct Item *cur = head, *prev = head;
  96. int fl = 0;
  97.  
  98. while(cur && (cur->next = delSpace(cur->next))) {
  99.   if (fl)
  100.    {
  101.     cur = skipWord(cur->next);
  102.     prev = cur;
  103.     cur = cur->next;
  104.     if (cur)
  105.       {
  106.         cur->c = ' ';
  107.       }
  108.    }else
  109.       cur->next = delWord(cur->next);
  110.   fl = !fl;
  111.  }
  112.  
  113. if (prev->next) //?????
  114.  {
  115.   free(prev->next);
  116.   prev->next = NULL;
  117.  }
  118.  
  119. return head;
  120. }
  121.  
  122.  
  123. struct Item *delSpace(struct Item *p)
  124. {
  125. struct Item *tmp;
  126.  
  127. while(p && ( (p->c == ' ') || (p->c == '\t')))
  128.   {
  129.    tmp = p;
  130.    p = p->next;
  131.    free(tmp);
  132.   }
  133. return p;
  134. }
  135.  
  136.  
  137. struct Item *skipWord(struct Item *p)
  138. {
  139. while (p->next && p->next->c != ' ' && p->next->c != '\t')
  140.   p = p->next;
  141. return p;
  142. }
  143.  
  144.  
  145. struct Item *delWord(struct Item *p)
  146. {
  147. struct Item *tmp;
  148.  
  149. while (p && p->c != ' ' && p->c != '\t')
  150.   {
  151.    printf("p->c=%c\n",p->c);
  152.    tmp = p;
  153.    p = p->next;
  154.    free(tmp);
  155.   }
  156. return p;
  157. }
  158.  
  159. struct Item *SaveInputStr(struct Item *p)
  160. {
  161. char ch;
  162.  
  163. struct Item *head=p;
  164. struct Item *prev,*cur;
  165.  
  166. printf("Write your hex string:\n");
  167.  
  168. //collecting struct
  169. while((ch=getchar())!='\n')
  170.   {
  171.    cur=(struct Item *)malloc(sizeof(struct Item));
  172.    if (head==NULL) head=cur;
  173.      else
  174.        prev->next=cur;
  175.    cur->next=NULL;
  176.    printf("simb=%c\n",ch);
  177.    cur->c=ch;
  178.    printf("cur->c=%c\n", cur->c);
  179.    prev=cur;
  180.   }
  181. return head;
  182. }
  183.  
  184.  
  185.  
  186.  
  187.  
  188. //==============================================================================
  189. //****END OF LIST FUNCTIONS****END OF LIST FUNCTIONS****END OF LIST FUNCTIONS***
  190. //==============================================================================
  191.  
  192.  
  193.  
  194. //______________________________________________________________________________
  195. //**MAIN*MAIN*MAIN*MAIN*MAIN*MAIN*MAIN*MAIN*MAIN*MAIN*MAIN*MAIN*MAIN*MAIN*MAIN**
  196. //______________________________________________________________________________
  197. int main(){
  198. struct Item *HexSpisok=NULL, *BinSpisok=NULL;
  199.  
  200. HexSpisok=SaveInputStr(HexSpisok);
  201. printList("HexSpisok", HexSpisok);
  202.  
  203. HexSpisok=SaveNewStr(HexSpisok);
  204.  
  205. printList("HexSpisok", HexSpisok);
  206.  
  207. BinSpisok=HexToBin(BinSpisok, HexSpisok);
  208. printList("BinSpisok", BinSpisok);
  209.  
  210. getchar();
  211. }
  212.  
  213.  
  214. struct Item *HexToBin(struct Item *BSpisok, struct Item *HSpisok){
  215.  
  216. int i=0, j=0, flag=1; //j for massiv dig[j]- BIN string
  217. char chr;
  218.  
  219. char ch,str[4];
  220. struct Item *head=BSpisok, *p=HSpisok;
  221. struct Item *prev, *cur;
  222.                    //   0      1        2      3       4       5
  223. static char *dig[]={ "0000", "0001", "0010", "0011", "0100", "0101",
  224. // 6      7       8        9      10     11       12      13     14
  225. "0110", "0111","1000", "1001", "1010", "1011", "1100", "1101", "1110",
  226. // 15   16   17   18    19     20     21     22     23   24
  227. "1111", "0", "1", "10", "11", "100", "101", "110", "111"," " };
  228. while ( (p->next!=NULL) && (p->next!=NULL) )
  229.   {
  230.    chr=p->c;
  231.    if (chr==' ')
  232.      {
  233.       flag=1;
  234.       j=24;
  235.      }
  236.      else if (chr>='A' && chr<='F') j=chr-'A'+10;
  237.             else if (chr>='a' && chr<='f') j=chr-'a'+10;
  238.                    else if (chr>='0' && chr<='9')
  239.                      if (flag!=1) j=chr-'0'; //flag is to find first char in the word.Just delete left zeroes
  240.                        else {
  241.                              j=chr-'0'+16;  //+15 is for massiv
  242.                              flag=0;
  243.                             }
  244.  
  245.  
  246.    strcpy(str, dig[j]);
  247.    j=strlen(str);
  248.    for (; i<j; i++)
  249.      {
  250.       cur=(struct Item *)calloc(1,sizeof(struct Item));
  251.       if (head==NULL) head=cur;
  252.         else
  253.           prev->next=cur;
  254.       cur->next=NULL;
  255.       cur->c=str[i];
  256.       printf("cur->c=%c\n", cur->c);
  257.       printf("%c\n", str[i]);
  258.  
  259.       prev=cur;
  260.      }
  261.    printf("-----------------\n");
  262.    j=0;
  263.    i=0;
  264.   p=p->next;
  265.  
  266.   }
  267. return head;
  268.  
  269. }
View raw paste Reply