Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* (Reviewed Dec, 14 2012)
- *
- * seqNpad - Script interpreter and Notepad for Integer Sequences.
- * Version 3.29 by: R. J. Cano, <aallggoorriitthhmmuuss@gmail.com>
- *
- * This program was devised and written for documentation purposes and is
- * subject to the terms of use of the On Line Encyclopedia Of Integer Sequences (TM).
- * (Visit: http://www.oeis.org/a217626)
- *
- * << If people do not believe that mathematics is simple, it is only because
- * they do not realize how complicated life is. >>
- *
- * --John von Neumann
- *
- *
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <time.h>
- typedef struct numLIFO { long value; struct numLIFO* belowLink; } numLIFO;
- typedef struct numBook { long value; struct numBook* prevLink;
- struct numBook* nextLink; } numBook;
- typedef struct digitsString { long integer_value; struct digitsString* nextString; } digitsString;
- digitsString *__theRing_HEAD, *__theRing_TAIL;
- /*
- * (Inherited from A207324 sequencer)
- *
- * Note about the ring: After calling close__the_ring() a first time, don't call it again before a call to
- * open_the_ring() of course for the same parameter list (else it would have possibly harmful and
- * unpredictible consequences).
- *
- */
- int _BatchInputMode= 1; /*
- This should be 0 if this program will be used as an user-ineractive interpreter.
- */
- int _Which_main= 2; /*
- -----------------------------------------
- # function call: In order to run:
- -----------------------------------------
- 0 main() seqNotepad,
- 1 Old_main_01() A207324 sequencer,
- 2 Old_main_02() A217626 sequwncer.
- -----------------------------------------
- Note: If proper intructions are given to
- seqNotepad, it replaces the mentioned
- sequencer for A217656 and may replace
- a sequencer for A215940.
- */
- int _a217626_fixTextGlitch= 1; /*
- Set it to zero if a modification made to runSeqAn()
- doesn't have anything to do about a217626.
- */
- int _ScriptMem01= 0; /* Similar in some way to "ditto" operator in Maple. */
- int _rec= 0; /* Internal use only. Don't touch it. */
- /*
- * The "Demo loop": As its name tell us, a demonstrative script.
- *
- * ( These numbers are internal instructions for being executed in sequence )
- *
- */
- int _Script[]= {-9, -12, -211, -7, 1, -1011, 9, 1, -3, 1, -2, -911, -666};
- /*
- * Important:
- *
- * Due the finiteness of the preceding array, every script must ends in ..."-2, 1}"
- * for return the control to the OS or well in ..."-2,-911,-666}" in order to close
- * an execution loop. Else, the interpreter will crash with a segmentation fault caused
- * by an array index out of bounds.
- *
- * None explanation about the use of this program as Script interpreter would be better than
- * a detailed study of the "main menu" and the functionality implemented in "requestUserInput()".
- *
- */
- char* _CtrlStr= "%li";
- char* seqAnDescriptorString= "A217626: First differences among the terms in A215940.\nA symmetric set of generators for permutations in the natural numbers";
- int _ScriptCursor= 0;
- int __savingCallsToMallocMode= 0;
- /* Should be set to 1 when compiling this source. (Unconditional) */
- int _firstCleanUp= 1;
- int _lastScriptCommand= 0;
- int allowChangeTheSign= 0;
- int _Is_SJT_Notepad= 0; /* Experimental feature not used yet. */
- int _QsortFirstRun= 1; /* Experimental. Only have sense to show "tau factor" the first time Qsort() is run. */
- /* Use it to generate additional information about the offsets in the "old main" functions. */
- int b_file_mode= 1;
- int notepadHasChanged= 0;
- numLIFO* __lastDecomposition= NULL;
- /* An acceptable definition for negative infinity inside an integer (computer) context. */
- long _NegInfty= -999999999;
- long _kronometer00= 0;
- long __SizeFor_BendingTheWireDemo= 5;
- long __OnPrintingViewModeCommutator= 0;
- long bigSigma= 0;
- long entries= 0;
- long justDeltaMode= 0;
- long offsetShifting= 0;
- long old_justDeltaMode= 0;
- long old_offsetShifting= 0;
- long old_valueToDivide= 1;
- long old_valueTosubtract= 0;
- long precedingEntry= 0;
- long valueToDivide= 1;
- long valueTosubtract= 0;
- numBook* lastEntry= NULL;
- numBook* auxBookTail= NULL;
- numBook* swapBookHead= NULL;
- numBook* swapBookTail= NULL;
- /*
- * Begin: Globals inherited from A207324 sequencer.
- */
- const long __mode_ZERO= 0;
- const long __mode_EQUAL= 1;
- const long __mode_DETERMINANT= 2;
- const long __TOKEN_TEMPLATE= 0;
- const long __UNIT= 1;
- long __config_generator[3][2];
- long generator_settings= 0;
- /*
- * End: Globals inherited from A207324 sequencer.
- */
- int Old_main_01(void);
- int Old_main_02(void);
- int addressHackingDemo(void);
- int appendNode(numBook**, numBook**, numBook**);
- int applyQsortTo(numBook**);
- int deleteBook(numBook**);
- int deleteNode(numBook**, numBook**);
- int discardLifoStack(numLIFO**);
- int extractNode(numBook**, numBook**);
- int isolated(numBook**);
- int loadNumBookFromDisk(numBook**);
- int main(void);
- int printNumBook(numBook**);
- int saveNumBookToDisk(numBook**);
- long KthDigitOf(long, int, int, int);
- long bookSize(numBook**);
- long factorial(long);
- long greatestPowerOfBaseIn(long,int);
- long howManyDigitsIn(long, int);
- long innerRecursion(long, long);
- long numLIFONumWriter(numLIFO*);
- long refPermutOfFirst_iter(int, int);
- long refPermutOfFirst_recu(int, int);
- numBook* Qsort(numBook**);
- numBook* createNode(long);
- void triConcat(numBook**,numBook**,numBook**,numBook**);
- void clearBookAndOptions(numBook**);
- void discardNumBook(numBook**);
- void extractFromBook(numBook**);
- void goodBye(void);
- void insertToBook(numBook**, numBook**, numBook**);
- void old_printNumBook(numBook**, long);
- void print_Seq_Notepad_Main_Menu_(void);
- void requestUserInput(long*);
- void retrieveOptions(void);
- void runBendingA217626Demo(numBook**, long);
- void runSeqAn(numBook**, long* n);
- void runRainDance(numBook**, long);
- void run_seqNpad(void);
- void saveOptions(void);
- void startupGreetings(void);
- void slow_Sort(numBook**);
- void transferNode(numBook**, numBook**, numBook**);
- void versusSort(numBook**);
- /*
- * Begin: Function declarations inherited from A207324 sequencer.
- */
- /*---------> non-standards called by Old_main_01(); <-----------------------------------------------------*/
- void configure_modes(void);
- void run_my_SJT_implementation(int, long);
- /*---------> non-standards called by others; <-----------------------------------------------------*/
- void finalizer_matrix(digitsString **, digitsString**);
- void print_matrix(digitsString **);
- void apply_SteinhausJohnsonTrotterAlgorithm(digitsString**, digitsString**, long);
- void mode_matrix_waterfall(void);
- digitsString* generate_square_matrix(digitsString**, digitsString**, digitsString**, digitsString**, long);
- void initializer_matrix(digitsString **, digitsString**);
- digitsString* extract_first_nodes(digitsString**, digitsString**, long);
- void concat_matrices_node_by_node(digitsString**, digitsString**, digitsString**);
- /*---------> additional non-standards (some of them called by more than one function); <-----------------------------------------------------*/
- long only_initialized(digitsString**);
- long absolute_value(long);
- digitsString* insert_new_node(long, digitsString**);
- void close__the_ring(digitsString**, digitsString**);
- void open_the_ring(digitsString**, digitsString**);
- long LeibnitzLaplaceSignature(long);
- /*------------------------------------------------------------------------------------------------------------------------------*/
- /*
- * End: Function declarations inherited from A207324 sequencer.
- */
- /*
- * ---------------------------------------------------------------------------------------------------------
- * Formerly the main() function for the A207324 sequencer.
- * ---------------------------------------------------------------------------------------------------------
- */
- /*
- * Begin: Declarations inherited from "deception.c";
- */
- numBook nullBookBase;
- numBook* nullBook= &nullBookBase;
- long howmany= 100000;
- float _lastRecursionScore= -0.33e0;
- float _maxXRecursionScore= -0.33e0;
- int main(void);
- long insertValue(long, numBook**, numBook**);
- long numBookDiscard(numBook**, numBook**);
- int numBookPrint(int, numBook*, numBook*, int, int);
- int printNodes(numBook*, numBook*);
- numBook* concat33(numBook**, numBook**, numBook**, numBook**, numBook**, numBook**, numBook**);
- void moveNode(numBook**, numBook**, numBook**, numBook**, numBook**, long);
- void aBadQuicksort01(numBook**, numBook**, numBook**, numBook**);
- void aBadQuicksort02(numBook**, numBook**, numBook**, numBook**);
- long insertValue(long newValue, numBook** whereHead, numBook** whereTail) {
- long ans= 0;
- numBook* newEntry= NULL;
- if ((whereHead != NULL) && (whereTail != NULL)) {
- if (*whereHead == NULL) {
- (*whereHead)= (numBook*) malloc(sizeof(numBook));
- (*whereHead)->value= newValue;
- (*whereHead)->nextLink= NULL;
- (*whereHead)->prevLink= NULL;
- (*whereTail)= (*whereHead);
- ans++;
- } else {
- newEntry= (numBook*) malloc(sizeof(numBook));
- newEntry->value= newValue;
- newEntry->nextLink= NULL;
- newEntry->prevLink= (*whereTail);
- (*whereTail)->nextLink= newEntry;
- (*whereTail)= newEntry;
- newEntry= NULL;
- ans++;
- }
- } else {
- ;
- }
- return ans;
- }
- long numBookDiscard(numBook** hheeaadd, numBook** ttaaiill) {
- numBook* leftSlayer= NULL;
- numBook* rightSlayer= NULL;
- long ans= 0;
- if (hheeaadd != NULL) {
- if (*hheeaadd != NULL) {
- if (ttaaiill != NULL) {
- if (*ttaaiill != NULL) {
- leftSlayer= *hheeaadd;
- rightSlayer= *ttaaiill;
- while (
- ( ((*hheeaadd)->nextLink) != (*ttaaiill) )
- &&
- ( (*hheeaadd) != (*ttaaiill) )
- )
- {
- leftSlayer= *hheeaadd;
- rightSlayer= *ttaaiill;
- (*hheeaadd)= (*hheeaadd)->nextLink;
- (*ttaaiill)= (*ttaaiill)->prevLink;
- (*hheeaadd)->prevLink= NULL;
- (*ttaaiill)->nextLink= NULL;
- free(leftSlayer);
- free(rightSlayer);
- ans+= 2;
- }
- leftSlayer= NULL;
- rightSlayer= NULL;
- if ( (*hheeaadd) == (*ttaaiill) ) {
- *ttaaiill= NULL;
- free(*hheeaadd);
- *hheeaadd= NULL;
- ans+= 1;
- } else {
- (*hheeaadd)->nextLink= NULL;
- (*ttaaiill)->prevLink= NULL;
- free((*hheeaadd));
- free((*ttaaiill));
- (*hheeaadd)= NULL;
- (*ttaaiill)= NULL;
- ans+= 2;
- }
- }
- }
- }
- }
- return ans;
- }
- int printNodes(numBook* ref1, numBook* ref2) {
- int ans= 0;
- printf(" (");
- if (ref1 != NULL) {
- if (ref1->prevLink != NULL) {
- printf("<");
- } else {
- printf("!");
- }
- printf("%li", ref1->value);
- if (ref1->nextLink != NULL) {
- printf(">");
- } else {
- printf("!");
- }
- } else {
- printf("void");
- }
- printf(",");
- if (ref2 != NULL) {
- if (ref2->prevLink != NULL) {
- printf("<");
- } else {
- printf("!");
- }
- printf("%li", ref2->value);
- if (ref2->nextLink != NULL) {
- printf(">");
- } else {
- printf("!");
- }
- } else {
- printf("void");
- }
- printf(")");
- return ans;
- }
- int numBookPrint(int orientation, numBook* aBookHead, numBook* aBookTail, int columns, int tabDash) {
- int ans= 0;
- int rowFill= 0;
- numBook* cursor= NULL;
- if (orientation) {
- cursor= aBookHead;
- if (cursor != NULL) {
- printf("\n\n");
- while (cursor != NULL) {
- printf("%c%11li%c", ( (tabDash) ? '\t': ' ' ), cursor->value, ( (++rowFill%columns==0) ? '\n' : ' ' ) );
- cursor= cursor->nextLink;
- }
- printf("\n%li Nodes.", bookSize(&aBookHead));
- }
- else {
- printf("\n\n\t(Empty)");
- }
- } else {
- cursor= aBookTail;
- if (cursor != NULL) {
- printf("\n\n");
- while (cursor != NULL) {
- printf("%c%11li%c", ( (tabDash) ? '\t': ' ' ), cursor->value, ( (++rowFill%columns==0) ? '\n' : ' ' ) );
- cursor= cursor->prevLink;
- }
- printf("\n%li Nodes.", bookSize(&aBookHead));
- }
- else {
- printf("\n\n\t(Empty)");
- }
- }
- return ans;
- }
- numBook* concat33(numBook** copyOperand00Tail,
- numBook** copyOperand01Head,
- numBook** copyOperand01Tail,
- numBook** copyOperand02Head,
- numBook** copyOperand02Tail,
- numBook** copyOperand03Head,
- numBook** copyOperand03Tail) {
- numBook* operand00Head= NULL;
- numBook* operand00Tail= (copyOperand00Tail != NULL) ? *copyOperand00Tail : NULL;
- numBook* operand01Head= (copyOperand01Head != NULL) ? *copyOperand01Head : NULL;
- numBook* operand01Tail= (copyOperand01Tail != NULL) ? *copyOperand01Tail : NULL;
- numBook* operand02Head= (copyOperand02Head != NULL) ? *copyOperand02Head : NULL;
- numBook* operand02Tail= (copyOperand02Tail != NULL) ? *copyOperand02Tail : NULL;
- numBook* operand03Head= (copyOperand03Head != NULL) ? *copyOperand03Head : NULL;
- numBook* operand03Tail= (copyOperand03Tail != NULL) ? *copyOperand03Tail : NULL;
- if (copyOperand01Head != NULL) { *copyOperand01Head= NULL; }
- if (copyOperand01Tail != NULL) { *copyOperand01Tail= NULL; }
- if (copyOperand02Head != NULL) { *copyOperand02Head= NULL; }
- if (copyOperand02Tail != NULL) { *copyOperand02Tail= NULL; }
- if (copyOperand03Head != NULL) { *copyOperand03Head= NULL; }
- if (copyOperand03Tail != NULL) { *copyOperand03Tail= NULL; }
- if (operand00Tail != NULL) {
- if ( (operand01Head != NULL) || (operand02Head != NULL) || (operand03Head != NULL) ) {
- if ( (operand01Head != NULL) && (operand02Head != NULL) && (operand03Head != NULL) ) {
- if ( (operand01Tail != NULL) && (operand02Tail != NULL) && (operand03Tail != NULL) ) {
- operand00Head= operand01Head;
- operand01Tail->nextLink= operand02Head;
- operand02Head->prevLink= operand01Tail;
- operand02Tail->nextLink= operand03Head;
- operand03Head->prevLink= operand02Tail;
- operand00Tail= operand03Tail;
- }
- }
- if ( (operand01Head != NULL) && (operand02Head == NULL) && (operand03Head == NULL) ) {
- if (operand01Tail != NULL) {
- operand00Head= operand01Head;
- operand00Tail= operand01Tail;
- }
- }
- if ( (operand01Head == NULL) && (operand02Head != NULL) && (operand03Head == NULL) ) {
- if (operand02Tail != NULL) {
- operand00Head= operand02Head;
- operand00Tail= operand02Tail;
- }
- }
- if ( (operand01Head == NULL) && (operand02Head == NULL) && (operand03Head != NULL) ) {
- if (operand03Tail != NULL) {
- operand00Head= operand03Head;
- operand00Tail= operand03Tail;
- }
- }
- if ( (operand01Head != NULL) && (operand02Head != NULL) && (operand03Head == NULL) ) {
- if ( (operand01Tail != NULL) && (operand02Tail != NULL) ) {
- operand00Head= operand01Head;
- operand01Tail->nextLink= operand02Head;
- operand02Head->prevLink= operand01Tail;
- operand00Tail= operand02Tail;
- }
- }
- if ( (operand01Head == NULL) && (operand02Head != NULL) && (operand03Head != NULL) ) {
- if ( (operand02Tail != NULL) && (operand03Tail != NULL) ) {
- operand00Head= operand02Head;
- operand02Tail->nextLink= operand03Head;
- operand03Head->prevLink= operand02Tail;
- operand00Tail= operand03Tail;
- }
- }
- if ( (operand01Head != NULL) && (operand02Head == NULL) && (operand03Head != NULL) ) {
- if ( (operand01Tail != NULL) && (operand03Tail != NULL) ) {
- operand00Head= operand01Head;
- operand01Tail->nextLink= operand03Head;
- operand03Head->prevLink= operand01Tail;
- operand00Tail= operand03Tail;
- }
- }
- (*copyOperand00Tail)= operand00Tail;
- operand01Head= NULL;
- operand01Tail= NULL;
- operand02Head= NULL;
- operand02Tail= NULL;
- operand03Head= NULL;
- operand03Tail= NULL;
- }
- }
- return operand00Head;
- }
- void moveNode(numBook** whichNode, numBook** fromHead, numBook** fromTail, numBook** toHead, numBook** toTail, long copy_whatPos) {
- numBook* tmp01= NULL;
- numBook* tmp02= NULL;
- numBook* tmp03= NULL;
- numBook* tmp04= NULL;
- long bound= 0;
- long counter= 0;
- long whatPos= copy_whatPos;
- if (whichNode != NULL) {
- if (*whichNode != NULL) {
- if (fromHead != NULL) {
- if (*fromHead != NULL) {
- if (fromTail != NULL) {
- if (*fromTail != NULL) {
- if ( ((*whichNode) == (*fromHead)) && ((*whichNode) == (*fromTail)) ) {
- (*fromHead)= NULL;
- (*fromTail)= NULL;
- } else if ( ((*whichNode) == (*fromHead)) && ((*whichNode) != (*fromTail)) ) {
- tmp01= (*fromHead)->nextLink;
- tmp01->prevLink= NULL;
- (*whichNode)->nextLink= NULL;
- (*fromHead)= tmp01;
- } else if ( ((*whichNode) != (*fromHead)) && ((*whichNode) == (*fromTail)) ) {
- tmp02= (*fromTail)->prevLink;
- tmp02->nextLink= NULL;
- (*whichNode)->prevLink= NULL;
- (*fromTail)= tmp02;
- } else if ( ((*whichNode) != (*fromHead)) && ((*whichNode) != (*fromTail)) ) {
- tmp01= (*whichNode)->prevLink;
- tmp02= (*whichNode)->nextLink;
- (*whichNode)->nextLink= NULL;
- (*whichNode)->prevLink= NULL;
- tmp01->nextLink= tmp02;
- tmp02->prevLink= tmp01;
- tmp01= NULL;
- tmp02= NULL;
- }
- if (toHead != NULL) {
- if (toTail != NULL) {
- if ( ((*toHead) == NULL) && ((*toTail) == NULL) ) {
- (*toHead)= (*whichNode);
- (*toTail)= (*whichNode);
- }
- else {
- bound= bookSize(toHead);
- if (whatPos == bound) {
- whatPos= 0;
- }
- switch (whatPos) {
- case -1:
- if (bound >= 1) {
- (*whichNode)->nextLink= (*toHead);
- (*toHead)->prevLink= (*whichNode);
- (*toHead)= (*whichNode);
- }
- break;
- case 0:
- if (bound >= 1) {
- (*toTail)->nextLink= (*whichNode);
- (*whichNode)->prevLink= (*toTail);
- (*toTail)= (*whichNode);
- }
- break;
- default:
- if ((1<=whatPos) && (whatPos<bound)) {
- if ( (bound-whatPos) >= whatPos ) {
- tmp03= (*toHead);
- counter= 1;
- while (counter != whatPos) {
- counter++;
- tmp03= tmp03->nextLink;
- }
- }
- else {
- tmp03= (*toTail)->prevLink;
- counter= (bound-1);
- while (counter != whatPos) {
- counter--;
- tmp03= tmp03->prevLink;
- }
- }
- tmp04= tmp03->nextLink;
- tmp04->prevLink= (*whichNode);
- (*whichNode)->nextLink= tmp04;
- (*whichNode)->prevLink= tmp03;
- tmp03->nextLink= (*whichNode);
- tmp03= NULL;
- tmp04= NULL;
- }
- break;
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- void aBadQuicksort01(numBook** solutionHead, numBook** solutionTail, numBook** problemHead, numBook** problemTail) {
- numBook* leftCursor= NULL;
- numBook* rightCursor= NULL;
- numBook* equaArrayHead= NULL;
- numBook* equaArrayTail= NULL;
- numBook* lessArrayHead= NULL;
- numBook* lessArrayTail= NULL;
- numBook* greaArrayHead= NULL;
- numBook* greaArrayTail= NULL;
- numBook* lessArrayHeadSort= NULL;
- numBook* lessArrayTailSort= NULL;
- numBook* greaArrayHeadSort= NULL;
- numBook* greaArrayTailSort= NULL;
- numBook* pivotNode= NULL;
- long counter= 1;
- long bound= bookSize(problemHead);
- if (bound <= 1) {
- if (problemHead != NULL) {
- if (*problemHead != NULL) {
- if (problemTail != NULL) {
- if (*problemTail != NULL) {
- if (solutionHead != NULL) {
- if (solutionTail != NULL) {
- (*solutionHead)= (*problemHead);
- (*solutionTail)= (*problemTail);
- (*problemHead)= NULL;
- (*problemTail)= NULL;
- }
- }
- }
- }
- }
- }
- } else {
- if (problemHead != NULL) {
- if (*problemHead != NULL) {
- if (problemTail != NULL) {
- if (*problemTail != NULL) {
- if (solutionHead != NULL) {
- if (solutionTail != NULL) {
- pivotNode= *problemHead;
- bound= ( (bound/2) + (bound%2) );
- while (counter < bound) {
- pivotNode= pivotNode->nextLink;
- counter++;
- }
- moveNode(&pivotNode, problemHead, problemTail, &equaArrayHead, &equaArrayTail, 0);
- leftCursor= *problemHead;
- rightCursor= *problemTail;
- while (
- ( ((*problemHead)->nextLink) != (*problemTail) )
- &&
- ( (*problemHead) != (*problemTail) )
- )
- {
- leftCursor= *problemHead;
- rightCursor= *problemTail;
- if ( (leftCursor->value) == (pivotNode->value) ) {
- moveNode(&leftCursor, problemHead, problemTail, &equaArrayHead, &equaArrayTail, 0);
- } else if ( (leftCursor->value) < (pivotNode->value) ) {
- moveNode(&leftCursor, problemHead, problemTail, &lessArrayHead, &lessArrayTail, 0);
- } else { /* > */
- moveNode(&leftCursor, problemHead, problemTail, &greaArrayHead, &greaArrayTail, 0);
- }
- if ( (rightCursor->value) == (pivotNode->value) ) {
- moveNode(&rightCursor, problemHead, problemTail, &equaArrayHead, &equaArrayTail, 0);
- } else if ( (rightCursor->value) < (pivotNode->value) ) {
- moveNode(&rightCursor, problemHead, problemTail, &lessArrayHead, &lessArrayTail, 0);
- } else { /* > */
- moveNode(&rightCursor, problemHead, problemTail, &greaArrayHead, &greaArrayTail, 0);
- }
- }
- leftCursor= NULL;
- rightCursor= NULL;
- if ( (*problemHead) == (*problemTail) ) {
- leftCursor= *problemHead;
- if ( (leftCursor->value) == (pivotNode->value) ) {
- moveNode(&leftCursor, problemHead, problemTail, &equaArrayHead, &equaArrayTail, 0);
- } else if ( (leftCursor->value) < (pivotNode->value) ) {
- moveNode(&leftCursor, problemHead, problemTail, &lessArrayHead, &lessArrayTail, 0);
- } else { /* > */
- moveNode(&leftCursor, problemHead, problemTail, &greaArrayHead, &greaArrayTail, 0);
- }
- *problemHead= NULL;
- } else {
- leftCursor= *problemHead;
- rightCursor= *problemTail;
- if ( (leftCursor->value) == (pivotNode->value) ) {
- moveNode(&leftCursor, problemHead, problemTail, &equaArrayHead, &equaArrayTail, 0);
- } else if ( (leftCursor->value) < (pivotNode->value) ) {
- moveNode(&leftCursor, problemHead, problemTail, &lessArrayHead, &lessArrayTail, 0);
- } else { /* > */
- moveNode(&leftCursor, problemHead, problemTail, &greaArrayHead, &greaArrayTail, 0);
- }
- if ( (rightCursor->value) == (pivotNode->value) ) {
- moveNode(&rightCursor, problemHead, problemTail, &equaArrayHead, &equaArrayTail, 0);
- } else if ( (rightCursor->value) < (pivotNode->value) ) {
- moveNode(&rightCursor, problemHead, problemTail, &lessArrayHead, &lessArrayTail, 0);
- } else { /* > */
- moveNode(&rightCursor, problemHead, problemTail, &greaArrayHead, &greaArrayTail, 0);
- }
- (*problemHead)= NULL;
- (*problemTail)= NULL;
- }
- *solutionHead= nullBook;
- *solutionTail= nullBook;
- pivotNode= NULL;
- leftCursor= NULL;
- rightCursor= NULL;
- aBadQuicksort01(&lessArrayHeadSort, &lessArrayTailSort, &lessArrayHead, &lessArrayTail);
- aBadQuicksort01(&greaArrayHeadSort, &greaArrayTailSort, &greaArrayHead, &greaArrayTail);
- (*solutionHead)= concat33(solutionTail, &lessArrayHeadSort, &lessArrayTailSort, &equaArrayHead, &equaArrayTail, &greaArrayHeadSort, &greaArrayTailSort);
- }
- }
- }
- }
- }
- }
- }
- }
- /*
- * End: Declarations inherited from "deception.c";
- */
- void aBadQuicksort02(numBook** solutionHead, numBook** solutionTail, numBook** problemHead, numBook** problemTail) {
- numBook* auxCursor= NULL;
- numBook* leftCursor= NULL;
- numBook* rightCursor= NULL;
- numBook* lessArrayHead= NULL;
- numBook* lessArrayTail= NULL;
- numBook* greaArrayHead= NULL;
- numBook* greaArrayTail= NULL;
- numBook* lessArrayHeadSort= NULL;
- numBook* lessArrayTailSort= NULL;
- numBook* greaArrayHeadSort= NULL;
- numBook* greaArrayTailSort= NULL;
- numBook* pivotHead= NULL;
- numBook* pivotTail= NULL;
- long counter= 1;
- long bound= bookSize(problemHead);
- if (bound <= 1) {
- if (problemHead != NULL) {
- if (*problemHead != NULL) {
- if (problemTail != NULL) {
- if (*problemTail != NULL) {
- if (solutionHead != NULL) {
- if (solutionTail != NULL) {
- (*solutionHead)= (*problemHead);
- (*solutionTail)= (*problemTail);
- (*problemHead)= NULL;
- (*problemTail)= NULL;
- }
- }
- }
- }
- }
- }
- } else {
- if (problemHead != NULL) {
- if (*problemHead != NULL) {
- if (problemTail != NULL) {
- if (*problemTail != NULL) {
- if (solutionHead != NULL) {
- if (solutionTail != NULL) {
- auxCursor= *problemHead;
- bound= ( (bound/2) + (bound%2) );
- while (counter < bound) {
- auxCursor= auxCursor->nextLink;
- counter++;
- }
- moveNode(&auxCursor, problemHead, problemTail, &pivotHead, &pivotTail, 0);
- leftCursor= *problemHead;
- rightCursor= *problemTail;
- while (
- ( ((*problemHead)->nextLink) != (*problemTail) )
- &&
- ( (*problemHead) != (*problemTail) )
- )
- {
- leftCursor= *problemHead;
- rightCursor= *problemTail;
- if ( (leftCursor->value) <= (pivotHead->value) ) {
- moveNode(&leftCursor, problemHead, problemTail, &lessArrayHead, &lessArrayTail, 0);
- } else { /* > */
- moveNode(&leftCursor, problemHead, problemTail, &greaArrayHead, &greaArrayTail, 0);
- }
- if ( (rightCursor->value) <= (pivotHead->value) ) {
- moveNode(&rightCursor, problemHead, problemTail, &lessArrayHead, &lessArrayTail, 0);
- } else { /* > */
- moveNode(&rightCursor, problemHead, problemTail, &greaArrayHead, &greaArrayTail, 0);
- }
- }
- leftCursor= NULL;
- rightCursor= NULL;
- if ( (*problemHead) == (*problemTail) ) {
- leftCursor= *problemHead;
- if ( (leftCursor->value) <= (pivotHead->value) ) {
- moveNode(&leftCursor, problemHead, problemTail, &lessArrayHead, &lessArrayTail, 0);
- } else { /* > */
- moveNode(&leftCursor, problemHead, problemTail, &greaArrayHead, &greaArrayTail, 0);
- }
- *problemHead= NULL;
- } else {
- leftCursor= *problemHead;
- rightCursor= *problemTail;
- if ( (leftCursor->value) <= (pivotHead->value) ) {
- moveNode(&leftCursor, problemHead, problemTail, &lessArrayHead, &lessArrayTail, 0);
- } else { /* > */
- moveNode(&leftCursor, problemHead, problemTail, &greaArrayHead, &greaArrayTail, 0);
- }
- if ( (rightCursor->value) <= (pivotHead->value) ) {
- moveNode(&rightCursor, problemHead, problemTail, &lessArrayHead, &lessArrayTail, 0);
- } else { /* > */
- moveNode(&rightCursor, problemHead, problemTail, &greaArrayHead, &greaArrayTail, 0);
- }
- (*problemHead)= NULL;
- (*problemTail)= NULL;
- }
- *solutionHead= nullBook;
- *solutionTail= nullBook;
- leftCursor= NULL;
- rightCursor= NULL;
- aBadQuicksort02(&lessArrayHeadSort, &lessArrayTailSort, &lessArrayHead, &lessArrayTail);
- aBadQuicksort02(&greaArrayHeadSort, &greaArrayTailSort, &greaArrayHead, &greaArrayTail);
- (*solutionHead)= concat33(solutionTail, &lessArrayHeadSort, &lessArrayTailSort, &pivotHead, &pivotTail, &greaArrayHeadSort, &greaArrayTailSort);
- }
- }
- }
- }
- }
- }
- }
- }
- /*
- *
- */
- int Old_main_01(void) {
- int j, _OScurrentExecutionFeedbackCode = EXIT_SUCCESS;
- long N;
- configure_modes(); /* Don't change this, it is for initialization. */
- printf("%s%s","\n\nThis is the oeis.org/A207324 sequencer program. Welcome!", "\n\n\tShould be run the sequencer only for a particular SJT-array? (1=yes, 0=No) ");
- scanf("%i", &j);
- getchar();
- printf("%s","\n\n\tWhich is the upper size? (try first 1-10 on 32-bit machines): ");
- scanf("%li", &N);
- getchar();
- run_my_SJT_implementation(j, N);
- return _OScurrentExecutionFeedbackCode;
- }
- /*
- * ---------------------------------------------------------------------------------------------------------
- * Formerly the main() function for the A217626 sequencer.
- * ---------------------------------------------------------------------------------------------------------
- */
- int Old_main_02(void) {
- /* In the further it should be greater than the unit. */
- int upperBound= 1;
- numBook* beta= NULL;
- printf("\n\nThis is the oeis.org/A217626 sequencer program. Welcome!");
- do
- {
- printf("\nPlease enter N in order to generate the first (N!-1) terms of this sequence: ");
- scanf("%i",&upperBound);
- }
- while ( (0 >= upperBound) || (upperBound > 9) );
- printf("\nPlease wait... depending on the input you made this may take long.\n");
- runRainDance(&beta, upperBound);
- versusSort(&beta);
- old_printNumBook(&beta, upperBound);
- discardNumBook(&beta);
- return EXIT_SUCCESS;
- }
- int addressHackingDemo(void) {
- /*
- * This should print something like: "44, non-NULL, 134513984, nU"
- * (Where 134513984 is a memory address)
- */
- long array[3]= {0, 0, 0};
- char* dummy= "non-NULL";
- array[1]= (long) dummy;
- printf("\n%i%i, %s, %li, %c%c \n", sizeof(void*), sizeof(long), (char*)array[1], array[1], *((char*)(array[1]+2)), *((char*)(array[1]+5)) );
- return EXIT_SUCCESS;
- }
- int appendNode(numBook** WhereHead, numBook** WhereTail, numBook** addThis) {
- numBook* auxTail= NULL;
- int ans= ( isolated(addThis) && (WhereHead != NULL) );
- if (ans) {
- if (*WhereHead != NULL) {
- if (WhereTail != NULL) {
- auxTail= *WhereTail;
- } else {
- auxTail= *WhereHead;
- }
- while (auxTail->nextLink != NULL) { auxTail= auxTail->nextLink; }
- auxTail->nextLink= *addThis;
- (*addThis)->prevLink= auxTail;
- } else {
- *WhereHead= *addThis;
- if (WhereTail != NULL) {
- *WhereTail= *WhereHead;
- }
- }
- *addThis= NULL;
- }
- return ans;
- }
- int applyQsortTo(numBook** TheBook) {
- numBook* temp= NULL;
- int ans= (TheBook != NULL);
- if (ans) {
- temp= *TheBook;
- *TheBook= NULL;
- *TheBook= Qsort(&temp);
- }
- return ans;
- }
- int deleteBook(numBook** theBook) {
- int ans= (theBook != NULL);
- numBook* cursor= NULL;
- if (ans) {
- while (*theBook != NULL) {
- cursor= *theBook;
- *theBook= (*theBook)->nextLink;
- deleteNode(&cursor, theBook);
- }
- }
- return ans;
- }
- int deleteNode(numBook** nodE, numBook** origin) {
- int ans= extractNode(nodE, origin);
- if (ans) {
- free(*nodE);
- }
- return ans;
- }
- int discardLifoStack(numLIFO** lifoDecomposition) {
- int ans= (lifoDecomposition==NULL) ? -1 : 0;
- numLIFO* focus;
- if (ans != -1) while(*lifoDecomposition != NULL) {
- focus= *lifoDecomposition;
- *lifoDecomposition= (*lifoDecomposition)->belowLink;
- free(focus);
- ans++;
- }
- return ans;
- }
- int extractNode(numBook** nodE, numBook** origin) {
- int ans= (nodE != NULL);
- numBook* prevEnd= NULL;
- numBook* nextEnd= NULL;
- if (ans) {
- ans= 0;
- if ((*nodE)->prevLink != NULL) { prevEnd= (*nodE)->prevLink; }
- if ((*nodE)->nextLink != NULL) { nextEnd= (*nodE)->nextLink; }
- if ((prevEnd == NULL) && (nextEnd == NULL)) {
- ans= 1;
- }
- if ((prevEnd != NULL) && (nextEnd != NULL)) {
- (*nodE)->prevLink= NULL;
- (*nodE)->nextLink= NULL;
- prevEnd->nextLink= nextEnd;
- nextEnd->prevLink= prevEnd;
- ans= 1;
- }
- if ((prevEnd == NULL) && (nextEnd != NULL)) {
- (*nodE)->prevLink= NULL;
- (*nodE)->nextLink= NULL;
- nextEnd->prevLink= NULL;
- *origin= nextEnd;
- ans= 1;
- }
- if ((prevEnd != NULL) && (nextEnd == NULL)) {
- (*nodE)->prevLink= NULL;
- (*nodE)->nextLink= NULL;
- prevEnd->nextLink= NULL;
- ans= 1;
- }
- }
- return ans;
- }
- int isolated(numBook** Subject) {
- return ( ((*Subject)->prevLink==NULL) && ((*Subject)->nextLink==NULL) );
- }
- int loadNumBookFromDisk(numBook** theBook) {
- int ans= ( (theBook != NULL) ? 1 : 0 );
- if (ans) {
- /*
- * WhatToDo:
- * The code placed here should load a numBook previously saved in another
- * execution. Or better, allow the user select a file from a File dialog
- * box. may be it would be implemented on X for Unix/Linux easily inclusive
- * via external plugin-like components for example KDE's kdialog (it would
- * be as nice as it sounds). More time is required for make experimentation
- * on it.
- *
- */
- }
- return ans;
- }
- int main(void) {
- int (*runOld)(void);
- int ans;
- long portal[]= {(long)NULL, (long)&Old_main_01, (long)&Old_main_02};
- runOld= (int (*)(void)) portal[_Which_main];
- if (runOld == NULL) {
- ans= EXIT_SUCCESS;
- startupGreetings();
- run_seqNpad();
- goodBye();
- } else {
- ans= runOld();
- }
- return ans;
- }
- int printNumBook(numBook** TheBook) {
- int ans= ((TheBook != NULL) && (valueToDivide != 0));
- numBook* cursor= NULL;
- long lineIndex= 1;
- if (ans) {
- if (justDeltaMode) {
- cursor= *TheBook;
- precedingEntry= valueTosubtract;
- printf("\n-----------------------------------------------------");
- while(cursor != NULL) {
- printf("\n%10li\t%10li", (lineIndex + offsetShifting), ( (cursor->value - precedingEntry) / valueToDivide) );
- precedingEntry= (cursor->value);
- cursor= cursor->nextLink;
- lineIndex++;
- }
- printf("\n-----------------------------------------------------\n");
- } else {
- cursor= *TheBook;
- printf("\n-----------------------------------------------------");
- while(cursor != NULL) {
- printf("\n%10li\t%10li", (lineIndex + offsetShifting), ( (cursor->value - valueTosubtract) / valueToDivide) );
- cursor= cursor->nextLink;
- lineIndex++;
- }
- printf("\n-----------------------------------------------------\n");
- }
- }
- return ans;
- }
- int saveNumBookToDisk(numBook** TheBook) {
- int ans= (TheBook != NULL);
- if (ans) {
- /*
- * WhatToDo: The code placed here should flush the data on the notepad in a single disk file.
- */
- ans*= deleteBook(TheBook);
- }
- return ans;
- }
- long KthDigitOf(long copy_whichNum, int copy_whatBase, int copy_atPosition, int copy_withOrientation) {
- numLIFO* lifoDecomposition= NULL;
- numLIFO* focus;
- long whichNum= (copy_whichNum >= 0) ? copy_whichNum : -1*copy_whichNum;
- int whatBase= 10;
- int atPosition= copy_atPosition;
- int withOrientation= copy_withOrientation;
- int displacement;
- int deltaDisplacement= 1;
- long ans=-1;
- long numSize= howManyDigitsIn(whichNum,whatBase);
- long test;
- if ((whichNum==0) && (atPosition==1)) {
- ans=0;
- } else if ((atPosition >= 1) && (atPosition <= numSize) ) {
- while (whichNum != 0) {
- focus= NULL;
- test= 0;
- while (((whichNum-test) % whatBase) != 0) { test++; }
- focus= (numLIFO*) malloc(sizeof(numLIFO));
- focus->value= test;
- focus->belowLink= lifoDecomposition;
- lifoDecomposition=focus;
- whichNum-=test;
- whichNum/=whatBase;
- }
- }
- focus= lifoDecomposition;
- if (withOrientation == -1) {
- atPosition*=-1;
- atPosition+=numSize+1;
- }
- displacement=1;
- while(focus != NULL) {
- if (displacement==atPosition) ans= focus->value;
- focus= focus->belowLink;
- displacement+=deltaDisplacement;
- }
- if (!__savingCallsToMallocMode) {
- discardLifoStack(&lifoDecomposition);
- } else {
- __lastDecomposition= lifoDecomposition;
- }
- return ans;
- }
- long bookSize(numBook** Array) {
- numBook* cursor= (Array != NULL) ? *Array : NULL;
- long count= 0;
- while (cursor != NULL) {
- count++;
- cursor= cursor->nextLink;
- }
- return count;
- }
- long factorial(long copy_arg) {
- long arg, ans;
- ans= 1;
- arg= (copy_arg >= 0) ? copy_arg : -1*copy_arg;
- while (arg > 0) { ans*= arg--; }
- return (copy_arg >= 0) ? ans : -1*ans;
- }
- long greatestPowerOfBaseIn(long whichNum, int whatBase) {
- long ans= -1;
- if (whatBase > 1) ans+= (0 == whichNum) ? (1+_NegInfty) : (1+floor(fabs(log(whichNum))/log(whatBase)));
- return ans;
- }
- long howManyDigitsIn(long whichNum, int whatBase) {
- long ans= -1;
- if (whatBase > 1) {
- switch (whichNum) {
- case 0:
- case 1:
- ans= 1;
- break;
- default:
- if (whichNum == whatBase) {
- ans= 2;
- } else {
- ans= (1 + greatestPowerOfBaseIn(whichNum, whatBase));
- }
- }
- }
- return ans;
- }
- long innerRecursion(long arg1, long arg2) {
- return ((arg1==arg2) ? arg1 : arg2 + 10*innerRecursion(arg1, arg2-1));
- }
- long numLIFONumWriter(numLIFO* Q) {
- return (((Q->belowLink)==NULL) ? Q->value : Q->value + 10*numLIFONumWriter(Q->belowLink));
- }
- long refPermutOfFirst_iter(int arg, int base) {
- long ans=0;
- int nu;
- for (nu=0; nu < arg; nu++) {
- ans+=(arg-nu)*floor(pow(base,nu));
- }
- return ans;
- }
- long refPermutOfFirst_recu(int arg, int base) {
- return innerRecursion(1,arg);
- }
- numBook* Qsort(numBook** array) {
- numBook* cursor01= NULL;
- numBook* cursor02= NULL;
- numBook* equa_head= NULL;
- /*
- numBook* equa_tail= NULL;
- */
- long addr_head[]= {0,0,0};
- int addrSize= 3;
- int index= 0;
- numBook** arg00a= NULL;
- numBook** arg01a= NULL;
- numBook** arg02a= NULL;
- numBook** arg03a= NULL;
- numBook* arg00b= NULL;
- numBook* less_head= NULL;
- numBook* less_tail= NULL;
- numBook* grea_head= NULL;
- numBook* grea_tail= NULL;
- numBook* answ_head= (array != NULL) ? *array : NULL;
- long arrayLength= bookSize(array);
- long pivot= (arrayLength/2)+(arrayLength%2);
- long counter= 0;
- if (arrayLength <= 1) {
- array= NULL;
- } else {
- cursor01= *array;
- for (counter=0; (counter < pivot); counter++) { cursor01= cursor01->nextLink; }
- extractNode(&cursor01, array);
- equa_head= cursor01;
- /*
- equa_tail= cursor01;
- */
- cursor01= NULL;
- cursor02= *array;
- while (cursor02 != NULL) {
- cursor01= cursor02;
- cursor02= cursor02->nextLink;
- extractNode(&cursor01, array);
- if ( (cursor01->value) <= (equa_head->value) ) {
- transferNode(&cursor01, &less_head, &less_tail);
- } /*else if ( (cursor01->value) == (equa_head->value) ) {
- transferNode(&cursor01, &equa_head, &equa_tail);
- }*/ else {
- transferNode(&cursor01, &grea_head, &grea_tail);
- }
- }
- applyQsortTo(&less_head);
- applyQsortTo(&grea_head);
- /*
- triConcat(&answ_head, &less_head, &equa_head, &grea_head);
- */
- /* Inline substitution for the preceding call... */
- arg00a= &answ_head;
- arg01a= &less_head;
- arg02a= &equa_head;
- arg03a= &grea_head;
- addr_head[0]= (long)arg01a; addr_head[1]= (long)arg02a; addr_head[2]= (long)arg03a;
- arg01a= NULL; arg02a= NULL; arg03a= NULL;
- while ( bookSize( (numBook**) addr_head[index]) == 0 ) {index++;}
- if ( index < addrSize ) {
- (*arg00a)= (*((numBook**)(addr_head[index])));
- arg00b= (*arg00a);
- while ( arg00b->nextLink != NULL ) { arg00b= arg00b->nextLink; }
- index++;
- }
- while ( index < addrSize ) {
- if ( bookSize( (numBook**) addr_head[index]) != 0 ) {
- arg00b->nextLink= (*((numBook**)(addr_head[index])));
- (*((numBook**)(addr_head[index])))->prevLink= arg00b;
- while ( arg00b->nextLink != NULL ) { arg00b= arg00b->nextLink; }
- }
- index++;
- }
- }
- return answ_head;
- }
- numBook* createNode(long firstElement) {
- numBook* genesis= (numBook*) malloc(sizeof(numBook));
- genesis->value= firstElement;
- genesis->prevLink= NULL;
- genesis->nextLink= NULL;
- return genesis;
- }
- void triConcat(numBook** arg00a, numBook** arg01a, numBook** arg02a, numBook** arg03a) {
- long addr_head[]= {0,0,0};
- int addrSize= 3;
- int index= 0;
- numBook* arg00b= NULL;
- addr_head[0]= (long)arg01a; addr_head[1]= (long)arg02a; addr_head[2]= (long)arg03a;
- arg01a= NULL; arg02a= NULL; arg03a= NULL;
- while ( bookSize( (numBook**) addr_head[index]) == 0 ) {index++;}
- if ( index < addrSize ) {
- (*arg00a)= (*((numBook**)(addr_head[index])));
- arg00b= (*arg00a);
- while ( arg00b->nextLink != NULL ) { arg00b= arg00b->nextLink; }
- index++;
- }
- while ( index < addrSize ) {
- if ( bookSize( (numBook**) addr_head[index]) != 0 ) {
- arg00b->nextLink= (*((numBook**)(addr_head[index])));
- (*((numBook**)(addr_head[index])))->prevLink= arg00b;
- while ( arg00b->nextLink != NULL ) { arg00b= arg00b->nextLink; }
- }
- index++;
- }
- }
- void clearBookAndOptions(numBook** theBook) {
- auxBookTail= NULL;
- swapBookHead= NULL;
- swapBookTail= NULL;
- deleteBook(theBook);
- entries= 0;
- _firstCleanUp= 0;
- valueTosubtract= 0;
- valueToDivide= 1;
- offsetShifting= 0;
- justDeltaMode= 0;
- old_justDeltaMode= 0;
- old_offsetShifting= 0;
- old_valueToDivide= 1;
- old_valueTosubtract= 0;
- _Is_SJT_Notepad= 0;
- notepadHasChanged= 0;
- }
- void discardNumBook(numBook** bookOfPermutations) {
- deleteBook(bookOfPermutations);
- }
- void extractFromBook(numBook** catch) {
- numBook* prev= (*catch)->prevLink;
- numBook* next= (*catch)->nextLink;
- (*catch)->prevLink= NULL;
- (*catch)->nextLink= NULL;
- if ( (prev != NULL) && (next != NULL) ) {
- prev->nextLink= next;
- next->prevLink= prev;
- }
- if ( (prev == NULL) && (next != NULL) ) {
- next->prevLink= NULL;
- }
- if ( (prev != NULL) && (next == NULL) ) {
- prev->nextLink= NULL;
- }
- if ( (prev == NULL) && (next == NULL) ) {
- ; /* Nothing! :-) */
- }
- }
- void goodBye(void) {
- if ( !(_BatchInputMode) ) {
- printf("\n\nThat's all for now. (Exiting to the OS)\n\n");
- }
- }
- void insertToBook(numBook** ElementToInsert, numBook** BookHead, numBook** BookTail) {
- if ( (ElementToInsert != NULL) && ( (BookHead != NULL) && (BookTail != NULL) ) ) {
- if (*BookHead == NULL) {
- *BookHead= *ElementToInsert;
- *BookTail= *BookHead;
- } else {
- if (*BookTail == NULL) {
- *BookTail= *BookHead;
- while ((*BookTail)->nextLink != NULL) { *BookTail= (*BookTail)->nextLink; }
- }
- (*BookTail)->nextLink= *ElementToInsert;
- (*BookTail)= *ElementToInsert;
- }
- *ElementToInsert= NULL;
- }
- }
- void old_printNumBook(numBook** theBook, long forFirst) {
- long ground= refPermutOfFirst_iter(forFirst, 10);
- /*
- * By default it should be initialized as zero!!
- */
- long offsetCounter= 0;
- numBook* cursor;
- cursor= *theBook;
- if (!b_file_mode) {
- printf("\n------------------");
- while (cursor != NULL) {
- if ((cursor->value - ground)/(10-1) != 0) {
- printf("\n%li\t%li", ++offsetCounter, ((cursor->value - ground)/(10-1)) );
- }
- ground=cursor->value;
- cursor= cursor->nextLink;
- }
- printf("\n------------------\n");
- } else {
- printf("\n------------------");
- while (cursor != NULL) {
- if ((cursor->value - ground)/(10-1) != 0) {
- printf("\n%li\t%li", ++offsetCounter, ((cursor->value - ground)/(10-1)) );
- }
- ground=cursor->value;
- cursor= cursor->nextLink;
- }
- printf("\n------------------\n");
- }
- }
- void print_Seq_Notepad_Main_Menu_(void) {
- if ( !(_BatchInputMode) ) {
- printf("\n");
- printf("\n\t===========================================================================");
- printf("\n\t seqNpad ver:3.29");
- printf("\n\t===========================================================[ Main menu ]===");
- printf("\n\t -1 Multiply by -1 the last entry.");
- printf("\n\t -2 Exit to the OS.");
- printf("\n\t -3 Print the notepad.");
- printf("\n\t -4 For delete a number.");
- printf("\n\t -5 For move a number.");
- printf("\n\t -6 Sort the notepad (applying Qsort).");
- printf("\n\t -7 Set the output modifiers.");
- printf("\n\t -8 Set a shifting for the offsets.");
- printf("\n\t -9 Reset to a new and empty notepad.");
- printf("\n\t -10 Sum/multiply each entry in the notebook with/by a given value.");
- printf("\n\t -11 Insert a number.");
- printf("\n\t -12 Run sequencer for a(n) and store its first terms in a NEW notepad.");
- printf("\n\t===========================================================================");
- printf("\n\t Every non-negative number will be stored directly in the current notepad.");
- }
- }
- void requestUserInput(long* reader) {
- if (_BatchInputMode) {
- *reader= _Script[_ScriptCursor++];
- switch (*reader) {
- case -112:
- *reader=_ScriptMem01;
- break;
- case -211:
- _rec= 1;
- *reader= -911;
- case -911: if (_lastScriptCommand != -6) {
- _lastScriptCommand= -911;
- printf("(Awaiting for user input) ");
- scanf(_CtrlStr, reader);
- getchar();
- if (_rec) {
- _ScriptMem01= *reader;
- _rec= 0;
- }
- } break;
- case -1011:
- if (_ScriptMem01 != 0) {
- *reader= refPermutOfFirst_iter(_ScriptMem01, 10);
- } else {
- *reader= -2;
- }
- break;
- case -666: if (_lastScriptCommand != -6) {
- _ScriptCursor= 0;
- _firstCleanUp= 1;
- printf("%li\n", *reader);
- } break;
- case -321: if (_lastScriptCommand != -6 ) {
- /*
- * Any code placed here should make a pause (devised for demonstrations).
- */
- _lastScriptCommand= -321;
- printf("%li\n", *reader);
- } break;
- default:
- printf("%li, ", *reader);
- _lastScriptCommand= *reader;
- break;
- }
- } else {
- scanf(_CtrlStr, reader);
- getchar();
- }
- }
- void retrieveOptions(void) {
- valueTosubtract= old_valueTosubtract;
- valueToDivide= old_valueToDivide;
- justDeltaMode= old_justDeltaMode;
- offsetShifting= old_offsetShifting;
- if (notepadHasChanged) { _Is_SJT_Notepad= 0; }
- }
- void runBendingA217626Demo(numBook** theBook, long wireSize) {
- printNumBook(theBook);
- }
- void runSeqAn(numBook** onThisBook, long* n) {
- /*
- *
- * This is a demo. Your a(n) generating function must be
- * implemented in C and properly placed here.
- *
- */
- /* */
- runRainDance(onThisBook, *n);
- applyQsortTo(onThisBook);
- /* */
- /* * /
- runRainDance(&swapBookHead, *n);
- swapBookTail= auxBookTail;
- auxBookTail= NULL;
- aBadQuicksort01(onThisBook, &auxBookTail, &swapBookHead, &swapBookTail);
- / * */
- *n= factorial(*n);
- /*
- * Due the inconvenience of not having a better way
- * of inverse function for the integer factorial,
- * the author decided use "n" as shown above.
- * However, let the user remember in this
- * case that "n" points to "entries".
- *
- * (Of course this comment is about the clear
- * difference existing in a general context
- * between the numbers n, and n!)
- *
- */
- }
- void runRainDance(numBook** Output, long forFirst) {
- numBook* emul= NULL;
- numBook** Input= &emul;
- numBook* qurzor= NULL;
- numBook* newPerm= NULL;
- numBook* OutputTail= NULL;
- numLIFO* add= NULL;
- numLIFO* swapper= NULL;
- long tmp= 0;
- long lambda= 0;
- int signature= 1;
- auxBookTail= NULL;
- __savingCallsToMallocMode= 1;
- if (__lastDecomposition != NULL) {
- discardLifoStack(&__lastDecomposition);
- }
- if ((Output != NULL) && (forFirst >= 1)) {
- (*Output)= (numBook*) malloc(sizeof(numBook));
- (*Output)->value= 1;
- (*Output)->prevLink= NULL;
- (*Output)->nextLink= NULL;
- for (lambda= 2; lambda<= forFirst; lambda++) {
- if (*Input != NULL) {
- discardNumBook(Input);
- }
- (*Input)= (*Output);
- *Output= NULL;
- OutputTail= NULL;
- qurzor= *Input;
- while (qurzor != NULL) {
- KthDigitOf(qurzor->value,10,1,1);
- add= (numLIFO*) malloc(sizeof(numLIFO));
- add->value= lambda;
- add->belowLink= __lastDecomposition;
- __lastDecomposition= add;
- newPerm= (numBook*) malloc(sizeof(numBook));
- newPerm->value= numLIFONumWriter(__lastDecomposition);
- newPerm->nextLink= NULL;
- if (*Output == NULL) {
- newPerm->prevLink= NULL;
- *Output= newPerm;
- OutputTail= *Output;
- } else {
- newPerm->prevLink= OutputTail;
- OutputTail->nextLink= newPerm;
- OutputTail= newPerm;
- }
- for (swapper=__lastDecomposition; swapper->belowLink != NULL; ) {
- tmp= swapper->value;
- add= swapper->belowLink;
- (swapper->value)=(add->value);
- (add->value)= tmp;
- swapper=swapper->belowLink;
- newPerm= (numBook*) malloc(sizeof(numBook));
- newPerm->value= numLIFONumWriter(__lastDecomposition);
- newPerm->nextLink= NULL;
- if (*Output == NULL) {
- (*Output)=newPerm;
- (*Output)->prevLink= NULL;
- OutputTail= *Output;
- }
- else {
- newPerm->prevLink= OutputTail;
- OutputTail->nextLink= newPerm;
- OutputTail=newPerm;
- }
- }
- signature*= -1;
- qurzor= qurzor->nextLink;
- add= NULL;
- discardLifoStack(&__lastDecomposition);
- }
- }
- }
- __savingCallsToMallocMode= 0;
- if (*Input != NULL) {
- discardNumBook(Input);
- }
- auxBookTail= OutputTail;
- }
- void run_seqNpad(void) {
- numBook* seqNotepad= NULL;
- numBook* pencil01= NULL;
- numBook* pencil02= NULL;
- numBook* pencil03= NULL;
- numBook* aThinWire= NULL;
- /*
- long gamma= 0;
- */
- long userInput01= 0;
- long userInput02= 0;
- long userInput03= 0;
- long userInput04= 0;
- long auxCounter01;
- long auxCounter02;
- long temp;
- do {
- loadNumBookFromDisk(&seqNotepad);
- print_Seq_Notepad_Main_Menu_();
- if ( !(_BatchInputMode) ) { printf("\n\n\t(Enter a seqNpad command) >> "); }
- requestUserInput(&userInput01);
- if (userInput01 >= 0) {
- pencil01= createNode(userInput01);
- lastEntry= pencil01;
- allowChangeTheSign= 1;
- appendNode(&seqNotepad, NULL, &pencil01);
- entries++;
- notepadHasChanged= 1;
- } else {
- __OnPrintingViewModeCommutator= userInput01;
- if (userInput01 != -1) {
- lastEntry= NULL;
- allowChangeTheSign= 0;
- }
- switch (__OnPrintingViewModeCommutator) {
- case -4:
- case -5:
- case -6:
- case -11:
- saveOptions();
- break;
- default:
- break;
- }
- switch (userInput01) {
- case -1:
- if (allowChangeTheSign) {
- lastEntry->value*= -1;
- lastEntry= NULL;
- allowChangeTheSign= 0;
- }
- break;
- case -2:
- printf("\nYou are about to exit the interpreter. \nAre you sure? (1=yes, 0=NO): ");
- do requestUserInput(&userInput02); while ( !( (userInput02 == 0) || (userInput02 == 1) ) );
- userInput01*= userInput02;
- printf("\n");
- break;
- case -3:
- printf("\n\nThe following information is part of the notepad:");
- printNumBook(&seqNotepad);
- printf("%li Element(s).\n", entries);
- if (entries > 0) {
- bigSigma= 0;
- pencil03= seqNotepad;
- precedingEntry= valueTosubtract;
- while (pencil03 != NULL) {
- bigSigma+= (long)( (pencil03->value - precedingEntry)/valueToDivide );
- if (justDeltaMode) { precedingEntry= pencil03->value; }
- pencil03= pencil03->nextLink;
- }
- printf("\nThe sum of every entry in the notepad is: %10li;\nAnd the average among all the entries is: %10.5f;\n", bigSigma, (float)(bigSigma/entries) );
- }
- do {
- printf("\nEnter \"1\" to continue when ready: ");
- requestUserInput(&userInput03);
- } while (userInput03 != 1);
- break;
- case -4: if (entries > 0) {
- do {
- printNumBook(&seqNotepad);
- printf("%li Element(s).\n", entries);
- printf("\nEnter the offset of the number you want to delete \n(See the left column, 0 to abort): ");
- if (entries == 1) { userInput01= 1; printf("1\n"); }
- else if (entries == 0) { userInput01= 0; printf("0\n"); }
- else { requestUserInput(&userInput01); }
- } while ( !((0 <= userInput01 ) || ( userInput01 <= entries )) );
- if (userInput01 != 0) {
- pencil01= seqNotepad;
- auxCounter01= 0;
- while ((pencil01 != NULL) && (++auxCounter01 != userInput01)) {
- pencil01= pencil01->nextLink;
- }
- if (auxCounter01 == userInput01) {
- if (entries > 1) { deleteNode(&pencil01, &seqNotepad); }
- else { deleteBook(&seqNotepad); }
- if (entries >= 1) { entries--; }
- printNumBook(&seqNotepad);
- printf("%li Element(s).\n", entries);
- }
- notepadHasChanged= 1;
- }
- } else {
- printf("\nThe notepad is empty. There is nothing to delete.\n");
- }
- break;
- case -5: if (entries > 0) {
- printNumBook(&seqNotepad);
- printf("%li Element(s).\n", entries);
- do {
- printf("\nEnter the offset of the number you want to move \n(See the left column, 0 to abort): ");
- requestUserInput(&userInput01);
- } while ( !( (userInput01 >= 0) && (userInput01 <= entries ) ) );
- do {
- printf("\nEnter the offset of the new position: \n(See the left column, 0 to abort): ");
- if (userInput01 != 0) { requestUserInput(&userInput02); } else { userInput02= 0; printf("0\n"); }
- } while ( !( (userInput02 >= 0) && (userInput02 <= entries ) ) );
- if (( userInput01 != 0) && (userInput02 != 0) && (userInput01 != userInput02) ) {
- pencil01= seqNotepad;
- auxCounter01= 0;
- while ((pencil01 != NULL) && (++auxCounter01 != userInput01)) {
- pencil01= pencil01->nextLink;
- }
- pencil02= seqNotepad;
- auxCounter02= 0;
- while ((pencil02 != NULL) && (++auxCounter02 != userInput02)) {
- pencil02= pencil02->nextLink;
- }
- temp=pencil01->value;
- pencil01->value= pencil02->value;
- pencil02->value= temp;
- }
- printNumBook(&seqNotepad);
- printf("%li Element(s).\n", entries);
- notepadHasChanged= 1;
- } else {
- printf("\nThe notepad is empty. There is nothing to move.\n");
- }
- break;
- case -6: if (entries > 1) {
- _kronometer00= 0;
- printf("\n\tApplying Qsort. Please wait...");
- _kronometer00-= clock();
- applyQsortTo(&seqNotepad);
- _kronometer00+= clock();
- if (_QsortFirstRun) {
- printf(" Ready. (\"tau\" factor: %.4f)", ((float)_kronometer00/entries));
- _QsortFirstRun= 0;
- } else {
- printf(" Ready.");
- }
- /*
- if (!_BatchInputMode) {
- printNumBook(&seqNotepad);
- printf("%li Element(s).\n", entries);
- do {
- printf("\nEnter \"1\" to continue when ready: ");
- requestUserInput(&userInput03);
- } while (userInput03 != 1);
- printf("\n");
- }
- */
- notepadHasChanged= 1;
- } else if (entries == 0) {
- printf("\nThe notepad is empty. There is nothing to sort.\n");
- }
- break;
- case -7:
- printf("\n Output modifiers settings:\n============================\n");
- printf("\nAll the entries of a notepad may be processed before printing them to stdout,");
- printf("\nthis is done subtracting and dividing each entry with certain values you may specify.");
- printf("\n\nBy default when printing, zero is subtracted to each entry and then it is divided by 1.");
- printf("\nDo you want to change these values? (1=yes, 0=NO): ");
- do requestUserInput(&userInput01); while ( !( (userInput01 == 0) || (userInput01 == 1) ) );
- if (userInput01) {
- printf("The following value will be subtracted to each entry in the notepad... \n(please enter an integer value): ");
- requestUserInput(&userInput02);
- do {
- printf("All the differences described before will be divided by... \n(please enter an integer value. Zero is not allowed): ");
- requestUserInput(&userInput03);
- } while (userInput03 == 0);
- valueTosubtract= userInput02;
- valueToDivide= userInput03;
- printf("Enable the \"justDelta\" feature? (1=yes, 0=NO): ");
- do {
- requestUserInput(&justDeltaMode);
- } while ( !( (justDeltaMode == 0) || (justDeltaMode == 1) ) );
- }
- break;
- case -8:
- printf("\nBy default, when displaying the entries of a notepad, the offset column starts in 1.");
- printf("\nYou may change this behavior by defining a \"shifting\" value. It will added to the inner");
- printf("\ncounter, fixing the offset in the way you want.");
- printf("\nEnter a new value for shifting (the current is %li): ",offsetShifting);
- requestUserInput(&userInput03);
- do {
- printf("\nYou are about to change the offset shifting from %li to %li; \n Is this correct? (1=yes, 0=NO): ", offsetShifting, userInput03);
- requestUserInput(&userInput02);
- } while ( !( (userInput02 != 0) || (userInput02 != 1) ) );
- if (userInput02) {
- offsetShifting= userInput03;
- printf("\nThe new starting offset will be: %li\n", (1+offsetShifting));
- }
- break;
- case -9:
- if ( (_firstCleanUp) && (_BatchInputMode) ) { clearBookAndOptions(&seqNotepad); } else { if (entries > 0) {
- printf("\nThis will delete the numbers stored in the notepad.\n Proceed? (1=yes, 0=NO): ");
- requestUserInput(&userInput02);
- if (userInput02) {
- printf("\nWARNING: You cannot undo this action.\n Are you sure? (1=yes, 0=NO): ");
- requestUserInput(&userInput03);
- if (userInput03) {
- clearBookAndOptions(&seqNotepad);
- printNumBook(&seqNotepad);
- printf("%li Element(s).\n", entries);
- }
- } } else {
- printf("\nThe notepad is already empty!\n");
- }
- }
- break;
- case -10:
- if (entries > 0) {
- do {
- printf("\nYou're about to: \n\t\tsum/multiply each entry in the notebook \n\t\twith/by a given value.\n\nProceed? (1=yes, 0=NO): ");
- requestUserInput(&userInput03);
- } while (!( (userInput03 >= 0) && (userInput03 <= 1) ));
- if (userInput03 == 1) {
- do {
- printf("\nSelect the operation (1=Sum, 0=Multiply): ");
- requestUserInput(&userInput02);
- } while ((userInput02 != 0) && (userInput02 != 1));
- printf("\nEnter the value (0 to abort): ");
- requestUserInput(&userInput04);
- if (userInput04 != 0) {
- if (userInput02 == 1) {
- pencil03= seqNotepad;
- while (pencil03 != NULL) {
- pencil03->value+= userInput04;
- pencil03= pencil03->nextLink;
- }
- }
- if (userInput02 == 0) {
- pencil03= seqNotepad;
- while (pencil03 != NULL) {
- pencil03->value*= userInput04;
- pencil03= pencil03->nextLink;
- }
- }
- }
- }
- } else {
- printf("\n\n\tBefore doing this, you should enter some numbers to the notebook.\n");
- }
- break;
- case -11:
- if (entries > 0) {
- printf("\nInserting a new number saves moving/deleting operations.");
- printNumBook(&seqNotepad);
- printf("%li Element(s).\n", entries);
- do {
- printf("\nPlease enter the offset for the element that would be placed below");
- printf("\nthe number that you want to insert (See the left column, 0 to abort): ");
- requestUserInput(&userInput02);
- } while ( !( (userInput02 >= 0) && (userInput02 <= entries ) ) );
- if (userInput02 != 0) {
- printf("\nEnter the new value to insert: ");
- requestUserInput(&userInput03);
- pencil03= createNode(userInput03);
- lastEntry= pencil03;
- userInput01= 1;
- pencil01= seqNotepad;
- while (userInput01 != userInput02) {
- pencil02=pencil01;
- pencil01=pencil01->nextLink;
- userInput01++;
- }
- if (userInput02 == 1) {
- pencil01->prevLink= pencil03;
- pencil03->nextLink= pencil01;
- seqNotepad= pencil03;
- } else {
- pencil02->nextLink= pencil03;
- pencil03->prevLink= pencil02;
- pencil01->prevLink= pencil03;
- pencil03->nextLink= pencil01;
- }
- allowChangeTheSign= 1;
- entries++;
- notepadHasChanged= 1;
- }
- } else {
- printf("\nThis action is not available while the notepad is empty. \n");
- }
- break;
- case -12:
- /*
- *
- * Since this step generates a SJT notepad, it should not mess with "notepadHasChanged" beyond
- * setting its value to zero. Why?: "notepadHasChanged" is a security measure to prevent wrong
- * assumptions when using Quicksort (in order to apply always the proper variation).
- *
- * Any other step modifying a notepad should take among its duties setting "notepadHasChanged" to "1"
- * ONLY if a real change in the notepad was made upon its execution (or leaving it unchanged if aborted).
- *
- * (The behavior described here is imperative!)
- *
- * A colateral effect: Every SJT notepads loaded MANUALLY by the user or via internal script won't be
- * treated as SJT when sorting.
- *
- * Then an additional measure to keep Qb taking advantage of knowing whenever a notepad is SJT or not:
- *
- * Check before applying Qsort the following:
- *
- * 1) Counting the number of entries for the given notepad, must result a factorial. [ O(n!) fixed ]
- * 2) Every entry must be a number compound of n digits.
- * 3) Every entry must be unique. [ O(n^2) in the worst case, precisely in a true SJT Nbook ]
- * 4) Multiplying all the digits in each entry, n! must arise as such product.
- *
- * Then select the proper variation according the case.
- *
- * Check this each time Qsort is applied would be certainly a waste. For such reason the "notepadHasChanged" flag
- * mechanism was implemented.
- *
- */
- if (entries > 0) {
- printf("\nThis will delete the numbers stored in the notepad.\n Proceed? (1=yes, 0=NO): ");
- requestUserInput(&userInput02);
- if (userInput02) {
- printf("\nWARNING: You cannot undo this action.\n Are you sure? (1=yes, 0=NO): ");
- requestUserInput(&userInput03);
- if (userInput03) {
- clearBookAndOptions(&seqNotepad);
- do {
- printf("\n%s.\n\nEnter \"m\" for generating the a(n) terms for n= 1..%s; m= ", seqAnDescriptorString, (_a217626_fixTextGlitch) ? "m!" : "m");
- requestUserInput(&userInput04);
- } while ( !( (userInput04 >= 1) && (userInput04 <= 11) ) );
- entries= userInput04;
- runSeqAn(&seqNotepad, &entries);
- _Is_SJT_Notepad= 1;
- notepadHasChanged= 0;
- }
- }
- }
- else {
- clearBookAndOptions(&seqNotepad);
- do {
- printf("\n%s.\n\nEnter \"m\" for generating the a(n) terms for n= 1..%s; m= ", seqAnDescriptorString, (_a217626_fixTextGlitch) ? "m!" : "m");
- requestUserInput(&userInput04);
- } while ( !( (userInput04 >= 1) && (userInput04 <= 11) ) );
- entries= userInput04;
- runSeqAn(&seqNotepad, &entries);
- _Is_SJT_Notepad= 1;
- notepadHasChanged= 0;
- }
- break;
- /*
- case -13: / * Experimental: Bending a piece of A217626 * /
- gamma= __SizeFor_BendingTheWireDemo;
- if (aThinWire == NULL) { runSeqAn(&aThinWire, &gamma); }
- runBendingA217626Demo(&aThinWire,gamma);
- break;
- */
- default:
- printf("\nUnknown command. Try \"-2\".\n");
- break;
- }
- switch (__OnPrintingViewModeCommutator) {
- case -4:
- case -5:
- case -6:
- case -11:
- retrieveOptions();
- break;
- default:
- break;
- }
- }
- } while (userInput01 != -2);
- if (aThinWire != NULL) { deleteBook(&aThinWire); }
- saveNumBookToDisk(&seqNotepad);
- }
- /* This is a friendly "keep out!... brave dog" banner at the LAB's closed door. */
- /* * /
- void run_seqNpad(void) {
- long pencil= 0;
- long sum= 0;
- long count= 0;
- _BatchInputMode= 0;
- printf("\n\n:-) \n\t\tHi OEIS!, \n\t\t\tHello seqFan!. \n\n\tThis is an Interpreter and Notepad for Integer Sequences.\n\t(Possibly yet under construction if you are reading this message)");
- do {
- printf("\n\n(There's nothing implemented yet. Please excuse me... \nEnter -2 to exit, or a nonn to sum, 0 to restart the sum) >> ");
- requestUserInput(&pencil);
- if (pencil == 0) {
- sum= 0;
- count= 0;
- } else if (pencil > 0) {
- sum+= pencil;
- count++;
- }
- } while (pencil != -2);
- if (count > 1) {
- printf("\n\n\tSum: %li; Average[%li number(s)]:%9.3f", sum, count, (float)sum/count);
- }
- printf("\n\nGood bye. Come back soon.\n\n");
- }
- / * */
- void saveOptions(void) {
- old_valueTosubtract= valueTosubtract;
- old_valueToDivide= valueToDivide;
- old_justDeltaMode= justDeltaMode;
- old_offsetShifting= offsetShifting;
- valueTosubtract= 0;
- valueToDivide= 1;
- justDeltaMode= 0;
- offsetShifting= 0;
- }
- void startupGreetings(void) {
- if ( !(_BatchInputMode) ) {
- printf("\n\tseqNpad - Script interpreter and Notepad for Integer Sequences ver:3.29 \n\t\tR. J. Cano, <aallggoorriitthhmmuuss@gmail.com>\n\n\tWelcome!");
- } else {
- printf("\nseqNpad^SI ver:3.29 - Script Interpreter (Batch mode On):\n\n");
- }
- }
- void slow_Sort(numBook** BookToSort) {
- numBook* fromWhere= *BookToSort;
- numBook* searchCursor;
- numBook* compare;
- long tmp;
- while (fromWhere != NULL) {
- compare=fromWhere;
- searchCursor= fromWhere;
- while (searchCursor != NULL) {
- if (compare->value > searchCursor->value) {
- tmp=searchCursor->value;
- searchCursor->value= compare->value;
- compare->value= tmp;
- }
- searchCursor= searchCursor->nextLink;
- }
- fromWhere= fromWhere->nextLink;
- }
- }
- void transferNode(numBook** BB, numBook** AA_head, numBook** AA_tail) {
- if (BB != NULL) {
- if (AA_head != NULL) {
- if (AA_tail != NULL) {
- if (*AA_head == NULL) {
- *AA_head= *BB;
- *AA_tail= *BB;
- *BB= NULL;
- } else {
- while ((*AA_tail)->nextLink != NULL) {
- (*AA_tail)= (*AA_tail)-> nextLink;
- }
- (*AA_tail)->nextLink= *BB;
- (*BB)->prevLink= *AA_tail;
- *AA_tail= *BB;
- *BB= NULL;
- }
- }
- }
- }
- }
- void versusSort(numBook** BookToSort) {
- /* */
- applyQsortTo(BookToSort);
- /* */
- /* * /
- slow_Sort(BookToSort);
- / * */
- }
- /*------------------------------------------------------------------------------------------------------------------------------*/
- void configure_modes(void) {
- __config_generator[__mode_ZERO][__TOKEN_TEMPLATE] = 0;
- __config_generator[__mode_ZERO][__UNIT] = 0;
- __config_generator[__mode_EQUAL][__TOKEN_TEMPLATE] = 0;
- __config_generator[__mode_EQUAL][__UNIT] = 1;
- __config_generator[__mode_DETERMINANT][__TOKEN_TEMPLATE] = 1;
- __config_generator[__mode_DETERMINANT][__UNIT] = 0;
- }
- /*------------------------------------------------------------------------------------------------------------------------------*/
- void run_my_SJT_implementation(int just_for_the_Nth_array, long N) {
- long k;
- digitsString *J0_head, *J0_tail;
- for (k=(just_for_the_Nth_array ? N : 1);k<=N;k++) {
- apply_SteinhausJohnsonTrotterAlgorithm(&J0_head, &J0_tail, k);
- print_matrix(&J0_head);
- finalizer_matrix(&J0_head, &J0_tail);
- }
- printf("\n\n\tSequencer execution completed successfully.\n");
- }
- /*------------------------------------------------------------------------------------------------------------------------------*/
- void finalizer_matrix(digitsString **stckPointerReference_HEAD, digitsString **stckPointerReference_TAIL) {
- while (*stckPointerReference_HEAD != NULL) {
- (*stckPointerReference_TAIL) = (*stckPointerReference_HEAD);
- (*stckPointerReference_HEAD) = (*stckPointerReference_HEAD)->nextString;
- free( *stckPointerReference_TAIL );
- }
- (*stckPointerReference_TAIL) = NULL;
- }
- /*------------------------------------------------------------------------------------------------------------------------------*/
- void print_matrix(digitsString **stckPointerReference_HEAD) {
- digitsString* focus= *stckPointerReference_HEAD;
- if ( NULL == focus ) {
- printf(" Not initialized! ");
- } else {
- if ( -1 == (focus->integer_value) ) {
- focus = (*stckPointerReference_HEAD)->nextString;
- if ( NULL == focus ) {
- printf(" initialized but empty. ");
- } else {;}
- } else {
- }
- while ( NULL != focus ) {
- printf("\n%li", (focus->integer_value) );
- focus = focus->nextString;
- }
- }
- }
- /*------------------------------------------------------------------------------------------------------------------------------*/
- void apply_SteinhausJohnsonTrotterAlgorithm(digitsString **J_head, digitsString **J_tail, long _SIZE) {
- digitsString *JJ_head;
- digitsString *JJ_tail;
- digitsString *P_head;
- digitsString *P_tail;
- digitsString *Q_head;
- digitsString *Q_tail;
- long diagonal;
- long _CASE = 1;
- mode_matrix_waterfall();
- initializer_matrix(J_head, J_tail);
- insert_new_node(1, J_tail);
- while (_CASE < _SIZE) {
- _CASE++;
- diagonal = -1;
- initializer_matrix(&JJ_head, &JJ_tail);
- while ( !only_initialized(J_head) ) {
- P_head = extract_first_nodes(J_head, &P_tail, (_CASE-1)) ;
- generate_square_matrix(&P_head, &P_tail, &Q_head, &Q_tail, _CASE*diagonal);
- concat_matrices_node_by_node(&JJ_tail, &Q_head, &Q_tail);
- finalizer_matrix(&P_head, &P_tail);
- diagonal *= -1;
- }
- finalizer_matrix(J_head, J_tail);
- *J_head = JJ_head;
- *J_tail = JJ_tail;
- JJ_head = NULL;
- JJ_tail = NULL;
- }
- }
- /*------------------------------------------------------------------------------------------------------------------------------*/
- void mode_matrix_waterfall(void) {
- generator_settings = __mode_DETERMINANT;
- }
- /*------------------------------------------------------------------------------------------------------------------------------*/
- digitsString* generate_square_matrix(digitsString **baseRef_head, digitsString **baseRef_tail, digitsString **ref_answer_head, digitsString **ref_answer_tail, long TOKEN_TEMPLATE) {
- long counting_L = 1;
- long counting_X = 1;
- long counting_Y = 1;
- long matrix_size = absolute_value(TOKEN_TEMPLATE);
- long newValuetobeIncluded;
- digitsString* BASE_head = *baseRef_head;
- digitsString* BASE_tail = *baseRef_tail;
- close__the_ring(&BASE_head, &BASE_tail);
- initializer_matrix(ref_answer_head, ref_answer_tail);
- matrix_size = absolute_value(TOKEN_TEMPLATE);
- while (counting_L <= (matrix_size*matrix_size)) {
- if (BASE_head->integer_value == -1) { BASE_head = BASE_head->nextString; }
- newValuetobeIncluded = (TOKEN_TEMPLATE < 0) ? ( ( counting_X + counting_Y == (matrix_size+1) ) ? (matrix_size*__config_generator[generator_settings][__TOKEN_TEMPLATE] + __config_generator[generator_settings][__UNIT]) : (0) ) : ( (counting_X - counting_Y == 0) ? (matrix_size*__config_generator[generator_settings][__TOKEN_TEMPLATE] + __config_generator[generator_settings][__UNIT]) : (0) );
- if ( ( 0 == newValuetobeIncluded ) && ( __mode_ZERO != generator_settings) ) {
- newValuetobeIncluded+= (BASE_head->integer_value);
- BASE_head = BASE_head->nextString;
- }
- insert_new_node(newValuetobeIncluded, ref_answer_tail);
- counting_L++;
- counting_X++;
- if (counting_X > matrix_size) {
- counting_X = 1;
- counting_Y++;
- }
- }
- open_the_ring(&BASE_head, &BASE_tail);
- return (*ref_answer_head);
- }
- /*----------------------------------------------------------------------------------------------------------------------(OJO)---*/
- void initializer_matrix(digitsString **stckPointerReference_HEAD, digitsString **stckPointerReference_TAIL) {
- (*stckPointerReference_HEAD) = NULL;
- (*stckPointerReference_TAIL) = insert_new_node( -1, stckPointerReference_HEAD );
- }
- /*------------------------------------------------------------------------------------------------------------------------------*/
- digitsString* extract_first_nodes(digitsString** refSource, digitsString** refRemainderTail, long extractions) {
- digitsString* newRefSource= *refSource;
- digitsString* answer= (*refSource)->nextString;
- digitsString* cursor= (*refSource);
- long steps= (0 == extractions) ? 1 : extractions;
- long counter=0;
- while (counter < steps) {
- cursor= cursor->nextString;
- counter++;
- }
- newRefSource->nextString= cursor->nextString;
- cursor->nextString= NULL;
- (*refRemainderTail)= cursor;
- *refSource = newRefSource;
- return answer;
- }
- /*------------------------------------------------------------------------------------------------------------------------------*/
- void concat_matrices_node_by_node(digitsString **operand_left_tail, digitsString **operand_right_head, digitsString **operand_right_tail) {
- digitsString *slayer = *operand_right_head;
- *operand_right_head = NULL;
- (*operand_left_tail)->nextString = slayer->nextString;
- slayer->nextString = NULL;
- free(slayer);
- *operand_left_tail = *operand_right_tail;
- *operand_right_tail = NULL;
- }
- /*------------------------------------------------------------------------------------------------------------------------------*/
- long only_initialized(digitsString** stckPointerReference_HEAD) {
- if ( 0 != ( NULL != stckPointerReference_HEAD ) ) {
- if ( 0 != ( NULL != *stckPointerReference_HEAD ) ) {
- if ( 0 != ( NULL == (*stckPointerReference_HEAD)->nextString ) ) {
- if ( -1 == (*stckPointerReference_HEAD)->integer_value ) {
- return 1;
- }
- }
- }
- }
- return 0;
- }
- /*------------------------------------------------------------------------------------------------------------------------------*/
- long absolute_value(long argument) {
- return ( argument * LeibnitzLaplaceSignature(argument) );
- }
- /*------------------------------------------------------------------------------------------------------------------------------*/
- digitsString* insert_new_node(long value_assigned, digitsString **stckPointerReference_TAIL) {
- digitsString *focus;
- digitsString *inserto = (digitsString *)( malloc( sizeof( digitsString ) ) );
- digitsString *answer_ref = inserto;
- inserto->integer_value = value_assigned;
- inserto->nextString = NULL;
- if (( NULL == (*stckPointerReference_TAIL) ) && (-1 == value_assigned)) {
- (*stckPointerReference_TAIL) = inserto;
- } else if ( NULL != (*stckPointerReference_TAIL) ) {
- focus = (*stckPointerReference_TAIL);
- while ( NULL != focus->nextString ) { focus = focus->nextString; }
- focus->nextString = inserto;
- if ( focus == (*stckPointerReference_TAIL) ) { (*stckPointerReference_TAIL) = inserto; }
- }
- return answer_ref;
- }
- /*------------------------------------------------------------------------------------------------------------------------------*/
- void close__the_ring(digitsString** stckPointerReference_HEAD, digitsString** stckPointerReference_TAIL) {
- __theRing_HEAD = *stckPointerReference_HEAD;
- __theRing_TAIL = *stckPointerReference_TAIL;
- (*stckPointerReference_TAIL)->nextString = *stckPointerReference_HEAD;
- }
- /*------------------------------------------------------------------------------------------------------------------------------*/
- void open_the_ring(digitsString** stckPointerReference_HEAD, digitsString** stckPointerReference_TAIL) {
- (*stckPointerReference_HEAD) = __theRing_HEAD;
- (*stckPointerReference_TAIL) = __theRing_TAIL;
- (*stckPointerReference_TAIL)->nextString = NULL;
- }
- /*------------------------------------------------------------------------------------------------------------------------------*/
- long LeibnitzLaplaceSignature(long argument) {
- long answer;
- answer = (argument < 0) ? (-1) : (1) ;
- return answer;
- }
- /*
- *
- --------------------------------------------------------------------------------------------------------------------------------
- Sample unix shell command for compiling this sourcecode in Linux and profiling its memory usage with GNU memusage:
- clear; cd /storage; g++ -x c -O2 -Wall -pedantic -lm -o ./seqNpadfix ./seqNpad_tails_fix.c && (echo "Compiled successfuly. Press ENTER.";read;clear; memusage ./seqNpad)
- --------------------------------------------------------------------------------------------------------------------------------
- ====================================================> APPENDIX <================================================================
- Note: C comment delimiters were replaced with />* for opening, and *</ for closing. After placing the following code in a separate
- file, retrieve the proper comment delimiters before compiling.
- ---[ The following is a dump of the file: ClangHackingCanoExperiment001.c ]-------------------------------------------------------
- ==================================================================================================================================
- />*
- *
- * -rw-r--r-- 1 KirkJamesT Enterprise 4161 Oct 21 22:54 ClangHackingCanoExperiment001.c
- *
- * Experimental program about exploring the possibilities of storagement for memory addresses of
- * compound datatype objects. An affirmative answer obtained here for the author's hytpothesis on
- * the use of pointers would let to optimize the concatenation process implemented as part of
- * the author's C code for C.A.R.'s Quicksort.
- *
- *</
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct userDefinedDataType { char* textString; struct userDefinedDataType* link; } userDefinedDataType;
- char* EmptyField= "Empty!";
- int main(void);
- int value(long);
- char evalSymbol(int, int);
- char* printStringField(long);
- int runDemo(void);
- int main(void) {
- return runDemo();
- }
- />* To be extremely careful about the value passed here will prevent painful crashes with "Segmentation fault" *</
- int value(long addr) {
- int ans= 0;
- if ( ((userDefinedDataType**)(addr)) != NULL ) {
- if ( *((userDefinedDataType**)(addr)) != NULL ) {
- ans+= strlen((*((userDefinedDataType**)(addr)))->textString);
- } else {;}
- }
- return ans;
- }
- char evalSymbol(int arg1, int arg2) {
- char ans=' ';
- if (arg1 == arg2) { ans='='; }
- if (arg1 > arg2) { ans='>'; }
- if (arg1 < arg2) { ans='<'; }
- return ans;
- }
- char* printStringField(long atAddr) {
- char* ans= EmptyField;
- if ( ((userDefinedDataType**)(atAddr)) != NULL ) {
- if ( *((userDefinedDataType**)(atAddr)) != NULL ) {
- ans= (*((userDefinedDataType**)(atAddr)))->textString;
- } else {
- ;
- }
- } else {
- ;
- }
- return ans;
- }
- int runDemo(void) {
- long addr[]= {0, 0, 0};
- long operand[]= {0, 0, 0};
- int howMany= 3;
- int index_i= 0;
- int index_j= 0;
- int index_k= 0;
- int UsefulPieceOf_A207324[]= {1,2,3,1,3,2,3,1,2,3,2,1,2,3,1,2,1,3};
- userDefinedDataType* testField00= NULL;
- userDefinedDataType* testField01= NULL;
- userDefinedDataType* testField02= NULL;
- testField00= (userDefinedDataType*) malloc(sizeof(userDefinedDataType));
- testField00->textString= "\"Zero\"";
- testField00->link= NULL;
- testField02= (userDefinedDataType*) malloc(sizeof(userDefinedDataType));
- testField02->textString= "\"Two \"";
- testField02->link= NULL;
- addr[0]= (long) &testField00;
- addr[1]= (long) &testField01;
- addr[2]= (long) &testField02;
- for (index_j=0; index_j <18; index_j++) {
- UsefulPieceOf_A207324[index_j]--;
- }
- printf("\n");
- printf("\nGiven the following character strings:\n");
- for (index_i=0; (index_i < howMany); index_i++) {
- if ( ((userDefinedDataType**)(addr[index_i])) != NULL ) {
- if ( *((userDefinedDataType**)(addr[index_i])) != NULL ) {
- printf("\n%s", (*((userDefinedDataType**)(addr[index_i])))->textString);
- } else {
- printf("\n%s", EmptyField);
- }
- }
- }
- printf("\n\nThere are six different ways of listing them:\n");
- index_j=-1;
- for (index_i=0; index_i < 6; index_i++) {
- for (index_k=0; index_k < howMany; index_k++) {
- operand[index_k]= addr[UsefulPieceOf_A207324[++index_j]];
- }
- printf("\n %s %c %s %c %s", printStringField(operand[0]), evalSymbol(value(operand[0]),value(operand[1])), printStringField(operand[1]), evalSymbol(value(operand[1]),value(operand[2])),printStringField(operand[2]));
- }
- printf("\n\nThe property compared here is the lenght in chars of each string field \namong those belonging to declared copies of \"userDefinedDataType\".");
- />*
- The same set of comparisons is got via a jungle of if/else statements.
- Using the information from A207324, specifically the terms correspondig to permutations for three objects
- (the permutations for the first 3 naturals in the A207324 context), we will be able to compare string lengths
- as shown below.
- In any case, since this code was devised for demonstrate the use of A207324, it would just a first glance.
- *</
- printf("\n\n");
- if (testField00 != NULL) { free(testField00); }
- if (testField01 != NULL) { free(testField01); }
- if (testField02 != NULL) { free(testField02); }
- />*
- This code may be taken as evidence of the fact that under the proper assumptions and when enough
- security measures are implemented, it is safe to work directly with the memory addresses.
- The author hopes that such behavior of any pointer having the same sizeof() than long still being
- true in another architectures distinct than Intel 32 Bits, else this code might fail.
- *</
- return EXIT_SUCCESS;
- }
- =====(EOF)=====================================================================================================================
- *
- * END of seqNpad_tails_fix.c (or a217626.c.txt)
- *
- *
- *
- * << If people do not believe that mathematics is simple, it is only because
- * they do not realize how complicated life is. >>
- *
- * --John von Neumann
Add Comment
Please, Sign In to add comment