Visualizzazione dei risultati da 1 a 8 su 8
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2009
    Messaggi
    12

    Programma Automa A Stati Finiti - Riconoscimento Di Stringhe

    Ho un problema.. nn mi riesce in parte questo programma in linguaggio C; per info andate qui:
    http://img99.imageshack.us/img99/960...amma009ta3.jpg

    ...

    per ora io ho fatto questo.. ma nn mi riesce andare avanti.. cioè.. il ciclo nn mi si ferma se premo x.. (capirete leggendo nel link).. cosa sbaglio...????

    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 10


    int main() {
    unsigned int scelta;
    int i;
    char line;
    char alfabeto [10] = {'a','b','c','d','e'};


    do {
    printf("1) PRIMO PROGRAMMA CHE RICONOSCE CABED\n");
    printf("2) SECONDO PROGRAMMA CHE RICONOSCE BABAB\n");
    scanf("%d",&scelta);
    } while (scelta < 1 || scelta > 2);


    switch (scelta) {

    case 1: // SCELTA PROGRAMMA CABED //


    for (i=0; i!='x';i++){
    printf("\nInserisci la lettera:");
    scanf("%s",&alfabeto);
    printf ("Digitato: %s",alfabeto);
    }
    }
    getch();
    return 0;
    }


    vi prego aiutatemi...

  2. #2
    Utente di HTML.it
    Registrato dal
    Jan 2009
    Messaggi
    12

    Programma Automi Linguaggio C

    Devo fare un programma in linguaggio C che simuli il comportamento di un automa riconoscitore di sequenze; l'alfabeto dei caratteri consentiti è a,b,c,d,e,x.
    L'utente potrà inserire i caratteri da tastiera uno alla volta ed il programma visualizzerà solamente i caratteri inseriti validi.
    Quando la sequenza è riconosciuta, il programma dovrà stampare un messaggio di conferma e proseguire nel suo lavoro.
    Il carattere x conclude il riconoscimento e termina il programma.
    Le stringhe da riconoscere sono:
    - CABED
    - BABAB.

    Per ora io ho fatto questo.. ma nn va come deve andare.... nel senso che il for è sempre attivo anche con il carattere x....:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX 10


    int main() {
    unsigned int scelta;
    int i;
    char line;
    char alfabeto [10] = {'a','b','c','d','e'};


    do {
    printf("1) PRIMO PROGRAMMA CHE RICONOSCE CABED\n");
    printf("2) SECONDO PROGRAMMA CHE RICONOSCE BABAB\n");
    scanf("%d",&scelta);
    } while (scelta < 1 || scelta > 2);


    switch (scelta) {

    case 1: // SCELTA PROGRAMMA CABED //


    for (i=0; i!='x';i++){
    printf("\nInserisci la lettera:");
    scanf("%s",&alfabeto);
    printf ("Digitato: %s",alfabeto);
    }
    }
    getch();
    return 0;
    }


    Vi prego aiutatemi...!!!

  3. #3
    Utente di HTML.it
    Registrato dal
    Jul 2008
    Messaggi
    1,326
    Scusa ma... ci sono delle cose un po' strane (a parte il fatto che hai aperto due topic per la stessa cosa, anche se non spetta a me fare il moderatore).

    Dichiari e inizializzi un array di char "alfabeto"... e poi lo leggi da tastiera come stringa?

    codice:
    printf("\nInserisci la lettera:");
    scanf("%s",&alfabeto);
    e poi... nel for il controllo lo fai sull'indice i, non sul carattere immesso... cioè cicli finché l'indice è diverso da 'x'ma piuttosto dovresti fare "finché il carattere è diverso da x".

    In ogni caso non ha senso quella scanf...

    sarebbe molto più sensato fare così (dato che hai dichiarato una variabile char "line")

    codice:
    do {
       printf("\nInserisci la lettera:");
       scanf("%c", &line);
       printf ("Digitato: %c",line);
    } while ( line != 'x' );
    Mi sa che devi ragionare meglio su quello che fai...

  4. #4
    Moderatore di Programmazione L'avatar di alka
    Registrato dal
    Oct 2001
    residenza
    Reggio Emilia
    Messaggi
    24,461

    Moderazione

    Ho riunito le discussioni aperte. Ad ogni modo, in futuro, si eviti di aprire più discussioni sullo stesso argomento: se occorre precisare dettagli, basta farlo nel thread già aperto.

    Ciao!
    MARCO BREVEGLIERI
    Software and Web Developer, Teacher and Consultant

    Home | Blog | Delphi Podcast | Twitch | Altro...

  5. #5
    Devi implementare la funzione di transizione in modo da passare da uno stato all'altro in base allo stato e al carattere corrente. Quando ti trovi in uno stato finale, stampi la stringa di riconoscimento.

    El còdigo fuente:
    codice:
    #include <stdio.h>
    #include <ctype.h>
    #include <conio.h>
    
    typedef enum tagStati
    {
    	S0,
    	S1,
    	S2,
    	S3,
    	S4,
    	S5,
    	S6,
    	S7,
    	S8
    } Stati;
    
    void Automa()
    {
    	Stati stato = S0;
    	int c = 0;
    
    	while ( c != 'X' )
    	{
    		//c = toupper(getch());
    		c = toupper(_getch());
    		if ( c == 'X' )
    		{
    			printf("digitato X\n");
    			break;
    		}
    		else if ( c == 'A' || c == 'B' || c == 'C' || c == 'D' || c == 'E' )
    		{
    			printf("digitato %c\n", c);
    		}
    
    		switch(stato)
    		{
    		case S0:
    			if ( c == 'C' )
    				stato = S1;
    			else if ( c == 'B' )
    				stato = S5;
    			break;
    		case S1:
    			if ( c == 'A' )
    				stato = S2;
    			else if ( c == 'C' )
    				stato = S1;
    			else if ( c == 'B' )
    				stato = S5;
    			break;
    		case S2:
    			if ( c == 'B' )
    				stato = S3;
    			else if ( c == 'C' )
    				stato = S1;
    			break;
    		case S3:
    			if ( c == 'E' )
    				stato = S4;
    			else if ( c == 'A' )
    				stato = S6;
    			else if ( c == 'C' )
    				stato = S1;
    			else if ( c == 'B' )
    				stato = S5;
    			break;
    		case S4:
    			if ( c == 'D' )
    			{
    				printf("Fermi tutti!!! CABED riconosciuta!\n");
    				stato = S0;
    			}
    			else if ( c == 'C' )
    				stato = S1;
    			else if ( c == 'B' )
    				stato = S5;
    			break;
    		case S5:
    			if ( c == 'A' )
    				stato = S6;
    			else if ( c == 'C' )
    				stato = S1;
    			else if ( c == 'B' )
    				stato = S5;
    			break;
    		case S6:
    			if ( c == 'B' )
    				stato = S7;
    			else if ( c == 'C' )
    				stato = S1;
    			break;
    		case S7:
    			if ( c == 'A' )
    				stato = S8;
    			else if ( c == 'C' )
    				stato = S1;
    			else if ( c == 'B' )
    				stato = S5;
    			break;
    		case S8:
    			if ( c == 'B' )
    			{
    				printf("Fermi tutti!!! BABAB riconosciuta!\n");
    				stato = S0;
    			}
    			else if ( c == 'C' )
    				stato = S1;
    			else if ( c == 'B' )
    				stato = S5;
    			break;
    		}
    	}
    }
    
    int main()
    {
    	Automa();
    
    	return 0;
    }

  6. #6
    Chiedo venia. Mancano alcune transizioni nella versione che ho postato prima.
    Questa è la versione corretta:

    codice:
    #include <stdio.h>
    #include <ctype.h>
    #include <conio.h>
    
    typedef enum tagStati
    {
    	S0,
    	S1,
    	S2,
    	S3,
    	S4,
    	S5,
    	S6,
    	S7,
    	S8
    } Stati;
    
    void Automa()
    {
    	Stati stato = S0;
    	int c = 0;
    
    	while ( c != 'X' )
    	{
    		//c = toupper(getch());
    		c = toupper(_getch());
    		if ( c == 'X' )
    		{
    			printf("digitato X\n");
    			break;
    		}
    		else if ( c == 'A' || c == 'B' || c == 'C' || c == 'D' || c == 'E' )
    		{
    			printf("digitato %c\n", c);
    		}
    
    		switch(stato)
    		{
    		case S0:
    			if ( c == 'C' )
    				stato = S1;
    			else if ( c == 'B' )
    				stato = S5;
    			break;
    		case S1:
    			if ( c == 'A' )
    				stato = S2;
    			else if ( c == 'B' )
    				stato = S5;
    			else if ( c == 'D' || c == 'E' )
    				stato = S0;
    			break;
    		case S2:
    			if ( c == 'B' )
    				stato = S3;
    			else if ( c == 'C' )
    				stato = S1;
    			else if ( c == 'A' || c == 'D' || c == 'E' )
    				stato = S0;
    			break;
    		case S3:
    			if ( c == 'E' )
    				stato = S4;
    			else if ( c == 'A' )
    				stato = S6;
    			else if ( c == 'C' )
    				stato = S1;
    			else if ( c == 'B' )
    				stato = S5;
    			else if ( c == 'D' )
    				stato = S6;
    			break;
    		case S4:
    			if ( c == 'D' )
    			{
    				printf("Fermi tutti!!! CABED riconosciuta!\n");
    				stato = S0;
    			}
    			else if ( c == 'C' )
    				stato = S1;
    			else if ( c == 'B' )
    				stato = S5;
    			else if ( c == 'A' || c == 'E' )
    				stato = S0;
    			break;
    		case S5:
    			if ( c == 'A' )
    				stato = S6;
    			else if ( c == 'C' )
    				stato = S1;
    			else if ( c == 'D' || c == 'E' )
    				stato = S0;
    			break;
    		case S6:
    			if ( c == 'B' )
    				stato = S7;
    			else if ( c == 'C' )
    				stato = S1;
    			else if ( c == 'A' || c == 'D' || c == 'E' )
    				stato = S0;
    			break;
    		case S7:
    			if ( c == 'A' )
    				stato = S8;
    			else if ( c == 'C' )
    				stato = S1;
    			else if ( c == 'B' )
    				stato = S5;
    			else if ( c == 'D' || c == 'E' )
    				stato = S0;
    			break;
    		case S8:
    			if ( c == 'B' )
    			{
    				printf("Fermi tutti!!! BABAB riconosciuta!\n");
    				stato = S5;
    			}
    			else if ( c == 'C' )
    				stato = S1;
    			else if ( c == 'A' || c == 'D' || c == 'E' )
    				stato = S0;
    			break;
    		}
    	}
    }
    
    int main()
    {
    	Automa();
    
    	return 0;
    }

  7. #7
    oliva i compiti si fanno da se

  8. #8
    Questa invece è una versione modificata del programma di Vincenzo1968 che fa uso di un minor numero di stati interni, a patto di introdurre due nuove variabili con funzione di selettore e carattere precedente.
    codice:
    #include <stdio.h>
    #include <ctype.h>
    #include <conio.h>
    
    typedef enum tagStati
    {
    	S0,
    	S10,
    	S11,
    	S2,
    	S30,
    	S31
    
    } Stati;
    
    void Automa()
    {
    	Stati stato = S0;
    	// s= selettore; prv= carattere precedentemente digitato
    	int s,prv,c = 0;
    
    	while ( c != 'X' )
    	{
    		//c = toupper(getch());
    		c = toupper(_getch());
    		if ( c == 'X' )
    		{
    			printf("digitato X\n");
    			break;
    		}
    		else if ( c == 'A' || c == 'B' || c == 'C' || c == 'D' || c == 'E' )
    		{
    			printf("digitato %c\n", c);
    		}
    
    		switch(stato)
    		{
    		case S0:
    			if (( c == 'C' ) || ( c == 'B' ))
    			{
    				prv=c;
    				stato = S10;
    			}
    			else 
    				stato = S0;
    			break;
    		case S10:
    			if (prv=='C')
    				s=0;
    			else
    				s=1;
    			if ( c == 'A' )
    				stato = S11;
    			else if (( c == 'C' ) || ( c == 'B' ))
    			{
    				prv=c;
    				stato = S10;
    			}
    			else
    				stato = S0;
    			break;
    		case S11:
    			if ( c == 'B' )
    				stato = S2;
    			else if ( c == 'C' )
    			{
    				prv=c;
    				stato = S10;
    			}
    			else
    				stato = S0;
    			break;
    		case S2:
    			if (( c == 'E' ) && ( s == 0 ))
    				stato = S30;
    			else if (( c == 'A') && ( s == 1))
    				stato = S31;
    			else if (( c == 'C' ) || ( c == 'B' ))
    			{
    				prv=c;
    				stato = S10;
    			}
    			else
    				stato = S0;
    			break;
    		case S30:
    			if ( c == 'D' )
    			{
    				printf("Fermi tutti!!! CABED riconosciuta!\n");
    				stato = S0;
    			}
    			else if (( c == 'C' ) || ( c == 'B' ))
    			{
    				prv=c;
    				stato = S10;
    			}
    			else stato = S0;
    			break;
    		case S31:
    			if ( c == 'B' )
    			{
    				printf("Fermi tutti!!! BABAB riconosciuta!\n");
    				stato = S0;
    			}
    			else if ( c == 'C' )
    			{
    				prv=c;
    				stato = S10;
    			}
    			else stato = S0;
    			break;
    		}
    	}
    }
    
    int main()
    {
    	Automa();
    
    	return 0;
    }
    Saluti

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 © 2025 vBulletin Solutions, Inc. All rights reserved.