/* * ReconhecedorSaldo.java * Created on 11 de Junho de 2007, 14:21, by giuliano * * parseNumbers.cpp * Convertido para C++ em: * @date 21/10/2009 * @author Derzu Omaia * */ #include "parserNumbers.h" #include "dprintf.h" #include "stringTokenizer.h" #include using namespace std; // TODO fazer o parser de numbers romanos namespace Util { const string ParserNumbers::centenas [] = {"cento","duzentos","trezentos","quatrocentos","quinhentos", "seiscentos","setecentos","oitocentos","novecentos"}; const string ParserNumbers::dezenas1 [] = {"dez","onze","doze","treze","catorze","quinze", "dezesseis","dezessete","dezoito","dezenove"}; const string ParserNumbers::dezenas2 [] = {"vinte","trinta","quarenta","cinquenta", "sessenta","setenta","oitenta","noventa"}; const string ParserNumbers::milhar [] = {"mil", "milhao", "bilhao"}; const string ParserNumbers::milhares [] = {"mil", "milhoes", "bilhoes"}; const string ParserNumbers::unidades [] = {"zero", "um", "dois", "tr�s", "quatro", "cinco", "seis", "sete", "oito", "nove"}; ParserNumbers::ParserNumbers() { } string ParserNumbers::pontua(string num) { string aux = ""; int cont = 0; // DDPRINTF("num.length() = %d\n", num.length()); for(int i = num.length()-1 ; i >= 0; i--) { cont++; if( ( cont % 3 == 1) && (i < (int)num.length()-1) ) aux = aux+"."; // DDPRINTF("i = %d\n", i); aux = aux + num.at(i); } num = aux; aux = ""; for(int i = num.length()-1 ; i >= 0; i--) aux = aux + num.at(i); return aux; } string ParserNumbers::analisaTrinca(string num) { string aux = ""; bool centena = false; bool dezena = false; if(num.compare("100")==0) { return "cem"; } else { if(num.length()==3) { int indice = (num.at(0)-'0')-1; if(indice >= 0) { aux = centenas[indice]; centena = true; } } if(num.length() >= 2) { int indice = (num.at(num.length()-2)-'0')-1; if(indice == 0) { indice = (num.at(num.length()-1)-'0'); if(centena) { aux = aux + " e "; } aux = aux + dezenas1[indice]; dezena = true; } else if(indice >= 1) { indice = (num.at(num.length()-2)-'0')-1; if(centena) { aux = aux + " e "; } aux = aux + dezenas2[indice-1]; //Unidades indice = (num.at(num.length()-1)-'0'); if(indice > 0) { aux = aux + " e "; aux = aux + unidades[indice]; } dezena = true; } else { indice = (num.at(num.length()-1)-'0'); if(indice>0) { if(centena) { aux = aux + " e "; } //DDPRINTF("Unidade: %d\n", indice); aux = aux + unidades[indice]; } } } else { int indice = (num.at(num.length()-1)-'0'); if(dezena) { aux = aux + " e "; } aux = aux + unidades[indice]; } } return aux; } // TODO Verificar se tem $R o $D ou euro string ParserNumbers::parse(string num) { StringTokenizer st1 = StringTokenizer(num.c_str(),","); string inteiro = st1.getNext(); StringTokenizer st = StringTokenizer(pontua(inteiro).c_str(), "."); string texto = ""; int tokens = st.getLength(); //DPRINTF("num eh %s\n", num.c_str()); //DPRINTF("inteiro eh %s\n", inteiro.c_str()); //DPRINTF("tokens eh %d\n", tokens); if(tokens <= 4) { string lastToken = ""; while(st.hasNext()) { tokens--; lastToken = st.getNext(); //DPRINTF("lastToken eh %s\n", lastToken.c_str()); string aux = analisaTrinca(lastToken); //DPRINTF("aux eh %s\n", aux.c_str()); //DPRINTF("aux.length() %d\n", aux.length()); // TODO ta com o problema do 7 mill, ajeitar. texto += aux; if(tokens>0) { if (aux.length()>0) { if((lastToken.at(lastToken.length()-1)-'0') == 1) { texto += " "+milhar[tokens-1] +" e "; } else { texto += " "+milhares[tokens-1] +" e "; } } } } // comentei essa parte /* if(atoi(lastToken.c_str()) == 1) texto += " real"; else texto += " reais";*/ string aux2 = ""; if(st1.hasNext()) aux2 = st1.getNext(); if(atoi(aux2.c_str()) > 0) { //DPRINTF("entrei aqui"); texto += " e "+analisaTrinca("0"+aux2); if(atoi(aux2.c_str()) == 1) { texto += " centavo"; } else { texto += " centavos"; } } //DDPRINTF("Final: %s \n", texto.c_str()); } else DDDPRINTF("Numero invalido!\n"); // remove o "e" do final //while (texto.at(texto.size()-1)==' ' || texto.at(texto.size()-1)=='e') while (texto.at(texto.size()-2)==' ' && texto.at(texto.size()-1)=='e') texto.erase(texto.begin()+texto.size()-1); return texto; } }