those who commit
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
#include "Calculator.h"
|
||||
#include <ctype.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
#include <ctype.h>
|
||||
|
||||
static char logic_log[4096];
|
||||
|
||||
@@ -15,7 +15,8 @@ typedef struct {
|
||||
static double parse_expression(Parser *p);
|
||||
|
||||
static void skip_ws(Parser *p) {
|
||||
while (p->buffer[p->pos] == ' ') p->pos++;
|
||||
while (p->buffer[p->pos] == ' ')
|
||||
p->pos++;
|
||||
}
|
||||
|
||||
static double parse_factor(Parser *p) {
|
||||
@@ -27,7 +28,8 @@ static double parse_factor(Parser *p) {
|
||||
if (p->buffer[p->pos] == '(') {
|
||||
p->pos++;
|
||||
double res = parse_expression(p);
|
||||
if (p->buffer[p->pos] == ')') p->pos++;
|
||||
if (p->buffer[p->pos] == ')')
|
||||
p->pos++;
|
||||
return res;
|
||||
}
|
||||
char *endptr;
|
||||
@@ -50,7 +52,7 @@ static double parse_term(Parser *p) {
|
||||
char step[256];
|
||||
|
||||
snprintf(step, sizeof(step), "<div>%g %c %g = <b>%g</b></div>", old, op,
|
||||
right, left);
|
||||
right, left);
|
||||
strncat(logic_log, step, sizeof(logic_log) - strlen(logic_log) - 1);
|
||||
} else
|
||||
break;
|
||||
@@ -72,7 +74,7 @@ static double parse_expression(Parser *p) {
|
||||
char step[256];
|
||||
|
||||
snprintf(step, sizeof(step), "<div>%g %c %g = <b>%g</b></div>", old, op,
|
||||
right, left);
|
||||
right, left);
|
||||
strncat(logic_log, step, sizeof(logic_log) - strlen(logic_log) - 1);
|
||||
} else
|
||||
break;
|
||||
@@ -82,33 +84,34 @@ static double parse_expression(Parser *p) {
|
||||
|
||||
double evaluate(const char *expr) {
|
||||
logic_log[0] = '\0';
|
||||
if (!expr || strlen(expr) == 0) return 0.0;
|
||||
if (!expr || strlen(expr) == 0)
|
||||
return 0.0;
|
||||
Parser p = {expr, 0};
|
||||
return parse_expression(&p);
|
||||
}
|
||||
|
||||
InfoBox fetch_calc_data(char *math_input) {
|
||||
InfoBox info = {NULL, NULL, NULL, NULL};
|
||||
if (!math_input) return info;
|
||||
if (!math_input)
|
||||
return info;
|
||||
|
||||
double result = evaluate(math_input);
|
||||
|
||||
char html_output[5120];
|
||||
snprintf(html_output, sizeof(html_output),
|
||||
"<div class='calc-container' style='line-height: 1.6;'>"
|
||||
"%s"
|
||||
"<div style='margin-top: 8px; border-top: 1px solid #eee; "
|
||||
"padding-top: 8px; font-size: 1.2em;'>"
|
||||
"<b>%g</b>"
|
||||
"</div>"
|
||||
"</div>",
|
||||
strlen(logic_log) > 0 ? logic_log : "<div>Constant value</div>",
|
||||
result);
|
||||
"<div class='calc-container' style='line-height: 1.6;'>"
|
||||
"%s"
|
||||
"<div style='margin-top: 8px; border-top: 1px solid #eee; "
|
||||
"padding-top: 8px; font-size: 1.2em;'>"
|
||||
"<b>%g</b>"
|
||||
"</div>"
|
||||
"</div>",
|
||||
strlen(logic_log) > 0 ? logic_log : "<div>Constant value</div>",
|
||||
result);
|
||||
|
||||
info.title = strdup("Calculation");
|
||||
info.extract = strdup(html_output);
|
||||
info.thumbnail_url =
|
||||
strdup("/static/calculation.svg");
|
||||
info.thumbnail_url = strdup("/static/calculation.svg");
|
||||
info.url = strdup("#");
|
||||
|
||||
return info;
|
||||
|
||||
Reference in New Issue
Block a user