Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 19
  1. #1
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    142

    [C++] Cancellare una categoria di caratteri su un array

    Salve ragazzi sono nuovo del forum, è da molto che vi seguo e vedo che siete molto preparati, il mio problema consiste nell' eliminare in un array di caratteri tutti quei caratteri che nn sono alfabetici, potreste aiutarmi? Adesso vi posto il mio pezzo di codice su come avevo incominciato:

    codice:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    typedef char tvettchar[100];
    
    bool assegna(char C){
         return ((C >= '!')&&(C <= '/')||(C >= ':')&&(C <= '@')||(C >= '{')&&(C <= '~'));
    }
    
    void acquisiscivett(tvettchar &vc, int &N){  
        do{  
          cout<<"Inserisci quanti caratteri inserire ";
          cin>>N;  
        }while((N<1)||(N>100));
        for(int I=0;I<N;I++){
          cout<<"Inserisci il "<<I+1<<char(248)<<" carattere: ";
          cin>>vc[I];      
        }
    } 
    
    int ricerca(tvettchar vc, int inf, int sup, char canc){
          if(inf > sup)
            return -1;
          else{  
            int centro = (inf + sup)/2;                     
            if(assegna(canc) == assegna(vc[centro])){
              return centro;      
            }
            else{
              if(assegna(canc) < assegna(vc[centro])){
                return ricerca(vc,inf,centro-1,canc);
              }
              else{
                return ricerca(vc,centro+1,sup,canc);
              }
            } 
          }       
    }
    
    void scambia(char &A,char &B){
         char aiuto;
         aiuto = A;
         A = B;
         B = aiuto;
    }
    
    void ordinavett(tvettchar &vc, int N){
        int I, sup, u_s;
        sup = N - 1;
        while(sup > -1){
          u_s = -1;
          for(I=0;I<sup;I++){
            if(vc[I]>vc[I+1]){
              scambia(vc[I],vc[I+1]);
              u_s = I;
            }                 
          }
          sup = u_s;
        }                    
    }
    
    void sposta(tvettchar &vc, int N){
         char canc;
         int posiz = ricerca(vc,0,N-1,canc);
         for(int I=posiz;I<N-1;I++)
           vc[I] = vc[I+1];
    }
    
    void cancella(tvettchar &vc, int &N){
         int I;
         for(I=0;I<N;I++){    
           if((vc[I] <= 'A')&&(vc[I] >= 'Z')||(vc[I] <= 'a')&&(vc[I] >= 'z')){
             sposta(vc,N);
             N--;
           }
         }    
    }
    
    void visualizzavett(tvettchar vc, int N){
         int I;
         for(I=0;I<N;I++){
           cout<<I+1<<char(248)<<" carattere: "<<vc[I]<<endl;      
         }
    }
    
    int main()
    {
        tvettchar vc;
        int N;
        
        acquisiscivett(vc,N);
        ordinavett(vc,N);
        cancella(vc,N);
        visualizzavett(vc,N);
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  2. #2
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    142
    ah dimenticavo il problema è che non cancella i caratteri non alfabetici

  3. #3
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    E' un esercizio di scuola o puoi usare la STL?
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  4. #4
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    142
    No è un esercizio scolastico, ma ci sto sbattendo sopra la testa da un bel pò, potresti aiutarmi??

  5. #5
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Intanto questa condizione è sbagliata:
    codice:
    if((vc[I] <= 'A')&&(vc[I] >= 'Z')||(vc[I] <= 'a')&&(vc[I] >= 'z')){
    Lo stesso carattere non può essere minore di 'A' e contemporanemente maggiore di 'Z'.

    codice:
    void sposta(tvettchar &vc, int N){
       char canc; // questa non è inizializzata.
       int posiz = ricerca(vc,0,N-1,canc);
       for(int I=posiz;I<N-1;I++)
          vc[I] = vc[I+1];
    }
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  6. #6
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    142
    scusami allora potresti dirmi come devo fare per far funzionare questo programma perchè proprio non ci arrivo è già da un bel pò che ci raggiono sù

  7. #7
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Inizia col correggere la condizione e indicare cosa debba contenere quella variabile canc.
    Lo stesso carattere non può essere minore di 'A' e contemporanemente maggiore di 'Z'.
    Il carattere $ può essere minore di A e contemporaneamente essere maggiore di Z?
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

  8. #8
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    142
    ok e dopo ver fatto questo?

  9. #9
    Utente di HTML.it
    Registrato dal
    Apr 2009
    Messaggi
    142
    Adesso l' ho modificato così ma niente ancora non funziona mi restituisce gli stessi caratteri che inserisco:
    #include <cstdlib>
    #include <iostream>

    using namespace std;

    typedef char tvettchar[100];

    bool assegna(char C){
    return ((C >= '!')&&(C <= '/')||(C >= ':')&&(C <= '@')||(C >= '{')&&(C <= '~'));
    }

    void acquisiscivett(tvettchar &vc, int &N){
    do{
    cout<<"Inserisci quanti caratteri inserire ";
    cin>>N;
    }while((N<1)||(N>100));
    for(int I=0;I<N;I++){
    cout<<"Inserisci il "<<I+1<<char(248)<<" carattere: ";
    cin>>vc[I];
    }
    }

    int ricerca(tvettchar vc, int inf, int sup, char canc){
    if(inf > sup)
    return -1;
    else{
    int centro = (inf + sup)/2;
    if(assegna(canc) == assegna(vc[centro])){
    return centro;
    }
    else{
    if(assegna(canc) < assegna(vc[centro])){
    return ricerca(vc,inf,centro-1,canc);
    }
    else{
    return ricerca(vc,centro+1,sup,canc);
    }
    }
    }
    }

    void sposta(tvettchar &vc, int N){
    char C;
    for(int I=ricerca(vc,0,N-1,assegna(C));I<N-1;I++)
    vc[I] = vc[I+1];
    }

    void cancella(tvettchar &vc, int &N){
    int I;
    for(I=0;I<N;I++){
    if(!assegna){
    sposta(vc,N);
    N--;
    }
    }
    }

    void visualizzavett(tvettchar vc, int N){
    int I;
    for(I=0;I<N;I++){
    cout<<I+1<<char(248)<<" carattere: "<<vc[I]<<endl;
    }
    }

    int main()
    {
    tvettchar vc;
    int N;

    acquisiscivett(vc,N);
    cancella(vc,N);
    visualizzavett(vc,N);

    system("PAUSE");
    return EXIT_SUCCESS;
    }

  10. #10
    Utente di HTML.it L'avatar di shodan
    Registrato dal
    Jun 2001
    Messaggi
    2,381
    Per come è riportato il codice, non compila nemmeno.
    codice:
    void sposta(tvettchar &vc, int N){
       char C; // Si può sapere cosa dovrebbe contenere?
       for(int I=ricerca(vc,0,N-1,assegna(C));I<N-1;I++)
       vc[I] = vc[I+1];
    }
    
    void cancella(tvettchar &vc, int &N){
        int I;
        for(I=0;I<N;I++){
             if(!assegna){ // E' una funzione o una variabile? E se è una funzione com'è possibile che compili? E cosa dovrebbe ricevere quella funzione?
                sposta(vc,N);
                N--;
            }
        }
    }
    This code and information is provided "as is" without warranty of any kind, either expressed
    or implied, including but not limited to the implied warranties of merchantability and/or
    fitness for a particular purpose.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.