karlangadas

DICCIONARIO

May 13th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.46 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. /*
  8.  * File:   main.cpp
  9.  * Author: alulab14
  10.  *
  11.  * Created on 13 de mayo de 2017, 02:25 PM
  12.  */
  13.  
  14. #include <cstdlib>
  15. #include <iostream>
  16. #include <cstring>
  17. #define INC 5
  18. using namespace std;
  19.  
  20. /*
  21.  *
  22.  */
  23. void generarLetras(char *&letras,char *&letrasmin){
  24.     char auxletras[100];
  25.     char auxletrasmin[100];
  26.     char linea[6];
  27.     int nletras=26;
  28.     for(int i=0;i<26;i++){
  29.         auxletras[i]='A'+i;
  30.         auxletrasmin[i]='a'+i;
  31.     }
  32.     while(cin.getline(linea,5) and (strlen(linea)!=0)){
  33.         auxletras[nletras]=linea[0];
  34.         auxletrasmin[nletras]=linea[3];
  35.         nletras++;
  36.     }
  37.    
  38.     letras=new char[nletras+1];
  39.     letrasmin=new char[nletras+1];
  40.     for(int i=0;i<nletras;i++){
  41.         letras[i]=auxletras[i];
  42.         letrasmin[i]=auxletrasmin[i];
  43.     }
  44.     letras[nletras]='\0';
  45.     letrasmin[nletras]='\0';
  46. }
  47. void memExacta_str(char**input,char**&output,int numElem){
  48.     output=new char*[numElem+1];
  49.     for(int i=0;i<numElem;i++){
  50.         int size=strlen(input[i])+1;
  51.         output[i]=new char[size];
  52.         strcpy(output[i],input[i]);
  53.     }
  54.     output[numElem]=NULL;
  55. }
  56.  
  57. void split_Tokens(char* linea, char ** & palabras, int n){
  58.     int numPal = 0;
  59.     char *auxPal[500];
  60.     if(!isblank(linea[0])){
  61.         auxPal[numPal] = linea;
  62.         numPal++;
  63.     }
  64.     for(int i = 1; linea[i] != '\0'; i++){
  65.         if(!isblank(linea[i]) and (isblank(linea[i-1]) or (linea[i-1] == '\0')) ){    
  66.             auxPal[numPal] = &linea[i];
  67.             numPal++;      
  68.         }
  69.         if(isblank(linea[i]) and !isblank(linea[i-1]) ){
  70.             linea[i] = '\0';
  71.         }
  72.         if(numPal == n) break;    
  73.     }
  74.     memExacta_str(auxPal,palabras,numPal);
  75. }
  76. void a_mayus(char *word,char *letras,char *letrasmin){
  77.     bool encontrado;
  78.     cout<<word<<endl;
  79.     for(int i=0;i<strlen(word);i++){
  80.         encontrado=0;
  81.         for(int j=0;j<strlen(letras);j++){
  82.             if(word[i]==letras[j]){
  83.                 encontrado=1;
  84.                 break;
  85.             }
  86.         }
  87.         if (encontrado) continue;
  88.         for(int j=0;j<strlen(letras);j++){
  89.             if(word[i]==letrasmin[j]){
  90.                 word[i]=letras[j];
  91.                 break;
  92.             }
  93.         }
  94.     }
  95.     cout<<word<<endl;
  96. }
  97. template <typename T>
  98. void incrementarNum(T *&lst,int numElem){
  99.     if(lst==NULL){
  100.         lst=new T[INC+1];
  101.         for(int i=0;i<INC;i++) lst[i]=-1;
  102.     }
  103.     else{
  104.         T *lstAux=new T[numElem+INC+1];
  105.         for(int i=0;i<numElem;i++) lstAux[i]=lst[i];
  106.         for(int i=numElem;i<=numElem+INC;i++) lstAux[i]=-1;
  107.         delete []lst;
  108.         lst=lstAux;
  109.     }
  110. }
  111. template <typename T>
  112. void agregarNum(T *&lst,T elem){
  113.     if(lst==NULL){
  114.         incrementarNum(lst,0);
  115.         lst[0]=elem;
  116.         for(int i=0;i<INC;i++) lst[i]=-1;
  117.     }
  118.     else{
  119.         int numElem=0;
  120.         for(int i=0;lst[numElem]!=-1;i++){
  121.             numElem++;
  122.         }
  123.         if(numElem%INC==0) {incrementarNum(lst,numElem);}
  124.         lst[numElem]=elem;
  125.     }  
  126. }
  127.  
  128. void incrementar(char **&lst,int numElem){
  129.     if(lst==NULL){
  130.         lst=new char*[INC+1];
  131.         for(int i=0;i<INC;i++) lst[i]=NULL;
  132.     }
  133.     else{
  134.         char **lstAux=new char *[numElem+INC+1];
  135.         for(int i=0;i<numElem;i++) lstAux[i]=lst[i];
  136.         for(int i=numElem;i<=numElem+INC;i++) lstAux[i]=NULL;
  137.         delete []lst;
  138.         lst=lstAux;
  139.     }
  140. }
  141. void agregarPalabra(char**&lst,char *elem){
  142.     cout<<"Vamos a agregar "<<elem<<endl;
  143.     if(lst==NULL){
  144.         cout<<"Nulo: "<<elem<<endl;
  145.         incrementar(lst,0);
  146.         lst[0]=elem;
  147.     }
  148.     else{
  149.         int numElem=0,idx=0;
  150.         for(int i=0;lst[numElem];i++){
  151.             if(strcmp(lst[numElem],elem)==0) return;
  152.             if(strcmp(lst[numElem],elem)<0) idx++;
  153.             numElem++;
  154.         }
  155.         if(numElem%INC==0) incrementar(lst,numElem);
  156.         for(int i=numElem;i>=idx;i--)
  157.             lst[i]=lst[i-1];
  158.         lst[idx]=elem;
  159.     }
  160.    
  161.     {
  162.         char ***tweets;
  163.         tweets = new char**[6];
  164.     }
  165.    
  166. }
  167.  
  168.  
  169.  
  170. void agregarDic(char ***&palabras,char *word,char *letras,char *letrasmin,int *&valoresEnteros,double *&valoresDePuntoFlotante){
  171.     //Si es palabra
  172.     if(!isdigit(word[0])){  
  173.         //Es palabra
  174.         a_mayus(word,letras,letrasmin);
  175.         //Si comienza con letra normal
  176.         if(word[0]>='A' and word[0]<='Z'){
  177.             cout<<"Agregar en indice: "<<word[0]-'A'<<endl;
  178.             agregarPalabra(palabras[word[0]-'A'],word);
  179.         }
  180.         else{
  181.             //Si comienza con letra especial
  182.             for(int i=26;i<strlen(letras);i++)
  183.                 if(letras[i]==word[0]){
  184.                     agregarPalabra(palabras[i],word);
  185.                     break;
  186.                 }
  187.         }
  188.         //cout << "LA WORD YA PROCESADA = " << word << endl;
  189.         return;
  190.     }
  191.     //Veamos si es float
  192.     bool isFloat=0;
  193.     for(int i=0;i<strlen(word);i++)
  194.         if(word[i]=='.') isFloat=1;
  195.    
  196.     if(isFloat)
  197.         agregarNum(valoresDePuntoFlotante,atof(word));
  198.     else
  199.         agregarNum(valoresEnteros,atoi(word));
  200. }
  201. void leerPalabras(char *letras,char *letrasmin,char ***&palabras,int *&valoresEnteros,double *&valoresDePuntoFlotante){
  202.     valoresEnteros=NULL;
  203.     valoresDePuntoFlotante=NULL;
  204.     int nletras=strlen(letras);
  205.     palabras=new char**[nletras+1];
  206.     for(int i=0;i<nletras+1;i++) palabras[i]=NULL;
  207.     char linea[400];  
  208.     while(cin.getline(linea,390)){
  209.         cout<<"LA LINEA: "<<linea<<endl;
  210.         char **words;
  211.         split_Tokens(linea,words,-1);
  212.         for(int i=0;words[i]!=NULL;i++){
  213.             cout<< "La word a analizar es : " << words[i] << endl;
  214.             agregarDic(palabras,words[i],letras,letrasmin,valoresEnteros,valoresDePuntoFlotante);
  215.         }
  216.         delete []palabras;
  217.     }
  218. }
  219. void crearDiccionario(char *&letras,char ***&palabras,int *valoresEnteros,double *valoresDePuntoFlotante){
  220.     char *letrasmin;
  221.     generarLetras(letras,letrasmin);
  222.     leerPalabras(letras,letrasmin,palabras,valoresEnteros,valoresDePuntoFlotante);
  223. }
  224. int main(int argc, char** argv) {
  225.     char *letras,***palabras;
  226.     int *valoresEnteros;
  227.     double *valoresDePuntoFlotante;
  228.     crearDiccionario(letras,palabras,valoresEnteros,valoresDePuntoFlotante);
  229.    
  230.     return 0;
  231. }
Advertisement
Add Comment
Please, Sign In to add comment