Re: функция ввода числа с клавиатуры
Добавлено: 02 май 2009, 10:45
У меня есть такая функция. Могу продать недорого.
Код: Выделить всё
#include "stdafx.h"
#include <conio.h>
double InputDouble()
{
int p = 0;
bool end_line = false;
TCHAR _str[32] = _T("");
const int back_key = 0x08;
const int enter_key = 0x0D;
int len_max = sizeof(_str)/sizeof(TCHAR)-1;
//
while (!end_line)
{
int ch = _gettch();
if (p == len_max && ch != back_key && ch != enter_key)
continue;
if (ch == '.' && _tcschr(_str, '.') != NULL)
continue;
if (ch == '-' && _str[0] != '\0')
continue;
switch (ch)
{
case enter_key :
end_line = true;
break;
case back_key :
if (p > 0)
{
_str[--p] = '\0';
_tprintf(_T("\b \b"));
}
break;
default:
if (_istdigit(ch) > 0 || ch == '.' || ch == '-')
{
_str[p++] = ch;
_str[p] = '\0';
if (p == len_max)
{
_str[--p] = '\0';
} else
{
_tprintf(_T("%c"), ch);
}
}
break;
}
}
//
return _tstof(_str);
}
int _tmain(int argc, _TCHAR* argv[])
{
_cputts(_T("бБНД ВХЯКЮ: "));
double num = InputDouble();
_cputts(_T("\r\n"));
_tcprintf(_T("вХЯКН = %f\r\n"), num);
//
_gettch();
return 0;
}