用C的atoi实施 [英] atoi implementation in C

查看:357
本文介绍了用C的atoi实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白以下的atoi 执行code,特别是这一行: K =(K<< 3)+ (K<< 1)+(* p) - '0';

在code:

  INT my_atoi(字符* P){
    INT K = 0;
    而(* P){
        K =(K&下; 3;)+(K&所述;&所述; 1)+(* P) - '0';
        p ++;
     }
     复位K;
}

有人能解释一下?

另一个问题:什么应该是的ATOF 实施算法


解决方案

  K =(K<< 3)+(K<< 1);

办法

  K = K *2³+ K * 2 1 = K * 8 + K * 2 = K * 10

帮助吗?

* P - '0'项增加了下一个数字的价值;这个工作,因为C要求的数字字符具有连续的值,这样'1'=='0'+ 1 '2'== '0'+ 2

至于你的第二个问题( ATOF ),这应该是其自身的问题,它是一个论文的主题,不是简单的回答...

I can't understand the following atoi implementation code, specifically this line: k = (k<<3)+(k<<1)+(*p)-'0';

The code:

int my_atoi(char *p) {
    int k = 0;
    while (*p) {
        k = (k<<3)+(k<<1)+(*p)-'0';
        p++;
     }
     return k;
}

Can someone explain it to me ?

Another question: what should be the algorithm of atof implementation ?

解决方案

k = (k << 3) + (k << 1);

means

k = k * 2³ + k * 2¹ = k * 8 + k * 2 = k * 10

Does that help?

The *p - '0' term adds the value of the next digit; this works because C requires that the digit characters have consecutive values, so that '1' == '0' + 1, '2' == '0' + 2, etc.

As for your second question (atof), that should be its own question, and it's the subject for a thesis, not something simple to answer...

这篇关于用C的atoi实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