//-----------------------------------------------------------------
bool FormulasLoader::end__logbase()
{
    if ( mNodeListStack.empty() )
    {
        // TODO error handling
        return false;
    }

    NodeVector nodes = mNodeListStack.top();
    mNodeListStack.pop();
    size_t numOfNodes = nodes.size();

    if ( numOfNodes == 0 )
    {
        // TODO error handling
        return false;
    }

    appendNewNode( nodes[ 0 ] );

    return true;
}
//-----------------------------------------------------------------
bool FormulasLoader::end__apply()
{
    mCurrentApplyHasChild = true;

    if ( mNodeListStack.empty() )
    {
        // TODO error handling
        return false;
    }

    NodeVector nodes = mNodeListStack.top();
    mNodeListStack.pop();
    size_t numOfNodes = nodes.size();

    if ( numOfNodes == 0 )
    {
        // TODO error handling
        return false;
    }

    if ( mOperatorStack.empty() )
    {
        mCurrentFormula->getMathmlAsts().allocMemory( nodes.size() );
        for (size_t i=0; i<nodes.size(); ++i)
        {
            mCurrentFormula->getMathmlAsts().append( nodes[ i ] );
        }
    }
    else
    {
        Operator op = mOperatorStack.top();
        mOperatorStack.pop();

        if ( isFunction( op ) )
        {
            MathML::AST::INode* node = createFunctionOperation( nodes, op );
            appendNewNode( node );
        }
        else if ( op == USER_DEFINED_FUNCTION )
        {
            MathML::AST::INode* node = createUserDefinedFunctionOperation( nodes );
            appendNewNode( node );
        }
        else
        {
            switch ( numOfNodes )
            {

            case 1:
            {
                MathML::AST::INode* node = createUnaryOperation( nodes, op );
                appendNewNode( node );
                break;
            }
            default:
            {
                MathML::AST::INode* node = createMultiOperandOperation( nodes, op );
                appendNewNode( node );
                break;
            }
            }
        }
    }

    return true;
}
Exemple #3
0
int main() {
  int i; // List indices
  
  FILE *fin = fopen("beads.in", "r");
  
  int num;
  fscanf(fin, "%d\n", &num);
  
  NODE *curr_node = appendNewNode(NULL);
  
  for (i = 0; i < num; i++) {
    char new_char;
    fscanf(fin, "%c", &new_char);
    
    COLOR new_color = charToColor(new_char);
    if (new_color == INVALID_COLOR) {
      printf("Encountered unexpected char: %c\n", new_char);
      exit(EXIT_FAILURE);
    }
    
    // Check if this is the first bead in the current node
    if (curr_node->color == INVALID_COLOR) {
      curr_node->color = new_color;
      curr_node->count++;
      continue;
    }
    
    // Check if we can add this bead to the current node
    if (curr_node->color == new_color) {
      curr_node->count++;
      continue;
    }
    
    // We need to start a new node
    curr_node = appendNewNode(curr_node);
    curr_node->count++;
    curr_node->color = new_color;
  }
  fclose(fin);

  // Now that we have a circular linked list of nodes, we check each possible
  // split point and pick the max
  int curr_max = 0;
  NODE *start = curr_node;
  do {
    int split = splitValue(curr_node);
    if (split > curr_max) {
      curr_max = split;
    }
    curr_node = curr_node->next;
  } while (start != curr_node);

  FILE *fout = fopen("beads.out", "w");
  fprintf(fout, "%d\n", curr_max);
  fclose(fout);

  // Clean up allocated memory...
  while (curr_node != NULL) {
    curr_node = freeNode(curr_node);
  }
}