2 * Copyright (C) 2009 Rich Burridge
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU General Public License as published by the Free Software
6 * Foundation, either version 2 of the License, or (at your option) any later
7 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
14 #include <sys/types.h>
18 #include "mp-equation.h"
19 #include "mp-serializer.h"
23 static MpSerializer
*result_serializer
;
26 solve(const char *equation
)
29 MPEquationOptions options
;
31 gchar
*result_str
= NULL
;
33 memset(&options
, 0, sizeof(options
));
36 options
.angle_units
= MP_DEGREES
;
38 ret
= mp_equation_parse(equation
, &options
, &z
, NULL
);
40 if (ret
== PARSER_ERR_MP
)
41 fprintf(stderr
, "Error %s\n", mp_get_error());
43 fprintf(stderr
, "Error %d\n", ret
);
45 result_str
= mp_serializer_to_string(result_serializer
, &z
);
46 printf("%s\n", result_str
);
52 /* Adjust user input equation string before solving it. */
58 str
[strlen(str
)-1] = '\0'; /* Remove newline at end of string. */
59 for (i
= 0; str
[i
] != '\0'; i
++) { /* Remove whitespace. */
60 if (str
[i
] != ' ' && str
[i
] != '\t')
64 if (j
> 0 && str
[j
-1] == '=') /* Remove trailing '=' (if present). */
69 main(int argc
, char **argv
)
71 char *equation
, *line
;
72 size_t nbytes
= MAXLINE
;
74 /* Seed random number generator. */
75 srand48((long) time((time_t *) 0));
78 setlocale(LC_ALL
, "");
80 result_serializer
= mp_serializer_new(MP_DISPLAY_FORMAT_AUTOMATIC
, 10, 9);
82 equation
= (char *) malloc(MAXLINE
* sizeof(char));
85 line
= fgets(equation
, nbytes
, stdin
);
90 if (line
== NULL
|| strcmp(equation
, "exit") == 0 || strcmp(equation
, "quit") == 0 || strlen(equation
) == 0)