Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.58 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. int myAtoi(char *s, int * error)
  6. {
  7.     int result = 0;
  8.     int sign = 1;
  9.     int i = 0;
  10.     int j = 0;
  11.     int decimal = 0;
  12.     int count = 0;
  13.  
  14.     double index = 0;
  15.  
  16.     *error = 11;
  17.  
  18.     for (j = 0; s[j] != '\0'; j++)
  19.     {
  20.         count = count + 1; /// I ASSUME THIS COUNTS HOW MANY CHARACTERS THERE ARE?
  21.     }
  22.  
  23.     for (i = i; s[i] != '\0'; i++) // NEED TO TELL IT WHAT I = ELSE IT'LL BITCH ABOUT IT
  24.     {
  25.         while (s[i] == ' ')
  26.         {
  27.             i++;
  28.         }
  29.  
  30.         if (s[0] == '0' && s[1] == 'x')
  31.         {
  32.             index = count - i;
  33.             if(s[i] >= '0' && s[i] <= '9')
  34.             {
  35.                 decimal = decimal + (s[i] - 48) * pow(16.0, index);
  36.             }
  37.             else if(s[i] >= 'a' && s[i] <= 'f')
  38.             {
  39.                 decimal = decimal + (s[i] - 87) * pow(16.0, index);
  40.             }
  41.             else if((s[i] > '9' && s[i] < 'a') || (s[i] >= 'g'))
  42.             {
  43.                 index = count - i + 1;
  44.                 decimal = (decimal/pow(16.0, index));
  45.                 return decimal;
  46.             }
  47.             else
  48.             {
  49.                 i++;
  50.             }
  51.  
  52.             result = decimal;
  53.  
  54.         }
  55.         else
  56.         {
  57.             while (s[i] == ' ' || s[i] == '0')
  58.             {
  59.                 i++;
  60.             }
  61.  
  62.             if (s[i] == '-')
  63.             {
  64.                 sign = -1;
  65.                 i++;
  66.             }
  67.  
  68.             for (i = i; s[i] != '\0'; i++) // NEED TO TELL IT WHAT I = ELSE IT'LL BITCH ABOUT IT
  69.             {
  70.                 if(s[i] > ':' || s[i] < '/')
  71.                 {
  72.                     return result;
  73.                 }
  74.  
  75.                 result = result * 10 + s[i] - '0';  ///Minus '0' ascii 48 from answer to get back to decimal value
  76.                 result = sign * result;
  77.             }
  78.         }
  79.     }
  80.  
  81.     return result;
  82. }
  83.  
  84. int simpleTest(char *s, int * error)
  85. {
  86.     *error = 10;
  87.     return(1);
  88. }
  89.  
  90. void displayString(char *s)
  91. {
  92.     int i = 0;
  93.     while (s[i] != '\0')
  94.     {
  95.         printf("%c", s[i]);
  96.         i += 1;
  97.     }
  98.     printf("\n");
  99. }
  100.  
  101. int testAtoi(char* s, int* value)
  102. {
  103.     int index = 0;
  104.  
  105.     /// Find the index of the first non-white space
  106.     while (s[index] == ' ' || s[index] == '\t')
  107.     {
  108.         index += 1;
  109.     }
  110.  
  111.     if (!(s[index] >= '0' && s[index] <= '9')) ///If it's NOT a number.
  112.     {
  113.         printf("Please use only hex preceeded by 0x or decimal integers\n");
  114.         return(1); /// Return 1 on failure. In this case, the first non-white space character wasn't a number.
  115.     }
  116.     else if (s[index] == '0' && s[index + 1] == 'x') ///Is it hex?
  117.     {
  118.         index = index + 2; /// Move to the start of the hex-Value
  119.         while (s[index] != '\0')
  120.         {
  121.             if (!(s[index] >= '0' && s[index] <= '9') && !((s[index] >= 'a' &&  s[index] <= 'f'))) ///If it's NOT a number or hex value.
  122.             {
  123.                 return(1); ///Fail. Could do break in here to ignore anything after the number.
  124.             }
  125.             else
  126.             {
  127.                 if (s[index] >= '0' && s[index] <= '9') /// If it's a number:
  128.                 {
  129.                     *value = (*value) * 16 + (s[index] - 48); ///Multiplying by the base moves everything one place to the left. You can do this how you want tbf.
  130.                 }
  131.                 else
  132.                 {
  133.                     *value = (*value) * 16 + (s[index] - 87); ///Multiplying by the base moves everything one place to the left. You can do this how you want tbf.
  134.                 }
  135.             }
  136.             index += 1; /// Increment to next char
  137.         }
  138.         return(0); ///Success.
  139.     }
  140.     else ///Assume it's decimal.
  141.     {
  142.         while (s[index] != '\0') ///Until the end of the string.
  143.         {
  144.             if (!(s[index] >= '0' && s[index] <= '9'))///If it's NOT a number.
  145.             {
  146.                 return(1); ///Fail. Could do break in here to ignore anything after the number.
  147.             }
  148.             else
  149.             {
  150.                 *value = (*value) * 10 + (s[index] - 48); ///Multiplying by the base moves everything one place to the left. You can do this how you want tbf.
  151.             }
  152.  
  153.             index += 1; /// Increment to next char
  154.  
  155.         }
  156.         return(0); ///Success.
  157.     }
  158. }
  159.  
  160. int main() // MAIN SHOULD COME AFTER THE FUNCTION UNLES YOU PROTOTYPE THE FUNCTION FIRST.
  161. {
  162.     char str[] = "0xff";
  163.     int value = 0;
  164.  
  165.     if (testAtoi(str, &value) == 0)
  166.     {
  167.         printf("VALUE : %d\n", value);
  168.     }
  169.     else
  170.     {
  171.         printf("Please use only hex preceeded by 0x or decimal integers\n");
  172.     }
  173.  
  174.     int iAmAnInt = 10;
  175.  
  176.     printf("I AM THE VALUE OF iAmAnInt: %d\n", iAmAnInt);
  177.  
  178.     int* iAmAPointerToAnInt; /// A pointer to an int called iAmAPointerToAnInt.
  179.  
  180.     printf("I AM THE POINTER iAmAPointerToAnInt: %p\n", iAmAPointerToAnInt);
  181.  
  182.     printf("I AM THE VALUE STORED IN THE ADDRESS iAmAPointerToAnInt: %d\n", (*iAmAPointerToAnInt));
  183.  
  184.     printf("I AM THE ADDRESS/POINTER TO iAmAnInt: %p\n", &iAmAPointerToAnInt);
  185.  
  186.     printf("I HAVE DONE iAmAPointerToAnInt = &iAmAnInt\n");
  187.  
  188.     iAmAPointerToAnInt = &iAmAnInt; ///I assign the address of iAmAnInt (a pointer to an int) to the pointer to an int variable name.
  189.  
  190.     printf("I AM THE THE NEW POINTER iAmAPointerToAnInt: %p\n", iAmAPointerToAnInt);
  191.  
  192.     printf("I AM THE THE NEW VALUE STORED IN THE ADDRESS iAmAPointerToAnInt: %d\n", (*iAmAPointerToAnInt));
  193.  
  194.     printf("I AM THE THE NEW ADDRESS/POINTER TO iAmAnInt: %p\n", &iAmAPointerToAnInt);
  195.  
  196.     return 0;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement