用C的atoi实施 [英] atoi implementation in C
问题描述
我不明白以下的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屋!