예제 #1
0
const std::string SM_Util::show_size ( const int &x )
{
    if( x <= 0 )
        return "";

    /* Bytes */
    if( x < 1024 )
        return itoa( x ) + " B";

    /* KBytes */
    else if( x >= 1024 && x < 1048576 )
    {
        double n = ( double ) x / 1024;
        return dtoa( n ) + " KB";
    }

    /* MBytes */
    else if( x >= 1048576 && x < 1073741824 )
    {
        double n = ( double ) x / 1048576;
        return dtoa( n ) + " MB";
    }

    /* GBytes */
    else
    {
        double n = ( double ) x / 1073741824;
        return dtoa( n ) + " GB";
    }
}
예제 #2
0
bool dtoa(double v, char decimal_point, Result& result, std::true_type)
{
    if (v == 0)
    {
        result.push_back('0');
        result.push_back('.');
        result.push_back('0');
        return true;
    }

    int length = 0;
    int k;

    char buffer[100];

    double u = std::signbit(v) ? -v : v;
    if (jsoncons::detail::grisu3(u, buffer, &length, &k))
    {
        if (std::signbit(v))
        {
            result.push_back('-');
        }
        // min exp: -4 is consistent with sprintf
        // max exp: std::numeric_limits<double>::max_digits10
        jsoncons::detail::prettify_string(buffer, length, k, -4, std::numeric_limits<double>::max_digits10, result);
        return true;
    }
    else
    {
        return dtoa(v, decimal_point, result, std::false_type());
    }
}
예제 #3
0
std::string dtoa(double value) {
    std::string data;
    data.resize(25);
    auto end = dtoa(value, const_cast<char*>(data.data()));
    data.resize(end - data.data());
    return data;
}
예제 #4
0
bool CFYSPrintDoc::WriteXmlStreamHeader(CString& strXmlPath)
{
	CString strStreamHdr = strXmlPath + STREAMHDR_XML;
	FILE* op = NULL;
	errno_t err = fopen_s(&op, strStreamHdr, "wb");
	if (!op || err != 0)
	{
		SetError(String("Failed to open %s.", strStreamHdr));
		return false;
	}
	fprintf(op, "<?xml version='1.0' encoding='UTF-8'?>\r\n");
	fprintf(op, "<!-- Confidential and Proprietory Information of 4YourSoul.com Inc..-->\r\n");
	fprintf(op, "<FYS_DataStreamHeader Version='1' ");
	fprintf(op, "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>\r\n");

	CString strCnPXmlFile = strXmlPath + CREATEPRINT_XML;
	DWORD dwSize = FileSize(strCnPXmlFile);
	DWORD dwFileSize = dwSize;
	for (int i=0; i<m_arFileSpecs.m_nSize; i++)
		dwFileSize += m_arFileSpecs[i]->dwSize;

	fprintf(op, "\t<Files Count='%s' TotalSize='%s'>\r\n", dtoa(m_arFileSpecs.m_nSize+1, 10), dtoa(dwFileSize, 10));
	DWORD dwchkSum = 1;
	DWORD dwoffSet = 0;
	CString strTmp;

	dwFileSize = dwSize;
	CCRC32::FileCrc32Assembly(strCnPXmlFile, dwchkSum);
	strTmp.Format("%X", dwchkSum);
	fprintf(op, "\t\t\t<File Type='DataEnvelope' Name='CreateAndPrint.xml' Size='%s' CRC32='%s' StartOffset='0' />\r\n",
				dtoa(dwFileSize,0), strTmp);

	dwoffSet += dwFileSize;
	for (int i=0; i<m_arFileSpecs.m_nSize; i++)
	{
		strTmp.Format("%X", m_arFileSpecs[i]->dwCRC32);
		fprintf(op, "\t\t\t<File Type='%s' Name='%s' Size='%s' CRC32='%s' StartOffset='%s' />\r\n",
					m_arFileSpecs[i]->Type, m_arFileSpecs[i]->FileName, dtoa(m_arFileSpecs[i]->dwSize,0), strTmp, dtoa(dwoffSet,0));
		dwoffSet += m_arFileSpecs[i]->dwSize;
	}

	fprintf(op, "\t</Files>\r\n");
	fprintf(op, "</FYS_DataStreamHeader>\r\n");
	fclose(op);

	return true;
}
예제 #5
0
size_t ma_fcvt(double x, int precision, char *to, my_bool *error)
{
  int decpt, sign, len, i;
  char *res, *src, *end, *dst= to;
  char buf[DTOA_BUFF_SIZE];
  DBUG_ASSERT(precision >= 0 && precision < NOT_FIXED_DEC && to != NULL);
  
  res= dtoa(x, 5, precision, &decpt, &sign, &end, buf, sizeof(buf));

  if (decpt == DTOA_OVERFLOW)
  {
    dtoa_free(res, buf, sizeof(buf));
    *to++= '0';
    *to= '\0';
    if (error != NULL)
      *error= TRUE;
    return 1;
  }

  src= res;
  len= end - src;

  if (sign)
    *dst++= '-';

  if (decpt <= 0)
  {
    *dst++= '0';
    *dst++= '.';
    for (i= decpt; i < 0; i++)
      *dst++= '0';
  }

  for (i= 1; i <= len; i++)
  {
    *dst++= *src++;
    if (i == decpt && i < len)
      *dst++= '.';
  }
  while (i++ <= decpt)
    *dst++= '0';

  if (precision > 0)
  {
    if (len <= decpt)
      *dst++= '.';
    
    for (i= precision - MAX(0, (len - decpt)); i > 0; i--)
      *dst++= '0';
  }
  
  *dst= '\0';
  if (error != NULL)
    *error= FALSE;

  dtoa_free(res, buf, sizeof(buf));

  return dst - to;
}
예제 #6
0
void Function::graph(void)
{
	char s[32];
	
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0.0, 1.0, 0.0);
	drawAxis();

	glRasterPos2f(-1, 1.3);
	text("Graph init funtion");

	glRasterPos2f(x.back(), 0.1);
	text("X");
	glRasterPos2f(-0.2, y.back());
	glFlush();
	
	for(auto iterx = begin(x), itery = begin(y);
		   	iterx != end(x), itery != end(y);
		   	iterx++, itery++)
	{
		glRasterPos2f(0.0, *itery);
		text("-");
		glRasterPos2f(0.004, *itery);
		text(dtoa(s, *itery));

		glRasterPos2f(*iterx, -0.01);
		text("|");
		glRasterPos2f(*iterx, 0.009);
		text(dtoa(s, *iterx));
	}
	glFlush();

	glColor3f(1.0, 0.0, 0.0);
	glBegin(GL_LINE_STRIP);
		for(auto iterx = begin(x), itery = begin(y); 
				iterx != end(x), itery != end(y);
			   	iterx++, itery++)
		{
			glVertex2d(*iterx, *itery);
		}
	glEnd();
	glFlush();
}
예제 #7
0
static int __attribute__((__noinline__)) ap2double(char *buf,char type,int width, int trunc, va_list ap)
{
	double dbl;

#ifndef NEWFP
	//	EP ex;
#endif
	dbl = va_arg(ap, double);
	dtoa (buf, dbl, type, width, trunc);
	trunc = 0;
}
예제 #8
0
파일: ldtoa.c 프로젝트: 0-T-0/linux-sgx
char *
__ldtoa(long double *ld, int mode, int ndigits, int *decpt, int *sign,
    char **rve)
{
	char *ret;

	ret = dtoa((double)*ld, mode, ndigits, decpt, sign, rve);
	if (*decpt == 9999)
		*decpt = INT_MAX;
	return ret;
}
예제 #9
0
char *dtoa(unsigned long num)
{//  123 "123"
	int i = 0;
	if(num < 10){
		U_tmp_buf[0] = num + '0';
		U_tmp_buf[1] = '\0';
	} else {
		dtoa(num / 10);
		while(U_tmp_buf[i]){
			i++;
		}
		U_tmp_buf[i] = num % 10 + '0';
		U_tmp_buf[i + 1] = '\0';
	}
	return U_tmp_buf;
}
예제 #10
0
파일: reals.c 프로젝트: Pachot/mlworks
static mlval decimal_rep (mlval arg)
{
  int dec;
  int sign;
  char * digits;
  mlval result;
  digits = dtoa (GETREAL(arg),0,100,&dec,&sign,NULL);
  root = allocate_string (strlen(digits) + 1);
  strcpy (CSTRING(root),digits);
  freedtoa (digits);
  result = allocate_record (3);
  FIELD (result,0) = root;
  FIELD (result,1) = MLINT (dec);
  FIELD (result,2) = sign ? MLTRUE : MLFALSE;
  return (result);
}
예제 #11
0
파일: snprintf.c 프로젝트: semenovf/cwt
/* %f or %g  floating point representation */
static void floating(struct DATA *p, double d)
{
	CWT_CHAR *tmp, *tmp2;
	int i;

	DEF_PREC(p);
	d = ROUND(d, p);
	tmp = dtoa(d, p->precision, &tmp2);

	/* calculate the padding. 1 for the dot */
	p->width = p->width -
			((d > 0. && p->justify == RIGHT) ? 1:0) -
            ((p->space == FOUND) ? 1:0) -
            ((int)cwt_str_ns()->strLen(tmp)) - p->precision - 1;

	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);  /* put 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 */
  
	PAD_LEFT(p);
} 
예제 #12
0
파일: word.c 프로젝트: tyru/forth-interp
// evaluate each member and set result to word->tokstr.
// NOTE: word->type and its value must be set.
void
forth_uneval_word(ForthInterp *interp, ForthWord *word)
{
    char tmp[interp->max_word_len];    // c99

    if (word->tokstr.str != NULL) return;


    if (word->type == WORD_DIGIT) {
        ASSERT(interp, word->digitval.is_set);

        if (! dtoa(word->digitval.digit, tmp, interp->max_word_len, 10))
            forth_die(interp, "dtoa", FORTH_ERR_CONVERT_FAILED);

        word_set_tokstr_copy(word, tmp);
    }
    else if (word->type == WORD_STRING) {
        // TODO
    }
    else if (word->type == WORD_FUNC) {
        forth_error(interp, "tried to convert word func to string.", FORTH_ERR_CONVERT_FAILED);

        // no strict? (in Perl)
        // word_set_str_copy(uneval, WORD_FUNC_STR);
    }
    else if (word->type == WORD_UNDEF) {
        forth_error(interp, "tried to convert undefined word to string.", FORTH_ERR_CONVERT_FAILED);

        // no strict? (in Perl)
        // word_set_str_copy(uneval, WORD_FUNC_STR);
    }
    else {
        // never reach this block
        ASSERT(interp, 0);
    }
}
예제 #13
0
bool CFYSPrintDoc::GetResourcesElement()
{
	bool bVal = AllocateHeap((DWORD)&m_pRes, "	<Resources>\r\n");
	if (!m_pRes)
	{
		SetError("Failed to allocate memory for Resources element.");
		return false;
	}

	for (int i=0; i<m_arFileSpecs.GetSize(); i++)
	{
		CString strTemp;

		if (m_arFileSpecs[i]->Type != "Font")
			continue;

		strTemp.Format("		<Font FontName='%s' FileName='%s' FileSize='%s' />\r\n",
					m_arFileSpecs[i]->Name, m_arFileSpecs[i]->FileName, dtoa(m_arFileSpecs[i]->dwSize, 0));

		bVal = AppendStringToHeap((DWORD)&m_pRes, strTemp);
	}
	bVal = AppendStringToHeap((DWORD)&m_pRes, "	</Resources>\r\n");
	return true;
}
예제 #14
0
bool dtoa(double v, char decimal_point, Result& result)
{
    return dtoa(v, decimal_point, result, std::integral_constant<bool,std::numeric_limits<double>::is_iec559>());
}
예제 #15
0
inline std::string toString(long double num) {
    return dtoa(num);
}
예제 #16
0
 char *
