Exemplo n.º 1
0
const char *CTabFmt::NumFmt(ColFmt &col, const char *szDat, char *szOut, int cbOut, double *rVal)
{
	int i, len;
	double val;
	const char *p;

	if (szDat && ((col.dec > -1) || col.sum))
		val = strtodx(szDat);
	else
		val = 0.0;

	*szOut++ = '\0'; --cbOut;

	if ((col.dec > -1) || col.sum) {
		if (col.dec > -1) {
			if (val == 0.0) {
				if (col.zer == '-') {
					len = _snprintf(szOut, cbOut, "%*s", col.dec+2, "-");
				} else if (col.zer == ' ' || col.zer == '_') {
					len = 0;  *szOut = '\0';
				} else {
					len = _snprintf(szOut, cbOut, "%.*f", col.dec, val);
				}
				if (col.dch && col.dch != '.') {
					char *n;
					if ((n = strchr(szOut, '.'))) {
						*n = col.dch;
					}
				}
				p = szOut;
			} else {
				len = _snprintf(szOut, cbOut, "%.*f", col.dec, val);

				if (col.dec > 0 && col.trz) {
					int t = col.dec - col.trz + 1;
					p = szOut + len - 1;
					while (t-- > 0 && *p == '0' && *p != '.')
						((char&) *p--) = ' ';
				}

				if (fabs(val) >= 1000.0) {
					char *n = szOut + cbOut - 1;
					*n-- = '\0';
					p = szOut + len - 1;

					if (col.dec > 0)
						while ((*n-- = *p--) != '.');
					else if (*p == '.')
						--p;

					if (*n && n[1] == '.' && col.dch)
						n[1] = col.dch;

					if (*p) {
						*n-- = *p--;
						i = 0;
						while (*p) {
							if (col.tho && isdigit(*p)) {
								++i;
								if (!(i % 3)) *n--=col.tho;
							}
							*n-- = *p--;
						}
					}
					p = n + 1;
				} else {
					if (col.dch && col.dch != '.') {
						char *n;
						if ((n = strchr(szOut, '.'))) {
							*n = col.dch;
						}
					}
					p = szOut;
				}
			}
		} else {
			p = szDat;
		}
	} else {
		p = szDat;
	}

	*rVal = val;

	return p;
}
Exemplo n.º 2
0
int getlex ()			/* ввод лексемы */
{
	/* побочный эффект - заполнение lexval и lexsym */
	register int c, i, radix = 10, neg = 0;

	if (backlexflag) {
		backlexflag = 0;
		return (lexlex);
	}
loop:
	switch (c = getchar ()) {
	case EOF:
		return (0);
	case ';':
		while ((c = getchar ()) >= 0 && c!='\n')
			continue;
	case ' ': case '\t': case '\f': case '\n': case '\r':
		goto loop;
	case '"':
		lexlen = 0;
		while ((c = getchar ()) >= 0 && c!='"') {
			if (c == '\\') {
				c = getchar ();
				if (c < 0)
					break;
				switch (c) {
				case '\n': continue;
				case 'n': c = '\n'; break;
				case 't': c = '\t'; break;
				case 'r': c = '\r'; break;
				case 'b': c = '\b'; break;
				case 'f': c = '\f'; break;
				case '0': case '1': case '2': case '3':
				case '4': case '5': case '6': case '7':
					c = getoct (c);
					break;
				}
			}
			lexsym[lexlen++] = c;
		}
		return (lexlex = TSTRING);
	case '#':
		c = getchar ();
		switch (c) {
		default: ungetc (c, stdin); return (lexlex = '#');
		case '(': return (lexlex = TVECTOR);
		case '\\': lexval = getchr ();  return (lexlex = TCHAR);
		case 't': case 'T': lexval = 1; return (lexlex = TBOOL);
		case 'f': case 'F': lexval = 0; return (lexlex = TBOOL);
		case 'b': case 'B': radix = 2; break;
		case 'o': case 'O': radix = 8; break;
		case 'd': case 'D': radix = 10; break;
		case 'x': case 'X': radix = 16; break;
		}
		c = getchar ();
		if (! isdigitx (c, radix) && c!='-' && c!='+' && c!='.') {
			ungetc (c, stdin);
			lexval = 0;
			return (lexlex = TINTEGER);
		}
		break;
	}
	if (c=='-' || c=='+') {
		lexsym[0] = c;
		c = getchar ();
		if (! isdigitx (c, radix)) {
			ungetc (c, stdin);
			c = lexsym[0];
			goto lexsym;
		}
		neg = (lexsym[0] == '-');
	}
	if (isdigitx (c, radix) || c=='.') {
		i = 0;
		while (i < MAXLEX-1 && c>=0 && isdigitx (c, radix)) {
			lexsym[i++] = c;
			c = getchar ();
		}
		if ((c=='.' || c=='E' || c=='e' || c=='S' ||
		    c=='s' || c=='F' || c=='f' || c=='D' ||
		    c=='d' || c=='L' || c=='l') && i<MAXLEX-1) {
			if (c=='.') {
				lexsym[i++] = c;
				c = getchar ();
				while (i < MAXLEX-1 && c>=0 && isdigitx (c, radix)) {
					lexsym[i++] = c;
					c = getchar ();
				}
			}
			if ((c=='E' || c=='e' || c=='S' || c=='s' ||
			    c=='F' || c=='f' || c=='D' || c=='d' ||
			    c=='L' || c=='l') && i<MAXLEX-1) {
				lexsym[i++] = c;
				c = getchar ();
				if ((c=='+' || c=='-') && i<MAXLEX-1) {
					lexsym[i++] = c;
					c = getchar ();
				}
				while (i < MAXLEX-1 && c>=0 && isdigitx (c, radix)) {
					lexsym[i++] = c;
					c = getchar ();
				}
			}
			if (c >= 0)
				ungetc (c, stdin);
			lexsym[i] = 0;
			lexrealval = strtodx (lexsym, 0, radix);
			if (neg)
				lexrealval = -lexrealval;
			return (lexlex = TREAL);
		}
		if (c >= 0)
			ungetc (c, stdin);
		lexsym[i] = 0;
		lexval = strtol (lexsym, 0, radix);
		if (neg)
			lexval = -lexval;
		return (lexlex = TINTEGER);
	}
	if (isletterx (c)) {
lexsym:         i = 0;
		while (i < MAXLEX-1) {
			lexsym[i++] = c;
			c = getchar ();
			if (c < 0)
				break;
			if (! isletterx (c)) {
				ungetc (c, stdin);
				break;
			}
		}
		if (i == 1 && lexsym[0] == '.')
			return (lexlex = '.');
		lexsym[i] = 0;
		return (lexlex = TSYMBOL);
	}
	return (lexlex = c);
}