charstod.c

来自「这是一个同样来自贝尔实验室的和UNIX有着渊源的操作系统, 其简洁的设计和实现易」· C语言 代码 · 共 71 行

C
71
字号
#include <u.h>#include <libc.h>#include "fmtdef.h"/* * Reads a floating-point number by interpreting successive characters * returned by (*f)(vp).  The last call it makes to f terminates the * scan, so is not a character in the number.  It may therefore be * necessary to back up the input stream up one byte after calling charstod. */doublefmtcharstod(int(*f)(void*), void *vp){	double num, dem;	int neg, eneg, dig, exp, c;	num = 0;	neg = 0;	dig = 0;	exp = 0;	eneg = 0;	c = (*f)(vp);	while(c == ' ' || c == '\t')		c = (*f)(vp);	if(c == '-' || c == '+'){		if(c == '-')			neg = 1;		c = (*f)(vp);	}	while(c >= '0' && c <= '9'){		num = num*10 + c-'0';		c = (*f)(vp);	}	if(c == '.')		c = (*f)(vp);	while(c >= '0' && c <= '9'){		num = num*10 + c-'0';		dig++;		c = (*f)(vp);	}	if(c == 'e' || c == 'E'){		c = (*f)(vp);		if(c == '-' || c == '+'){			if(c == '-'){				dig = -dig;				eneg = 1;			}			c = (*f)(vp);		}		while(c >= '0' && c <= '9'){			exp = exp*10 + c-'0';			c = (*f)(vp);		}	}	exp -= dig;	if(exp < 0){		exp = -exp;		eneg = !eneg;	}	dem = __fmtpow10(exp);	if(eneg)		num /= dem;	else		num *= dem;	if(neg)		return -num;	return num;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?