g_fmt(register char *b, double x)
{
	register int i, k;
	register char *s;
	int decpt, j, sign;
	char *b0, *s0, *se;

	b0 = b;
#ifdef IGNORE_ZERO_SIGN
	if (!x) {
		*b++ = '0';
		*b = 0;
		goto done;
		}
#endif
	s = s0 = dtoa(x, 0, 0, &decpt, &sign, &se);
	if (sign)
		*b++ = '-';
	if (decpt == 9999)  {		// Infinity or Nan
		while(*b++ = *s++);
		goto done0;
		}
	if (decpt <= -4 || decpt > se - s + 5) {
		*b++ = *s++;
		if (*s) {
			*b++ = '.';
			while(*b = *s++)
				b++;
			}
		*b++ = 'e';
		// sprintf(b, "%+.2d", decpt - 1);
		if (--decpt < 0) {
			*b++ = '-';
			decpt = -decpt;
			}
		else
			*b++ = '+';
		for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10);
		for(;;) {
			i = decpt / k;
			*b++ = i + '0';
			if (--j <= 0)
				break;
			decpt -= i*k;
			decpt *= 10;
			}
		*b = 0;
		}
	else if (decpt <= 0) {
		*b++ = '.';
		for(; decpt < 0; decpt++)
			*b++ = '0';
		while(*b++ = *s++);
		}
	else {
		while(*b = *s++) {
			b++;
			if (--decpt == 0 && *s)
				*b++ = '.';
			}
		for(; decpt > 0; decpt--)
			*b++ = '0';
		*b = 0;
		}
 done0:
	freedtoa(s0);
 done:
	return b0;
	}
