1、strtoi
#include <stdio.h> int strtoi(const char *s) { int i, j = 0; while(*s) { for(i = 0; i <= 9; i++) { if(*s - ‘0‘ == i) j = j * 10 + i; } s++; } return j; } int main(void) { char str[128]; printf("str: "); scanf("%s", str); printf("result afrer concersion: %d\n", strtoi(str)); return 0; }
2、
#include <stdio.h> long strtol(const char *s) //?????????????????? { int i; long j = 0; while(*s) { for(i = 0; i < 9; i++) { if(*s - ‘0‘ == i) j = j * 10 + (long)i; } s++; } return j; } int main(void) { char str[128]; printf("str: "); scanf("%s", str); printf("result after conversion: %d\n", strtol(str)); return 0; }
3、
#include <stdio.h> double strtof(const char *s) { int i; double j = 0.0; while(*s) { for(i = 0; i <= 9; i++) { if(*s - ‘0‘ == i) j = j * 10 + (double)i; } s++; } return j; } int main(void) { char str[128]; printf("str: "); scanf("%s", str); printf("result after conversion: %f\n", strtof(str)); return 0; }
原文:https://www.cnblogs.com/liujiaxin2018/p/14840508.html