用于显示给定数字不是实数的代码,如果它在C#中包含两个点 [英] code for displaying that the given number is not a real number if it contains two dots in C#
问题描述
如果在C#中包含两个点,则显示给定数字不是实数的代码。
我正在研究编译器构造的词法分析器阶段。我hava写了一个代码,如果它包含一个点,它将数字显示为实数,但如果它包含两个点,则它不会产生错误。
这是我的代码..........
code for displaying that the given number is not a real number if it contains two dots in C#.
i am working on lexical analyzer phase of complier construction. i hava written a code that display the number as a real number if it contains one dot int it, but if it contains two dot it does not generate an error.
here is my code..........
bool containReal = ss.Any(char.IsDigit) && ss.Contains('.');
if (containReal)
{
MessageBox.Show("this is a Real Number");
break;
}
//invalid b/c of two dots
string str2 = ss.Substring(1, ss.IndexOf('.'));
string str3 = ss.Substring(ss.IndexOf('.') + 1);
bool chk_dot = (str2.Any(char.IsLetter) || str3.Any(char.IsLetter) || str2.IndexOfAny("!@%^&*()#$-_+<>".ToCharArray()) != -1 || str3.IndexOfAny("!@%^&*()#$-_+<>".ToCharArray()) != -1);
if ( chk_dot)
{
MessageBox.Show("Error: Line num " + linecount + " Invalid b/c of two dots in a real number");
errorcount++;
break;
}
任何人都可以请帮助我................
can anyone please help me................
推荐答案
-_ +<> .ToCharArray ())!= -1 || str3.IndexOfAny( !@%^& *()#
-_+<>".ToCharArray()) != -1 || str3.IndexOfAny("!@%^&*()#
-_ +<> .ToCharArray())!= -1);
if (chk_dot)
{
MessageBox.Show( 错误:行数 + linecount + 实数中两个点的b / c无效);
errorcount ++;
break ;
}
-_+<>".ToCharArray()) != -1); if ( chk_dot) { MessageBox.Show("Error: Line num " + linecount + " Invalid b/c of two dots in a real number"); errorcount++; break; }
任何人都可以帮助我............... 。
can anyone please help me................
使用不同的人工方法验证实数有很多问题。你不需要任何一个。比如,该号码被声明为double
。验证是:
There are many questions on validation of real numbers using different artificial methods. You don't need any of them. Say, the number is declared asdouble
. The the validation is:
string numericLiteral = // from input of the analyzer, part of user's code
bool isValid = double.TryParse(numericLiteral, out value);
那就是。没有人关心文字中的错误;编译器从不在错误描述中显示它。您只能显示文字的值,并告诉用户 isValid == false
:无效的数字格式。
That's is. Nobody cares what's exactly wrong in the literal; compilers never show it in the error description. You can only show the value of the literal and tell the user if isValid == false
: "invalid numeric format".
这篇关于用于显示给定数字不是实数的代码,如果它在C#中包含两个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!