domingo, 26 de fevereiro de 2017

Comparando arrays de caracteres

Vamos preencher dois arrays de chars e comparar suas posições.
A comparação é feita em pares iguais de posições. Ex:
Posição 0 de A x Posição 0 de B, para sabermos qual elemento
é maior ou menor ou igual, em cada par comparado.

Veja abaixo imagens do programa em execução:



Veja abaixo o código do programa:


import java.util.Scanner;

public class PROJETO {
     public static int tam = 20;
     static int Comp_Num ( int a, int b ) {
         int res = 0;
         if ( a == b )
              res = a;
         else if ( a > b ) {
              res = b;
         } else if ( a < b ) {
              res = a;
         }
         return res;
     }
     public static void main ( String args [ ] ) {
         J jht = new J ( );
         jht.textbackground ( jht.WHITE );
         jht.Moldura ( ( short ) 1, ( short ) 27, ( short ) 2, ( short ) 78 );
         jht.title ( " " );
         int z = 0, y = 0;
         int a = 0;
         //int [ ] A = new int [ tam ];
         //int [ ] B = new int [ tam ];
         char [ ] A = new char [ tam ];
         char [ ] B = new char [ tam ];
         int i, p, q, n = 0;
         do {
              jht.cls ( );
              jht.textcolor ( jht.LIGHTRED );
              jht.gotoxy ( ( int ) 25, ( int ) 3 );
              System.out.printf ( "COMPARANDO DOIS ARRAYS DE CARACTERES" );
              jht.textcolor ( jht.LIGHTBLUE );
              jht.gotoxy ( ( int ) 25, ( int ) 5 );
              System.out.printf ( "Quantos números terá o array A ? " );
              jht.textcolor ( jht.LIGHTRED );
              Scanner sc = new Scanner ( System.in );
              p = sc.nextInt ( );
              jht.textcolor ( jht.BLACK );
              jht.gotoxy ( ( int ) 20, ( int ) 9 );
              if ( p < 0 || p > tam ) {
                   System.out.printf ( "Número Acima de 0 e menor que %d ", tam );
                   jht.getche ( );
                   jht.cls ( );
              }
         } while ( ( p < 0 ) || ( p > tam ) );
         for ( i = 0; i < p; i++ ) {
              jht.cls ( );
              jht.textcolor ( jht.LIGHTRED );
              jht.gotoxy ( ( int ) 25, ( int ) 3 );
              System.out.printf ( "COMPARANDO DOIS ARRAYS DE CARACTERES" );
              jht.textcolor ( jht.LIGHTBLUE );
              jht.gotoxy ( ( int ) 32, ( int ) 5 );
              System.out.printf ( "Insira o " );
              jht.textcolor ( jht.LIGHTRED );
              jht.gotoxy ( ( int ) 40, ( int ) 5 );
              System.out.printf ( " %do", i + 1 );
              jht.textcolor ( jht.LIGHTBLUE );
              jht.gotoxy ( ( int ) 44, ( int ) 5 );
              System.out.printf ( "Número : " );
              jht.textcolor ( jht.LIGHTRED );
              Scanner sc = new Scanner( System.in );
              String c = sc.next();
              A [ i ] = c.charAt( 0 );
         }
         do {
              jht.cls ( );
              jht.textcolor ( jht.LIGHTRED );
              jht.gotoxy ( ( int ) 25, ( int ) 3 );
              System.out.printf ( "COMPARANDO DOIS ARRAYS DE CARACTERES" );
              jht.textcolor ( jht.LIGHTBLUE );
              jht.gotoxy ( ( int ) 25, ( int ) 5 );
              System.out.printf ( "Quantos números terá o array B ? " );
              jht.textcolor ( jht.LIGHTRED );
              Scanner sc = new Scanner ( System.in );
              q = sc.nextInt ( );
              jht.textcolor ( jht.BLACK );
              jht.gotoxy ( ( int ) 20, ( int ) 9 );
              if ( q < 0 || q > tam ) {
                   System.out.printf ( "Número Acima de 0 e menor que %d ", tam );
                   jht.getche ( );
                   jht.cls ( );
              }
         } while ( ( q < 0 ) || ( q > tam ) );
         for ( i = 0; i < q; i++ ) {
              jht.cls ( );
              jht.textcolor ( jht.LIGHTRED );
              jht.gotoxy ( ( int ) 25, ( int ) 3 );
              System.out.printf ( "COMPARANDO DOIS ARRAYS DE CARACTERES" );
              jht.textcolor ( jht.LIGHTBLUE );
              jht.gotoxy ( ( int ) 32, ( int ) 5 );
              System.out.printf ( "Insira o " );
              jht.textcolor ( jht.LIGHTRED );
              jht.gotoxy ( ( int ) 40, ( int ) 5 );
              System.out.printf ( " %do", i + 1 );
              jht.textcolor ( jht.LIGHTBLUE );
              jht.gotoxy ( ( int ) 44, ( int ) 5 );
              System.out.printf ( "Número : " );
              jht.textcolor ( jht.LIGHTRED );
              Scanner sc = new Scanner( System.in );
              String c = sc.next();
              B [ i ] = c.charAt( 0 );
         }
         jht.textcolor ( jht.LIGHTBLUE );
         jht.gotoxy ( ( int ) 22, ( int ) 7 );
         System.out.printf ( "Caracteres inseridos no array A ==> " );
         jht.textcolor ( jht.LIGHTRED );
         System.out.printf ( "%d", p );
         jht.textcolor ( jht.LIGHTRED );
         jht.gotoxy ( ( int ) 22, ( int ) 9 );
         for ( i = 0; i < p; i++ ) {
              System.out.printf ( " %c ", A [ i ] );
         }
         jht.textcolor ( jht.LIGHTBLUE );
         jht.gotoxy ( ( int ) 22, ( int ) 11 );
         System.out.printf ( "Caracteres inseridos no array B ==> " );
         jht.textcolor ( jht.LIGHTRED );
         System.out.printf ( "%d", q );
         jht.textcolor ( jht.LIGHTRED );
         jht.gotoxy ( ( int ) 22, ( int ) 13 );
         n = Comp_Num ( p, q );
         for ( i = 0; i < q; i++ ) {
              System.out.printf ( " %c ", B [ i ] );
         }
         jht.getche ( );

         jht.gotoxy ( ( int ) 22, ( int ) 14 );
         for ( i = 0; i < n; i++ ) {
              jht.textcolor ( jht.LIGHTRED );
              if ( A [ i ] > B [ i ] ) {
                   jht.textcolor ( jht.LIGHTRED );
                   System.out.printf ("\n\t\t%d° ", ( i + 1 ) );
                   jht.textcolor ( jht.LIGHTBLUE );
                   System.out.printf ("Elemento do array ");
                   jht.textcolor ( jht.LIGHTRED );
                   System.out.printf ("A");
                   jht.textcolor ( jht.LIGHTBLUE );
                   System.out.printf (" é maior que o ");
                   jht.textcolor ( jht.LIGHTRED );
                   System.out.printf ("%d° ", ( i + 1 ) );
                   jht.textcolor ( jht.LIGHTBLUE );
                   System.out.printf ("do array ");
                   jht.textcolor ( jht.LIGHTRED );
                   System.out.printf ("B");
              }
              jht.textcolor ( jht.LIGHTBLUE );
              if ( A [ i ] < B [ i ] ) {
                   jht.textcolor ( jht.LIGHTRED );
                   System.out.printf ("\n\t\t%d° ", ( i + 1 ) );
                   jht.textcolor ( jht.LIGHTBLUE );
                   System.out.printf ("Elemento do array ");
                   jht.textcolor ( jht.LIGHTRED );
                   System.out.printf ("A");
                   jht.textcolor ( jht.LIGHTBLUE );
                   System.out.printf (" é menor que o ");
                   jht.textcolor ( jht.LIGHTRED );
                   System.out.printf ("%d° ", ( i + 1 ) );
                   jht.textcolor ( jht.LIGHTBLUE );
                   System.out.printf ("do array ");
                   jht.textcolor ( jht.LIGHTRED );
                   System.out.printf ("B");
              } else {
                   if ( A [ i ] == B [ i ] ) {
                        jht.textcolor ( jht.LIGHTRED );
                        System.out.printf ("\n\t\t%d° ", ( i + 1 ) );
                        jht.textcolor ( jht.LIGHTBLUE );
                        System.out.printf ("Elemento do array ");
                        jht.textcolor ( jht.LIGHTRED );
                        System.out.printf ("A");
                        jht.textcolor ( jht.LIGHTBLUE );
                        System.out.printf (" é igual ao ");
                        jht.textcolor ( jht.LIGHTRED );
                        System.out.printf ("%d° ", ( i + 1 ) );
                        jht.textcolor ( jht.LIGHTBLUE );
                        System.out.printf ("do array ");
                        jht.textcolor ( jht.LIGHTRED );
                        System.out.printf ("B");
                   }
              }
         }
         jht.textcolor ( jht.LIGHTBLUE );
         System.out.printf ( "\n\t\tO array ");
         jht.textcolor ( jht.LIGHTRED );
         System.out.printf ("A");
         jht.textcolor ( jht.LIGHTBLUE );
         System.out.printf (" tem");
         jht.textcolor ( jht.LIGHTRED );
         System.out.printf (" %d", p );
         jht.textcolor ( jht.LIGHTBLUE );
         System.out.printf (" elementos");
         jht.textcolor ( jht.LIGHTBLUE );
         System.out.printf ( "\n\t\tO array ");
         jht.textcolor ( jht.LIGHTRED );
         System.out.printf ("B");
         jht.textcolor ( jht.LIGHTBLUE );
         System.out.printf (" tem");
         jht.textcolor ( jht.LIGHTRED );
         System.out.printf (" %d", q );
         jht.textcolor ( jht.LIGHTBLUE );
         System.out.printf (" elementos");
         if ( p > q ) {
              jht.textcolor ( jht.LIGHTBLUE );
              System.out.printf ( "\n\t\tO array ");
              jht.textcolor ( jht.LIGHTRED );
              System.out.printf ("A");
              jht.textcolor ( jht.LIGHTBLUE );
              System.out.printf (" tem");
              jht.textcolor ( jht.LIGHTRED );
              System.out.printf (" %d", p - q );
              jht.textcolor ( jht.LIGHTBLUE );
              System.out.printf (" elementos a mais que o array ");
              jht.textcolor ( jht.LIGHTRED );
              System.out.printf ("B");
         } else if ( q > p ) {
              jht.textcolor ( jht.LIGHTBLUE );
              System.out.printf ( "\n\t\tO array ");
              jht.textcolor ( jht.LIGHTRED );
              System.out.printf ("B");
              jht.textcolor ( jht.LIGHTBLUE );
              System.out.printf (" tem");
              jht.textcolor ( jht.LIGHTRED );
              System.out.printf (" %d", q - p );
              jht.textcolor ( jht.LIGHTBLUE );
              System.out.printf (" elementos a mais que o array ");
              jht.textcolor ( jht.LIGHTRED );
              System.out.printf ("A");
         } else if ( p == q ) {
              jht.textcolor ( jht.LIGHTBLUE );
              System.out.printf ( "\n\t\tO array ");
              jht.textcolor ( jht.LIGHTRED );
              System.out.printf ("A");
              jht.textcolor ( jht.LIGHTBLUE );
              System.out.printf (" tem o mesmo número de elementos do array ");
              jht.textcolor ( jht.LIGHTRED );
              System.out.printf ("B");
         }
         jht.textcolor ( jht.LIGHTRED );
         System.out.printf ( "\n\n\n\t\t\t\tMUITO OBRIGADO" );
         jht.textbackground ( jht.LIGHTMAGENTA );
         //A coluna esquerda da moldura está sendo
         //refeita através deste método apaga
         jht.Apaga ( ( short ) 4, ( short ) 26, ( short ) 0,( short ) 2 );
         jht.cursortype ( ( short ) 0 );//Apaga o cursor
         jht.getche ( );
     }
}

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.