~ubuntu-branches/ubuntu/lucid/gcalctool/lucid-updates

« back to all changes in this revision

Viewing changes to src/gcalccmd.c

  • Committer: Bazaar Package Importer
  • Author(s): Robert Ancell
  • Date: 2010-04-07 19:41:56 UTC
  • mfrom: (1.3.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20100407194156-hgj8g002ffce2de0
Tags: 5.30.0.is.5.28.2-0ubuntu1
Downgrade to 5.28.2 as the new version does not support number bases
as well.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  $Header$
2
 
 *
3
 
 *  Copyright (c) 2009 Rich Burridge
4
 
 *           
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)
8
 
 *  any later version.
9
 
 *           
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.
14
 
 *           
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
18
 
 *  02111-1307, USA.
19
 
 */
20
 
 
21
 
#include <stdio.h>
22
 
#include <stdlib.h>
23
 
#include <string.h>
24
 
#include <sys/types.h>
25
 
#include <time.h>
26
 
 
27
 
#include "mp-equation.h"
28
 
 
29
 
#define MAXLINE 1024
30
 
 
31
 
static void
32
 
solve(const char *equation)
33
 
{
34
 
    int ret;
35
 
    MPEquationOptions options;
36
 
    MPNumber z;
37
 
    char result_str[MAXLINE];
38
 
    
39
 
    memset(&options, 0, sizeof(options));
40
 
    options.wordlen = 32;
41
 
    options.angle_units = MP_DEGREES;
42
 
    
43
 
    ret = mp_equation_parse(equation, &options, &z, NULL);
44
 
 
45
 
    if (ret == PARSER_ERR_MP)
46
 
        fprintf(stderr, "Error %s\n", mp_get_error());
47
 
    else if (ret)        
48
 
        fprintf(stderr, "Error %d\n", ret);
49
 
    else {
50
 
        mp_cast_to_string(&z, 10, 9, 1, result_str, MAXLINE);
51
 
        printf("%s\n", result_str);
52
 
    }
53
 
}
54
 
 
55
 
 
56
 
/* Adjust user input equation string before solving it. */
57
 
void
58
 
str_adjust(char *str)
59
 
{
60
 
    int i, j = 0;
61
 
 
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')
65
 
            str[j++] = str[i];
66
 
    }
67
 
    str[j] = '\0';
68
 
    if (j > 0 && str[j-1] == '=')     /* Remove trailing '=' (if present). */
69
 
        str[j-1] = '\0';
70
 
}
71
 
 
72
 
int
73
 
main(int argc, char **argv)
74
 
{
75
 
    char *equation;
76
 
    int bytes_read;
77
 
    size_t nbytes = MAXLINE;
78
 
 
79
 
    /* Seed random number generator. */
80
 
    srand48((long) time((time_t *) 0));
81
 
 
82
 
    while (1) {
83
 
        printf("> ");
84
 
        equation = (char *) malloc(MAXLINE * sizeof(char));
85
 
        bytes_read = getline(&equation, &nbytes, stdin);
86
 
 
87
 
        if (bytes_read != -1) {
88
 
            str_adjust(equation);
89
 
            if (!strcmp(equation, "exit") || !strcmp(equation, "quit") ||
90
 
                strlen(equation) == 0) {
91
 
                printf("\n");
92
 
                exit(1);
93
 
            } else {
94
 
                solve(equation);
95
 
                free(equation);
96
 
            }
97
 
        }
98
 
    }
99
 
}