From 71e589eb708d2fd653b7b31bd645f50f22cdf7aa Mon Sep 17 00:00:00 2001 From: ov075wu Date: Tue, 27 Jan 2026 12:32:27 +0100 Subject: [PATCH] refresh --- sk2/calculator.c | 139 +++++++++++++++++++++++------------------------ 1 file changed, 68 insertions(+), 71 deletions(-) diff --git a/sk2/calculator.c b/sk2/calculator.c index ed6c3c8..ddc7233 100644 --- a/sk2/calculator.c +++ b/sk2/calculator.c @@ -5,20 +5,20 @@ #include #include -static const char *ptr; +static const char *cur; -static void skip_spaces(void) { - while (*ptr && isspace(*ptr)) { - ptr++; +static void skip_ws(void) { + while (*cur && isspace(*cur)) { + cur++; } } -static int match(const char *s) { - int len = strlen(s); - if (strncmp(ptr, s, len) == 0) { - ptr += len; +static int starts_with(const char *name) { + int len = strlen(name); + if (strncmp(cur, name, len) == 0) { + cur += len; return 1; } return 0; @@ -26,104 +26,101 @@ static int match(const char *s) { -static double parse_expression(void); +static double read_expr(void); -static double parse_number(void) { - skip_spaces(); +static double read_number(void) { + skip_ws(); char *end; - double val = strtod(ptr, &end); - ptr = end; - return val; + double res = strtod(cur, &end); + cur = end; + return res; } -static double parse_factor(void) { - skip_spaces(); +static double read_factor(void) { + skip_ws(); - - if (*ptr == '(') { - ptr++; - double val = parse_expression(); - skip_spaces(); - if (*ptr == ')') { - ptr++; + if (*cur == '(') { + cur++; + double res = read_expr(); + skip_ws(); + if (*cur == ')') { + cur++; } - return val; + return res; } - - if (match("sin")) { - skip_spaces(); - return sin(parse_factor()); + if (starts_with("sin")) { + skip_ws(); + return sin(read_factor()); } - if (match("cos")) { - skip_spaces(); - return cos(parse_factor()); + if (starts_with("cos")) { + skip_ws(); + return cos(read_factor()); } - if (match("sqrt")) { - skip_spaces(); - return sqrt(parse_factor()); + if (starts_with("sqrt")) { + skip_ws(); + return sqrt(read_factor()); } - if (match("log")) { - skip_spaces(); - return log(parse_factor()); + if (starts_with("log")) { + skip_ws(); + return log(read_factor()); } - if (match("pow")) { - skip_spaces(); - if (*ptr == '(') ptr++; - double a = parse_expression(); - skip_spaces(); - if (*ptr == ',') ptr++; - double b = parse_expression(); - skip_spaces(); - if (*ptr == ')') ptr++; - return pow(a, b); + if (starts_with("pow")) { + skip_ws(); + if (*cur == '(') cur++; + double left = read_expr(); + skip_ws(); + if (*cur == ',') cur++; + double right = read_expr(); + skip_ws(); + if (*cur == ')') cur++; + return pow(left, right); } - - return parse_number(); + return read_number(); } -static double parse_term(void) { - double val = parse_factor(); +static double read_term(void) { + double res = read_factor(); while (1) { - skip_spaces(); - if (*ptr == '*') { - ptr++; - val *= parse_factor(); - } else if (*ptr == '/') { - ptr++; - val /= parse_factor(); + skip_ws(); + if (*cur == '*') { + cur++; + res *= read_factor(); + } else if (*cur == '/') { + cur++; + res /= read_factor(); } else { break; } } - return val; + return res; } -static double parse_expression(void) { - double val = parse_term(); +static double read_expr(void) { + double res = read_term(); while (1) { - skip_spaces(); - if (*ptr == '+') { - ptr++; - val += parse_term(); - } else if (*ptr == '-') { - ptr++; - val -= parse_term(); + skip_ws(); + if (*cur == '+') { + cur++; + res += read_term(); + } else if (*cur == '-') { + cur++; + res -= read_term(); } else { break; } } - return val; + return res; } double evaluate_expression(const char *expr) { - ptr = expr; - return parse_expression(); + cur = expr; + return read_expr(); }