コード例 #1
0
ファイル: postfix.cpp プロジェクト: stakere/CS33001
int main()
{
        Stack<char> theStack(100);
	string::string aLine, postfixExpression;
        int i = 0;
        std::ifstream infixFile("infix.txt");
        infixFile >> aLine;                             // priming read
        while(!infixFile.eof())
                int length = aLine.length();
                postfixExpression = "";
 
                for(i = 0; i < length; i++)
                {
                        char lineChar = aLine[i];
                        //IF symbol is an operand (variable) append it to postfixExpression
                        if (lineChar == 'A' || lineChar == 'B' || lineChar == 'C' || lineChar == 'D' || lineChar == 'E')
                        postfixExpression += lineChar;
 
                        //IF symbol is an operator ( + , -  , * , / ) determine to push or pop
                        else if(lineChar == '+' || lineChar == '-' || lineChar == '*' || lineChar == '/')
                        {
                                while(theStack.getTop() != '(' && precedence(theStack.getTop()) >= precedence(lineChar) && !theStack.IsEmpty())
                                {
                                        postfixExpression += theStack.pop();             // pop if top is greater than current
                                }
                                theStack.push(lineChar);                                                // push if current is greater than top
                        }
 
                        // IF symbol is ( push to stack
                        else if(lineChar == '(')
                                theStack.push(lineChar);
 
                        // IF symbol is ) determine to pop and put into expression or discard the parenthesis
                        else if(lineChar == ')')
                        {
                                while(theStack.getTop() != '(')
                                {
                                        postfixExpression += theStack.pop();             // put operators into expression
                                }
                                theStack.pop();                                                                 // pop ( from stack and discard
                        }
                }
                while(!theStack.IsEmpty())
                {
                        postfixExpression += theStack.pop();                             // pop reamining symbols from stack
                }
                std::cout << postfixExpression << std::endl;                                               // display result
                infixFile >> aLine;                                                                             // continuation read
               
        }
コード例 #2
0
/**
 * This method scans the sequence of tokens to determine whether or not the
 * tag structure of the document is well formed. In well formed cases, we can
 * skip doing residual style handling and allow inlines to contain block-level
 * elements.
 *
 * @param aFinalChunk Is unused.
 * @return Success (currently, this function cannot fail).
 */
nsresult nsHTMLTokenizer::ScanDocStructure(bool aFinalChunk)
{
  nsresult result = NS_OK;
  if (!mTokenDeque.GetSize()) {
    return result;
  }

  CHTMLToken* theToken = (CHTMLToken*)mTokenDeque.ObjectAt(mTokenScanPos);

  // Start by finding the first start tag that hasn't been reviewed.
  while (mTokenScanPos > 0) {
    if (theToken) {
      eHTMLTokenTypes theType = eHTMLTokenTypes(theToken->GetTokenType());  
      if (theType == eToken_start &&
          theToken->GetContainerInfo() == eFormUnknown) {
        break;
      }
    }
    theToken = (CHTMLToken*)mTokenDeque.ObjectAt(--mTokenScanPos);
  }

  // Now that we know where to start, let's walk through the
  // tokens to see which are well-formed. Stop when you run out
  // of fresh tokens.

  nsDeque       theStack(0);
  nsDeque       tempStack(0);
  int32_t       theStackDepth = 0;
  // Don't bother if we get ridiculously deep.
  static  const int32_t theMaxStackDepth = 200;

  while (theToken && theStackDepth < theMaxStackDepth) {
    eHTMLTokenTypes theType = eHTMLTokenTypes(theToken->GetTokenType());
    eHTMLTags       theTag  = (eHTMLTags)theToken->GetTypeID();

    if (nsHTMLElement::IsContainer(theTag)) { // Bug 54117
      bool theTagIsBlock  = gHTMLElements[theTag].IsMemberOf(kBlockEntity);
      bool theTagIsInline = theTagIsBlock
                              ? false
                              : gHTMLElements[theTag].IsMemberOf(kInlineEntity);

      if (theTagIsBlock || theTagIsInline || eHTMLTag_table == theTag) {
        switch(theType) {
          case eToken_start:
            {
              if (gHTMLElements[theTag].ShouldVerifyHierarchy()) {
                int32_t earlyPos = FindLastIndexOfTag(theTag, theStack);
                if (earlyPos != kNotFound) {
                  // Uh-oh, we've found a tag that is not allowed to nest at
                  // all. Mark the previous one and all of its children as 
                  // malformed to increase our chances of doing RS handling
                  // on all of them. We want to do this for cases such as:
                  // <a><div><a></a></div></a>.
                  // Note that we have to iterate through all of the chilren
                  // of the original malformed tag to protect against:
                  // <a><font><div><a></a></div></font></a>, so that the <font>
                  // is allowed to contain the <div>.
                  // XXX What about <a><span><a>, where the second <a> closes
                  // the <span>?
                  nsDequeIterator it(theStack, earlyPos), end(theStack.End());
                  while (it < end) {
                    CHTMLToken *theMalformedToken = 
                        static_cast<CHTMLToken*>(it++);
                  
                    theMalformedToken->SetContainerInfo(eMalformed);
                  }
                }
              }

              theStack.Push(theToken);
              ++theStackDepth;
            }
            break;
          case eToken_end: 
            {
              CHTMLToken *theLastToken =
                static_cast<CHTMLToken*>(theStack.Peek());
              if (theLastToken) {
                if (theTag == theLastToken->GetTypeID()) {
                  theStack.Pop(); // Yank it for real 
                  theStackDepth--;
                  theLastToken->SetContainerInfo(eWellFormed);
                } else {
                  // This token wasn't what we expected it to be! We need to
                  // go searching for its real start tag on our stack. Each
                  // tag in between the end tag and start tag must be malformed

                  if (FindLastIndexOfTag(theTag, theStack) != kNotFound) {
                    // Find theTarget in the stack, marking each (malformed!)
                    // tag in our way.
                    theStack.Pop(); // Pop off theLastToken for real.
                    do {
                      theLastToken->SetContainerInfo(eMalformed);
                      tempStack.Push(theLastToken);
                      theLastToken = static_cast<CHTMLToken*>(theStack.Pop());
                    } while (theLastToken && theTag != theLastToken->GetTypeID());
                    // XXX The above test can confuse two different userdefined 
                    // tags.

                    NS_ASSERTION(theLastToken,
                                 "FindLastIndexOfTag lied to us!"
                                 " We couldn't find theTag on theStack");
                    theLastToken->SetContainerInfo(eMalformed);

                    // Great, now push all of the other tokens back onto the
                    // stack to preserve the general structure of the document.
                    // Note that we don't push the target token back onto the
                    // the stack (since it was just closed).
                    while (tempStack.GetSize() != 0) {
                      theStack.Push(tempStack.Pop());
                    }
                  }
                }
              }
            }
            break;
          default:
            break; 
        }
      }
    }

    theToken = (CHTMLToken*)mTokenDeque.ObjectAt(++mTokenScanPos);
  }

  return result;
}