Calculator/unix.cpp

60 lines
1.3 KiB
C++
Raw Normal View History

2023-11-26 15:56:00 +01:00
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "calc.yy.h"
#include "calculator.h"
extern void yyparse ();
extern "C" int yywrap () {
return 1;
}
static char * from_file (const char * filename) {
struct stat statbuf;
int r = stat (filename, & statbuf);
if (r) return nullptr;
char * buffer = (char*) malloc (statbuf.st_size + 1);
FILE * in = fopen (filename,"r");
r = fread (buffer, 1, statbuf.st_size, in);
if (r != statbuf.st_size) {
free (buffer);
fclose (in);
return nullptr;
}
buffer [r] = '\0';
fclose (in);
return buffer;
}
void emitData (double * x, double * y, const int len) {
for (int n=0; n<len; n++) {
printf("f(%g) = %g\n", x[n], y[n]);
}
}
/*
static const char * multipliers [] = {"f","p","n","μ","m","","k","M","G","T","P"};
static void test () {
const int ofs = 5 * 3;
for (int n=-15; n<18; n++) {
div_t dt = div (n + ofs, 3);
printf("n=%d, q=%d, r=%d (%d)\t%g %s\n", n, dt.quot, dt.rem, dt.quot * 3 + dt.rem - ofs,
pow(10.0, dt.rem), multipliers[dt.quot]);
}
}
*/
int main () {
char * buffer = from_file("test.txt");
if (!buffer) return 1;
initData();
YY_BUFFER_STATE result = yy_scan_string(buffer);
yyparse();
yy_delete_buffer(result);
free (buffer);
finiData();
//test();
return 0;
}