예제 #17
0
char *
format_float(double f, char *buf)
{ char *end, *o=buf;
  int decpt, sign;
  char *s = dtoa(f, 0, 30, &decpt, &sign, &end);

  DEBUG(2, Sdprintf("decpt=%d, sign=%d, len = %d, '%s'\n",
		    decpt, sign, end-s, s));

  if ( sign )
    *o++ = '-';

  if ( decpt <= 0 )			/* decimal dot before */
  { if ( decpt <= -4 )
    { *o++ = s[0];
      *o++ = '.';
      if ( end-s > 1 )
      { memcpy(o, s+1, end-s-1);
	o += end-s-1;
      } else
	*o++ = '0';
      sprintf(o, "e%d", decpt-1);
    } else
    { int i;

      *o++ = '0';
      *o++ = '.';
      for(i=0; i < -decpt; i++)
	*o++ = '0';
      memcpy(o, s, end-s);
      o[end-s] = 0;
    }
  } else if ( end-s > decpt )		/* decimal dot inside */
  { memcpy(o, s, decpt);
    o += decpt;
    *o++ = '.';
    memcpy(o, s+decpt, end-s-decpt);
    o[end-s-decpt] = 0;
  } else				/* decimal dot after */
  { int i;
    int trailing = decpt-(int)(end-s);

    if ( decpt > 15 )			/* over precision: use eE */
    { *o++ = s[0];
      *o++ = '.';
      if ( end-s > 1 )
      { trailing += (int)(end-s)-1;
	memcpy(o, s+1, end-s-1);
	o += end-s-1;
      } else
	*o++ = '0';
      sprintf(o, "e+%d", trailing);
    } else				/* within precision trail with .0 */
    { memcpy(o, s, end-s);
      o += end-s;

      for(i=(int)(end-s); i<decpt; i++)
	*o++ = '0';
      *o++ = '.';
      *o++ = '0';
      *o = 0;
    }
  }

  freedtoa(s);

  return buf;
}
예제 #18
0
//Fileinput
int Gene::Interpreter(string DataName, string& DataItem, bool Input) {
	int DataID = TranslateFileHeader(DataName,GENE);
	
	if (DataID == -1) {
		AddData(DataName.data(),DataItem.data(),STRING);
		//FErrorFile() << "UNRECOGNIZED REFERENCE: " << GetData("FILENAME",STRING) << " data reference: " << DataName << " not recognized." << endl;
		//FlushErrorFile();
		return FAIL;
	}
	
	switch (DataID) {
		case GENE_DBLINK: {
			if (Input) {
				AddData(DataName.data(),DataItem.data(),DATABASE_LINK);
			} else {
				DataItem = GetAllDataString(DataName.data(),DATABASE_LINK);
			}
			break;
		} case GENE_COORD: {
			if (Input) {
				vector<string>* Strings = StringToStrings(DataItem,".");
				if (Strings->size() >= 2) {
					AddData("START COORD",atof((*Strings)[0].data()));
					AddData("END COORD",atof((*Strings)[1].data()));
				}
				delete Strings;
			} else {
				double Start = GetDoubleData("START COORD");
				double End = GetDoubleData("END COORD");
				if (Start != FLAG && End != FLAG) {
					DataItem.assign(dtoa(Start));
					DataItem.append("..");
					DataItem.append(dtoa(End));

				}
			}
			break;
		} case GENE_REACTION: {
			if (Input) {
				//Do nothing... reactions will add themselves to the genes
			} else {
				for (int i=0; i < int(ReactionList.size()); i++) {
					DataItem.append(ReactionList[i]->GetData("DATABASE",STRING));
					if (i < int(ReactionList.size()-1)) {
						DataItem.append("\t");
					}
				}
			}
			break;
		} case GENE_PARALOG: {
			if (Input) {
				vector<string>* Strings = StringToStrings(DataItem,"\t ");
				for (int i=0; i < int(Strings->size()); i += 2) {
					if (AddData("PARALOGS",(*Strings)[i].data(),STRING)) {;
						AddData("PARA SIMS",atof((*Strings)[i+1].data()),false);
					}
				}
				delete Strings;
			} else {
				vector<string> AllParalogs = GetAllData("PARALOGS",STRING);
				vector<double> AllSims = GetAllData("PARA SIMS");
				for (int i=0; i < int(AllParalogs.size()); i++) {
					DataItem.append(AllParalogs[i]);
					DataItem.append(" ");
					DataItem.append(dtoa(AllSims[i]));
					if (i < int(AllParalogs.size()-1)) {
						DataItem.append("\t");
					}
				}
			}
			break;
		} case GENE_ORTHOLOG: {
			if (Input) {
				vector<string>* Strings = StringToStrings(DataItem,"\t ");
				for (int i=0; i < int(Strings->size()); i += 2) {
					if (AddData("ORTHOLOGS",(*Strings)[i].data(),STRING)) {;
						AddData("ORTHO SIMS",atof((*Strings)[i+1].data()),false);
					}
				}
				delete Strings;
			} else {
				vector<string> AllParalogs = GetAllData("ORTHOLOGS",STRING);
				vector<double> AllSims = GetAllData("ORTHO SIMS");
				for (int i=0; i < int(AllParalogs.size()); i++) {
					DataItem.append(AllParalogs[i]);
					DataItem.append(" ");
					DataItem.append(dtoa(AllSims[i]));
					if (i < int(AllParalogs.size()-1)) {
						DataItem.append("\t");
					}
				}
			}
			break;
		} case GENE_DOUBLE: {
			if (Input) {
				AddData(DataName.data(),DataItem.data(),DOUBLE);
			} else {
				DataItem = GetAllDataString(DataName.data(),DOUBLE);
			}
			break;
		} case GENE_STRING: {
			if (Input) {
				AddData(DataName.data(),DataItem.data(),STRING);
			} else {
				DataItem = GetAllDataString(DataName.data(),STRING);
			}
			break;
		} case GENE_QUERY: {
			if (Input) {
				if (GetParameter("save query data on input").compare("1") == 0) {
					AddData(DataName.data(),DataItem.data(),STRING);
				}
			} else {
				DataItem = Query(DataName);
			}
			break;
		} case GENE_LOAD: {
			if (Input) {
				LoadGene(DataItem);			
			} else {
				DataItem = GetData("DATABASE",STRING);
			}
			break;
		} default: {
			//FErrorFile() << "UNRECOGNIZED DATA ID: Data ID: " << DataID << " input for data reference: " << DataName << " not recognized. Check gene code.";
			//FlushErrorFile();
			return FAIL;
		}
	}	
	
	return SUCCESS;
}
예제 #19
0
void cSetup::Store(const char *Name, double &Value, const char *Plugin)
{
  Store(Name, dtoa(Value), Plugin);
}
예제 #20
0
파일: f-math.c 프로젝트: Tectorum/rebol
REBINT Emit_Decimal(REBYTE *cp, REBDEC d, REBFLG trim, REBYTE point, REBINT decimal_digits) {
	REBYTE *start = cp, *sig, *rve;
	REBINT e, sgn, digits_obtained;

	/* sanity checks */
	if (decimal_digits < MIN_DIGITS) decimal_digits = MIN_DIGITS;
	else if (decimal_digits > MAX_DIGITS) decimal_digits = MAX_DIGITS;

	sig = (REBYTE *) dtoa (d, 0, decimal_digits, &e, &sgn, (char **) &rve);

	digits_obtained = rve - sig;

	/* handle sign */
	if (sgn) *cp++ = '-';

	if (trim == DEC_MOLD_PERCENT) e += 2;

	if ((e > decimal_digits) || (e <= -6)) {
		/* e-format */
		*cp++ = *sig++;

		/* insert the radix point */
		*cp++ = point;

		/* insert the rest */
		memcpy(cp, sig, digits_obtained - 1);
		cp += digits_obtained - 1;
	} else if (e > 0) {
		if (e <= digits_obtained) {
			/* insert digits preceding point */
			memcpy (cp, sig, e);
			cp += e;
			sig += e;

			*cp++ = point;

			/* insert digits following point */
			memcpy(cp, sig, digits_obtained -  e);
			cp += digits_obtained - e;
		} else {
			/* insert all digits obtained */
			memcpy (cp, sig, digits_obtained);
			cp += digits_obtained;

			/* insert zeros preceding point */
			memset (cp, '0', e - digits_obtained);
			cp += e - digits_obtained;

			*cp++ = point;
		}
		e = 0;
	} else {
		*cp++ = '0';

		*cp++ = point;

		memset(cp, '0', -e);
		cp -= e;

		memcpy(cp, sig, digits_obtained);
		cp += digits_obtained;

		e = 0;
	}

	// Add at least one zero after point (unless percent or pair):
	if (*(cp - 1) == point) {if (trim) cp--; else *cp++ = '0';}

	// Add E part if needed:
	if (e) {
		*cp++ = 'e';
		INT_TO_STR(e - 1, cp);
		cp = strchr(cp, 0);
	}

 	if (trim == DEC_MOLD_PERCENT) *cp++ = '%';
	*cp = 0;
	return cp - start;
}
예제 #21
0
char *
ldtoa(long double *ld, int mode, int prec, int *exp, int *sign, char **strEnd)
{
   double d = (double) *ld; // ghetto fabulous
   return dtoa(d, mode, prec, exp, sign, strEnd);
}
예제 #22
0
static inline jsstr_t *number_to_fixed(double val, int prec)
{
    WCHAR buf[NUMBER_DTOA_SIZE];
    int dec_point, size, buf_size, buf_pos;
    BOOL neg = FALSE;
    jsstr_t *ret;
    WCHAR *str;

    TRACE("%lf %d\n", val, prec);

    if(val < 0) {
        neg = TRUE;
        val = -val;
    }

    if(val >= 1)
        buf_size = log10(val)+prec+2;
    else
        buf_size = prec ? prec+1 : 2;
    if(buf_size > NUMBER_DTOA_SIZE)
        buf_size = NUMBER_DTOA_SIZE;

    dtoa(val, buf, buf_size, &dec_point);
    dec_point++;
    size = 0;
    if(neg)
        size++;
    if(dec_point > 0)
        size += dec_point;
    else
        size++;
    if(prec)
        size += prec+1;

    ret = jsstr_alloc_buf(size);
    if(!ret)
        return NULL;

    str = ret->str;
    size = buf_pos = 0;
    if(neg)
        str[size++] = '-';
    if(dec_point > 0) {
        for(;buf_pos<buf_size-1 && dec_point; dec_point--)
            str[size++] = buf[buf_pos++];
    }else {
        str[size++] = '0';
    }
    for(; dec_point>0; dec_point--)
        str[size++] = '0';
    if(prec) {
        str[size++] = '.';

        for(; dec_point<0 && prec; dec_point++, prec--)
            str[size++] = '0';
        for(; buf_pos<buf_size-1 && prec; prec--)
            str[size++] = buf[buf_pos++];
        for(; prec; prec--) {
            str[size++] = '0';
        }
    }
    str[size++] = 0;
    return ret;
}
예제 #23
0
static inline jsstr_t *number_to_exponential(double val, int prec)
{
    WCHAR buf[NUMBER_DTOA_SIZE], *pbuf;
    int dec_point, size, buf_size, exp_size = 1;
    BOOL neg = FALSE;
    jsstr_t *ret;
    WCHAR *str;

    if(val < 0) {
        neg = TRUE;
        val = -val;
    }

    buf_size = prec+2;
    if(buf_size<2 || buf_size>NUMBER_DTOA_SIZE)
        buf_size = NUMBER_DTOA_SIZE;
    dtoa(val, buf, buf_size, &dec_point);
    buf_size--;
    if(prec == -1)
        for(; buf_size>1 && buf[buf_size-1]=='0'; buf_size--)
            buf[buf_size-1] = 0;

    size = 10;
    while(dec_point>=size || dec_point<=-size) {
        size *= 10;
        exp_size++;
    }

    if(buf_size == 1)
        size = buf_size+2+exp_size; /* 2 = strlen(e+) */
    else if(prec == -1)
        size = buf_size+3+exp_size; /* 3 = strlen(.e+) */
    else
        size = prec+4+exp_size; /* 4 = strlen(0.e+) */
    if(neg)
        size++;

    ret = jsstr_alloc_buf(size);
    if(!ret)
        return NULL;

    str = ret->str;
    size = 0;
    pbuf = buf;
    if(neg)
        str[size++] = '-';
    str[size++] = *pbuf++;
    if(buf_size != 1) {
        str[size++] = '.';
        while(*pbuf)
            str[size++] = *pbuf++;
        for(; prec>buf_size-1; prec--)
            str[size++] = '0';
    }
    str[size++] = 'e';
    if(dec_point >= 0) {
        str[size++] = '+';
    }else {
        str[size++] = '-';
        dec_point = -dec_point;
    }
    size += exp_size;
    do {
        str[--size] = '0'+dec_point%10;
        dec_point /= 10;
    }while(dec_point>0);
    size += exp_size;
    str[size] = 0;

    return ret;
}
예제 #24
0
파일: g_fmt.c 프로젝트: 215010wk/skynet
int
fpconv_g_fmt(char *b, double x, int precision)
{
	register int i, k;
	register char *s;
	int decpt, j, sign;
	char *b0, *s0, *se;

	b0 = b;
#ifdef IGNORE_ZERO_SIGN
	if (!x) {
		*b++ = '0';
		*b = 0;
		goto done;
		}
#endif
	s = s0 = dtoa(x, 2, precision, &decpt, &sign, &se);
	if (sign)
		*b++ = '-';
	if (decpt == 9999) /* Infinity or Nan */ {
		while((*b++ = *s++));
		/* "b" is used to calculate the return length. Decrement to exclude the
		 * Null terminator from the length */
		b--;
		goto done0;
		}
	if (decpt <= -4 || decpt > precision) {
		*b++ = *s++;
		if (*s) {
			*b++ = '.';
			while((*b = *s++))
				b++;
			}
		*b++ = 'e';
		/* sprintf(b, "%+.2d", decpt - 1); */
		if (--decpt < 0) {
			*b++ = '-';
			decpt = -decpt;
			}
		else
			*b++ = '+';
		for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10);
		for(;;) {
			i = decpt / k;
			*b++ = i + '0';
			if (--j <= 0)
				break;
			decpt -= i*k;
			decpt *= 10;
			}
		*b = 0;
		}
	else if (decpt <= 0) {
		*b++ = '0';
		*b++ = '.';
		for(; decpt < 0; decpt++)
			*b++ = '0';
		while((*b++ = *s++));
		b--;
		}
	else {
		while((*b = *s++)) {
			b++;
			if (--decpt == 0 && *s)
				*b++ = '.';
			}
		for(; decpt > 0; decpt--)
			*b++ = '0';
		*b = 0;
		}
 done0:
	freedtoa(s0);
