1
0
mirror of https://github.com/NevermindZZT/letter-shell.git synced 2025-01-21 10:02:54 +08:00

修复浮点参数自动转换

This commit is contained in:
Letter 2019-04-10 22:21:01 +08:00
parent 86896167aa
commit e5a0166072
2 changed files with 29 additions and 19 deletions

14
shell.c
View File

@ -196,7 +196,7 @@ static void shellDisplayReturn(SHELL_TypeDef *shell, int value)
{ {
char str[11] = "0000000000"; char str[11] = "0000000000";
unsigned int v = value; unsigned int v = value;
char i = 9; char i = 10;
char tmp; char tmp;
shellDisplay(shell, "Return: "); shellDisplay(shell, "Return: ");
@ -207,22 +207,22 @@ static void shellDisplayReturn(SHELL_TypeDef *shell, int value)
} }
while (v) while (v)
{ {
str[i--] = v % 10 + 48; str[--i] = v % 10 + 48;
v /= 10; v /= 10;
} }
shellDisplay(shell, str); shellDisplay(shell, str + i - (value == 0));
v = value; v = value;
if (value < 0) if (value < 0)
{ {
v = (unsigned int)value; v = (unsigned int)value;
} }
i = 7; i = 8;
str[8] = 0; str[8] = 0;
while (v) while (v)
{ {
tmp = v %16; tmp = v & 0x0000000F;
str[i--] = (tmp > 9) ? (tmp + 87) : (tmp + 48); str[--i] = (tmp > 9) ? (tmp + 87) : (tmp + 48);
v /= 16; v >>= 4;
} }
shellDisplay(shell, ", 0x"); shellDisplay(shell, ", 0x");
shellDisplay(shell, str); shellDisplay(shell, str);

View File

@ -36,15 +36,13 @@ static NUM_Type shellExtNumType(char *string)
{ {
type = NUM_TYPE_OCT; type = NUM_TYPE_OCT;
} }
else
while (*p++)
{ {
while (*p++) if (*p == '.' && *(p + 1) != 0)
{ {
if (*p == '.' && *(p + 1) != 0) type = NUM_TYPE_FLOAT;
{ break;
type = NUM_TYPE_FLOAT;
break;
}
} }
} }
@ -175,6 +173,8 @@ static unsigned int shellExtParseNumber(char *string)
char offset = 0; char offset = 0;
signed char sign = 1; signed char sign = 1;
unsigned int valueInt = 0; unsigned int valueInt = 0;
float valueFloat = 0.0;
unsigned int devide = 0;
if (*string == '-') if (*string == '-')
{ {
@ -206,17 +206,27 @@ static unsigned int shellExtParseNumber(char *string)
p = string + offset + ((sign == -1) ? 1 : 0); p = string + offset + ((sign == -1) ? 1 : 0);
if (type != NUM_TYPE_FLOAT) while (*p)
{ {
while (*p) if (*p == '.')
{ {
valueInt = valueInt * radix + shellExtToNum(*p); devide = 1;
p++; p++;
continue;
} }
valueInt = valueInt * radix + shellExtToNum(*p);
devide *= 10;
p++;
}
if (type == NUM_TYPE_FLOAT && devide != 0)
{
valueFloat = (float)valueInt / devide * sign;
return *(unsigned int *)(&valueFloat);
}
else
{
return valueInt * sign; return valueInt * sign;
} }
return 0;
} }