Preset.h
#ifndef PRESET_H_INCLUDED
#define PRESET_H_INCLUDED
int Status[10][10]={{},{2,0,0,0,0},{2,3,5,0,8},{4,0,0,0,0},{4,0,5,0,8},{7,0,0,6,0},{7,0,0,0,0},{7,0,0,0,8},{0,0,0,0,0}};
int TranS(int t, char c); //状态转移
void Act(int t,char c); //当前状态动作
#endif // PRESET_H_INCLUDED
main.cpp
#include <iostream>
#include <cmath>
#include "Preset.h"
using namespace std;
int N,p,m,e;
double num;
int TranS(int t, char c);
void Act(int t,char c);
int main()
{
string Str;
//int a=2147483647;
//cout << a;
//a+=1;
//cout << a;
while(cout << "Input the num:" << endl,cin >> Str)
{
int s=1;
char ch;
int Pointer=0;
while(s!=8 && s!=0)
{
Act(s,ch);
ch=Str[Pointer++];
s=TranS(s,ch);
}
if(s==8)
{
Act(s,ch);
cout << "Your num is:" << endl << num << endl;
}
else cout << "error!" << endl;
}
return 0;
}
int TranS(int t, char c)
{
switch(c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return Status[t][0];
case '.':
return Status[t][1];
case 'e':
case 'E':
return Status[t][2];
case '+':
case '-':
return Status[t][3];
case 0:
return Status[t][4];
}
return 0;
}
void Act(int t,char c)
{
switch(t)
{
case 1:
N=p=m=num=0;
e=1;
break;
case 2:
N=10*N+c-'0';
break;
case 4:
N=10*N+c-'0';
m++;
break;
//case 5:
// e=1;
// break;
case 6:
if(c=='-')e=-1;
break;
case 7:
p=10*p+c-'0';
break;
case 8:
num=N*pow(10,e*p-m);
break;
}
}