#ifdef IGNORE_ZERO_SIGN
 done:
#endif
	return b - b0;
	}
예제 #25
0
int
__printf_render_float(struct __printf_io* io, const struct printf_info* pi, const void* const* arg) {
    int prec;       /* precision from format; <0 for N/A */
    char* dtoaresult;   /* buffer allocated by dtoa */
    char expchar;       /* exponent character: [eEpP\0] */
    char* cp;
    int expt;       /* integer value of exponent */
    int signflag;       /* true if float is negative */
    char* dtoaend;      /* pointer to end of converted digits */
    char sign;      /* sign prefix (' ', '+', '-', or \0) */
    int size;       /* size of converted field or string */
    int ndig;       /* actual number of digits returned by dtoa */
    int expsize;        /* character count for expstr */
    char expstr[MAXEXPDIG + 2]; /* buffer for exponent string: e+ZZZ */
    int nseps;      /* number of group separators with ' */
    int nrepeats;       /* number of repeats of the last group */
    const char* grouping;   /* locale specific numeric grouping rules */
    int lead;       /* sig figs before decimal or group sep */
    long double ld;
    double d;
    int realsz;     /* field size expanded by dprec, sign, etc */
    int dprec;      /* a copy of prec if [diouxX], 0 otherwise */
    char ox[2];     /* space for 0x; ox[1] is either x, X, or \0 */
    int prsize;             /* max size of printed field */
    int ret;        /* return value accumulator */
    char* decimal_point;    /* locale specific decimal point */
    int n2;         /* XXX: for PRINTANDPAD */
    char thousands_sep; /* locale specific thousands separator */
    char buf[BUF];      /* buffer with space for digits of uintmax_t */
    const char* xdigs;
    int flag;
    prec = pi->prec;
    ox[1] = '\0';
    sign = pi->showsign;
    flag = 0;
    ret = 0;
    thousands_sep = *(localeconv()->thousands_sep);
    grouping = NULL;

    if (pi->alt) {
        grouping = localeconv()->grouping;
    }

    decimal_point = localeconv()->decimal_point;
    dprec = -1;

    switch (pi->spec) {
    case 'a':
    case 'A':
        if (pi->spec == 'a') {
            ox[1] = 'x';
            xdigs = __lowercase_hex;
            expchar = 'p';
        } else {
            ox[1] = 'X';
            xdigs = __uppercase_hex;
            expchar = 'P';
        }

        if (prec >= 0) {
            prec++;
        }

        if (pi->is_long_double) {
            ld = *((long double*)arg[0]);
            dtoaresult = cp =
                             __hldtoa(ld, xdigs, prec,
                                      &expt, &signflag, &dtoaend);
        } else {
            d = *((double*)arg[0]);
            dtoaresult = cp =
                             __hdtoa(d, xdigs, prec,
                                     &expt, &signflag, &dtoaend);
        }

        if (prec < 0) {
            prec = dtoaend - cp;
        }

        if (expt == INT_MAX) {
            ox[1] = '\0';
        }

        goto fp_common;

    case 'e':
    case 'E':
        expchar = pi->spec;

        if (prec < 0) { /* account for digit before decpt */
            prec = DEFPREC + 1;
        } else {
            prec++;
        }

        break;

    case 'f':
    case 'F':
        expchar = '\0';
        break;

    case 'g':
    case 'G':
        expchar = pi->spec - ('g' - 'e');

        if (prec == 0) {
            prec = 1;
        }

        break;

    default:
        assert(pi->spec == 'f');
    }

    if (prec < 0) {
        prec = DEFPREC;
    }

    if (pi->is_long_double) {
        ld = *((long double*)arg[0]);
        dtoaresult = cp =
                         __ldtoa(&ld, expchar ? 2 : 3, prec,
                                 &expt, &signflag, &dtoaend);
    } else {
        d = *((double*)arg[0]);
        dtoaresult = cp =
                         dtoa(d, expchar ? 2 : 3, prec,
                              &expt, &signflag, &dtoaend);

        if (expt == 9999) {
            expt = INT_MAX;
        }
    }

fp_common:

    if (signflag) {
        sign = '-';
    }

    if (expt == INT_MAX) {  /* inf or nan */
        if (*cp == 'N') {
            cp = (pi->spec >= 'a') ? "nan" : "NAN";
            sign = '\0';
        } else {
            cp = (pi->spec >= 'a') ? "inf" : "INF";
        }

        size = 3;
        flag = 1;
        goto here;
    }

    ndig = dtoaend - cp;

    if (pi->spec == 'g' || pi->spec == 'G') {
        if (expt > -4 && expt <= prec) {
            /* Make %[gG] smell like %[fF] */
            expchar = '\0';

            if (pi->alt) {
                prec -= expt;
            } else {
                prec = ndig - expt;
            }

            if (prec < 0) {
                prec = 0;
            }
        } else {
            /*
             * Make %[gG] smell like %[eE], but
             * trim trailing zeroes if no # flag.
             */
            if (!pi->alt) {
                prec = ndig;
            }
        }
    }

    if (expchar) {
        expsize = exponent(expstr, expt - 1, expchar);
        size = expsize + prec;

        if (prec > 1 || pi->alt) {
            ++size;
        }
    } else {
        /* space for digits before decimal point */
        if (expt > 0) {
            size = expt;
        } else { /* "0" */
            size = 1;
        }

        /* space for decimal pt and following digits */
        if (prec || pi->alt) {
            size += prec + 1;
        }

        if (grouping && expt > 0) {
            /* space for thousands' grouping */
            nseps = nrepeats = 0;
            lead = expt;

            while (*grouping != CHAR_MAX) {
                if (lead <= *grouping) {
                    break;
                }

                lead -= *grouping;

                if (*(grouping + 1)) {
                    nseps++;
                    grouping++;
                } else {
                    nrepeats++;
                }
            }

            size += nseps + nrepeats;
        } else {
            lead = expt;
        }
    }

here:
    /*
     * All reasonable formats wind up here.  At this point, `cp'
     * points to a string which (if not flags&LADJUST) should be
     * padded out to `width' places.  If flags&ZEROPAD, it should
     * first be prefixed by any sign or other prefix; otherwise,
     * it should be blank padded before the prefix is emitted.
     * After any left-hand padding and prefixing, emit zeroes
     * required by a decimal [diouxX] precision, then print the
     * string proper, then emit zeroes required by any leftover
     * floating precision; finally, if LADJUST, pad with blanks.
     *
     * Compute actual size, so we know how much to pad.
     * size excludes decimal prec; realsz includes it.
     */
    realsz = dprec > size ? dprec : size;

    if (sign) {
        realsz++;
    }

    if (ox[1]) {
        realsz += 2;
    }

    prsize = pi->width > realsz ? pi->width : realsz;

    /* right-adjusting blank padding */
    if (pi->pad != '0' && pi->left == 0) {
        ret += __printf_pad(io, pi->width - realsz, 0);
    }

    /* prefix */
    if (sign) {
        ret += __printf_puts(io, &sign, 1);
    }

    if (ox[1]) {    /* ox[1] is either x, X, or \0 */
        ox[0] = '0';
        ret += __printf_puts(io, ox, 2);
    }

    /* right-adjusting zero padding */
    if (pi->pad == '0' && pi->left == 0) {
        ret += __printf_pad(io, pi->width - realsz, 1);
    }

    /* leading zeroes from decimal precision */
    ret += __printf_pad(io, dprec - size, 1);

    if (flag) {
        ret += __printf_puts(io, cp, size);
    } else {
        /* glue together f_p fragments */
        if (!expchar) { /* %[fF] or sufficiently short %[gG] */
            if (expt <= 0) {
                ret += __printf_puts(io, "0", 1);

                if (prec || pi->alt) {
                    ret += __printf_puts(io, decimal_point, 1);
                }

                ret += __printf_pad(io, -expt, 1);
                /* already handled initial 0's */
                prec += expt;
            } else {
                PRINTANDPAD(cp, dtoaend, lead, 1);
                cp += lead;

                if (grouping) {
                    while (nseps > 0 || nrepeats > 0) {
                        if (nrepeats > 0) {
                            nrepeats--;
                        } else {
                            grouping--;
                            nseps--;
                        }

                        ret += __printf_puts(io, &thousands_sep, 1);
                        PRINTANDPAD(cp, dtoaend,
                                    *grouping, 1);
                        cp += *grouping;
                    }

                    if (cp > dtoaend) {
                        cp = dtoaend;
                    }
                }

                if (prec || pi->alt) {
                    ret += __printf_puts(io, decimal_point, 1);
                }
            }

            PRINTANDPAD(cp, dtoaend, prec, 1);
        } else {    /* %[eE] or sufficiently long %[gG] */
            if (prec > 1 || pi->alt) {
                buf[0] = *cp++;
                buf[1] = *decimal_point;
                ret += __printf_puts(io, buf, 2);
                ret += __printf_puts(io, cp, ndig - 1);
                ret += __printf_pad(io, prec - ndig, 1);
            } else { /* XeYYY */
                ret += __printf_puts(io, cp, 1);
            }

            ret += __printf_puts(io, expstr, expsize);
        }
    }

    /* left-adjusting padding (always blank) */
    if (pi->left) {
        ret += __printf_pad(io, pi->width - realsz, 0);
    }

    __printf_flush(io);

    if (dtoaresult != NULL) {
        freedtoa(dtoaresult);
    }

    return (ret);
}
예제 #26
0
파일: snprintf.c 프로젝트: semenovf/cwt
/* %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);
}
예제 #27
0
size_t ma_gcvt(double x, my_gcvt_arg_type type, int width, char *to,
               my_bool *error)
{
  int decpt, sign, len, exp_len;
  char *res, *src, *end, *dst= to, *dend= dst + width;
  char buf[DTOA_BUFF_SIZE];
  my_bool have_space, force_e_format;
  DBUG_ASSERT(width > 0 && to != NULL);
  
  /* We want to remove '-' from equations early */
  if (x < 0.)
    width--;

  res= dtoa(x, 4, type == MY_GCVT_ARG_DOUBLE ? width : MIN(width, FLT_DIG),
            &decpt, &sign, &end, buf, sizeof(buf));
  if (decpt == DTOA_OVERFLOW)
  {
    dtoa_free(res, buf, sizeof(buf));
    *to++= '0';
    *to= '\0';
    if (error != NULL)
      *error= TRUE;
    return 1;
  }

  if (error != NULL)
    *error= FALSE;

  src= res;
  len= end - res;

  /*
    Number of digits in the exponent from the 'e' conversion.
     The sign of the exponent is taken into account separetely, we don't need
     to count it here.
   */
  exp_len= 1 + (decpt >= 101 || decpt <= -99) + (decpt >= 11 || decpt <= -9);
  
  /*
     Do we have enough space for all digits in the 'f' format?
     Let 'len' be the number of significant digits returned by dtoa,
     and F be the length of the resulting decimal representation.
     Consider the following cases:
     1. decpt <= 0, i.e. we have "0.NNN" => F = len - decpt + 2
     2. 0 < decpt < len, i.e. we have "NNN.NNN" => F = len + 1
     3. len <= decpt, i.e. we have "NNN00" => F = decpt
  */
  have_space= (decpt <= 0 ? len - decpt + 2 :
               decpt > 0 && decpt < len ? len + 1 :
               decpt) <= width;
  /*
    The following is true when no significant digits can be placed with the
    specified field width using the 'f' format, and the 'e' format
    will not be truncated.
  */
  force_e_format= (decpt <= 0 && width <= 2 - decpt && width >= 3 + exp_len);
  /*
    Assume that we don't have enough space to place all significant digits in
    the 'f' format. We have to choose between the 'e' format and the 'f' one
    to keep as many significant digits as possible.
    Let E and F be the lengths of decimal representaion in the 'e' and 'f'
    formats, respectively. We want to use the 'f' format if, and only if F <= E.
    Consider the following cases:
    1. decpt <= 0.
       F = len - decpt + 2 (see above)
       E = len + (len > 1) + 1 + 1 (decpt <= -99) + (decpt <= -9) + 1
       ("N.NNe-MMM")
       (F <= E) <=> (len == 1 && decpt >= -1) || (len > 1 && decpt >= -2)
       We also need to ensure that if the 'f' format is chosen,
       the field width allows us to place at least one significant digit
       (i.e. width > 2 - decpt). If not, we prefer the 'e' format.
    2. 0 < decpt < len
       F = len + 1 (see above)
       E = len + 1 + 1 + ... ("N.NNeMMM")
       F is always less than E.
    3. len <= decpt <= width
       In this case we have enough space to represent the number in the 'f'
       format, so we prefer it with some exceptions.
    4. width < decpt
       The number cannot be represented in the 'f' format at all, always use
       the 'e' 'one.
  */
  if ((have_space ||
      /*
        Not enough space, let's see if the 'f' format provides the most number
        of significant digits.
      */
       ((decpt <= width && (decpt >= -1 || (decpt == -2 &&
                                            (len > 1 || !force_e_format)))) &&
         !force_e_format)) &&
      
       /*
         Use the 'e' format in some cases even if we have enough space for the
         'f' one. See comment for DBL_DIG.
       */
      (!have_space || (decpt >= -DBL_DIG + 1 &&
                       (decpt <= DBL_DIG || len > decpt))))
  {
    /* 'f' format */
    int i;

    width-= (decpt < len) + (decpt <= 0 ? 1 - decpt : 0);

    /* Do we have to truncate any digits? */
    if (width < len)
    {
      if (width < decpt)
      {
        if (error != NULL)
          *error= TRUE;
        width= decpt;
      }
      
      /*
        We want to truncate (len - width) least significant digits after the
        decimal point. For this we are calling dtoa with mode=5, passing the
        number of significant digits = (len-decpt) - (len-width) = width-decpt
      */
      dtoa_free(res, buf, sizeof(buf));
      res= dtoa(x, 5, width - decpt, &decpt, &sign, &end, buf, sizeof(buf));
      src= res;
      len= end - res;
    }

    if (len == 0)
    {
      /* Underflow. Just print '0' and exit */
      *dst++= '0';
      goto end;
    }
    
    /*
      At this point we are sure we have enough space to put all digits
      returned by dtoa
    */
    if (sign && dst < dend)
      *dst++= '-';
    if (decpt <= 0)
    {
      if (dst < dend)
        *dst++= '0';
      if (len > 0 && dst < dend)
        *dst++= '.';
      for (; decpt < 0 && dst < dend; decpt++)
        *dst++= '0';
    }

    for (i= 1; i <= len && dst < dend; i++)
    {
      *dst++= *src++;
      if (i == decpt && i < len && dst < dend)
        *dst++= '.';
    }
    while (i++ <= decpt && dst < dend)
      *dst++= '0';
  }
  else
  {
    /* 'e' format */
    int decpt_sign= 0;

    if (--decpt < 0)
    {
      decpt= -decpt;
      width--;
      decpt_sign= 1;
    }
    width-= 1 + exp_len; /* eNNN */

    if (len > 1)
      width--;

    if (width <= 0)
    {
      /* Overflow */
      if (error != NULL)
        *error= TRUE;
      width= 0;
    }
      
    /* Do we have to truncate any digits? */
    if (width < len)
    {
      /* Yes, re-convert with a smaller width */
      dtoa_free(res, buf, sizeof(buf));
      res= dtoa(x, 4, width, &decpt, &sign, &end, buf, sizeof(buf));
      src= res;
      len= end - res;
      if (--decpt < 0)
        decpt= -decpt;
    }
    /*
      At this point we are sure we have enough space to put all digits
      returned by dtoa
    */
    if (sign && dst < dend)
      *dst++= '-';
    if (dst < dend)
      *dst++= *src++;
    if (len > 1 && dst < dend)
    {
      *dst++= '.';
      while (src < end && dst < dend)
        *dst++= *src++;
    }
    if (dst < dend)
      *dst++= 'e';
    if (decpt_sign && dst < dend)
      *dst++= '-';

    if (decpt >= 100 && dst < dend)
    {
      *dst++= decpt / 100 + '0';
      decpt%= 100;
      if (dst < dend)
        *dst++= decpt / 10 + '0';
    }
    else if (decpt >= 10 && dst < dend)
      *dst++= decpt / 10 + '0';
    if (dst < dend)
      *dst++= decpt % 10 + '0';

  }

end:
  dtoa_free(res, buf, sizeof(buf));
  *dst= '\0';

  return dst - to;
}
예제 #28
0
파일: spitm.c 프로젝트: ruicouto/ArkOfGames
/**
 * Main function. Initializes and starts the game
 */
int spitm() {listP mao1,mao2,pl11,pl12,pl13,pl14,pl21,pl22,pl23,pl24;
    int stock1[stk],stock2[stk],baralho[(num*2)-2],pc1[sz],pc2[sz],pc3[sz];
    mao1=mao2=pl11=pl12=pl13=pl14=pl21=pl22=pl23=pl24=NULL;
    int vencedor=0,turno=1,o,d,i=0,n=0,x=(LINES/2),y=(COLS/2);

    initscr();
    cbreak();
    keypad(stdscr,TRUE);
    noecho();
    start_color();
    init_pair(1,COLOR_WHITE,COLOR_GREEN);
    init_pair(2,COLOR_RED,COLOR_GREEN);
    bkgd(COLOR_PAIR(1));
    attron(COLOR_PAIR(2));

    limpa(pc1,13);
    limpa(pc2,13);
    limpa(pc3,13);
    shuff(baralho);
    atoa(baralho,stock1,20);
    atoa(baralho,stock2,20);
    mao1=atod(baralho,mao1,5);
    mao2=atod(baralho,mao2,5);
    while(!i) {
        if(stock1[0] > stock2[0]) {
            turno=1;i++;
        };break;
	    if(stock1[0] < stock2[0]) {
            turno=2;
            i++;
        };break;
	    barStk(stock1,stk);
        barStk(stock2,stk);
	};
    
    while(vencedor==0) {
        while(elemN(mao1)<5)
            mao1=atod(baralho,mao1,1);
        while(turno==1)	{
            clear();
            mostrac(mao1,pl11, pl12,pl13, pl14, mao2, pl21, pl22, pl23,pl24, pc1, pc2, pc3, stock1, stock2,turno);
            echo();
            mvprintw(22,0,"Insira origem(1-10) e destino (1-7):__ __");
            mvscanw(22,36," %d %d",&o,&d);
            if(o>0 && o<6) {
                if(d==1) {
                    if (showi(mao1,elemN(mao1)-o) == topAr(pc1)+1) {
                        dtoa(mao1,elemN(mao1)-o,pc1);
                    };
                };
                if(d==2) {
                    if (showi(mao1,elemN(mao1)-o) == topAr(pc2)+1) {
                        dtoa(mao1,elemN(mao1)-o,pc2);
                    };
                };
			    if(d==3) {
                    if (showi(mao1,elemN(mao1)-o) == topAr(pc3)+1) {
                        dtoa(mao1,elemN(mao1)-o,pc3);
                    };
                };
    			if(d==4) {
                    pl11=poe(pl11,showi(mao1,elemN(mao1)-o));
                    if(elemN(mao1) == o) {
                        mao1=rmUlt(mao1);turno=2;
                    } else {
                        rmI(mao1,elemN(mao1)-(o));
                        turno=2;
                    };
                };
	
			    if(d==5) {
                    pl12=poe(pl12,showi(mao1,elemN(mao1)-o));
                    if(elemN(mao1) == o) {
                        mao1=rmUlt(mao1);
                        turno=2;
                    } else {
                        rmI(mao1,elemN(mao1)-(o));
                        turno=2;
                    };
                };
			    if(d==6) {
                    pl13=poe(pl13,showi(mao1,elemN(mao1)-o));
                    if(elemN(mao1) == o) {
                        mao1=rmUlt(mao1);
                        turno=2;
                    } else {
                        rmI(mao1,elemN(mao1)-(o));
                        turno=2;
                    };
                };
			    if(d==7) {
                    pl14=poe(pl14,showi(mao1,elemN(mao1)-o));
                    if(elemN(mao1) == o) {
                        mao1=rmUlt(mao1);
                        turno=2;
                    } else {
                        rmI(mao1,elemN(mao1)-(o));
                        turno=2;
                    };
                };
			}; //end if(o>0 && o<6)
            if(o==10) {
                if(d==1){
                    if (topAr(stock1) == topAr(pc1)+1) {
                        addAe(stock1,pc1);
                    };
                };
			    if(d==2) {
                    if (topAr(stock1) == topAr(pc2)+1) {
                        addAe(stock1,pc2);
                    };
                };
			    if(d==3) {
                    if (topAr(stock1) == topAr(pc3)+1) {
                        addAe(stock1,pc3);
                    };
                };
            };
            if(o>5 && o< 10) {
                if(o==6 && d==1 && (elemN(pl11)!=0)) {
                    if (showi(pl11,elemN(pl11)-o) == topAr(pc1)+1) {
                        dtoa(pl11,elemN(pl11)-1,pc1);
                    };
                };
			    if(o==7 && d==1 && (elemN(pl12)!=0)) {
                    if (showi(pl12,elemN(pl12)-o) == topAr(pc1)+1) {
                        dtoa(pl12,elemN(pl12)-1,pc1);
                    };
                };
			    if(o==8 && d==1 && (elemN(pl13)!=0)) {
                    if (showi(pl13,elemN(pl13)-o) == topAr(pc1)+1) {
                        dtoa(pl13,elemN(pl13)-1,pc1);
                    };
                };
                if(o==9 && d==1 && (elemN(pl14)!=0)) {
                    if (showi(pl14,elemN(pl14)-o) == topAr(pc1)+1) {
                        dtoa(pl14,elemN(pl14)-1,pc1);
                    };
                };
            
                if(o==6 && d==2 && (elemN(pl11)!=0)) {
                    if (showi(pl11,elemN(pl11)-o) == topAr(pc2)+1) {
                        dtoa(pl11,elemN(pl11)-1,pc2);
                    };
                };
                if(o==7 && d==2 && (elemN(pl12)!=0)) {
                    if (showi(pl12,elemN(pl12)-o) == topAr(pc2)+1) {
                        dtoa(pl12,elemN(pl12)-1,pc2);
                    };
                };
                if(o==8 && d==2 && (elemN(pl13)!=0)) {
                    if (showi(pl13,elemN(pl13)-o) == topAr(pc2)+1) {
                        dtoa(pl13,elemN(pl13)-1,pc2);
                    };
                };
                if(o==9 && d==2 && (elemN(pl14)!=0)) {
                    if (showi(pl14,elemN(pl14)-o) == topAr(pc2)+1) {
                        dtoa(pl14,elemN(pl14)-1,pc2);
                    };
                };

                if(o==6 && d==3 && (elemN(pl11)!=0)) {
                    if (showi(pl11,elemN(pl11)-o) == topAr(pc3)+1) {
                        dtoa(pl11,elemN(pl11)-1,pc3);
                    };
                };
                if(o==7 && d==3 && (elemN(pl12)!=0)) {
                    if (showi(pl12,elemN(pl12)-o) == topAr(pc3)+1) {
                        dtoa(pl12,elemN(pl12)-1,pc3);
                    };
                };
                if(o==8 && d==3 && (elemN(pl13)!=0)) {
                    if (showi(pl13,elemN(pl13)-o) == topAr(pc3)+1) {
                        dtoa(pl13,elemN(pl13)-1,pc3);
                    };
                };
                if(o==9 && d==3 && (elemN(pl14)!=0)) {
                    if (showi(pl14,elemN(pl14)-o) == topAr(pc3)+1) {
                        dtoa(pl14,elemN(pl14)-1,pc3);
                    };
                };
            };//end if(o>5 && o< 10)
        }; //end while(turno==1)

        n=0;
        while ((stock1[n]==0) && (n<stk)) {
            n++;
        };
        // Winner is player 1
        if (stock1[n] == 0) {
            vencedor=1;
            turno=1;
        }

        // Tie
        if(baralho[num*2-3]==0)	{
            vencedor=3;
        };

        // Clean central stack
        if(pc1[0]==13) {
            atoa(pc1,baralho,13);
        };
        if(pc2[0]==13) {
            atoa(pc2,baralho,13);
        };
        if(pc3[0]==13) {
            atoa(pc3,baralho,13);
        };

        // fill hand 2
        while(elemN(mao2)<5) {
            mao2=atod(baralho,mao2,1);
        };

        while(turno==2) {
            clear();
            mostrac(mao1,pl11, pl12,pl13, pl14, mao2, pl21, pl22, pl23,pl24, pc1, pc2, pc3, stock1, stock2,turno);
            echo();
            mvprintw(22,0,"Insira origem(1-10) e destino (1-7):__ __");
            mvscanw(22,36,"%d %d",&o,&d);
            if(o>0 && o<6) {
                if(d==1) {
                    if (showi(mao2,elemN(mao2)-o) == topAr(pc1)+1) {
                        dtoa(mao2,elemN(mao2)-o,pc1);};
                    };
                    if(d==2) {
                        if (showi(mao2,elemN(mao2)-o) == topAr(pc2)+1) {
                            dtoa(mao2,elemN(mao2)-o,pc2);
                        };
                    };
                    if(d==3) {
                        if (showi(mao2,elemN(mao2)-o) == topAr(pc3)+1) {
                            dtoa(mao2,elemN(mao2)-o,pc3);
                        };
                    };
                    if(d==4) {
                        pl21=poe(pl21,showi(mao2,elemN(mao2)-o));
                        if(elemN(mao2) == o) {
                            mao2=rmUlt(mao2);turno=1;
                        } else {
                            rmI(mao2,elemN(mao2)-(o));
                            turno=1;
                        };
                    };
                    if(d==5) {
                        pl22=poe(pl22,showi(mao2,elemN(mao2)-o));
                        if(elemN(mao2) == o) {
                            mao2=rmUlt(mao2);
                            turno=1;
                        } else {
                            rmI(mao2,elemN(mao2)-(o));
                            turno=1;
                        };
                    };
                    if(d==6) {
                        pl23=poe(pl23,showi(mao2,elemN(mao2)-o));
                        if(elemN(mao2) == o) {
                            mao2=rmUlt(mao2);
                            turno=1;
                        } else {
                            rmI(mao2,elemN(mao2)-(o));
                            turno=1;
                        };
                    };
                    if(d==7) {
                        pl24=poe(pl24,showi(mao2,elemN(mao2)-o));
                        if(elemN(mao2) == o) {
                            mao2=rmUlt(mao2);
                            turno=1;
                        } else {
                            rmI(mao2,elemN(mao2)-(o));
                            turno=1;
                        }
                    };
		        };
                if(o==10) {
                    if(d==1){
                        if (topAr(stock2) == topAr(pc1)+1) {
                            addAe(stock2,pc1);
                        };
			        };
		            if(d==2) {
                        if (topAr(stock2) == topAr(pc2)+1) {
                            addAe(stock2,pc2);
                        };
			        };
                    if(d==3) {
                        if (topAr(stock2) == topAr(pc3)+1)  {
                            addAe(stock2,pc3);
                        };
				    };
                };
                if(o>5 && o< 10) {
                    if(o==6 && d==1 && (elemN(pl21)!=0)) {
                        if (showi(pl21,elemN(pl21)-o) == topAr(pc1)+1) {
                            dtoa(pl21,elemN(pl21)-1,pc1);
                        };
                    };
                    if(o==7 && d==1 && (elemN(pl22)!=0)) {
                        if (showi(pl22,elemN(pl22)-o) == topAr(pc1)+1) {
                            dtoa(pl22,elemN(pl22)-1,pc1);
                        };
                    };
                if(o==8 && d==1 && (elemN(pl23)!=0)) {
                    if (showi(pl23,elemN(pl23)-o) == topAr(pc1)+1) {
                        dtoa(pl23,elemN(pl23)-1,pc1);
                    };
                };
                if(o==9 && d==1 && (elemN(pl24)!=0)) {
                    if (showi(pl24,elemN(pl24)-o) == topAr(pc1)+1) {
                        dtoa(pl24,elemN(pl24)-1,pc1);
                    };
                };
                if(o==6 && d==2 && (elemN(pl21)!=0)) {
                    if (showi(pl21,elemN(pl21)-o) == topAr(pc2)+1) {
                        dtoa(pl21,elemN(pl21)-1,pc2);
                    };
                };
                if(o==7 && d==2 && (elemN(pl22)!=0)) {
                    if (showi(pl22,elemN(pl22)-o) == topAr(pc2)+1) {
                        dtoa(pl22,elemN(pl22)-1,pc2);
                    };
			    };
                if(o==8 && d==2 && (elemN(pl23)!=0)) {
                    if (showi(pl23,elemN(pl23)-o) == topAr(pc2)+1) {
                         dtoa(pl23,elemN(pl23)-1,pc2);
                     };
                };
                if(o==9 && d==2 && (elemN(pl24)!=0)) {
                    if (showi(pl24,elemN(pl24)-o) == topAr(pc2)+1) {
                        dtoa(pl24,elemN(pl24)-1,pc2);
                    };
                };

                if(o==6 && d==3 && (elemN(pl21)!=0)) {
                    if (showi(pl21,elemN(pl21)-o) == topAr(pc3)+1) {
                        dtoa(pl21,elemN(pl21)-1,pc3);
                    };
                };
                if(o==7 && d==3 && (elemN(pl22)!=0)) {
                    if (showi(pl22,elemN(pl22)-o) == topAr(pc3)+1) {
                        dtoa(pl22,elemN(pl22)-1,pc3);
                    };
                };
                if(o==8 && d==3 && (elemN(pl23)!=0)) {
                    if (showi(pl23,elemN(pl23)-o) == topAr(pc3)+1) {
                        dtoa(pl23,elemN(pl23)-1,pc3);
                    };
                };
                if(o==9 && d==3 && (elemN(pl24)!=0)) {
                    if (showi(pl24,elemN(pl24)-o) == topAr(pc3)+1) {
                        dtoa(pl24,elemN(pl24)-1,pc3);
                    };
                };
             };
        };
        n=0;
        while ((stock2[n]==0) && (n<stk)) {
            n++;
        };
        // Winner is player 2
        if (stock2[n] == 0) {
            vencedor=1;
            turno=1;
        }

        // Tie
        if(baralho[num*2-3]==0) {
            vencedor=3;
        };

        // Clean central stack
        if(pc1[0]==13) {
            atoa(pc1,baralho,13);
        };
        if(pc2[0]==13) {
            atoa(pc2,baralho,13);
        };
        if(pc3[0]==13) {
            atoa(pc3,baralho,13);
        };
    };
    if((vencedor==1)||(vencedor==2)) {
        clear();
        mvprintw(x-1,y-10,"+------------------------------------+");
        mvprintw(x,y-10,"|O jogador %d vence!                 |",vencedor);
        mvprintw(x+1,y-10,"+------------------------------------+");
        getch();
    } else {
        clear();
        mvprintw(x-1,y-10,"+------------------------------------+");
        mvprintw(x,y-10,"|Empate, não há vencedores.          |");
        mvprintw(x+1,y-10,"+------------------------------------+");
        getch();
    };
    endwin();
    bkgd(COLOR_PAIR(2));
    return 0;
}
예제 #29
0
/*
 * Tipareste valorile in dreptunghiuri.
 */
void print_numbers()
{
    char s[5];

    switch (idx)
    {
    case 1:
        set(1);
        itoa(transX, s, 10);
        setcolor(BLACK);
        outtextxy(13+textwidth("x = "), 16, "лллл");
        setcolor(RED);
        outtextxy(15+textwidth("x = "), 16, s);
        break;
    case 2:
        set(1);
        itoa(transY, s, 10);
        setcolor(BLACK);
        outtextxy(13+textwidth("y = "), 31, "лллл");
        setcolor(RED);
        outtextxy(15+textwidth("y = "), 31, s);
        break;
    case 3:
        set(2);
        dtoa(scaleX, s);
        setcolor(BLACK);
        outtextxy(13+textwidth("x = "), 16, "ллллл");
        setcolor(RED);
        outtextxy(15+textwidth("x = "), 16, s);
        break;
    case 4:
        set(2);
        dtoa(scaleY, s);
        setcolor(BLACK);
        outtextxy(13+textwidth("y = "), 31, "ллллл");
        setcolor(RED);
        outtextxy(15+textwidth("y = "), 31, s);
        break;
    case 5:
        set(3);
        dtoa(rotA, s);
        setcolor(BLACK);
        outtextxy(13+textwidth("р = "), 16, "ллллл");
        setcolor(RED);
        outtextxy(15+textwidth("р = "), 16, s);
        break;
    case 6:
        set(4);
        itoa(simX, s, 10);
        setcolor(BLACK);
        outtextxy(13+textwidth("x = "), 16, "лллл");
        setcolor(RED);
        outtextxy(15+textwidth("x = "), 16, s);
        break;
    case 7:
        set(4);
        itoa(simY, s, 10);
        setcolor(BLACK);
        outtextxy(13+textwidth("y = "), 31, "лллл");
        setcolor(RED);
        outtextxy(15+textwidth("y = "), 31, s);
        break;
    case 8:
        set(4);
        dtoa(simA, s);
        setcolor(BLACK);
        outtextxy(93+textwidth("р = "), 16, "ллллл");
        setcolor(RED);
        outtextxy(95+textwidth("р = "), 16, s);
        break;
    }
}
예제 #30
0
    size_t operator()(double val, Result& result)
    {
        size_t count = 0;

        chars_format format = override_.format() != chars_format() ? override_.format() : chars_format::general;

        int decimal_places;
        if (override_.decimal_places() != 0)
        {
            decimal_places = override_.decimal_places();
        }
        else
        {
            format = chars_format::general;
            decimal_places = 0;
        }             

        char number_buffer[200]; 
        int length = 0;

        switch (format)
        {
        case chars_format::fixed:
            {
                length = snprintf(number_buffer, sizeof(number_buffer), "%1.*f", decimal_places, val);
                if (length < 0)
                {
                    JSONCONS_THROW(json_runtime_error<std::invalid_argument>("print_double failed."));
                }
                dump_buffer(number_buffer, length, decimal_point_, result);
            }
            break;
        case chars_format::scientific:
            {
                length = snprintf(number_buffer, sizeof(number_buffer), "%1.*e", decimal_places, val);
                if (length < 0)
                {
                    JSONCONS_THROW(json_runtime_error<std::invalid_argument>("print_double failed."));
                }
                dump_buffer(number_buffer, length, decimal_point_, result);
            }
            break;
        case chars_format::general:
            {
                if (override_.precision() != 0)
                {
                    int precision = override_.precision();
                    length = snprintf(number_buffer, sizeof(number_buffer), "%1.*g", precision, val);
                    if (length < 0)
                    {
                        JSONCONS_THROW(json_runtime_error<std::invalid_argument>("print_double failed."));
                    }
                    dump_buffer(number_buffer, length, decimal_point_, result);
                }
                else
                {
                    if (!dtoa(val, decimal_point_, result))
                    {
                        JSONCONS_THROW(json_runtime_error<std::invalid_argument>("print_double failed."));
                    }
                }             
                break;
            }
            default:
                JSONCONS_THROW(json_runtime_error<std::invalid_argument>("print_double failed."));
                break;
        }
        return count;
    }