简体   繁体   English

在C中创建一个atoi函数

[英]Creating an atoi function in C

I try to create atoi function, and think I made right code, but when I run it, it shows wrong one. 我尝试创建atoi函数,并认为我编写了正确的代码,但是当我运行它时,它显示了错误的代码。 I'm trying to figure it out, but don't know what I made it wrong please check the code and give some help 我正在尝试找出答案,但不知道我做错了什么,请检查代码并提供帮助

My code is 我的代码是

#include <stdio.h>

int my_atoi(char *str)
{
  int i;
  int res;
  int sign;

  i = 0;
  res = 0;
  sign = 1;//sign of '-' or '+'
  while(str[i] == ' ' || (str[i] >= 9 && str[i] <= 13))
  {
    i++;
  }
  if(str[i] == '-')
  {
    sign = -1;
    i++;
  }
  else if(str[i] == '+')
  {
    sign = 1;
    i++;
  }
  while(str[i] >= '0' && str[i] <= '9')
  {
    res = res * 10 + str[i] + '0';
    i++;
  }
  return(res * sign);// to make integer which has value of '-' or '+'
}

int main(void)
{
  char str[] = "-2018shiba";
  printf("%d\n", my_atoi(str));
  return(0);
}

When I run it, it shows -108674 当我运行它时,它显示为-108674

I am seeing multiple mistakes here. 我在这里看到多个错误。

  1. If you want to convert a ASCII character into the corresponding integer you need to subtract '0'. 如果要将ASCII字符转换为相应的整数,则需要减去 “ 0”。 Take a look at the ASCII table: for instance '7' is mapped by decimal value 55. Hence if you want to get 7 then you need to subtract the ASCII of '0' which is 48 (55 - 48 = 7): 看一下ASCII表:例如,'7'由十进制值55映射。因此,如果要获得7,则需要减去'0'的ASCII,即48(55-48 = 7):
int foo = str[i] - '0';
  1. In the very last while loop of my_atoi . my_atoi的最后一个while循环中。 The value of an indexed numeral string representation is calculated by multiplying the value of str[i] with the numerical base to the power of the index starting from behind . 索引数字字符串表示的值是通过将str [i]的值与数字基数乘以从后面开始的索引的幂来计算的。
    For example lets take a look at "1337": 例如,让我们看一下“ 1337”:

    7*10^0 + 3*10^1 + 3*10^2 + 1*10^3 = 7 + 30 + 300 + 1000 = 1337

    As you can see, the 7 has the numerical index 0 and so on. 如您所见,7的数字索引为0,依此类推。 Assuming you want to just ignore shiba your code be looking something like this: 假设你要忽略代码来寻找这样的事情

#include <stdio.h>
#include <string.h>

// Return base^(exponent)
int my_pow(int base, unsigned int exponent)
{
  if (exponent == 0) {
    return 1;
  } else {
    int result = base;
    for (int i = 1; i < exponent; i++) {
      result *= base;
    }
    return result;
  }
}

int my_atoi(char *str, size_t len)
{
  int i;
  int res;
  int sign;

  i = 0;
  res = 0;
  sign = 1;//sign of '-' or '+'
  while(str[i] == ' ' || (str[i] >= 9 && str[i] <= 13))
  {
    i++;
  }
  if(str[i] == '-')
  {
    sign = -1;
    i++;
  }
  else if(str[i] == '+')
  {
    sign = 1;
    i++;
  }

  // Store the index where the number string starts
  int j = i-1;
  // Find the ending index of the number string
  i = len;
  while (str[i] < '0' || str[i] > '9') {
      i--;
  }
  int num_end = i;

  // Now start at the ending
  while(i > j)
  {
    if (str[i] >= '0' && str[i] <= '9') {
      res += my_pow(10, num_end-i) * (str[i] - '0');
    } else {
      // If a character unequal to a digit is found then skip it
      num_end--;
    }

    i--;
  }
  return(res * sign);// to make integer which has value of '-' or '+'
}

int main(void)
{
  char str[] = "-2018shiba";
  printf("%d\n", my_atoi(str, strlen(str)));
  char str2[] = "-20X18shiba";
  printf("%d\n", my_atoi(str2, strlen(str2)));
  return(0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM