整理思路:
#include <stdio.h> int len(char *s) { int len = 0; while (s[len]) { len++; } return len; } int myPower10(int len) { if (len == 0) return 1; if (len == 1) return 10; int power = 10; for (int i = 1; i < len; i++) power *= 10; return power; } int myCToInt(char c) { c = c - ‘0‘; return c; } int myAtoi(char *s) { int i, volue = 0,lens = len(s); for (i = 0; i < lens; i++) { volue += myCToInt(s[i]) * myPower10(lens - i - 1); } return volue; } int main() { char a[] = "3321"; int volue = myAtoi(a); printf("%d", volue); return 0; }
运行:
3321
时间: 2024-10-05 10:28:39