/ Published in: C
Don't forget the dec2bin.h library
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#include <stdio.h> #include <unistd.h> //for the sleep function #include "dec2bin.h" //function to get the binary equivalent of the rule #include <string.h> //for the string comparison int decimal; char binary[9]; char neighbors[4]; char bin1[4] = "111"; char bin2[4] = "110"; char bin3[4] = "101"; char bin4[4] = "100"; char bin5[4] = "011"; char bin6[4] = "010"; char bin7[4] = "001"; char bin8[4] = "000"; char singleLine[31], lineTemp[31]; int i, j, iMin, iPlus, stringTest; void applyRules(); /****************************************************************/ //Function to initiate the line to all 0, except the center one /****************************************************************/ void initLine(){ for (i = 0; i < 31; i++) { singleLine[i] = '0'; lineTemp[i] = '0'; } singleLine[15] = '1'; lineTemp[15] = '1'; } /****************************************************************/ //Function to print the line /****************************************************************/ void printLine(){ for (i = 0; i < 31; i++) { if (singleLine[i] == '0') { } else if (singleLine[i] == '1'){ } } applyRules(); } /****************************************************************/ //Function to apply the rules /****************************************************************/ void applyRules(){ for (i = 0; i < 31; i++) { if (i == 0){ iMin = 30; iPlus = i+1; } if (i == 30){ iPlus = 0; iMin = i-1; } else if (i != 0 && i != 30) { iMin = i-1; iPlus = i+1; } neighbors[0] = singleLine[iMin]; neighbors[1] = singleLine[i]; neighbors[2] = singleLine[iPlus]; lineTemp[i] = binary[0]; } lineTemp[i] = binary[1]; } lineTemp[i] = binary[2]; } lineTemp[i] = binary[3]; } lineTemp[i] = binary[4]; } lineTemp[i] = binary[5]; } lineTemp[i] = binary[6]; } lineTemp[i] = binary[7]; } } for (j = 0; j < 31; j++) { singleLine[j] = lineTemp[j]; } usleep(50000); printLine(); } /****************************************************************/ /****************************************************************/ int main (int argc, const char * argv[]) { initLine(); dec2bin(decimal, binary); printLine(); return 0; }
URL: www.autofasurer.net