Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.27 KB | None | 0 0
  1. #include "stdio.h"
  2.  
  3. #define SUCCESS 1;
  4. #define WRONG_SPACE 101;
  5. #define WRONG_AUTOMAT 102;
  6.  
  7. void printBinary(int number, int bitsCount);
  8.  
  9. int main(int argc, char* argv) {
  10.  
  11.     const int dimension = 1; // assignment
  12.     const int colorsCount = 2; // assignment
  13.  
  14.     int space;
  15.     unsigned long long rule;
  16.     scanf_s("%d%llu", &space, &rule);
  17.    
  18.     if(space < 0 || space > 2) {
  19.         printf_s("Chybna velikost okoli!\n");
  20.         return WRONG_SPACE;
  21.     }
  22.  
  23.     int exponent = 2*space + 1;
  24.     int bitsCount = 1 << exponent;
  25.     unsigned long long automataCount = ((unsigned long long) 1) << bitsCount;
  26.  
  27.     if(rule < 0 || rule > automataCount) {
  28.         printf_s("Chybne cislo pravidla!\n");
  29.         return WRONG_AUTOMAT;
  30.     }
  31.  
  32.     // print the input
  33.     printf_s("Velikost okoli: %d\n", space);
  34.     printf_s("Pocet automatu: %llu\n", automataCount);
  35.     printf_s("Cislo automatu: %d\n\n", rule);
  36.  
  37.     // print the rules
  38.     for(int i = 0; i < bitsCount; i++) {
  39.         printBinary(i, exponent); // the i in binary - bitsCount places, "." is zero, "*" is one
  40.         char next = (rule & (1 << i)) > 0 ? '*' : '.';
  41.         printf_s("\n%*c\n\n", exponent/2 + 1, next);
  42.     }
  43.  
  44.     return SUCCESS;
  45. }
  46.  
  47. void printBinary(int number, int bitsCount) {
  48.     for(int i = 1 << bitsCount - 1; i > 0; i >>= 1) {      
  49.         printf_s("%c", (number & i) == i ? '*' : '.');
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement