Esempio n. 1
0
/*
 * This function return the fraction part of a double
 * and set in ip the integral part.
 * In many ways it resemble the modf() found on most Un*x
 */
static double integral(double real, double *ip)
{ 
	int j;
	double i, s, p;
	double real_integral = 0.;

	/* take care of the obvious */
	/* equal to zero ? */
	if (real == 0.) {
		*ip = 0.;
		return (0.);
	}

	/* negative number ? */
	if (real < 0.)
		real = -real;

	/* a fraction ? */
	if ( real < 1.) {
		*ip = 0.;
		return real;
	}

	/* the real work :-) */
	for (j = log_10(real); j >= 0; j--) {
		p = pow_10(j);
		s = (real - real_integral)/p;
		i = 0.;

		while (i + 1. <= s)
			i++;
		real_integral += i*p;
	}

	*ip = real_integral;
	return (real - real_integral);
}
Esempio n. 2
0
int main(void)
{
    float total = 0, M = 0;
    short input = 0, input_err = 0;
	
	do {
	    system("clear");
	    
	    disp_ops();
	    
	    printf("* M = %-73f*\n", M);	    
	    for (int i = 0; i < 80; ++i) {
	        printf("*");
	    }
	    printf("\n");
	    
	    printf("* %-76f *\n", total);
	    for (int i = 0; i < 80; ++i) {
	        printf("*");
	    }
	    printf("\n");
		
		do {
		    printf("Operation: ");
		    input_err = shortnum(&input);
		}while(input_err == 1 || input_err == 2);
		
		printf("\n");
		
		switch (input) {
		    case 1: add(&total, M);
			        break;
		    case 2:	subtract(&total, M);
			        break;
		    case 3: multiply(&total, M);
			        break;
		    case 4:	divide(&total, M);
			        break;
		    case 5: power(&total, M);
			        break;
		    case 6:	total = square(total);
			        break;
		    case 7: square_rt(&total);
			        break;
		    case 8: total = pow((float)total, 0.333333);
			        break;
		    case 9: percent(&total);
			        break;
		    case 10: factorial(&total);
		             break;
		    case 11: total = sin(total);
		             break;
		    case 12: total = cos(total);
			         break;
		    case 13: total = tan(total);
			         break;
		    case 14: log_10(&total);
			         break;
		    case 15: total = exp(total);
			         break;
    		case 16: M += total;
    		         break;
		    case 17: M -= total;
			         break;
    		case 18: M = 0;
    		         break;
		    case 19: total = 0;
			         break;
		    case 20: break;
		}
	}while(input != 20);
	
	return 0;
}
Esempio n. 3
0
int cwt_vsnprintf(CWT_CHAR *string, size_t length, const CWT_CHAR *format, va_list args)
{
	struct DATA data;
	CWT_CHAR conv_field[MAX_FIELD];
	double d; /* temporary holder */
	int state;
	int i;

	data.length = length - 1; /* leave room for '\0' */
	data.holder = string;
	data.pf = format;
	data.counter = 0;

	/* sanity check, the string must be > 1 */
	if (length < 1)
		return -1;


	for (; *data.pf && (data.counter < data.length); data.pf++) {
		if ( *data.pf == _T('%') ) { /* we got a magic % cookie */
			conv_flag((CWT_CHAR *)0, &data); /* initialise format flags */

			for (state = 1; *data.pf && state;) {
				switch (*(++data.pf)) {
				case _T('\0'): /* a NULL here ? ? bail out */
            		*data.holder = _T('\0');
					return data.counter;
					break;

				case _T('f'):  /* float, double */
            		STAR_ARGS(&data);
					d = va_arg(args, double);
					floating(&data, d);
					state = 0;
					break;

				case _T('g'):
				case _T('G'):
					STAR_ARGS(&data);
					DEF_PREC(&data);
					d = va_arg(args, double);
					i = log_10(d);

					/* for '%g|%G' ANSI: use f if exponent
					 * is in the range or [-4,p] exclusively
					 * else use %e|%E
					 */
					if (-4 < i && i < data.precision)
						floating(&data, d);
					else
						exponent(&data, d);
					state = 0;
					break;

				case _T('e'):
				case _T('E'):  /* Exponent double */
					STAR_ARGS(&data);
					d = va_arg(args, double);
					exponent(&data, d);
					state = 0;
					break;

				case _T('u'):
				case _T('d'):  /* decimal */
					STAR_ARGS(&data);
					if (data.a_long == FOUND)
						d = va_arg(args, long);
					else
						d = va_arg(args, int);
					decimal(&data, d);
					state = 0;
					break;

				case _T('o'):  /* octal */
					STAR_ARGS(&data);
					if (data.a_long == FOUND)
						d = va_arg(args, long);
					else
						d = va_arg(args, int);
					octal(&data, d);
					state = 0;
					break;

				case _T('x'):
				case _T('X'):  /* hexadecimal */
					STAR_ARGS(&data);
				  	if (data.a_long == FOUND)
				  		d = va_arg(args, long);
				  	else
				  		d = va_arg(args, int);
				  	hexa(&data, d);
				  	state = 0;
				  	break;

				case _T('c'): /* character */
					d = va_arg(args, int);
					PUT_CHAR((CWT_CHAR)d, &data);
					state = 0;
					break;

				case _T('s'):  /* string */
					STAR_ARGS(&data);
					strings(&data, va_arg(args, CWT_CHAR *));
					state = 0;
					break;

				case _T('n'):
					*(va_arg(args, int *)) = data.counter; /* what's the count ? */
				  	state = 0;
				  	break;

				case _T('l'):
					data.a_long = FOUND;
					break;

				case _T('h'):
					break;

				case _T('%'):  /* nothing just % */
					PUT_CHAR(_T('%'), &data);
					state = 0;
					break;

				case _T('#'): case _T(' '): case _T('+'): case _T('*'):
				case _T('-'): case _T('.'): case _T('0'): case _T('1'):
				case _T('2'): case _T('3'): case _T('4'): case _T('5'):
				case _T('6'): case _T('7'): case _T('8'): case _T('9'):
					/* initialize width and precision */
					for (i = 0; isflag(*data.pf); i++, data.pf++)
						if (i < MAX_FIELD - 1)
							conv_field[i] = *data.pf;

					conv_field[i] = _T('\0');
					conv_flag(conv_field, &data);
					data.pf--;   /* went to far go back */
					break;

				default:
					/* is this an error ? maybe bail out */
					state = 0;
					break;
				} /* end switch */
Esempio n. 4
0
/* %e %E %g exponent representation */
static void exponent(struct DATA *p, double d)
{
	CWT_CHAR *tmp, *tmp2;
	int j, i;

	DEF_PREC(p);
	j = log_10(d);
	d = d / pow_10(j);  /* get the Mantissa */
	d = ROUND(d, p);
	tmp = dtoa(d, p->precision, &tmp2);

	/* 1 for unit, 1 for the '.', 1 for 'e|E',
	 * 1 for '+|-', 3 for 'exp' */
	/* calculate how much padding need */
	p->width = p->width -
			((d > 0. && p->justify == RIGHT) ? 1:0) -
			((p->space == FOUND) ? 1:0) - p->precision - 7;

	PAD_RIGHT(p);
	PUT_PLUS(d, p);
	PUT_SPACE(d, p);

	while (*tmp) {/* the integral */
		PUT_CHAR(*tmp, p);
		tmp++;
	}

	if (p->precision != 0 || p->square == FOUND)
		PUT_CHAR(_T('.'), p);  /* the '.' */

	if (*p->pf == _T('g') || *p->pf == _T('G')) /* smash the trailing zeros */
		for (i = ((int)cwt_str_ns()->strLen(tmp2)) - 1; i >= 0 && tmp2[i] == _T('0'); i--)
			tmp2[i] = _T('\0');

	for (; *tmp2; tmp2++)
		PUT_CHAR(*tmp2, p); /* the fraction */

	if (*p->pf == _T('g') || *p->pf == _T('e')) { /* the exponent put the 'e|E' */
		PUT_CHAR(_T('e'), p);
	} else {
		PUT_CHAR(_T('E'), p);
	}

	if (j > 0) {  /* the sign of the exp */
		PUT_CHAR(_T('+'), p);
	} else {
		PUT_CHAR(_T('-'), p);
		j = -j;
	}

	tmp = itoa((double)j);

	if (j < 9) {  /* need to pad the exponent with 0 '000' */
		PUT_CHAR(_T('0'), p);
		PUT_CHAR(_T('0'), p);
	} else if (j < 99) {
		PUT_CHAR(_T('0'), p);
	}

	while (*tmp) { /* the exponent */
		PUT_CHAR(*tmp, p);
		tmp++;
	}
	PAD_LEFT(p);
}