写我自己的atoi功能

时间:2015-10-30 06:01:50

标签: c

我正在尝试将一串数字字符转换为相应的整数形式。请说明代码有什么问题。我想坚持使用指针。我知道指针str指向我的字符串中的第一个字符。所以,每次我在循环中调用我的函数时,我希望指针增加1,并将字符的值添加到我的数组中的一个节点。出于某种原因,虽然我无法这样做。这是代码。

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

int ctoi(char *c);

int main (void)
{
    char *str;
    int A[20];
    int i = 0;
    str = (char*) malloc(20 * sizeof(char));
    printf("Input the string. ");
    scanf("%s", str);
    while(str != '\0')   
    {
        A[i] = ctoi(str);
        i++;
        str++;
    }
    for(i = 0; i < strlen(str); i++)
        printf("%d", A[i]);

    getchar();
    getchar();
    return 0;
}

int ctoi(char *c)
{
    int a;
    a= *c - '0';
    return a;
}

7 个答案:

答案 0 :(得分:5)

for (i=0;i<strlen(str);i++) printf("%d", A[i]);

此处strlen将返回0,因为您在上一个循环中更新了str。将其替换为:

for(i=0;i<len;i++) 

其中len是输入字符串的长度。在使用str in while循环

之前找到它
while(str!='\0') should be `while(*str!='\0')`

。你会得到的 。但是对于编写自己的atoi函数,您不需要将数字存储在数组中

答案 1 :(得分:1)

请试试这个有效,myatoi()功能大概在20年前从经典的“THE C PROGRAMMING LANGUAGE”中解放出来,得到了这本书。

#include <stdio.h>

main()
{
    char temp[99];

    strcpy(temp , "34");

    printf( "\n %d " , myatoi(temp));
    strcpy( temp , "8642");
    printf( "\n %d " , myatoi(temp));
}


int myatoi( char s[])
{
    int i,n,sign;
    // skip white space
    for( i=0 ; s[i]==' ' || s[i]=='\n' ||s[i]=='\t';i++) ;

    sign=1;
    if( s[i]=='+' || s[i]=='-')
        sign=( s[i++]=='+' ? 1 : -1 );

    for( n=0; s[i]>='0' && s[i]<='9' ; i++)
        n=10*n+s[i]-'0' ;

    return(sign*n);
}

答案 2 :(得分:1)

OP的代码需要一些(至少2个)修复才能完成。见***

int main (void)
{
    char *str;
    int A[20];
    int i = 0;

    // ***  Cast not needed, '* sizeof(char)' not needed
    str = malloc(20);

    printf("Input the string. ");
    scanf("%s", str);

    // ***
    char *str_original = str;
    while(*str != '\0')


    {
        A[i] = ctoi(str);
        i++;
        str++;
    }

    // ***
    str = str_original;

    for(i = 0; i < strlen(str); i++)
        printf("%d", A[i]);

    // ***
    free(str);  // Good to return memory
    str = NULL;

    getchar();
    getchar();
    return 0;
}

将字符串转换为int

的简单方法
int strtoi(const char *s) {
  int sum = 0;
  char ch;
  char sign = *s;
  if (*s == '-' || *s == '+') s++;
  while ((ch = *s++) >= '0' && ch <= '9') {
    sum = sum * 10 - (ch - '0');
  }
  if (sign != '-') {
    sum = -sum;
  }
  return sum;
}

注意:此代码在0的负侧积累总和,以避免在尝试解析INT_MIN的字符串时出现UB。修改后的代码可以跳过前导空格,添加文本错误检测,溢出检测等。

答案 3 :(得分:1)

这是我的自定义atoi功能,使用debug gestion处理unsigned int:

int     my_getnbr(char *str)
{
  int   nb;
  int   sign;
  int   i;

  nb = 0;
  sign = 0;
  i = -1;
  if (!str)
    return (0);
  while (str[++i])
    if (str[i] < '0' && str[i] > '9' && str[i] != '-' && str[i] != '+')
      return (0);
  i = 0;
  while (str[i] != '\0' && (str[i] == '-' || str[i] == '+'))
    if (str[i++] == '-')
      ++sign;
  while (str[i] && (str[i] >= '0' && str[i] <= '9'))
    {
      nb = (nb * 10) + (str[i++] - '0');
      if (str[i] == ' ')
        i++;
    }
  return (((sign % 2) == 1) ? ((nb) * (-1)) : (nb));
}

用那个主要测试:

int main()
{
  printf("%d\n", my_getnbr("-42"));
  printf("%d\n", my_getnbr("-+-+--42"));
  printf("%d\n", my_getnbr("-0"));
  printf("%d\n", my_getnbr("590310"));
  return (0);
}

没有泄漏,结果如下:

-42
42
0
590310

答案 4 :(得分:0)

首先

while(str!='\0')应该是

while(*str!='\0')

您应该比较内容,而不是地址。

