Advertisement
Gibua

Untitled

Mar 20th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <locale.h>
  4.  
  5. typedef struct{
  6.   int tamanhoInicial;
  7.   int qtdeElementos;
  8.   int* vetorDinamico;
  9.   int tamanhoVetor;
  10. } NossaLista;
  11.  
  12. void printVetor(int *v, int tamanhoVetor){
  13.   printf("\n (");
  14.   for(int i = 0; i < tamanhoVetor; i++){
  15.     printf(" %d ", v[i]);
  16.   }
  17.   printf(")\n");
  18. }
  19.  
  20. void zerarVetor(int *v,int posInicial, int tamanhoVetor){
  21.   for (int i = posInicial; i < tamanhoVetor; i++) {
  22.     v[i]=0;
  23.   }
  24. }
  25.  
  26. int somaComponentes(int *v, int tamanhoVetor){
  27.   int soma = 0;
  28.   for (size_t i = 0; i < tamanhoVetor; i++) {
  29.     soma += v[i];
  30.   }
  31.   return soma;
  32. }
  33.  
  34. void atribuirValores(int *v,int tamanhoVetor){
  35.   for (size_t i = 0; i < tamanhoVetor; i++){
  36.     v[i] = (i*i+7)-(i%3)*2;
  37.   }
  38. }
  39.  
  40. int adicionaElemento(int *v, int i){
  41.  printf("\n Digite o elemento: ");
  42.  scanf("%d", &v[i]);
  43. }
  44.  
  45. void aumentaVetor(int *v,int *tamanhoVetor){
  46.   int i=0;
  47.   printf("\n Digite por quanto você quer aumentar o vetor: ");
  48.   scanf("%d", &i);
  49.   int j = *tamanhoVetor;
  50.   *tamanhoVetor += i;
  51.   v = realloc(v,sizeof(int) * (*tamanhoVetor));
  52.   zerarVetor(v, j,*tamanhoVetor);
  53. }
  54.  
  55. void menu(NossaLista estrutura){
  56.  
  57.   int escolha, continuar = 1, pos = 0;
  58.   while(continuar){
  59.     printf("\n Digite a sua opção: \n 1 - Imprimir o vetor\n 2 - Adicionar Elemento\n 3 - Aumentar Vetor\n 4 - Terminar o programa\n > ");
  60.     scanf("%d", &escolha);
  61.     switch (escolha) {
  62.       case 1:
  63.         printVetor(estrutura.vetorDinamico,estrutura.tamanhoVetor);
  64.         break;
  65.       case 2:
  66.         adicionaElemento(estrutura.vetorDinamico,estrutura.qtdeElementos);
  67.         estrutura.qtdeElementos++;
  68.         //pos = pos%tamanhoInicial;
  69.         break;
  70.       case 3:
  71.         aumentaVetor(estrutura.vetorDinamico, &estrutura.tamanhoVetor);  
  72.         break;
  73.       case 4:
  74.         printf("\n O programa irá fechar!");
  75.         continuar = 0;
  76.       default:
  77.         if (continuar!=0) printf("\n Opção inválida!!!\n");
  78.     }
  79.   }
  80. }
  81.  
  82. int main(){
  83.   setlocale(LC_CTYPE,"");
  84.  
  85.   NossaLista listaDinamica;
  86.  
  87.   printf(" Digite o tamanho inicial do vetor: ");
  88.   scanf("%d", &listaDinamica.tamanhoInicial);
  89.   listaDinamica.qtdeElementos = 0;
  90.   listaDinamica.vetorDinamico = malloc(sizeof(int) * listaDinamica.tamanhoInicial);
  91.   listaDinamica.tamanhoVetor = listaDinamica.tamanhoInicial;
  92.   zerarVetor(listaDinamica.vetorDinamico, 0, listaDinamica.tamanhoVetor);
  93.   menu(listaDinamica);
  94.   printVetor(listaDinamica.vetorDinamico,listaDinamica.tamanhoVetor);
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement