Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Miscellaneous ProblemsRSS Feeds

Program that uses this DFA and validates whether an entered string is valid float or not

Posted By: Kyle Evans     Category: C++ Programming     Views: 5815

Generate a DFA to validate a float data of C Language. Write a C/C++ program that uses this DFA and validates whether an entered string is valid float or not.

4 5
state d .
0 start 1 2
1 digit 1 2
2 dot 3 --
3 real 3 --

Code for Program that uses this DFA and validates whether an entered string is valid float or not in C++ Programming

#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <process.h>

int main()
{
    int flag = 0;
    char ch = NULL;
    char state = '0';
    char token[20] = {NULL};
    char stt[5][3] = {NULL, '4', '5',
            '0', '1', '2',
            '1', '1', '2',
            '2', '3', NULL,
            '3', '3', NULL};

    cout<<"Input float data : ";
    cin>>token;

    for(int i=0; token[i] != NULL; i++)
    {
        if(isdigit(token[i]))
        {
            ch = '4';
        }
        elseif (token[i] == '.')
        {
            ch = '5';
        }
        else
        {
            flag=1;
            printf("Invalide value");
            break;
        }

        for(int j=1; j<3 && ch != stt[0][j]; j++);
        for(int k=1; k<5 && state != stt[k][0]; k++);
        state =stt[k][j];
        if(state==NULL)
        {
            flag=1;
            printf("Invalide value");
            break;
        }
    }
    if(flag==0)
    {
        printf("Valide value");
    }
    getch();
    return 0;
}
  
Share: 



Kyle Evans
Kyle Evans author of Program that uses this DFA and validates whether an entered string is valid float or not is from London, United Kingdom.
 
View All Articles

 

Other Interesting Articles in C++ Programming:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!