Guest User

Untitled

a guest
Jun 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////////////////////
  2. // This program can determin very accurately the nature of the user input,
  3. // it detects whether it is an integer, a float, a number in scientific notation
  4. // or simply an invalid input. To be capable of doing this the program uses a simple FSM
  5. // (FINITE STATE MACHINE) to represent the possible states of the input.( INT, FLOAT,.. )
  6. // author: Gonzales Cenelia
  7. ///////////////////////////////////////////////////////////////////////////////////////////
  8. #include <iostream>
  9. using std::cin;
  10. using std::endl;
  11. using std::cout;
  12.  
  13. //===========================================================================
  14. // the list of all the possible states for the current FSM
  15. //===========================================================================
  16. enum STATE{ START, INT, FLOAT, SCIENTIFIC, EXPONENT, S1, S2, INVALID } state;
  17.  
  18. STATE Transition( char *str );
  19. void PrintState( STATE state );
  20.  
  21. int main() {
  22. // declaring buffer variable
  23. char buffer[32] = {0};
  24. // geting input from the user
  25. cout << "\nPlease enter a number: ";
  26. cin.getline( buffer, 32 );
  27. // compute final state
  28. STATE FINAL_STATE = Transition(buffer);
  29. // prints the final state
  30. PrintState(FINAL_STATE);
  31. return 0;
  32. }
  33.  
  34. //================================================
  35. // makes the transition from one state to another
  36. //================================================
  37. STATE Transition( char *str ) {
  38. int NEXT_SYMBOL;
  39. for( ; *str && state != INVALID; str++ ) {
  40. NEXT_SYMBOL = *str;
  41. switch(state) {
  42. case START:
  43. if(isdigit(NEXT_SYMBOL)) {
  44. state = INT;
  45. }
  46. else if( NEXT_SYMBOL == '+' || NEXT_SYMBOL == '-' ) {
  47. state = S1;
  48. }
  49. else if( NEXT_SYMBOL == '.' ) {
  50. state = FLOAT;
  51. }
  52. else {
  53. state = INVALID;
  54. }
  55. break;
  56. case S1:
  57. if(isdigit(NEXT_SYMBOL)) {
  58. state = INT;
  59. }
  60. else if( NEXT_SYMBOL == '.' ) {
  61. state = FLOAT;
  62. }
  63. else if(!isdigit(NEXT_SYMBOL)) {
  64. state = INVALID;
  65. }
  66. break;
  67. case INT:
  68. if( NEXT_SYMBOL == '.' ) {
  69. state = FLOAT;
  70. }
  71. else if(!isdigit(NEXT_SYMBOL)) {
  72. state = INVALID;
  73. }
  74. break;
  75. case FLOAT:
  76. if( NEXT_SYMBOL == 'E' || NEXT_SYMBOL == 'e' ) {
  77. state = S2;
  78. }
  79. else if(!isdigit(NEXT_SYMBOL)) {
  80. state = INVALID;
  81. }
  82. break;
  83. case S2:
  84. if( NEXT_SYMBOL == '+' || NEXT_SYMBOL == '-' ) {
  85. state = EXPONENT;
  86. }
  87. else {
  88. state = INVALID;
  89. }
  90. break;
  91. case EXPONENT:
  92. if(isdigit(NEXT_SYMBOL)) {
  93. state = SCIENTIFIC;
  94. }
  95. else {
  96. state = INVALID;
  97. }
  98. break;
  99. case SCIENTIFIC:
  100. if(!isdigit(NEXT_SYMBOL)) {
  101. state = INVALID;
  102. }
  103. break;
  104. }
  105. }
  106. return state;
  107. }
  108.  
  109. //=====================================
  110. // prints the current state of the FSM
  111. //=====================================
  112. void PrintState( STATE state ) {
  113. cout << "\nFSM state: ";
  114. switch(state) {
  115. case INT:
  116. cout << "INT ";
  117. break;
  118. case FLOAT:
  119. cout << "FLOAT ";
  120. break;
  121. case SCIENTIFIC:
  122. cout << "SCIENTIFIC ";
  123. break;
  124. case INVALID:
  125. cout << "INVALID ";
  126. break;
  127. }
  128. }
Add Comment
Please, Sign In to add comment