Advertisement
AhnafAbdullah27

Brihodakar Large Number System

Aug 16th, 2020
2,860
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.52 KB | None | 0 0
  1. /**
  2.     Brihodakar(Gigantic) Large Number System: Implementation for 100-digit numbers. (designed to be easily extendable)
  3.     Made by Ahnaf Abdullah, 2020
  4.  
  5. **/
  6.  
  7. #include <stdio.h>
  8. #include <conio.h>
  9.  
  10. //char brihodakar1[105] = "", brihodakar2[105] = "", brihodakar3[105] = ""; // char here only serves for smaller footprint, made global to streamline through use of functions
  11. //int i; // Global loop control var
  12. // WHY ARE WE DEPRECATING SO MANY TINGS!?
  13.  
  14. const int max_num_size = 100;  // DEFINES THE WORKING AREA OF THE BRIHODAKAR ARRAY: change this for dynamically large arrays, make sure to change declarations on BrihodakarX[] accordingly, always make Brihodakar array size slightly larger than this value
  15.  
  16. void display_num(char brihodakar[]);                                            //  BLNS: Displays the Brihodakar array in human-readable format
  17. void input_to_array(char brihodakar[], int numsize);                            //  BLNS: Regulated input segment (numsize allows for custom input size)
  18. void array_management(char brihodakar[], int option);                           //  BLNS: Multi-purpose array management function
  19.                                                                                 /*          Option 1: Input/Unorganized data -> Standard Brihodakar array
  20.                                                                                             Option 2: Standard Brihodakar array -> Big-endian converted Brihodakar array for operations
  21.                                                                                 */
  22. void add_array(char conv_array1[], char conv_array2[], char answer[]);          //  BLNS: Adds two big-endian converted Brihodakar arrays, brihodakar1 and brihodakar2
  23. void subtract_array(char conv_array1[], char conv_array2[], char answer[]);     //  BLNS: Subtracts two big-endian converted Brihodakar arrays, conv_array1 MINUS conv_array2. ORDER MATTERS
  24. void copy_array(char destination[], const char source[]);                       //  BLNS: Copies the source array into the destination array
  25.  
  26.  
  27. int main(void)
  28. {
  29.     char brihodakar1[max_num_size+5] = "";          // The stars of the show: these arrays hold ordered data representing numbers,
  30.     char brihodakar2[max_num_size+5] = "";          // according to the Brihodakar Large Number System specifications, which is
  31.     char brihodakarans[max_num_size+5] = "";        // actually quite simple.
  32.  
  33.                                                     /* General form for declaring an abstract Brihodakar array
  34.                                                             char <name>[max_num_size+5] = "";    */
  35.  
  36.     printf("Testing the implementation of the Brihodakar Large Number System(BLNS). ");
  37.  
  38.     // BLNS: regulated input segment
  39.     printf("\nPlease enter the first number: \n");
  40.     input_to_array(brihodakar1, max_num_size);
  41.     printf("\nPlease enter the second number: \n");
  42.     input_to_array(brihodakar2, max_num_size);
  43.  
  44.     printf("\n\n\nDEBUG: Displaying array\n");
  45.     display_num(brihodakar1); // DEBUG
  46.     printf("\n");
  47.     display_num(brihodakar2); // DEBUG
  48.  
  49.     array_management(brihodakar1, 1);
  50.     array_management(brihodakar2, 1);
  51.         // brihodakarans is a non-input array and hence it has no terminating character. This must be added
  52.         brihodakarans[0] = 'A';
  53.         array_management(brihodakarans, 1);
  54.  
  55.     printf("\n\n\nDEBUG: Displaying array after basic management\n");
  56.     display_num(brihodakar1);
  57.     printf("\n");
  58.     display_num(brihodakar2);
  59.  
  60.     // Conversion to Big-Endian for addition
  61.     array_management(brihodakar1, 2);
  62.     array_management(brihodakar2, 2);
  63.     array_management(brihodakarans, 2);
  64.  
  65.     printf("\n\n\nDEBUG: Displaying array after big-endian conversion\n");
  66.     display_num(brihodakar1);
  67.     printf("\n");
  68.     display_num(brihodakar2);
  69.     printf("\n");
  70.     display_num(brihodakarans);
  71.  
  72.  
  73.     // Addition query (DEBUG)
  74.     printf("\n\n\nAdding the two arrays\n");
  75.     add_array(brihodakar1, brihodakar2, brihodakarans);
  76.     printf("DEBUG: Displaying array after addition\n");
  77.     display_num(brihodakarans);
  78.     array_management(brihodakarans, 1);
  79.     printf("\n\n\nAdding the two arrays, result: \n");
  80.     display_num(brihodakarans);
  81.  
  82.     // Copying test, copy Brihodakarans to Brihodakar 1 and 2
  83.     copy_array(brihodakar1, brihodakarans);
  84.     copy_array(brihodakar2, brihodakarans);
  85.  
  86.     printf("\n\n\nDEBUG: Copying answer to first and second array, displaying array\n");
  87.     display_num(brihodakar1);
  88.     printf("\n");
  89.     display_num(brihodakar2);
  90.     printf("\n");
  91.     display_num(brihodakarans);
  92.  
  93.     return 0;
  94. }
  95.  
  96. void display_num(char brihodakar[])
  97. {
  98.     int m;
  99.     for (m=0; brihodakar[m] != 'A'; m++)
  100.     {
  101.         printf("%d", brihodakar[m]);
  102.     }
  103. }
  104.  
  105. void input_to_array(char brihodakar[], int numsize)
  106. {
  107.     char in;
  108.     int i;
  109.  
  110.     input:
  111.         for (i=0; i<numsize; i++)
  112.         {
  113.             in = getche();
  114.  
  115.             if (in >= '0' && in <= '9')
  116.             {
  117.                 brihodakar[i] = in - '0';
  118.                 if (i == numsize - 1)
  119.                     brihodakar[i+1] = 'A';
  120.             }
  121.             else
  122.             {
  123.                 if (in == '\r')
  124.                 {
  125.                     brihodakar[i] = 'A'; // Terminating character, use this to test
  126.                     break;
  127.                 }
  128.                 if (in == '.')
  129.                 {
  130.                     printf("\nSorry, Brihodakar is for integers only... Please try again\nPlease enter the number: ");
  131.                     goto input;
  132.                 }
  133.  
  134.                 printf("   Illegitimate input, please enter numbers only, try again\nPlease enter the number: ");
  135.                 // gotophobes... Explain how you can restart the whole input sequence without goto...
  136.                 // Oh yeah, and it has to be intuitive and simple. The goto here isn't blowing up the program, so pls stop whining
  137.                 goto input;
  138.             }
  139.         }
  140. }
  141.  
  142. void array_management(char brihodakar[], int option)
  143. {
  144.     int i;
  145.  
  146.     switch (option)
  147.     {
  148.     case 1: //  Input --> Standard Brihodakar array
  149.         {
  150.             if (!(brihodakar[0] == 0 && brihodakar[1] == 'A'))            // remove all leading zeroes before number
  151.             {
  152.                 while (brihodakar[0] == 0)     // Move every element one space forward IF there is a zero in front of it
  153.                 {
  154.                     int k;
  155.                     for (k = 0; k<max_num_size; k++)
  156.                         brihodakar[k] = brihodakar[k+1];
  157.                 }          // This operation can cause a large string of zeroes in brihodakar[] to turn into a null array. So, just in case
  158.  
  159.                 if (brihodakar[0] == 'A') // Turns a null array into an array of 0.
  160.                 {
  161.                     brihodakar[0] = 0; brihodakar[1] = 'A';
  162.                 }
  163.             }
  164.         }
  165.         break;
  166.     case 2: // Standard Brihodakar array --> Big-Endian Brihodakar array for operations
  167.         {
  168.             // WARMING: DO NOT EXECUTE THIS OPERATION ON AN UNORDERED ARRAY (perform array_management(array, 1); first)
  169.             while (brihodakar[max_num_size] != 'A')
  170.             {
  171.                 int k;
  172.                 for (k = max_num_size; k >= 0; k--)
  173.                 {
  174.                     brihodakar[k] = brihodakar[k-1];
  175.                 }
  176.             }
  177.         }
  178.         break;
  179.     }
  180.  
  181. }
  182.  
  183. void add_array(char conv_array1[], char conv_array2[], char answer[])
  184. {
  185.     // Test big-endianess of arrays
  186.     int i;
  187.  
  188.     if (conv_array1[max_num_size] != 'A' || conv_array2[max_num_size] != 'A'|| answer[max_num_size] != 'A')
  189.     {
  190.         printf("--DEBUG: Addition failed: Improper array configuration. Please initialize/set arrays properly and try again\n");
  191.         return;
  192.     }
  193.     else
  194.     {
  195.         char tempstore[max_num_size-5] = "";
  196.         copy_array(tempstore, conv_array1); // this allows us to add carries to conv_array1, since the original value of conv_array1 is now stored here.
  197.  
  198.         for (i=max_num_size-1; i>=0; i--)
  199.         {
  200.             int digitholder;
  201.  
  202.             digitholder = conv_array1[i] + conv_array2[i];
  203.  
  204.             if (digitholder > 9)
  205.             {
  206.                 digitholder = digitholder - 10;
  207.                 answer[i] = digitholder;   // Brihodakar3 is the answer array that holds the computation, it must be initialized to Big-Endian 0 array first.
  208.                 if (i > 0)
  209.                     conv_array1[i-1] = conv_array1[i-1] + 1;
  210.                 else
  211.                     printf("\nWARNING!!!! OVERFLOW!!! THE ANSWER MAY BE WRONG\n");
  212.             }
  213.             else
  214.             {
  215.                 answer[i] = digitholder;
  216.             }
  217.         }
  218.  
  219.         copy_array(conv_array1, tempstore);
  220.     }
  221. }
  222.  
  223. void copy_array(char destination[], const char source[])
  224. {
  225.     int i=0;
  226.     while (1)
  227.     {
  228.         destination[i] = source[i];
  229.         if (destination[i] == 'A')
  230.             break;
  231.         i++;
  232.     }
  233. }
  234.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement