在没有外部函数的情况下重现atoi()
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在没有外部函数的情况下重现atoi()相关的知识,希望对你有一定的参考价值。
我希望重现atoi()
函数的行为,而不使用任何其他函数,如strtonum()
。我真的不明白如何将char
或char *
转换为int而不将其转换为ASCII值。我在C中这样做,有什么建议吗?
答案
哇我很慢。很多人回答说......顺便说一句,这是我用符号检测的简单例子。如果遇到无效的char,它将返回字符串的第一个解析部分:
#include <stdio.h>
int my_atoi(const char* input) {
int accumulator = 0, // will contain the absolute value of the parsed number
curr_val = 0, // the current digit parsed
sign = 1; // the sign of the returned number
size_t i = 0; // an index for the iteration over the char array
// Let's check if there is a '-' in front of the number,
// and change the final sign if it is '-'
if (input[0] == '-') {
sign = -1;
i++;
}
// A '+' is also valid, but it will not change the value of
// the sign. It is already +1!
if (input[0] == '+')
i++;
// I think it is fair enough to iterate until we reach
// the null char...
while (input[i] != ' ') {
// The char variable has already a "numeric"
// representation, and it is known that '0'-'9'
// are consecutive. Thus by subtracting the
// "offset" '0' we are reconstructing a 0-9
// number that is then casted to int.
curr_val = (int)(input[i] - '0');
// If atoi finds a char that cannot be converted to a numeric 0-9
// it returns the value parsed in the first portion of the string.
// (thanks to Iharob Al Asimi for pointing this out)
if (curr_val < 0 || curr_val > 9)
return accumulator;
// Let's store the last value parsed in the accumulator,
// after a shift of the accumulator itself.
accumulator = accumulator * 10 + curr_val;
i++;
}
return sign * accumulator;
}
int main () {
char test1[] = "234";
char test2[] = "+6543";
char test3[] = "-1234";
char test4[] = "9B123";
int a = my_atoi(test1);
int b = my_atoi(test2);
int c = my_atoi(test3);
int d = my_atoi(test4);
printf("%d, %d, %d, %d
", a, b, c, d);
return 0;
}
它打印:
234, 6543, -1234, 9
另一答案
考虑到你的系统使用atoi
的ASCII
的实现将是这样的(记住,数字也可以是负数)
int catoi(const char *string)
{
int res = 0;
int sign = 1;
if (*string == '-')
{
sign = -1;
string++;
}
if (*string == '+') string++;
while (*string >= '0' && *string <= '9')
{
res = res * 10 + (*string - '0')
string++;
}
return (sign < 0) ? (-res) : res;
}
另一答案
尝试使用参考页面中的代码:Write your own atoi()
// A simple C program for implementation of atoi
#include <stdio.h>
// A simple atoi() function
int PersonalA2I(char *str)
{
int res = 0; // Initialize result
// Iterate through all characters of input string and
// update result
for (int i = 0; str[i] != ' '; ++i)
res = res*10 + str[i] - '0';
// return result.
return res;
}
// Driver program to test above function
int main()
{
char str[] = "123456";
int val = PersonalA2I(str);
printf ("%d ", val);
return 0;
}
另一答案
您可以转换char
或char*
如下:
int myAtoi(char *str)
{
int dec = 0, i=0;
for (i = 0; str[i] != ' '; ++i)
dec = dec*10 + str[i] - '0';
return dec;
}
另一答案
直接来自Brian W. Kernighan, Dennis M. Ritchie: The C Programming Language:
/* atoi: convert s to integer */
int atoi(char s[])
{
int i, n;
n = 0;
for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
n = 10 * n + (s[i] - '0');
return n;
}
另一答案
另一个可以用于长ASCII字符串的小的:
#include <stdio.h>
long long int my_atoi(const char *c)
{
long long int value = 0;
int sign = 1;
if( *c == '+' || *c == '-' )
{
if( *c == '-' ) sign = -1;
c++;
}
while (*c >= '0' && *c <= '9') // to detect digit == isdigit(*c))
{
value *= 10;
value += (int) (*c-'0');
c++;
}
return (value * sign);
}
int main () {
char test1[] = "1234567890123456789";
char test2[] = "+7654312345678901234";
char test3[] = "-932112345678901234";
char test4[] = "9A123123456789012";
long long int a = my_atoi(test1);
long long int b = my_atoi(test2);
long long int c = my_atoi(test3);
long long int d = my_atoi(test4);
printf(" %lld
%lld
%lld
%lld
", a, b, c, d);
return 0;
}
输出:
1234567890123456789
7654312345678901234
-932112345678901234
9
以上是关于在没有外部函数的情况下重现atoi()的主要内容,如果未能解决你的问题,请参考以下文章