-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlotter.c
84 lines (66 loc) · 1.79 KB
/
Plotter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Coded by ScratchyCode
// Plot entered function between two points of its domain.
// Compile with -lm
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "tinyexpr.h"
#define DIM 10000
int control(void){
char path[] = "/usr/bin/gnuplot";
FILE *pf = fopen(path,"r");
if(pf == NULL){
fclose(pf);
return 1;
}else{
fclose(pf);
return 0;
}
}
int main(void){
long double inf, sup, inc;
double x, y;
te_variable vars[] = {{"x",&x}};
char expression[DIM];
// control that gnuplot is present on system
int gnupl = control(), i;
if(gnupl == 1){
printf("\nYou need gnuplot to graph the results.");
printf("\nInstall it with: sudo apt-get install gnuplot\n\n");
exit(2);
}
printf("\nEnter the analytical expression of f(x): ");
fgets(expression,DIM,stdin);
printf("Enter the lower abscissa (INF) of f(x): ");
scanf("%llf",&inf);
printf("Enter the upper abscissa (SUP) of f(x): ");
scanf("%llf",&sup);
printf("Enter the increment to represent: ");
scanf("%llf",&inc);
FILE *output = fopen("graph.dat","w");
if(output == NULL){
perror("\nError");
printf("\n");
exit(1);
}
printf("\n\t*** calculating ***\n");
x = inf;
do{
int err;
te_expr *n = te_compile(expression, vars, 1, &err);
if(n){
x += inc;
y = te_eval(n);
fprintf(output,"%lf\t%lf\n",x,y);
te_free(n);
}else{
printf("\t%*s^\nError", err-1, "");
exit(1);
}
}while(x <= sup);
printf("\n\t*** plotting ***\n");
fflush(output);
fclose(output);
return 0;
}