proj
This commit is contained in:
parent
f710e88793
commit
e3624f8853
1 changed files with 33 additions and 10 deletions
|
|
@ -161,20 +161,31 @@ void lcd_print_str(const char *str) {
|
||||||
lcd_data(*str++);
|
lcd_data(*str++);
|
||||||
}
|
}
|
||||||
|
|
||||||
void lcd_print_num(unsigned int num, unsigned int base) {
|
void lcd_print_num(int num, unsigned int base) {
|
||||||
char buffer[17];
|
char buffer[17];
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
unsigned int unum;
|
||||||
|
|
||||||
if (num == 0) {
|
if (base == MODE_DEC) {
|
||||||
|
if (num < 0) {
|
||||||
|
lcd_data('-');
|
||||||
|
num = -num;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unum = (unsigned int)num;
|
||||||
|
|
||||||
|
if (unum == 0) {
|
||||||
lcd_data('0');
|
lcd_data('0');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (num > 0 && i < 16) {
|
while (unum > 0 && i < 16) {
|
||||||
unsigned int d = num % base;
|
unsigned int d = unum % base;
|
||||||
buffer[i++] = (d < 10) ? ('0' + d) : ('A' + d - 10);
|
buffer[i++] = (d < 10) ? ('0' + d) : ('A' + d - 10);
|
||||||
num /= base;
|
unum /= base;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (i > 0)
|
while (i > 0)
|
||||||
lcd_data(buffer[--i]);
|
lcd_data(buffer[--i]);
|
||||||
}
|
}
|
||||||
|
|
@ -294,24 +305,36 @@ unsigned int is_valid_digit(unsigned int key) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ===== Arithmetic ===== */
|
/* ===== Arithmetic per base (optimized) ===== */
|
||||||
unsigned int base_arith(unsigned int a, unsigned int b,
|
int base_arith(int a, int b, unsigned int op, unsigned int base) {
|
||||||
unsigned int op, unsigned int base) {
|
int r = 0;
|
||||||
unsigned int r;
|
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case OP_ADD:
|
case OP_ADD:
|
||||||
r = a + b;
|
r = a + b;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case OP_SUB:
|
case OP_SUB:
|
||||||
r = (a >= b) ? (a - b) : 0;
|
r = a - b;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case OP_MUL:
|
case OP_MUL:
|
||||||
r = a * b;
|
r = a * b;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
r = a;
|
r = a;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For non-decimal modes, keep everything positive and base-limited
|
||||||
|
if (base != MODE_DEC) {
|
||||||
|
if (r < 0)
|
||||||
|
r = ((r % base) + base) % base;
|
||||||
|
else
|
||||||
|
r = r % (base * base * base * base * base); // safe wraparound for multi-digit bases
|
||||||
|
}
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue