This commit is contained in:
sherlock 2025-10-29 14:31:10 +05:30
parent f710e88793
commit e3624f8853

View file

@ -161,20 +161,31 @@ void lcd_print_str(const char *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];
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');
return;
}
while (num > 0 && i < 16) {
unsigned int d = num % base;
while (unum > 0 && i < 16) {
unsigned int d = unum % base;
buffer[i++] = (d < 10) ? ('0' + d) : ('A' + d - 10);
num /= base;
unum /= base;
}
while (i > 0)
lcd_data(buffer[--i]);
}
@ -294,24 +305,36 @@ unsigned int is_valid_digit(unsigned int key) {
return 1;
}
/* ===== Arithmetic ===== */
unsigned int base_arith(unsigned int a, unsigned int b,
unsigned int op, unsigned int base) {
unsigned int r;
/* ===== Arithmetic per base (optimized) ===== */
int base_arith(int a, int b, unsigned int op, unsigned int base) {
int r = 0;
switch (op) {
case OP_ADD:
r = a + b;
break;
case OP_SUB:
r = (a >= b) ? (a - b) : 0;
r = a - b;
break;
case OP_MUL:
r = a * b;
break;
default:
r = a;
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;
}