Добавил:
korayakov
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:
#include <iostream.h>
#include <fstream.h>
#include <conio.h>
#include <string.h>
const NUM = 21;
int SetOfStates[NUM];
int CurrentState;
int IsCorrectClick(char c);
int IsFinalState();
void InitStates()
{
for (int i=0; i<NUM+1; i++)
SetOfStates[i] = i;
}
void DispatchString(char * str)
{
InitStates();
CurrentState = 20;
for (int i=0; i<strlen(str)-1; i++)
{
if (!IsCorrectClick(str[i]) || ((i == strlen(str)-1)&&(!IsFinalState())))
{
cout<<endl<<str<<"\tError symbol: "<<i+1<<" Automat terminated" ;
return;
}
}
cout<<endl<<str<<" - OK";
}
int IsFinalState()
{
if (CurrentState == 20) return 0;
else return 1;
}
int IsCorrectClick(char c)
{
if ((c>='0') && (c<='9'))
{
if (CurrentState == 20)
{
CurrentState = c - 48;
return 1;
}
if ((CurrentState>=0) && (CurrentState<=19)) return 1;
if (CurrentState == 21)
{
CurrentState = c - 48 + 10;
return 1;
}
}
if ((c == '.') && (CurrentState>=0) && (CurrentState<=9))
{
CurrentState = 21;
return 1;
}
return 0;
}
int main()
{
ifstream fin("res.str");
clrscr();
if(!fin)
{
cout<<"Failure: open file res.str";
DispatchString("399.652222");
DispatchString("3993333333");
DispatchString("399.");
DispatchString(".65");
DispatchString("3.99999.65");
DispatchString("39d9.65");
getch();
return 1;
};
while(!fin.eof())
{
char s[80];
fin >> s;
DispatchString((char*)s);
};
fin.close();
getch();
return 0;
}