damihenrique

Vetor_Matriz_String

Nov 14th, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.33 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <cstring>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. int main(){
  10.    
  11.     // Lendo até EOF (Final de Arquivo)
  12.    
  13.     int a, b;
  14.    
  15.     while(cin>>a>>b){ // Assim ele le até o final de arquivo (EOF)
  16.        
  17.         // lógica...  
  18.        
  19.     }
  20.  
  21.     // ---------------------------------------------------------------------
  22.  
  23.     int v[5]; // vetor de 5 posições, indices 0, 1, 2, 3, 4 [Sempre começa do 0 até total - 1]
  24.    
  25.     // Lendo em um vetor
  26.     for(int i=0; i<5; i++)
  27.         cin>>v[i];  // Lendo no indice i
  28.    
  29.     // Exibindo vetor
  30.     for(int i=0; i<5; i++)
  31.         cout<<<v[i]<<" ";
  32.        
  33.     // Usando o indice do vetor como contador
  34.    
  35.     int n, x;
  36.     cin>>n; // Vetor de n posições
  37.    
  38.     for(int i=0; i<n; i++)
  39.         v[i] = 0; // zerando todo vetor
  40.    
  41.     for(int i=0; i<n; i++){
  42.         cin>>x;  
  43.         v[x]++; // conto 1 na posicao x
  44.     }
  45.        
  46.     // ---------------------------------------------------------------------
  47.    
  48.     int m[4][3]; // Matriz de 4 linhas[0, 1, 2, 3] e 3 colunas[0, 1, 2]
  49.    
  50.     //Lendo em uma matriz
  51.     for(int i=0; i<4; i++)
  52.         for(int j=0; j<3; j++)
  53.             cin>>m[i][j];  //Lendo na linha i, coluna j.
  54.    
  55.     // Exibindo a matriz
  56.     for(int i=0; i<4; i++){
  57.         for(int j=0; j<3; j++)
  58.             cout<<m[i][j]<<" ";  
  59.         cout<<endl;
  60.     }
  61.  
  62.     // ---------------------------------------------------------------------
  63.    
  64.     // Repetindo um laço N vezes
  65.    
  66.     int N;
  67.    
  68.     while(N--){
  69.        
  70.         // comandos...
  71.     }
  72.    
  73.     // ---------------------------------------------------------------------
  74.    
  75.     // Ordenando um vetor
  76.    
  77.     int v[1000]; // exemplo: 1000 tamanho máximo
  78.     int n;
  79.    
  80.     cin>>n; // tamanho do vetor atual
  81.    
  82.     for(int i=0; i<n; i++)  
  83.         cin>>v[i];
  84.    
  85.     sort(v, v+n);  //  Incluir biblioteca <algorithm>
  86.     //(nome do vetor, nome + qtdade de posições)  Isso se começar do indice 0
  87.    
  88.     // Exemplo começando do indice 1
  89.     sort(v+1,v+n+1);
  90.    
  91.     // Exibindo o vetor crescente
  92.     for(int i=0; i<n; i++)  
  93.         cout<<v[i]<<" ";
  94.        
  95.     // Exibindo o vetor decrescente
  96.     for(int i=n-1; i>=0; i--)  
  97.         cout<<v[i]<<" ";
  98.    
  99.    
  100.     // ---------------------------------------------------------------------
  101.    
  102.     // Brincando com strings (cadeia de caracteres/vetor de char)
  103.     // Adicionar bibliotecas <cstring> e <ctype.h>
  104.      
  105.     char texto[100], texto2[100], palavra[30];
  106.    
  107.     gets(texto); // lendo um texto, ele le toda a linha na variavel texto.
  108.     gets(texto2);
  109.    
  110.     cin>>palavra; // le até achar um espaço.
  111.    
  112.     // Tamanho de uma string
  113.     int tam;
  114.     tam = strlen(texto); // quantidade de caracteres na variavel texto, obs: espaço também é caractere.
  115.    
  116.     // Comparando duas strings
  117.     if(strcmp(texto,texto2) == 0){
  118.         // igual a 0 significa que os dois são iguais
  119.         // caso contrario, sao diferentes...
  120.     }
  121.    
  122.     // Copiar uma string em outra
  123.     strcpy(texto, texto2); // copia a segunda string na primeira!
  124.    
  125.     // percorrer um texto
  126.     for(int i=0; i<tam; i++){ // tam = tamanho do texto pego anteriormente /\
  127.         cout<<texto[i]<<" "; // Exibe cada caractere de um texto
  128.     }
  129.    
  130.     // Testar se é letra, número, maiscula, minuscula, etc..
  131.    
  132.     for(int i=0; i<tam; i++){
  133.        
  134.         if(isalpha(texto[i]))
  135.             // é letra
  136.        
  137.         if(isdigit(texto[i]))
  138.             // é número
  139.        
  140.         if(texto[i] == toupper(texto[i]))
  141.             // é maiuscula
  142.        
  143.         if(texto[i] == tolower(texto[i]))
  144.             // é minuscula  
  145.     }
  146.    
  147.     // OBS
  148.     // sempre quando ler um número e depois for ler uma string
  149.     // colocar um getchar(); entre as leituras
  150.     // Exemplo:
  151.        
  152.     int idade;
  153.     double altura;
  154.     char nome[20];
  155.    
  156.     cin>>idade;
  157.     getchar(); // entre um numero e uma string
  158.     gets(nome);  
  159.              // entre string e numero não precisa!
  160.     cin>>altura;
  161.    
  162.    
  163.     // Comparar para ver se uma letra está entre b e k por exemplo:
  164.        
  165.     if(texto[i] > 'b' && texto[i] < 'k'){
  166.         // c d e f g h i j  
  167.     }
  168.    
  169.  
  170.     return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment