示例#1
0
void double_to_string(double blah, pfc::string_base & p_out, int points = 10, bool ms = true)
{
	int decimal, sign;
	pfc::array_t<char> buffer;
	buffer.set_size(_CVTBUFSIZE);
	buffer.fill_null();
	_fcvt_s(buffer.get_ptr(), buffer.get_size(), blah*(ms ? 1000.0 : 1.0), points, &decimal, &sign);
	const char * ptr = buffer.get_ptr();
	if (decimal <= 0)
	{
		p_out.add_string("0.",2);
		while (decimal) 
		{
			p_out.add_byte('0');
			decimal ++;
		}
		p_out.add_string(ptr, pfc_infinite);
	}
	else
	{
		p_out.add_string(ptr, decimal);
		p_out.add_string(".",1);
		ptr += decimal;
		p_out.add_string(ptr,pfc_infinite);
	}
}
示例#2
0
char * Parser::FloatToString(float value)
{
	int decimal, sign;
	char* res = (char*)heap_my.get_mem(_CVTBUFSIZE);
	int buf = _fcvt_s(res, _CVTBUFSIZE, value, 100, &decimal, &sign);
	if (buf != 0)
		return nullptr;
	return res;
}
char *
dtoa(double d,       // IN
     int mode,       // IN
     int prec,       // IN
     int *expOut,    // OUT
     int *sign,      // OUT
     char **strEnd)  // OUT
{
   char *str = NULL;
   int dec;

#if defined(_WIN32)
   if (2 == mode) {
      str = malloc(_CVTBUFSIZE);
      if (str) {
         if (_ecvt_s(str, _CVTBUFSIZE, d, prec, &dec, sign)) {
            free(str);
            str = NULL;
         }
      }
   } else {
      ASSERT(3 == mode);
      str = malloc(_CVTBUFSIZE);
      if (str) {
         if (_fcvt_s(str, _CVTBUFSIZE, d, prec, &dec, sign)) {
            free(str);
            str = NULL;
         }
      }

      /*
       * When the value is not zero but rounds to zero at prec digits,
       * the Windows fcvt() sometimes returns the empty string and
       * a negative dec that goes too far (as in -dec > prec).
       * For example, converting 0.001 with prec 1 results in
       * the empty string and dec -2.  (See bug 253674.)
       *
       * We just clamp dec to -prec when this happens.
       *
       * While this may appear to be a safe and good thing to
       * do in general.  It really only works when the result is
       * all zeros or empty.  Since checking for all zeros is
       * expensive, we only check for empty string, which works
       * for this bug.
       */

      if (str && *str == '\0' && dec < 0 && dec < -prec) {
	 dec = -prec;
      }
   }
#else // _WIN32
   static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

   if (2 == mode) {
      pthread_mutex_lock(&mutex);
      str = strdup(ecvt(d, prec, &dec, sign));
      pthread_mutex_unlock(&mutex);
   } else {
      ASSERT(3 == mode);

#ifdef __APPLE__
      /*
       * The Mac fcvt() returns "" when prec is 0, so we have to
       * compensate.  See bug 233530.
       * While it is conceivable that fcvt(round(d), 1) can return
       * a string that doesn't end in 0, it doesn't seem to happen
       * in practice (on the Mac).  The problematic case that we
       * want to avoid is a last digit greater than 4, which requires
       * rounding up, which we don't want to do, which is why we're
       * doing the rounding on the number instead of after fcvt()
       * in the first place.
       * -- edward
       */

      if (prec == 0) {
	 size_t l;
         pthread_mutex_lock(&mutex);
	 str = strdup(fcvt(round(d), 1, &dec, sign));
         pthread_mutex_unlock(&mutex);
	 if (str) {
	    l = strlen(str);
	    ASSERT(l > 0);
	    l--;
	    ASSERT(str[l] == '0');
	    str[l] = '\0';
         }
      } else 
#endif // __APPLE__
      {
         pthread_mutex_lock(&mutex);
         str = strdup(fcvt(d, prec, &dec, sign));
         pthread_mutex_unlock(&mutex);
      }
   }
#endif // _WIN32

   if (str) {
      *strEnd = str + strlen(str);

      /* strip trailing zeroes */
      while ((*strEnd > str) && ('0' == *((*strEnd) - 1))) {
         (*strEnd)--;
      }

      *expOut = dec;
   }

   return str;
}
void  User_Interface::run()
{
	switch (this->status){

	case OUTPUT_DATA:
		{
			int max = this->calcMtrl->size();
			for (unsigned int i = 0; i < max; i++)
			{
				this->calcMtrl->pop_back();
			}
			this->redraw_window();
			this->type_build_box->set_top_index(0);
			this->num_floors_edit->set_text("");
			this->length_edit->set_text("");
			this->width_edit->set_text("");
			this->mat_fund_box->set_top_index(0);
			this->mat_wall_box->set_top_index(0);
			this->mat_roof_box->set_top_index(0);
			//this->mat_panel_box->set_top_index(0);

			this->enable_all(true);
			this->OK_button->set_text("Расчет");

			this->OK_button->show(SW_SHOW);
			this->show(SW_SHOW);
			this->status = INPUT_DATA;
			break;
		}

		case INPUT_DATA:
		{
			if (!this->materials->empty()){
				int type_build = this->type_build_box->get_top_index();

				int num_floors = _ttoi(this->num_floors_edit->get_text());
				double length = _ttof(this->length_edit->get_text());
				double width = _ttof(this->width_edit->get_text());

				int mat_fund = this->mat_fund_box->get_id_top_material();
				
				int mat_wall = this->mat_wall_box->get_id_top_material();
				
				int mat_roof = this->mat_roof_box->get_id_top_material();
				
				//int mat_panel = this->mat_panel_box->get_id_top_material();

				bool podval = this->checkbox->isChecked();

				Building* building = nullptr;
				switch (type_build)
				{
				case 0:
					building = new Home((double)width, (double)length, num_floors);
					break;
				case 1:
					building = new Office((double)width, (double)length, num_floors);
					break;
				case 2:
					building = new Storage((double)width, (double)length, num_floors);
					break;
				case 3:
					building = new Garage((double)width, (double)length, num_floors);
					break;
				default:
					break;
				}


				if (building){

					building->createFoundation(mat_fund, podval);
					building->createWall(type_build, mat_wall);
					building->createRoof(mat_roof, type_build);
					building->calculate();
					building->addMaterials(this->materials, this->calcMtrl);

					this->enable_all(false);
					this->OK_button->set_text("Новый");
					this->OK_button->show(SW_SHOW);
					this->status = OUTPUT_DATA;

					int X = 10, Y = 320;
					this->textout("Материал", 10, Y);
					this->textout("Количество", 230, Y);
					this->textout("Цена/шт", 330, Y);
					this->textout("Сумма грн", 400, Y);
					this->line(5,Y-5, 475, Y-5);
					this->line(5, Y + 20, 475, Y + 20);
					this->line(5, Y - 5, 5, Y + 20);
					this->line(225, Y - 5, 225, Y + 20);
					this->line(325, Y - 5, 325, Y + 20);
					this->line(395, Y - 5, 395, Y + 20);
					this->line(475, Y - 5, 475, Y + 20);
					Y += 25;
					double itogo = 0;
					char * buf = 0;
					int decimal;
					int sign;
					int err;
					if (!this->calcMtrl->empty()){
						for (unsigned int i = 0; i < this->calcMtrl->size(); i++)
						{
							if (this->calcMtrl->at(i)->count != 0)
							{
								char name[25];
								strncpy_s(name,25, this->calcMtrl->at(i)->type.c_str(), 24);
								this->textout((TCHAR*)name, 10, Y);


								buf = (char*)malloc(_CVTBUFSIZE);
								err = _fcvt_s(buf, _CVTBUFSIZE, this->calcMtrl->at(i)->count, 0, &decimal, &sign);
								this->textout((TCHAR*)buf, 230, Y);
								_itoa_s(this->calcMtrl->at(i)->price, buf, _CVTBUFSIZE, 10);
								this->textout((TCHAR*)buf, 330, Y);
								double summa = this->calcMtrl->at(i)->count * this->calcMtrl->at(i)->price;
								err = _fcvt_s(buf, _CVTBUFSIZE, summa, 0, &decimal, &sign);
								this->textout((TCHAR*)buf, 400, Y);
								itogo += summa;
								this->line(5, Y + 20, 475, Y + 20);
								this->line(5, Y - 5, 5, Y + 20);
								this->line(225, Y - 5, 225, Y + 20);
								this->line(325, Y - 5, 325, Y + 20);
								this->line(395, Y - 5, 395, Y + 20);
								this->line(475, Y - 5, 475, Y + 20);
								Y += 25;
							}
						}

						this->textout("Итого:", 350, Y);
						err = _fcvt_s(buf, _CVTBUFSIZE, itogo, 0, &decimal, &sign);
						this->textout((TCHAR*)buf, 400, Y);
						this->line(345, Y + 20, 475, Y + 20);
						this->line(345, Y - 5, 345, Y + 20);
						this->line(395, Y - 5, 395, Y + 20);
						this->line(475, Y - 5, 475, Y + 20);
					}
				}
			}
			else{
				this->textout("Нет данных о строительных материалах", 10, 320);
				this->status = OUTPUT_DATA;
			}
			break;
		}

		

	}
}