在打印返回的数据时,您正在进行

for(i=0;i<strlen(str);i++)
   printf("%d", A[i]);

str已经解析到最后一次。所以长度可能是0。

while循环更改为

 while(*str!='\0')   
      {   
        A[i]=ctoi(*str);
        i++;
        str++;
      } 

你的功能

int ctoi(char c)
 {
 int a;
 a= c-'0'; 
 return a;
 }

答案 5 :(得分:0)

atoibase内没有strtol转换灵活性的简单/* a quick atoi replacement */ int atoi2 (char *s) { int nmax = (1ULL << 31) - 1; /* INT_MAX */ long long n = 0; /* the number to return */ size_t m = 1; /* multiplier for place */ size_t l = 0; /* length of string */ char *p = s; while (*p++) l++; /* get string length */ p -= 2; /* position at last char */ while (l--) /* for each char in string */ { /* verify a digit or '-' sign */ if ((*p >= '0' && *p <= '9') || *p == '-') { if (*p == '-') { /* if '-' is first char */ if (p == s) n = -n; /* negate value */ } else { /* otherwise normal conversion */ n += (*p - '0') * m; if (n > nmax) { /* prevent overflow */ fprintf (stderr, "atoi2() error: conversion > INT_MAX.\n"); exit (EXIT_FAILURE); } m *= 10; } } p--; } return (int) n; } 替换有几种方法。最简单的方法通常是找到要转换的字符串的长度,然后向前移动到字符串的前面,预先形成从字符串到整数的转换。一个简单的例子是:

#include <stdio.h>
#include <stdlib.h>

int atoi2 (char *s);

int main (int argc, char **argv) {

    if (argc < 1) return 1;

    printf ("\n string : %s, conversion : %d\n\n",
            argv[1], atoi2 (argv[1]));

    return 0;
}

要测试的简单驱动程序可能是:

$ ./bin/atoi2 321

 string : 321, conversion : 321

$ ./bin/atoi2 -321

 string : -321, conversion : -321


$ ./bin/atoi2 2147483647

 string : 2147483647, conversion : 2147483647

$ ./bin/atoi2 2147483648
atoi2() error: conversion > INT_MAX.

示例使用/输出

def express_checkout

    listing = Listing.find(session[:listing_id].to_f)

    response = EXPRESS_GATEWAY.setup_purchase(session[:amount].to_f,
                                              ip: request.remote_ip,
                                              return_url: "http://esignature.lvh.me:3000/en/transactions/status",
                                              cancel_return_url: "http://esignature.lvh.me:3000/",
                                              currency: "USD",
                                              allow_guest_checkout: true,
                                              items: [{name: listing.title, description: listing.description, quantity: session[:number_of_days], amount: listing.price_cents},
                                                      {name: "Service Charge" ,description: "Service Charge", amount: session[:service_charge]}
                                              ]
    )


    redirect_to EXPRESS_GATEWAY.redirect_url_for(response.token)

  end

  def status
    if (params[:token].present? && params[:PayerID].present?)
      token = params[:token]
      @response = EXPRESS_GATEWAY.details_for(token).params
      listing = Listing.find(session[:listing_id].to_f)

      express_purchase_options = {
          :ip => request.remote_ip,
          :token => params[:token],
          :payer_id => params[:PayerID],
          items: [{ name: listing.title, description: listing.description, quantity: session[:number_of_days], amount: listing.price_cents},
                  { name: "Service Charge", description: "Service Charge", amount: session[:service_charge ]}
          ]
      }

      response = EXPRESS_GATEWAY.purchase(session[:amount].to_f, express_purchase_options)


      if response.message == "Success"
        listing = Listing.find(session[:listing_id].to_f)
        BookingInfo.create!(listing_id: listing.id, start_on: session[:start_date].to_date, end_on: session[:end_date].to_date)
        render 'status'
      else
        render 'status_error'
      end
      reset_session_params
    else
      reset_session_params
      redirect_to homepage_without_locale_path
    end

  end 

如果您有任何疑问,请随时提出。

答案 6 :(得分:0)

这是一个自定义atoi函数,可以避免使用大多数标准库函数

/*** _atoi - finds the first set of integers in a given string
 * @s: string entered
* Return: first number sequence
**/

int _atoi(char *s)
{
    int length = 0, negativeCount = 0, count = 0, num = 0;

    while (s[length] != '\0')
    {
        length++;
    }
    while (count < length)
    {
        if (s[count] == '-')
        {
            negativeCount++;
        }
        if (s[count] >= 48 && s[count] <= 57)
        {
            /* ascii values for numbers */
            for (; s[count] >= 48 && s[count] <= 57; count++)
            {
                num = (10 * num - (s[count] - 48));
            }
            break;
        }
        count++;
    }
    if (negativeCount % 2 != 0)
    {
        return (num);
    }
    else
    {
        return (-num);
    }
}
相关问题