Last active
July 11, 2018 08:01
-
-
Save kupp1/c8c2b5ce91dcfb412cde6d59339ca5b6 to your computer and use it in GitHub Desktop.
c lang atoi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "atoi.h" | |
int main (int argc, char* argv[]) { | |
printf("%d\n", atoi(argv[1])); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int atoi (char str[]) { //with negative digits processing | |
int number = 0; | |
if ( ( (int)(*str) == 45 ) ) { | |
str++; | |
while(*(str) != '\0') { | |
if ( ( (int)(*(str)) >= 48 ) && ( (int)(*(str)) <= 57 ) ) { | |
number = number*10 + (*str) - '0'; | |
} else { | |
return 0; | |
} | |
str++; | |
} | |
return number*(-1); | |
} else { | |
while(*(str) != '\0') { | |
if ( ( (int)(*(str)) >= 48 ) && ( (int)(*(str)) <= 57 ) ) { | |
number = number*10 + (*str) - '0'; | |
} else { | |
return 0; | |
} | |
str++; | |
} | |
return number; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@GIGJK Можешь радоваться. Теперь еще более криво