3 * Copyright (c) 2009 Rich Burridge
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 #include <sys/types.h>
28 #include "mp-equation.h"
29 #include "mp-serializer.h"
33 static MpSerializer
*result_serializer
;
36 solve(const char *equation
)
39 MPEquationOptions options
;
41 gchar
*result_str
= NULL
;
43 memset(&options
, 0, sizeof(options
));
46 options
.angle_units
= MP_DEGREES
;
48 ret
= mp_equation_parse(equation
, &options
, &z
, NULL
);
50 if (ret
== PARSER_ERR_MP
)
51 fprintf(stderr
, "Error %s\n", mp_get_error());
53 fprintf(stderr
, "Error %d\n", ret
);
55 result_str
= mp_serializer_to_string(result_serializer
, &z
);
56 printf("%s\n", result_str
);
62 /* Adjust user input equation string before solving it. */
68 str
[strlen(str
)-1] = '\0'; /* Remove newline at end of string. */
69 for (i
= 0; str
[i
] != '\0'; i
++) { /* Remove whitespace. */
70 if (str
[i
] != ' ' && str
[i
] != '\t')
74 if (j
> 0 && str
[j
-1] == '=') /* Remove trailing '=' (if present). */
79 main(int argc
, char **argv
)
81 char *equation
, *line
;
82 size_t nbytes
= MAXLINE
;
84 /* Seed random number generator. */
85 srand48((long) time((time_t *) 0));
88 setlocale(LC_ALL
, "");
90 result_serializer
= mp_serializer_new(MP_DISPLAY_FORMAT_AUTOMATIC
, 10, 9);
92 equation
= (char *) malloc(MAXLINE
* sizeof(char));
95 line
= fgets(equation
, nbytes
, stdin
);
100 if (line
== NULL
|| strcmp(equation
, "exit") == 0 || strcmp(equation
, "quit") == 0 || strlen(equation
) == 0)