#include #include #include #include "project.h" /******************************************************** * Outputs the paper-folding sequence * * Using the folding instructions given. * * David Longbottom * ********************************************************/ int main (int argc, char *argv[]) { int *instr; int i, n=0, max; char c; int allocated = 15; int order, j, b; //read in folding instructions instr = (int *)malloc(allocated*sizeof(int)); while ((c = getchar()) != '\n' && c != EOF) { instr[n] = c; n++; if(n >= allocated) { allocated *=2; instr = (int *)realloc(instr,allocated*sizeof(int)); } } max = (int)pow(2,n) - 1; fprintf(stderr,"%i folds gives %i creases.\n",n,max); //now generate sequence //see automaton for (i = 1 ; i <= max ; i++) { order = 0; j = i; //start b = 2; while(b == 2) { if ( !(j % 2) ) { //is j even? j = j / 2; order++; } else { b = ((j-1) / 2) % 2; //b = 0 or 1 } } b = (instr[order] == A ? b : 1-b ); //invert b if its instruction was so putchar( b == 0 ? A : B); } putchar('\n'); return(EXIT_SUCCESS); }