• Sábado 1 de Junio de 2024, 11:04

Autor Tema:  Como Valido un Ciclo???  (Leído 1057 veces)

infonet

  • Nuevo Miembro
  • *
  • Mensajes: 14
    • Ver Perfil
Como Valido un Ciclo???
« en: Martes 1 de Septiembre de 2009, 20:50 »
0
Estimados tengo la dura tare de Validar en C++ con DEV C++ el Cliclo For y tengo una vaga ide, plis si me pueden apoyar gracias

les adjunto el codigo realizado hasta el momento

Por ejemplo que me valide esta instruccion:

for(x=1;x<=10;x=x+1)


Código: C++
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <conio.h>
  6. #include <windows.h>
  7. #include <math.h>
  8.  
  9. void gotoxy(short x, short y)
  10. { HANDLE hConsoleOutput; COORD Cursor_Pos = {x, y};
  11. hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
  12. SetConsoleCursorPosition(hConsoleOutput, Cursor_Pos); }
  13.  
  14.  
  15. void carga_caracteres()
  16. {
  17.  int i  ;  
  18.  
  19.  char car_mayusculas[100];
  20.  char car_minusculas[100];
  21.  char car_letras[100];
  22.  
  23.  
  24.  
  25. // for ( i=65; 90; i++);  // mayusculas
  26. //  car_mayusculas[i]=i;
  27.  
  28. // for (i=97; 122; i++);  // minusculas
  29. //  car_minusculas[i]=i;
  30.  
  31. //return;
  32.  }
  33.  
  34.  
  35.  
  36. void  est_exit(int num)
  37. {
  38.  switch(num)
  39.    {
  40.      case 0 :gotoxy(10,10);printf("Debe ingresar una f, en la primera posicion ");break;
  41.      case 1 :gotoxy(10,11);printf("Debe ingresar una o, en la segunda posicion");break;
  42.      case 1 :gotoxy(10,12);printf("Debe ingresar una r, en la tercera posicion");break;
  43.      case 2 :gotoxy(10,13);printf("Debe ingresar parentecis (");break;
  44.      case 3 :gotoxy(10,14);printf("Debe ingresar parentecis )");break;
  45.      case 4 :gotoxy(10,15);printf("Debe ingresar un blanco");break;
  46.      case 5 :gotoxy(10,16);printf("Debe ingresar una condición");break;
  47.      case 6 :gotoxy(10,17);printf("Debe ingresar una una llave {");break;
  48.      case 7 :gotoxy(10,18);printf("Debe ingresar una una llave }");break;
  49. }
  50.  getch();
  51. }
  52.  
  53.    
  54. main()
  55. {
  56. char sentencia [100];
  57. int n;
  58. int i;
  59. int cont=0;
  60. char validos[11];
  61.  validos[0]='(';
  62.  validos[1]=')';
  63.  validos[2]='{';
  64.  validos[3]='}';
  65.  validos[4]='<';
  66.  validos[5]='>';
  67.  validos[6]='=';
  68.  validos[7]='!';
  69.  validos[8]='&';
  70.  validos[9]='|';
  71.  validos[10]=char(32);
  72.  
  73.  
  74.       system("cls");            
  75.       gotoxy(5,5);printf(" Ingrese sentencia--> ");scanf("%s",sentencia);
  76.      
  77.       n=strlen(sentencia);
  78.      
  79.       i=0;
  80.      
  81. //      do {
  82.           if (sentencia[i] != 'f')
  83.              est_exit(i);
  84.           else
  85.             {          
  86.               i=i+1;
  87.               if (sentencia[i]!= 'o')
  88.                   est_exit(i);
  89.               else
  90.                  {          
  91.               i=i+1;
  92.               if (sentencia[i]!= 'r')
  93.                   est_exit(i);
  94.               else
  95.              {
  96.                 i=i+1;
  97.                 printf("%d",i);
  98.                 if ( sentencia[i] !=  ' ' )
  99.                    est_exit(4);
  100.                 else;
  101.                   do {
  102.                      cont=cont+1;
  103.                      i=i+1;
  104.                     } while ( sentencia[i]= '(') ;
  105.                 }    
  106.              }    
  107.            
  108. //         } while ( i != n);  
  109.            
  110.      
  111. }
  112.  
  113.  
  114.  

infonet

  • Nuevo Miembro
  • *
  • Mensajes: 14
    • Ver Perfil
Re: Como Valido un Ciclo???
« Respuesta #1 en: Miércoles 2 de Septiembre de 2009, 04:35 »
0
También estaba pensando en dejar en una variable el largo de la sentencia y con  otro for validar…
Que Opinan

infonet

  • Nuevo Miembro
  • *
  • Mensajes: 14
    • Ver Perfil
Re: Como Valido un Ciclo???
« Respuesta #2 en: Lunes 7 de Septiembre de 2009, 12:55 »
0
LISTO.
Código: C++
  1.  
  2.  
  3. int Sigue,Tamano,Cont,PosIni,i,PosVec,j,PosParen,Error,CanTonk,Tamano2,k,Opcion;
  4. char CadenaLeida[150],Bloques[50][150],SiFor[150],Tokens[50][150],NuevaCadena[150];
  5. int pIni;
  6. char wh[10],od[10];
  7.  
  8. int EsOperador(char QueAnalizar)
  9. {if (QueAnalizar=='+')
  10.  {return(1);}
  11.  else if (QueAnalizar=='-')
  12.  {return(1);}
  13.  else if (QueAnalizar=='*')
  14.  {return(1);}
  15.  else if (QueAnalizar=='/')
  16.  {return(1);}
  17.  else if (QueAnalizar=='(')
  18.  {return(2);}
  19.  else if (QueAnalizar==')')
  20.  {return(2);}
  21.  else if (QueAnalizar=='[')
  22.  {return(2);}
  23.  else if (QueAnalizar==']')
  24.  {return(2);}
  25.  else if (QueAnalizar=='<')
  26.  {return(3);}
  27.  else if (QueAnalizar=='>')
  28.  {return(3);}
  29.  else if (QueAnalizar=='=')
  30.  {return(3);}
  31.  else if (QueAnalizar=='!')
  32.  {return(3);}
  33.  else
  34.  {return(0);}
  35. }
  36.  
  37. void EsFor()
  38. {printf("nntDIGITE LA SENTENCIA: ");
  39.  scanf("%s",CadenaLeida);
  40.  
  41.  Tamano=strlen(CadenaLeida);
  42.  PosParen=0;
  43.  for(i=0;i<=Tamano-1;i++)
  44.  {
  45.   if(CadenaLeida[i]=='(')
  46.   {PosVec=0;
  47.    for(j=0;j<=i-1;j++)
  48.    {SiFor[PosVec]=CadenaLeida[j];
  49.     PosVec=PosVec+1;
  50.    }
  51.    PosParen=i;
  52.    break;
  53.   }
  54.  }
  55.  if(PosParen!=3)
  56.  {printf("ntLA SENTENCIA DIGITADANO NO CUMPLE CON LA SINTAXIS DEL FOR");
  57.   printf("ntERROR: %s", SiFor);
  58.  }
  59.  else
  60.   if(strcmp(SiFor,"for")!=0 && strcmp(SiFor,"For")!=0 && strcmp(SiFor,"FOR")!=0)
  61.   {printf("ntLA SENTENCIA DIGITADANO NO CUMPLE CON LA SINTAXIS DEL FOR");
  62.   printf("ntERROR: %s", SiFor);
  63.  }
  64.  else if (CadenaLeida[Tamano-1]!=')')
  65.  {printf("ntLA SENTENCIA DIGITADANO CUMPLE CON LA SINTAXIS DEL FOR");
  66.   printf("ntERROR: %c FALTA PARENTESIS DE CIERRE FINAL", CadenaLeida[Tamano-1]);
  67.  }
  68.  else
  69.  {Cont=0;
  70.   PosIni=4;
  71.   for(i=4;i<=Tamano-2;i++)
  72.   {
  73.    if(CadenaLeida[i]==';')
  74.    {PosVec=0;
  75.     for(j=PosIni;j<=i;j++)
  76.     {Bloques[Cont][PosVec]=CadenaLeida[j];
  77.      PosVec=PosVec+1;
  78.     }
  79.     Cont=Cont+1;
  80.     PosIni=i+1;
  81.    }
  82.   }
  83.  
  84.   PosVec=0;
  85.   for(j=PosIni;j<=Tamano-2;j++)
  86.   {Bloques[Cont][PosVec]=CadenaLeida[j];
  87.    PosVec=PosVec+1;
  88.   }
  89.   if (Cont<2)
  90.   {printf("ntLA SENTENCIA DIGITADANO CUMPLE CON LA SINTAXIS DEL FOR");
  91.    printf("ntERROR: FALTAN BLOQUES DEL FOR 'for([init-expr];[cond-expr];[loop-expr])'");
  92.   }
  93.   else if (Cont!=2)
  94.   {printf("ntLA SENTENCIA DIGITADANO CUMPLE CON LA SINTAXIS DEL FOR");
  95.    printf("ntERROR: HAY 3 BLOQUES PARA EL FOR 'for([init-expr];[cond-expr];[loop-expr])'");
  96.   }
  97.   else
  98.   {
  99.    strcpy(Tokens[0],SiFor);
  100.    Tokens[1][0]=CadenaLeida[3];
  101.    CanTonk=1;
  102.      for(i=0;i<=Cont;i++)
  103.    {
  104.     PosVec=0;
  105.     for(k=0;k<=149;k++)
  106.     {NuevaCadena[k]=0;}
  107.     Tamano2=strlen(Bloques[i])-1;
  108.      for(j=0;j<=Tamano2;j++)
  109.     {if(EsOperador(Bloques[i][j])==0 && Bloques[i][j]!=';')
  110.      {NuevaCadena[PosVec]=Bloques[i][j];
  111.       PosVec=PosVec+1;
  112.      }
  113.      else
  114.      {if (strlen(NuevaCadena)!=0)
  115.       {CanTonk=CanTonk+1;
  116.        for(k=0;k<=PosVec;k++)
  117.        {Tokens[CanTonk][k]=NuevaCadena[k];}
  118.              PosVec=0;
  119.        for(k=0;k<=149;k++)
  120.        {NuevaCadena[k]=0;}
  121.       }
  122.       CanTonk=CanTonk+1;
  123.       Tokens[CanTonk][0]=Bloques[i][j];
  124.      }
  125.     }
  126.    }
  127.    if (strlen(NuevaCadena)!=0)
  128.    {CanTonk=CanTonk+1;
  129.     for(k=0;k<=PosVec;k++)
  130.     {Tokens[CanTonk][k]=NuevaCadena[k];}
  131.     PosVec=0;
  132.     for(k=0;k<=149;k++)
  133.     {NuevaCadena[k]=0;}
  134.    }
  135.    CanTonk=CanTonk+1;
  136.    Tokens[CanTonk][0]=41; //Ascii=')'
  137.    printf("nntTOKENS IDENTIFICADOS");
  138.    for(i=0;i<=CanTonk;i++)
  139.    {if (strlen(Tokens[i])>0)
  140.     {printf("nt%dt%s",i+1,Tokens[i]);}
  141.     else
  142.     {printf("nt%dt%c",i+1,Tokens[i]);}
  143.     {printf("ttTOKENS OK VALIDADO..!!!");}
  144.     }
  145.    }
  146.   }
  147. }
  148. main()
  149. {Sigue=0;
  150.  do
  151.  {
  152.   system("cls");
  153.   printf("nnttttPROGRAMA    QUE    VALIDA");
  154.   printf("nntttttCICLO FORnn");
  155.   printf("nnttt1. ANALIZAR CICLO FORnnn");
  156.   printf("nnttttDIGITE OPCION: ");
  157.   scanf("%d", &Opcion);
  158.   for(i=0;i<=149;i++)
  159.   {CadenaLeida[i]=0;
  160.    SiFor[i]=0;
  161.   }
  162.   for(i=0;i<=49;i++)
  163.   {for(j=0;j<=149;j++)
  164.    {Bloques[i][j]=0;
  165.     Tokens[i][j]=0;
  166.    }
  167.   }
  168.   if(Opcion==1)
  169.   {EsFor();
  170.    printf("nnntEJECUTA EL PROGRAMA NUEVAMENTE?? [SI=1][NO=0]: ");
  171.    scanf("%d",&Sigue);
  172.   }
  173.   else
  174.   {printf("nnERROE DIGITE UNA OPCION VALIDA.");}
  175.  }while(Sigue!=0);
  176. }
  177.  
  178.  

ocelot_76

  • Nuevo Miembro
  • *
  • Mensajes: 8
    • Ver Perfil
Re: Como Valido un Ciclo???
« Respuesta #3 en: Martes 8 de Septiembre de 2009, 12:50 »
0
Tengo una duda a que te refieres con validar si te refieres a lo k imagino k es simplemente k kon otros metodos justifikar k un bucle for funciona, yo kreo k lo mas sencillo es acerlo kon la recursividad de funciones, tan facil como llamar una funcion el numero de vezes k es el bucle for i en cada llamada incrementar ese contador

Salu2

El conozimiento es poder el poder es superioridad

OuGi