C程序检查是否输入了0和1的组合的有效浮点数 [英] C program to check if a valid floating point number with the combinations of 0's and 1's entered or not

查看:86
本文介绍了C程序检查是否输入了0和1的组合的有效浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入:1.0011

输出:有效

---------------------

输入:20.1001

输出:无效

------------------------

*浮点数应该只包含0和1和一个点(。)

*我必须用这个数字做一些计算,所以如果把它转换为char然后请将它转换为最后的浮点数。



我尝试了什么:



我可以通过类型转换轻松获得整数部分然后进行模运算以分隔每个数字并检查1或0,但不能在点之后获得这些数字(。)

我认为这将是一个更好的解决方案。

这个问题也可以表示为,wap检查有效的二进制浮点数是否。

我必须实现是检查数字是否只包含1,0和点(。)或不是。

Input: 1.0011
Output: Valid
---------------------
Input: 20.1001
Output: Invalid
------------------------
* Floating point number should contain only zeros and ones and a point(.)
* I have to do some calculation with that number, so if you convert it to char array then please convert it to float number at the end.

What I have tried:

I can get integer part easily by type casting then do modulo operating to separate each number and check 1 or 0, but can not get those number after point(.)
I think there will be a better solution of this.
This question can also be stated as, wap to check a valid binary floating point number is or not.
All I have to achieve is to check if the number contains only 1,0 and point(.) or not.

推荐答案

所以停止将其转换为数字并通过验证开始字符串。

创建一个bool值hasPoint,并将其设置为false。

循环输入字符串,依次检查每个字符。

如果它是'0'或'1',那就没关系。

如果它是'。',那么检查hasPoint如果它是假的,然后将其设置为true,一切都很好。

如果没问题,循环查看下一个字符。

否则,它是一个无效字符,你应该打印一条错误信息并停止。

循环之后,您可以使用标准浮点例程来转换teh值,因为您知道它有效。 (您可以将其作为循环的一部分,但这更复杂,因为小数更改where您需要插入值)。
So stop converting it to a number and start by validating the string.
Create a bool value "hasPoint", and set it to false.
loop through the input string, examining each character in turn.
If it is a '0' or a '1', then it's fine.
if it's a '.', then check "hasPoint" if it's false, then set it to true, and all is fine.
if it's fine, loop round to look at the next character.
Otherwise, it's an invalid character and you should print an error message and stop.
After the loop, you can use the standard floating point routines to convert teh value, because you know it it valid. (You could do it as part of the loop, but that's more complex because the decimal changes "where" you need to insert the value).


最简单的解决方案是检查字符串(字符)输入数字时(如问题所述):

The simplest solution would be checking the string (the characters) when the number is entered (as stated in the question):
bool bResult = (*input != 0);
int decPoint = 0;
while (bResult && *input)
{
    switch (*input)
    {
    case '.' : decPoint++;
    case '0' :
    case '1' : break;
    default : bResult = false;
    }
    input++;
}
if (decPoint != 1)
    bResult = false;





将输入转换为浮点值不起作用因为在大多数情况下这些不能代表实数。



例如,值1.0011在内部表示为0x3F80240B,转换时具有单精度(例如,当使用<$ c $时C> ATOF())。以全精度打印出来它将是1.00109994。



对于双精度,它将是0x3FF004816F0068DC,即1.0011000000000001。



您提到您已经能够检查整数部分。这也可以用于小数部分,只需将其视为整数:

使用 strchr()定位小数点,传递返回的指针加一转换函数 atoi()并执行与整数部分相同的检查。



Converting the input to a floating point value will not work because those can not represent real number in most cases.

For example the value 1.0011 is represented internally as 0x3F80240B with single precision upon conversion (e.g. when using atof()). Printing this out with full precision it will be 1.00109994.

For double precision it will be 0x3FF004816F0068DC which is 1.0011000000000001.

You mentioned that you are already able to check the integer part. This can be also used for the fractional part by just treating that as integer too:
Locate the decimal point using strchr(), pass the returned pointer plus one to the conversion function atoi() and perform the same check as for the integer part.


这篇关于C程序检查是否输入了0和1的组合的有效浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