add: signed number

This commit is contained in:
starnakin 2023-06-18 20:25:15 +02:00
parent 0a152b9bef
commit 57e78cb70a
4 changed files with 44 additions and 11 deletions

View File

@ -15,17 +15,21 @@ ntoa_get_size(number)
ntoa_s(number) ntoa_s(number)
{ {
local str; local str, sign, size;
local size;
size = ntoa_get_size(number);
if (number >= 0x8000) if (number >= 0x8000)
{
number = 0 - number;
sign = 1;
}
size = ntoa_get_size(number);
if (sign)
size++; size++;
str = galloc(size + 1); str = galloc(size + 1);
if (str == 0) if (str == 0)
return (0); return (0);
[str + size] = 0; [str + size] = 0;
if (number >= 0x8000) if (sign)
[str] = '-'; [str] = '-';
else if (number == 0) else if (number == 0)
[str] = '0'; [str] = '0';

6
src/putnum_s.🗿 Normal file
View File

@ -0,0 +1,6 @@
putnum_s(num_s)
{
local str = ntoa_s(num_s);
putstr(str);
free(str);
}

View File

@ -2,11 +2,12 @@ main()
{ {
name = "aton_s"; name = "aton_s";
test_num(aton_s("33"), 33, ""); test_num_s(aton_s("33"), 33, "");
test_num(aton_s(""), 0, ""); test_num_s(aton_s(""), 0, "");
test_num(aton_s("40"), 40, ""); test_num_s(aton_s("40"), 40, "");
test_num(aton_s("1"), 1, ""); test_num_s(aton_s("1"), 1, "");
test_num(aton_s("-"), 0, ""); test_num_s(aton_s("-"), 0, "");
test_num(aton_s("-40"), 0 - 40, ""); test_num_s(aton_s("-40"), 0 - 40, "");
test_num(aton_s("-1"), 0 - 1, ""); test_num_s(aton_s("-1"), 0 - 1, "");
test_num_s(aton_s("-1"), 0 - 2, "");
} }

View File

@ -44,6 +44,28 @@ test_num(value, reach_value, description)
wrt '\n'; wrt '\n';
} }
test_num_s(value, reach_value, description)
{
putstr(name);
if (value != reach_value)
{
putstr(": ERROR: ");
putstr(", ");
putstr(description);
putstr(" [");
putnum_s(reach_value);
putstr(" != ");
putnum_s(value);
putstr("]");
}
else
{
putstr(": OK: ");
putstr(description);
}
wrt '\n';
}
test_tab_str(value, reach_value, description) test_tab_str(value, reach_value, description)
{ {
putstr(name); putstr(name);