1 <pre>2 //============================================================================3 // Name : practica2.cpp4 // Author : gerardooscarjt@gmail.com5 // Version :6 // Copyright : Your copyright notice7 // Description : Práctica 28 //============================================================================9 10 //#include <iostream>11 #include <fstream>12 #include <iostream>13 #include <string>14 #include "Queue.h"15 16 using namespace std;17 18 char getNextChar() {19 return getc(stdin);20 }21 22 bool isDigit(char c) {23 return c>47 && c<58;24 }25 26 bool isLetter(char c) {27 return (c>96 && c<123) || (c>64 && c<91);28 }29 30 bool isOperator(char c) {31 return c == '+' || c == '=' || c == '-' || c == '/' || c == '\\' || c == '*' || c == '%';32 }33 34 bool isPuntuation(char c) {35 return c == '(' || c == ')' || c == '\n';36 }37 38 bool isBlank(char c) {39 return c == ' ';40 }41 42 bool getNextToken() {43 int number = 0;44 string identifier;45 int state = 1;46 char c;47 while (true) {48 c = getNextChar();49 if (c != -1) {50 switch (state) {51 case 1:52 if (isDigit(c)) {53 number = c-48;54 state = 2;55 } else if (isLetter(c)) {56 // identifier = letra;57 state = 3;58 identifier.clear();59 identifier.push_back(c);60 } else if (isBlank(c)) {61 // No hace nada :)62 } else if (isOperator(c)) {63 // Ya veremos que hace...64 state = 4;65 identifier.clear();66 identifier.push_back(c);67 68 } else if (isPuntuation(c)) {69 if (c == '(') printf("Token puntuacion: p_open\n");70 if (c == ')') printf("Token puntuacion: p_close\n");71 if (c == '\n') printf("Token puntuacion: eol\n");72 } else {73 // Lanzar error, caracter inesperado, esperaba dígito, letra, operador, puntuación o blanco.74 printf("Error: caracter inesperado, esperaba dígito, letra, operador, puntuación o blanco");75 }76 77 break;78 79 80 81 case 2:82 if (isDigit(c)) {83 number *=10;84 number += (c-48);85 } else {86 ungetc((int)c, stdin);87 printf("Token numero: %d\n", number);88 return true;89 }90 break;91 case 3:92 if (isLetter(c) || isDigit(c)) {93 identifier.push_back(c);94 } else {95 ungetc((int)c, stdin);96 printf("Token identificador: %s\n", identifier.data());97 return true;98 }99 break;100 case 4:101 if (isOperator(c)) {102 identifier.push_back(c);103 104 } else {105 ungetc((int)c, stdin);106 // comprobar en una lista si el operador existe107 printf("Token operador: %s\n", identifier.data());108 return true;109 }110 }111 } else {112 printf("Token puntuacion: eof\n");113 return false;114 }115 }116 }117 118 119 int main(int argc, char *argv[]){120 /* for (int i=0; i < argc; i++)121 printf("%s \n", argv[i]);122 */123 124 char valor;125 126 127 do {128 valor = getNextToken();129 //printf("ascii %d\n", valor);130 } while (valor);131 132 133 134 return 0;135 }136 137 </pre>
Este ShareCode tiene versiones:
- //=====================================... (24/04/2013)
- //=====================================... (24/04/2013)
- //=====================================... (24/04/2013)
- //=====================================... (24/04/2013)
Enlace
El enlace para compartir es: