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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
/* @(#)display.c 1.8 89/11/01
*
* Display manipulation routines used by calctool.
*
* Copyright (c) Rich Burridge.
* Sun Microsystems, Australia - All rights reserved.
*
* Basic algorithms, copyright (c) Ed Falk.
* Sun Microsystems, Mountain View.
*
* Permission is given to distribute these sources, as long as the
* copyright messages are not removed, and no monies are exchanged.
*
* No responsibility is taken for any errors or inaccuracies inherent
* either to the comments or the code of this program, but if
* reported to me then an attempt will be made to fix them.
*/
#include <stdio.h>
#include <strings.h>
#include <math.h>
#include "calctool.h"
#include "color.h"
#include "extern.h"
char_val(chr)
char chr ;
{
if (chr >= '0' && chr <= '9') return(chr - '0') ;
else if (chr >= 'a' && chr <= 'f') return(chr - 'a' + 10) ;
else return(-1) ;
}
clear_display()
{
int i ;
pointed = 0 ;
toclear = 1 ;
STRCPY(display, "0.") ;
for (i = 0; i < accuracy; i++) STRNCAT(display, "0", 1) ;
set_item(DISPLAYITEM, display) ;
hyperbolic = inverse = 0 ;
set_item(HYPITEM, " ") ;
set_item(INVITEM, " ") ;
disp_val = 0.0 ;
}
double
convert_display() /* Convert input string into a double. */
{
int exp, exp_sign, i, inum ;
double val ;
char *optr ;
val = 0.0 ;
exp = 0 ;
optr = display ;
while ((inum = char_val(*optr)) >= 0)
{
val = val * basevals[(int) base] + inum ;
*optr++ ;
}
if (*optr == '.')
for (i = 1; (inum = char_val(*++optr)) >= 0; i++)
val += inum / powers[i][(int) base] ;
while (*optr == ' ') optr++ ;
if (*optr != '\0')
{
if (*optr == '-') exp_sign = -1 ;
else exp_sign = 1 ;
while ((inum = char_val(*++optr)) >= 0)
exp = exp * basevals[(int) base] + inum ;
}
exp *= exp_sign ;
if (key_exp)
val *= pow((double) basevals[(int) base], (double) exp) ;
return(val) ;
}
get_label(n)
int n ;
{
if (tstate)
switch (buttons[n].value)
{
case CCTRL('c') :
case CCTRL('d') :
case CCTRL('e') :
case CCTRL('f') :
case CCTRL('g') :
case CCTRL('n') :
case CCTRL('r') :
case CCTRL('s') :
case CCTRL('t') : SPRINTF(pstr, "^%c ", buttons[n].value + 96) ;
break ;
case CCTRL('h') : STRCPY(pstr, "bsp ") ;
break ;
case '\177' : STRCPY(pstr, "del ") ;
break ;
default : SPRINTF(pstr, "%c ", buttons[n].value) ;
}
else STRCPY(pstr, buttons[n].str) ;
}
initialise()
{
error = 0 ; /* Currently no display error. */
cur_op = '?' ; /* No arithmetic operator defined yet. */
old_cal_value = '?' ;
result = 0.0 ; /* No previous result yet. */
last_input = 0.0 ;
}
char *
make_fixed(number, cmax) /* Convert fixed number. */
double number ; /* Value to convert. */
int cmax ; /* Maximum characters to generate. */
{
char *optr ;
double val ;
int ndig ; /* Total number of digits to generate. */
int ddig ; /* Number of digits to left of . */
int dval ;
optr = fnum ;
val = fabs(number) ;
if (number < 0.0) *optr++ = '-' ;
val += .5 / powers[accuracy][(int) base] ;
if (val < 1.0)
{
ddig = 0 ;
*optr++ = '0' ;
cmax-- ;
}
else
{
for (ddig = 0; val >= 1.0; ddig++)
val /= powers[1][(int) base] ;
}
ndig = MIN(ddig + accuracy, --cmax) ;
while (ndig-- > 0)
{
if (ddig-- == 0) *optr++ = '.' ;
val *= powers[1][(int) base] ;
dval = val ;
*optr++ = digits[dval] ;
val -= (int) val ;
}
*optr++ = '\0' ;
toclear = 1 ;
pointed = 0 ;
return(fnum) ;
}
char *
make_number(number) /* Convert display value to current base. */
double number ; /* Value to convert. */
{
double val ;
/* Try to apply sanity checks to try to detect if the number to be
* converted is not a valid number. These are based on the isnan and
* isinf routines, which are only available on certain machines.
*/
if (number != number || (number*0 != 0)) /* Same as isnan & isinf. */
{
STRCPY(display, "Error") ;
error = 1 ;
set_item(OPITEM, "CLR") ;
return(display) ;
}
val = fabs(number) ;
if (dtype == SCI ||
dtype == FIX && val != 0.0 && (val > max_fix[(int) base] ||
val < exp_p1[accuracy][(int) base]))
return(make_scientific(number)) ;
else return(make_fixed(number, MAX_DIGITS)) ;
}
char *
make_scientific(number) /* Convert scientific number. */
double number ; /* Value to convert. */
{
char fixed[MAX_DIGITS+1] ;
char *optr ;
double mant ; /* Mantissa */
double val ;
int exp = 0 ; /* Exponent */
int i ;
int eng = 0 ; /* Scientific not engineering value. */
double atmp ;
optr = snum ;
val = fabs(number) ;
if (number < 0.0) *optr++ = '-' ;
mant = val ;
atmp = 1.0 / powers[10][(int) base] ;
if (mant != 0.0)
{
while (mant >= powers[10][(int) base])
{
exp += 10 ;
mant *= atmp ;
}
while ((!eng && mant >= powers[1][(int) base]) ||
(eng && (mant >= powers[3][(int) base] || exp % 3 != 0)))
{
exp += 1 ;
mant /= powers[1][(int) base] ;
}
while (mant < atmp)
{
exp -= 10 ;
mant *= powers[10][(int) base] ;
}
while (mant < 1.0 || (eng && exp % 3 != 0))
{
exp -= 1 ;
mant *= powers[1][(int) base] ;
}
}
STRCPY(fixed, make_fixed(mant, MAX_DIGITS-6)) ;
for (i = 0; i < strlen(fixed); i++) *optr++ = fixed[i] ;
*optr++ = 'e' ;
if (exp < 0)
{
exp = -exp ;
*optr++ = '-' ;
}
else *optr++ = '+' ;
if ((*optr = digits[exp / ((int) powers[2][(int) base])]) != '0')
optr++ ;
exp %= (int) powers[2][(int) base] ;
*optr++ = digits[exp / ((int) powers[1][(int) base])] ;
exp %= (int) powers[1][(int) base] ;
*optr++ = digits[exp] ;
*optr++ = '\0' ;
toclear = 1 ;
pointed = 0 ;
return(snum) ;
}
process_item(n)
int n ;
{
int i,isvalid ;
if (n < 0 || n > TITEMS) return ;
current = buttons[n].value ;
if (current == 'X') current = 'x' ; /* Reassign "extra" values. */
if (current == '*') current = 'x' ;
if (current == '\015') current = '=' ;
if (current == 'Q') current = 'q' ;
if (error)
{
isvalid = 0 ; /* Must press a valid key first. */
for (i = 0; i < MAXVKEYS; i++)
if (current == validkeys[i]) isvalid = 1 ;
if (pending == '?') isvalid = 1 ;
if (!isvalid) return ;
error = 0 ;
}
if (pending)
{
for (n = 0; n < TITEMS; n++)
if (pending == buttons[n].value) break ;
}
switch (buttons[n].opdisp)
{
case OP_SET : set_item(OPITEM, buttons[n].str) ;
break ;
case OP_CLEAR : if (error) set_item(OPITEM, "CLR") ;
else set_item(OPITEM, "") ;
}
(*buttons[n].func)() ;
}
show_display(val)
double val ;
{
if (!error)
{
STRCPY(display, make_number(val)) ;
set_item(DISPLAYITEM, display) ;
}
}
|