Click here to Skip to main content
16,018,202 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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..........

C#
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................
Posted
Updated 11-Apr-14 15:24pm
v4
Comments
PIEBALDconsult 11-Apr-14 21:02pm    
Perhaps a Regular Expression? http://www.regular-expressions.info/floatingpoint.html


Judging by your code, you should not be writing a compiler.
Member 10740412 11-Apr-14 21:32pm    
just doing assignmet. not building a real compiler...
[no name] 11-Apr-14 21:21pm    
What happened to the answers you already have? And, I would really like to answer but since you cross-posted I am confused which place I would answer.
Member 10740412 11-Apr-14 21:28pm    
the latest post
[no name] 11-Apr-14 21:37pm    
But which one is that? Did you even bother running this code through a debugger? The answer is obvious if you did. What is it that you think is going to happen at the very first line of code if you have a string like "123.45.67"? ss.Contains('.') is true right? So you popup a message box and exit whatever loop you are in. Don't you think that you should be checking if 'ss' contains two dots before you break out?

1 solution

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 as double. The the validation is:
C#
string numericLiteral = // from input of the analyzer, part of user's code
bool isValid = double.TryParse(numericLiteral, out value);

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".

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900