Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- #include <stdio.h>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- int main(){
- // Lendo até EOF (Final de Arquivo)
- int a, b;
- while(cin>>a>>b){ // Assim ele le até o final de arquivo (EOF)
- // lógica...
- }
- // ---------------------------------------------------------------------
- int v[5]; // vetor de 5 posições, indices 0, 1, 2, 3, 4 [Sempre começa do 0 até total - 1]
- // Lendo em um vetor
- for(int i=0; i<5; i++)
- cin>>v[i]; // Lendo no indice i
- // Exibindo vetor
- for(int i=0; i<5; i++)
- cout<<<v[i]<<" ";
- // Usando o indice do vetor como contador
- int n, x;
- cin>>n; // Vetor de n posições
- for(int i=0; i<n; i++)
- v[i] = 0; // zerando todo vetor
- for(int i=0; i<n; i++){
- cin>>x;
- v[x]++; // conto 1 na posicao x
- }
- // ---------------------------------------------------------------------
- int m[4][3]; // Matriz de 4 linhas[0, 1, 2, 3] e 3 colunas[0, 1, 2]
- //Lendo em uma matriz
- for(int i=0; i<4; i++)
- for(int j=0; j<3; j++)
- cin>>m[i][j]; //Lendo na linha i, coluna j.
- // Exibindo a matriz
- for(int i=0; i<4; i++){
- for(int j=0; j<3; j++)
- cout<<m[i][j]<<" ";
- cout<<endl;
- }
- // ---------------------------------------------------------------------
- // Repetindo um laço N vezes
- int N;
- while(N--){
- // comandos...
- }
- // ---------------------------------------------------------------------
- // Ordenando um vetor
- int v[1000]; // exemplo: 1000 tamanho máximo
- int n;
- cin>>n; // tamanho do vetor atual
- for(int i=0; i<n; i++)
- cin>>v[i];
- sort(v, v+n); // Incluir biblioteca <algorithm>
- //(nome do vetor, nome + qtdade de posições) Isso se começar do indice 0
- // Exemplo começando do indice 1
- sort(v+1,v+n+1);
- // Exibindo o vetor crescente
- for(int i=0; i<n; i++)
- cout<<v[i]<<" ";
- // Exibindo o vetor decrescente
- for(int i=n-1; i>=0; i--)
- cout<<v[i]<<" ";
- // ---------------------------------------------------------------------
- // Brincando com strings (cadeia de caracteres/vetor de char)
- // Adicionar bibliotecas <cstring> e <ctype.h>
- char texto[100], texto2[100], palavra[30];
- gets(texto); // lendo um texto, ele le toda a linha na variavel texto.
- gets(texto2);
- cin>>palavra; // le até achar um espaço.
- // Tamanho de uma string
- int tam;
- tam = strlen(texto); // quantidade de caracteres na variavel texto, obs: espaço também é caractere.
- // Comparando duas strings
- if(strcmp(texto,texto2) == 0){
- // igual a 0 significa que os dois são iguais
- // caso contrario, sao diferentes...
- }
- // Copiar uma string em outra
- strcpy(texto, texto2); // copia a segunda string na primeira!
- // percorrer um texto
- for(int i=0; i<tam; i++){ // tam = tamanho do texto pego anteriormente /\
- cout<<texto[i]<<" "; // Exibe cada caractere de um texto
- }
- // Testar se é letra, número, maiscula, minuscula, etc..
- for(int i=0; i<tam; i++){
- if(isalpha(texto[i]))
- // é letra
- if(isdigit(texto[i]))
- // é número
- if(texto[i] == toupper(texto[i]))
- // é maiuscula
- if(texto[i] == tolower(texto[i]))
- // é minuscula
- }
- // OBS
- // sempre quando ler um número e depois for ler uma string
- // colocar um getchar(); entre as leituras
- // Exemplo:
- int idade;
- double altura;
- char nome[20];
- cin>>idade;
- getchar(); // entre um numero e uma string
- gets(nome);
- // entre string e numero não precisa!
- cin>>altura;
- // Comparar para ver se uma letra está entre b e k por exemplo:
- if(texto[i] > 'b' && texto[i] < 'k'){
- // c d e f g h i j
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment