calc.h (6017B)
1 /* calc.h - estado de la calculadora y manejo de teclas, compartido por la terminal, 2 * la ventana y la PicoCalc. Los frontends solo dibujan este estado y le pasan teclas. */ 3 #ifndef CALC_H 4 #define CALC_H 5 #include "expr.h" 6 #include "num.h" 7 #include "fmt.h" 8 #include "eval.h" 9 #include "form.h" 10 #include "matrix.h" 11 12 #define HIST_MAX 20 13 14 enum { MODE_CALC, MODE_EQN, MODE_TABLE, MODE_GRAPH, MODE_PROG, MODE_STAT, MODE_CMPLX, MODE_MATRIX, NMODES }; 15 enum { SCR_MAIN, SCR_MENU, SCR_FORM, SCR_CATALOG, SCR_KEYTEST }; 16 enum { FORM_SETUP = 1, FORM_EQN_TYPE, FORM_TABLE, FORM_GRAPH, FORM_STAT_TYPE, FORM_PROG, FORM_SOLVE, FORM_MAT_PICK, FORM_MAT_DIM }; 17 18 typedef struct { Expr in; Num res, res_im; uint8_t err, dec, polar, mkind; } Hist; 19 20 /* ecuaciones */ 21 enum { EQ_SYS2, EQ_SYS3, EQ_POLY2, EQ_POLY3, EQ_SOLVE }; 22 typedef struct { Num re, im; } Cplx; 23 typedef struct { 24 int type, phase; /* phase: 0 coeficientes (o ecuacion), 1 soluciones */ 25 Num coef[12]; /* sistemas: filas de (a b c) o (a b c d); polinomios: a b c d */ 26 int sel; /* celda elegida */ 27 bool editing, solved; 28 Cplx sol[3]; 29 int nsol, shown; /* solucion que se muestra */ 30 char note[40]; /* "Sin solucion", "Infinitas soluciones", ... */ 31 Expr solve_eq; /* SOLVE: la ecuacion con X */ 32 Num solve_x, solve_lr; 33 } EqnState; 34 35 /* tabla y grafico */ 36 #define TABLE_ROWS 30 37 typedef struct { 38 Expr f, g; 39 bool use_g; 40 Num start, end, step; 41 int rows, top, sel; 42 Num fx[TABLE_ROWS], gx[TABLE_ROWS], x[TABLE_ROWS]; 43 uint8_t ferr[TABLE_ROWS], gerr[TABLE_ROWS]; 44 int phase; /* 0 f(x), 1 g(x), 2 tabla */ 45 bool ready; 46 } TableState; 47 48 #define GRAPH_W 320 49 typedef struct { 50 Expr f, g; 51 bool use_g, autoy, ready; 52 int phase; /* 0 f(x), 1 g(x), 2 grafico */ 53 int curve; /* trazo sobre f (0) o g (1) */ 54 Dec xmin, xmax, ymin, ymax; 55 int16_t fy[GRAPH_W], gy[GRAPH_W]; /* fila de pantalla de cada columna (INT16_MIN = no hay) */ 56 int w, h; /* tamano del area de dibujo (lo pone el frontend) */ 57 int tx; /* columna del trazo */ 58 Num tval; 59 } GraphState; 60 61 /* programador */ 62 typedef struct { int base, bits; } ProgState; 63 64 /* estadistica */ 65 #define STAT_MAX 80 66 enum { ST_1VAR, ST_LIN, ST_QUAD, ST_LOG, ST_EXP, ST_ABEXP, ST_POW, ST_INV, ST_NTYPES }; 67 #define STAT_RES 24 68 typedef struct { 69 int type, n, row, col, top; 70 Num x[STAT_MAX], y[STAT_MAX]; 71 bool editing; 72 int phase; /* 0 datos, 1 resultados */ 73 int rtop, nres; 74 const char *rlabel[STAT_RES]; 75 Num rval[STAT_RES]; 76 uint8_t rerr[STAT_RES]; 77 Num ra, rb, rc; /* coeficientes de la ultima regresion (para x̂ ŷ) */ 78 int rtype; 79 bool rvalid; 80 } StatState; 81 82 /* matrices y vectores: 0-3 = A-D, 4 = Ans */ 83 typedef struct { 84 MVal m[5], v[5]; 85 MVal res; /* ultimo resultado (escalar, matriz o vector) */ 86 bool ans_mat; 87 int phase; /* 0 cuentas, 1 editando una matriz o vector */ 88 int target; /* 0-3 MatA-D, 4-7 VctA-D */ 89 int sel; 90 bool editing; 91 } MatState; 92 93 typedef struct Calc { 94 int mode, scr; 95 Expr in; 96 bool shift, sto; 97 bool shown; /* el resultado de 'in' esta en pantalla */ 98 Num res; 99 int err, err_pos; 100 bool want_dec; /* S⇔D */ 101 bool dms; /* resultado en grados, minutos y segundos */ 102 bool polar; /* complejos: r∠θ en vez de a+bi */ 103 Num res_im, ans_im, vars_im[NVARS]; 104 Hist hist[HIST_MAX]; 105 int nh, hsel; /* hsel: entrada del historial que se esta viendo (-1: nueva) */ 106 /* ajustes */ 107 int ang; 108 Fmt fmt; 109 bool dec_default; /* resultados en decimal (MathI/DecimalO) */ 110 Num vars[NVARS], ans, preans; 111 /* menus */ 112 int menu_sel; 113 Form form; 114 int form_kind; 115 int cat, cat_sel, cat_top; /* catalogo: categoria (-1 = lista de categorias) */ 116 int keytest; /* ultimo codigo en "probar teclado" */ 117 char msg[64]; 118 uint32_t version; /* cambia con cada modificacion */ 119 bool dirty_save; /* hay cambios para guardar */ 120 uint32_t (*rand)(void); 121 /* modos */ 122 EqnState eq; 123 TableState tb; 124 GraphState gr; 125 ProgState pg; 126 StatState st; 127 MatState mt; 128 } Calc; 129 130 /* la pantalla de Calcular (entrada + historial) sirve a varios modos */ 131 static inline bool calc_main_screen(const Calc *c) 132 { 133 return c->mode == MODE_CALC || c->mode == MODE_PROG || c->mode == MODE_CMPLX || 134 (c->mode == MODE_MATRIX && c->mt.phase == 0); 135 } 136 137 void calc_init(Calc *c); 138 /* Procesa una tecla (K_* o caracter). Devuelve true si hay que redibujar. */ 139 bool calc_key(Calc *c, int key); 140 /* contexto de evaluacion con los ajustes y variables actuales */ 141 EvalCtx calc_ctx(const Calc *c); 142 /* resultado actual como expresion natural (para dibujar) */ 143 bool calc_result_expr(const Calc *c, const Num *v, bool dec, Expr *out); 144 /* el resultado en pantalla y una entrada del historial (complejos, °′″, S⇔D) */ 145 void calc_res_expr(const Calc *c, Expr *out); 146 void calc_hist_expr(const Calc *c, const Hist *h, Expr *out); 147 bool calc_res_has_exact(const Calc *c); 148 const char *calc_err_text(int err); 149 /* titulos para la barra de estado */ 150 const char *calc_mode_name(int mode); 151 const char *calc_ang_name(int ang); 152 153 /* catalogo */ 154 typedef struct { const char *label; const char *text; } CatItem; 155 typedef struct { const char *name; const CatItem *items; int n; } CatGroup; 156 extern const CatGroup CATALOG[]; 157 extern const int CATALOG_N; 158 159 /* modos (core/modes.c) */ 160 void mode_enter(Calc *c, int mode); 161 bool mode_key(Calc *c, int key); 162 void graph_compute(Calc *c); 163 void table_compute(Calc *c); 164 165 /* guardado (core/persist.c): texto "clave=valor" por linea */ 166 int persist_save(const Calc *c, char *buf, int n); 167 void persist_load(Calc *c, const char *buf, int n); 168 169 #endif