Advertisement
Go-Ice

UVa [10878 - Decode the tape] by Go-Ice

Apr 10th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. int main(){
  7.     int readTape( char *indexFun );
  8.  
  9.     char strInput[12], strOut[10000];
  10.     char *index = &strInput[0];
  11.     int ASCII;
  12.     int cnt, power, length=0, tapeEnd=1;
  13.  
  14.     while (readTape(index)){
  15.         /* reset variables*/
  16.         ASCII = 0;
  17.         power = 7;
  18.  
  19.         if (strInput[0] == '_'){
  20.             tapeEnd = (tapeEnd == 0) ? 1 : 0 ;
  21.  
  22.             /* if it's tape's end, prints out the result */
  23.             if (tapeEnd){
  24.                 for ( cnt=0 ; cnt<length ; cnt++ )
  25.                     printf("%c", strOut[cnt]);
  26.                 length = 0; /* reset tape's length */
  27.             } /* END inner if*/
  28.  
  29.             continue;
  30.         } /* END outer if */
  31.  
  32.         /* decode the tape, translate the tape into ASCII code */
  33.         for ( cnt=1 ; cnt<10 ; cnt++ ){
  34.             if (strInput[cnt] == 'o')
  35.                 ASCII += pow(2, power);
  36.  
  37.             power = (cnt==6) ? power : --power ;
  38.         } /* END for */
  39.  
  40.         strOut[length++] = ASCII;
  41.  
  42.     } /* END while*/
  43.  
  44.     system("PAUSE");
  45.     return 0;
  46. }
  47.  
  48. int readTape( char *indexFun ){
  49.     int cnt;
  50.  
  51.     for ( cnt=0 ; cnt<12 ; cnt++, indexFun++ ){
  52.         *indexFun = getchar();
  53.         if (*indexFun == '\n')
  54.             return 1;
  55.     } /* END for */
  56.  
  57.     return 0;
  58. } /* END readTape() */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement