Advertisement
steverobinson

Message Encoding [Corrected]

Oct 15th, 2010
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4. class string_t
  5. {
  6.     public:
  7.         char a,b;
  8. };
  9.  
  10.  
  11. int main()
  12. {
  13.     char str_src[100],str_dest[150],string_temp[150],choice[7],str_org[100];
  14.     string_t str_mat[9][9];
  15.     int i,order,j,k,m,c,len;
  16.     //fflush(stdin);
  17.     //gets(choice);
  18.     cin>>choice;
  19.     if(strcmp(choice,"ENCODE")==0)
  20.     {
  21.             cin.ignore();
  22.             cin.getline(str_src,100);
  23.  
  24.             len=strlen(str_src);
  25.             strcpy(str_org,str_src);
  26.             order=1;
  27.             while(order*order<(len+1)/2)
  28.                 order+=2;
  29.  
  30.             for(i=len;i<order*order*2;i++)
  31.                 str_src[i]='/';
  32.             str_src[i]=0;
  33.             k=0;
  34.             for(i=0;i<order;i++)
  35.                 for(j=0;j<order;j++)
  36.                 {
  37.                     str_mat[i][j].a=str_src[k];
  38.                     str_mat[i][j].b=str_src[k+1];
  39.                     k+=2;
  40.                 }
  41.  
  42.           str_dest[0]=str_mat[order/2][order/2].a;
  43.           str_dest[1]=str_mat[order/2][order/2].b;
  44.  
  45.           i=order/2;
  46.           m;
  47.           j=order/2;
  48.           k=2;
  49.           c=1;
  50.           while(c<order)
  51.           {
  52.               for(m=1;m<=c;m++)
  53.               {
  54.                   j--;
  55.                   str_dest[k]=str_mat[i][j].a;
  56.                   str_dest[k+1]=str_mat[i][j].b;
  57.                   k+=2;
  58.               }
  59.               for(m=1;m<=c;m++)
  60.               {
  61.                   i--;
  62.                   str_dest[k]=str_mat[i][j].a;
  63.                   str_dest[k+1]=str_mat[i][j].b;
  64.                   k+=2;
  65.               }
  66.               c++;
  67.               for(m=1;m<=c;m++)
  68.               {
  69.                   j++;
  70.                   str_dest[k]=str_mat[i][j].a;
  71.                   str_dest[k+1]=str_mat[i][j].b;
  72.                   k+=2;
  73.               }
  74.               for(m=1;m<=c;m++)
  75.               {
  76.                   i++;
  77.                   str_dest[k]=str_mat[i][j].a;
  78.                   str_dest[k+1]=str_mat[i][j].b;
  79.                   k+=2;
  80.               }
  81.               c++;
  82.           }
  83.           for(m=1;m<order;m++)
  84.           {
  85.             j--;
  86.             str_dest[k]=str_mat[i][j].a;
  87.             str_dest[k+1]=str_mat[i][j].b;
  88.             k+=2;
  89.           }
  90.           str_dest[order*order*2]=0;
  91.           cout<<"OUTPUT IS\n"<<str_org;
  92.           cout<<endl<<str_dest;
  93.     }
  94.     else if(strcmp(choice,"DECODE")==0)
  95.     {
  96.         //fflush(stdin);
  97.         cin.ignore();
  98.         cin.getline(str_dest,150);
  99.         strcpy(str_org,str_dest);
  100.         len=strlen(str_dest);
  101.         order=1;
  102.         while((order*order)<len/2)
  103.             order+=2;
  104.         str_mat[order/2][order/2].a=str_dest[0];
  105.         str_mat[order/2][order/2].b=str_dest[1];
  106.         i=order/2;
  107.           m;
  108.           j=order/2;
  109.           k=2;
  110.           c=1;
  111.           while(c<order)
  112.           {
  113.               for(m=1;m<=c;m++)
  114.               {
  115.                   j--;
  116.                   str_mat[i][j].a=str_dest[k];
  117.                   str_mat[i][j].b=str_dest[k+1];
  118.                   k+=2;
  119.               }
  120.               for(m=1;m<=c;m++)
  121.               {
  122.                   i--;
  123.                   str_mat[i][j].a=str_dest[k];
  124.                   str_mat[i][j].b=str_dest[k+1];
  125.                   k+=2;
  126.               }
  127.               c++;
  128.               for(m=1;m<=c;m++)
  129.               {
  130.                   j++;
  131.                   str_mat[i][j].a=str_dest[k];
  132.                   str_mat[i][j].b=str_dest[k+1];
  133.                   k+=2;
  134.               }
  135.               for(m=1;m<=c;m++)
  136.               {
  137.                   i++;
  138.                   str_mat[i][j].a=str_dest[k];
  139.                   str_mat[i][j].b=str_dest[k+1];
  140.                   k+=2;
  141.               }
  142.               c++;
  143.           }
  144.           for(m=1;m<order;m++)
  145.           {
  146.             j--;
  147.             str_mat[i][j].a=str_dest[k];
  148.             str_mat[i][j].b=str_dest[k+1];
  149.             k+=2;
  150.           }
  151.           k=0;
  152.           for(i=0;i<order;i++)
  153.                 for(j=0;j<order;j++)
  154.                 {
  155.                     if(str_mat[i][j].a!='/')
  156.                     {
  157.                         str_src[k]=str_mat[i][j].a;
  158.                         k++;
  159.                     }
  160.                     if(str_mat[i][j].b!='/')
  161.                     {
  162.                             str_src[k]=str_mat[i][j].b;
  163.                             k++;
  164.                     }
  165.                 }
  166.             str_src[k]=0;
  167.  
  168.           cout<<"OUTPUT IS"<<endl<<str_org;
  169.           cout<<endl<<str_src;
  170.  
  171.     }
  172.  
  173.  
  174.  
  175.   return 0;
  176.  
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement