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>
27
#include "mp-equation.h"
32
solve(const char *equation)
35
MPEquationOptions options;
37
char result_str[MAXLINE];
39
memset(&options, 0, sizeof(options));
41
options.angle_units = MP_DEGREES;
43
ret = mp_equation_parse(equation, &options, &z, NULL);
45
if (ret == PARSER_ERR_MP)
46
fprintf(stderr, "Error %s\n", mp_get_error());
48
fprintf(stderr, "Error %d\n", ret);
50
mp_cast_to_string(&z, 10, 9, 1, result_str, MAXLINE);
51
printf("%s\n", result_str);
56
/* Adjust user input equation string before solving it. */
62
str[strlen(str)-1] = '\0'; /* Remove newline at end of string. */
63
for (i = 0; str[i] != '\0'; i++) { /* Remove whitespace. */
64
if (str[i] != ' ' && str[i] != '\t')
68
if (j > 0 && str[j-1] == '=') /* Remove trailing '=' (if present). */
73
main(int argc, char **argv)
77
size_t nbytes = MAXLINE;
79
/* Seed random number generator. */
80
srand48((long) time((time_t *) 0));
84
equation = (char *) malloc(MAXLINE * sizeof(char));
85
bytes_read = getline(&equation, &nbytes, stdin);
87
if (bytes_read != -1) {
89
if (!strcmp(equation, "exit") || !strcmp(equation, "quit") ||
90
strlen(equation) == 0) {