Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- int myAtoi(char *s, int * error)
- {
- int result = 0;
- int sign = 1;
- int i = 0;
- int j = 0;
- int decimal = 0;
- int count = 0;
- double index = 0;
- *error = 11;
- for (j = 0; s[j] != '\0'; j++)
- {
- count = count + 1; /// I ASSUME THIS COUNTS HOW MANY CHARACTERS THERE ARE?
- }
- for (i = i; s[i] != '\0'; i++) // NEED TO TELL IT WHAT I = ELSE IT'LL BITCH ABOUT IT
- {
- while (s[i] == ' ')
- {
- i++;
- }
- if (s[0] == '0' && s[1] == 'x')
- {
- index = count - i;
- if(s[i] >= '0' && s[i] <= '9')
- {
- decimal = decimal + (s[i] - 48) * pow(16.0, index);
- }
- else if(s[i] >= 'a' && s[i] <= 'f')
- {
- decimal = decimal + (s[i] - 87) * pow(16.0, index);
- }
- else if((s[i] > '9' && s[i] < 'a') || (s[i] >= 'g'))
- {
- index = count - i + 1;
- decimal = (decimal/pow(16.0, index));
- return decimal;
- }
- else
- {
- i++;
- }
- result = decimal;
- }
- else
- {
- while (s[i] == ' ' || s[i] == '0')
- {
- i++;
- }
- if (s[i] == '-')
- {
- sign = -1;
- i++;
- }
- for (i = i; s[i] != '\0'; i++) // NEED TO TELL IT WHAT I = ELSE IT'LL BITCH ABOUT IT
- {
- if(s[i] > ':' || s[i] < '/')
- {
- return result;
- }
- result = result * 10 + s[i] - '0'; ///Minus '0' ascii 48 from answer to get back to decimal value
- result = sign * result;
- }
- }
- }
- return result;
- }
- int simpleTest(char *s, int * error)
- {
- *error = 10;
- return(1);
- }
- void displayString(char *s)
- {
- int i = 0;
- while (s[i] != '\0')
- {
- printf("%c", s[i]);
- i += 1;
- }
- printf("\n");
- }
- int testAtoi(char* s, int* value)
- {
- int index = 0;
- /// Find the index of the first non-white space
- while (s[index] == ' ' || s[index] == '\t')
- {
- index += 1;
- }
- if (!(s[index] >= '0' && s[index] <= '9')) ///If it's NOT a number.
- {
- printf("Please use only hex preceeded by 0x or decimal integers\n");
- return(1); /// Return 1 on failure. In this case, the first non-white space character wasn't a number.
- }
- else if (s[index] == '0' && s[index + 1] == 'x') ///Is it hex?
- {
- index = index + 2; /// Move to the start of the hex-Value
- while (s[index] != '\0')
- {
- if (!(s[index] >= '0' && s[index] <= '9') && !((s[index] >= 'a' && s[index] <= 'f'))) ///If it's NOT a number or hex value.
- {
- return(1); ///Fail. Could do break in here to ignore anything after the number.
- }
- else
- {
- if (s[index] >= '0' && s[index] <= '9') /// If it's a number:
- {
- *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.
- }
- else
- {
- *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.
- }
- }
- index += 1; /// Increment to next char
- }
- return(0); ///Success.
- }
- else ///Assume it's decimal.
- {
- while (s[index] != '\0') ///Until the end of the string.
- {
- if (!(s[index] >= '0' && s[index] <= '9'))///If it's NOT a number.
- {
- return(1); ///Fail. Could do break in here to ignore anything after the number.
- }
- else
- {
- *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.
- }
- index += 1; /// Increment to next char
- }
- return(0); ///Success.
- }
- }
- int main() // MAIN SHOULD COME AFTER THE FUNCTION UNLES YOU PROTOTYPE THE FUNCTION FIRST.
- {
- char str[] = "0xff";
- int value = 0;
- if (testAtoi(str, &value) == 0)
- {
- printf("VALUE : %d\n", value);
- }
- else
- {
- printf("Please use only hex preceeded by 0x or decimal integers\n");
- }
- int iAmAnInt = 10;
- printf("I AM THE VALUE OF iAmAnInt: %d\n", iAmAnInt);
- int* iAmAPointerToAnInt; /// A pointer to an int called iAmAPointerToAnInt.
- printf("I AM THE POINTER iAmAPointerToAnInt: %p\n", iAmAPointerToAnInt);
- printf("I AM THE VALUE STORED IN THE ADDRESS iAmAPointerToAnInt: %d\n", (*iAmAPointerToAnInt));
- printf("I AM THE ADDRESS/POINTER TO iAmAnInt: %p\n", &iAmAPointerToAnInt);
- printf("I HAVE DONE iAmAPointerToAnInt = &iAmAnInt\n");
- iAmAPointerToAnInt = &iAmAnInt; ///I assign the address of iAmAnInt (a pointer to an int) to the pointer to an int variable name.
- printf("I AM THE THE NEW POINTER iAmAPointerToAnInt: %p\n", iAmAPointerToAnInt);
- printf("I AM THE THE NEW VALUE STORED IN THE ADDRESS iAmAPointerToAnInt: %d\n", (*iAmAPointerToAnInt));
- printf("I AM THE THE NEW ADDRESS/POINTER TO iAmAnInt: %p\n", &iAmAPointerToAnInt);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement