예제 #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()
예제 #2
0
LongInt LongInt::operator+(LongInt& int2)
{
	StackAr<int> stack1 = this->intStack;
	StackAr<int> stack2 = int2.intStack;
	StackAr<int> tempStack (1000001);
	LongInt newInt;
	int curr1;
	int curr2;
	int newDigit;
	int carry = 0;
		
	while (!stack1.isEmpty() && !stack2.isEmpty())
	{
		curr1 = stack1.top();
		stack1.pop();
		curr2 = stack2.top();
		stack2.pop();
		newDigit = curr1 + curr2 + carry;
		if (newDigit >= 10)
		{
			newDigit -= 10;
			carry = 1;
		}
		else
		{
			carry = 0;
		}
		tempStack.push(newDigit);
	}
	
	if (!stack1.isEmpty())
	{
		while (!stack1.isEmpty())
		{
			curr1 = stack1.top();
			stack1.pop();
			newDigit = curr1 + carry;
			if (newDigit >= 10)
			{
				newDigit -= 10;
				carry = 1;
			}
			else
			{
				carry = 0;
			}
			tempStack.push(newDigit);
		}
	}

	if (!stack2.isEmpty())
	{
		while (!stack2.isEmpty())
		{
			curr2 = stack2.top();
			stack2.pop();
			newDigit = curr2 + carry;
			if (newDigit >= 10)
			{
				newDigit -= 10;
				carry = 1;
			}
			else
			{
				carry = 0;
			}
			tempStack.push(newDigit);
		}
	}
	
	while (!tempStack.isEmpty())
	{
		newDigit = tempStack.top();
		tempStack.pop();
		newInt.addDigit(newDigit);
	}	
	return newInt;
}
예제 #3
0
	int Sit (char letter, int m, int state)
	{

						
		
			switch(state)
			{

				case 0 : 
					if(m == 0)
					{
						state = 2;
						 break;
					}
					else state = 0; break;
				case 1 : sec += 5; 
			

					if (m == 0) //check that it's the correct row
					{
					//	state = ST1;
						cout << " rows match" << endl;	
						state = 2;
						cout << " state 2" << endl;
						break;
					}
					else
					{
						state = 1; break;
					}
				case 2 : sec += 5; state = 3; cout<< "sto1 " << endl; break;
				case 3 : sec += 5; 
					cout << "sto2" << endl;
					if (letter == 'A' ) // if the stack needs to be popped
					{
						if (!ABC.isEmpty()) // B or C is sitting down in that row
						{						

//							state = O;
							state = 5; break;
						}
						else // no one blocking 
						{
										
//							state = S;
							state =  4; break;
						}								

					}
					else if (letter == 'F')
					{
						if (!DEF.isEmpty()) // D or E is sitting down
						{
//							state = O;
							state = 5; break;	
						}
						else // no one blocking
						{
//							state = S;
							state = 4; break;
						}
	
					}
					else if (letter == 'B') 
					{

						if (!ABC.isEmpty()) // A or C
						{
							if (ABC.top() == 'C') // stack must be popped
							{
//								state = O;
								state = 5; break;
							}
							else // stack doesn't need to be popped
							{
//								state = S;
								state = 4; break;
							}
						}
						else // no one blocking
						{
//							state = S; 
							state = 4;
							break;
						}
					}
					else if (letter == 'E')
					{

						if (!DEF.isEmpty()) // D or F 
						{
							if (DEF.top() == 'D') // pop stack
							{
//								state = O;
								state = 5; break;							
							}
							else // stack doesn't need popping
							{
//								state = S;
								state = 4; break;
							}
							
						}
						else // no one blocking
						{
//							state = S;
							state =  4; break;
						}
					}
					else //C or D
					{
//						state = S;
						state = 4;
						break;
					}

				case 4 :  

					if (letter == 'A' || letter == 'B' || letter == 'C')
					{
						ABC.push(letter);
						sec += 5;
						if (!temp.isEmpty()) // if someone had to be popped
						{
//							state = I;
							state = 6;
							break;
						}
						else
						{
//							cout << "sitting" << sec << endl;
							state =  7;
							break;
						}

					}	
					else
					{
						DEF.push(letter);
						sec += 5;
						if (!temp.isEmpty())
						{
//							state = I;
							state =  6;
							break;

						}
						else
						{					
//							cout << "sitting" << sec << endl;
							
							state =  7;
							break;
						}
					}
				case 5 : 
					

					if (letter == 'A' || letter == 'B')
					{
						if (letter == 'A')
						{						
							if (ABC.top() == 'C')
							{

								temp.push(ABC.top()); // move out C
								ABC.pop();
								sec += 5;
								if (!ABC.isEmpty()) // if B was also in
								{
									temp.push(ABC.top()); // move out A
									ABC.pop();
									sec += 5;
//									state = S;
									state =  4;
									break;	
								}
								else
								{
//									state = S; // if only C, go to sit
									state = 4;
									break;
								}
							}
						}
						else
						{
							temp.push(ABC.top()); // if letter B
							ABC.pop(); // pop off top which would be C
							sec += 5;
//							state = S;
							state =  4;
							break;
						}
					}
					else // E or F
					{
						if (letter == 'F')
						{						
							if (ABC.top() == 'D')
							{
								temp.push(DEF.top());
								DEF.pop();
								sec += 5;
								if (!DEF.isEmpty())
								{
									temp.push(DEF.top());
									DEF.pop();
									sec += 5;
//									state = S;
									state = 4;	
									break;
								}
								else
								{
//									state = S;
									state = 4;			
									break;
								}
							}
						}
						else
						{
							temp.push(DEF.top());
							DEF.pop();
							sec += 5;
							state = 4;
							break;
						}
						
						
					}

				case 6 : 
					while (!temp.isEmpty())
					{
						if (letter == 'A'|| letter == 'B' || letter == 'C')
						{
							ABC.push(temp.top());
							temp.pop();
							sec += 5;
						}
						else
						{
							DEF.push(temp.top());
							temp.pop();
							sec += 5;
						}
						
					}
//					cout << " blocked " << sec << endl;
					state = 7;
					break;
					
			}
		return state;
	}
예제 #4
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;			
}