Ejemplo n.º 1
0
void Encoder::writeMesg(const unsigned char *message, const int size, 
               unsigned char *encodedMessage, int *encodedSize,
               const StackAr<char> arr[256]) const
{
  bitset<8> bits;
  int curPos = 0;

  for (int i = 0; i < size; i++)
  {
    StackAr<char> key = arr[message[i]];
//    printStack(key);
    while (!key.isEmpty())
    {
      bits[curPos++] = (key.topAndPop() == '1');
      if (curPos == 8)
      {
        encodedMessage[*encodedSize] = *bits.to_string().c_str();

        (*encodedSize)++;
        curPos = 0;
//        cout << "size of mesg is " << *encodedSize << endl;
      }
    }
  }
  cout << endl;
}  // writeMesg()
Ejemplo n.º 2
0
	int Sit(char letter, int match, int state)
	{
		switch(state)
		{
			case 0: break;
			case 1: //cout << "waiting" << endl;
				if(match == 0)
				{
					state = 2;
					break;
				}
				break;
			case 2: //cout << " store 1 " << endl;
				state = 3; 
				break;
			case 3: //cout << "store 2" << endl;
				if(letter == 'A')
				{
					if(!ABC.isEmpty())
					{
						state = 5; break;
					}
					else
					{
						state = 4; break;

					}
				}
				else if(letter == 'F')
				{
					if(!DEF.isEmpty())
					{
						state = 5; break;
					}
					else
					{
						state = 4; break;

					}
				}
				else if(letter == 'B')
				{
					if(!ABC.isEmpty())
					{
						if(ABC.top() == 'C')
						{
							state = 5; break;
						}
						else
						{
							state = 4; break;
						}
					}
					else
					{
						state = 4; break;

					}
				}
				else if(letter == 'E')
				{
					if(!DEF.isEmpty())
					{
						if(DEF.top() == 'D') 
						{	
							state = 5; 
							break;
						}
					}

				}
				else
				{
					state = 4; break;
				}
			
			case 4: //cout << "sitting " << endl;
				if( letter == 'A' || letter == 'B' || letter == 'C')
				{
		//			cout << "? " << endl;
					ABC.push(letter);
					if(!tempABC.isEmpty())
					{
						 state = 6; break;
					}
					else
					{
						 state = 7; break;
					}
				} 
				else
				{
				//	cout << " ! " << endl;
					DEF.push(letter);
					if(!tempDEF.isEmpty()) 
					{
						state = 6;
						break;
					}
					else
					{
						
						state = 7;
						break;
					}
				}
				
			case 5: //cout << "out " << endl; 
				if(letter == 'A' || letter == 'B')
				{
					if(letter == 'A')
					{
					//	if(ABC.top() == 'C' || ABC.top() == 'B')
					//	{
							if(!ABC.isEmpty())
							{
								tempABC.push(ABC.topAndPop());
								state = 5;
								break;
							}
							else
							{
								state = 4;
								break;
							}
					//	}
					}

				
					else
					{
						tempABC.push(ABC.topAndPop());
						state = 4;
						break;
					}
				}
				else
				{
					if(letter == 'F')
					{
					//	if(DEF.top() == 'D' || ABC.top() == 'E')
					//	{
							if(!DEF.isEmpty())
							{
//								cout << " under " << endl;
								tempDEF.push(DEF.topAndPop());
								state = 5;
								break;
								
							}
							else
							{
								state = 4;
								break;
							}
					//	}
					}

				
					else
					{
						tempDEF.push(DEF.topAndPop());
						state = 4;
						break;
	
					}
				}
			case 6:	
			//	cout << "In" << endl;
			
					
			
					if(letter =='A' || letter == 'B' || letter == 'C')
					{
						while(!tempABC.isEmpty())
						{
//							cout << tempABC.top() << endl;
							ABC.push(tempABC.topAndPop());
//							cout << "add ABC" << endl;
						}
					}
					else
					{
						while(!tempDEF.isEmpty())
						{
//							cout << "temp " << tempDEF.top() << endl;
//							cout << "DEF " << DEF.top() << endl;
						DEF.push(tempDEF.topAndPop());
//							cout << "add DEF" << endl;			
					}
				}
			
		//		tempABC.makeEmpty();
		//		tempDEF.makeEmpty();
			state = 7;
			break;
	}	
	return state;			
}