Advertisement
Guest User

Ex3

a guest
Nov 16th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct vetor Vetor;
  5.  
  6. struct vetor{
  7.     int * numeros;
  8.     int tamanho;
  9.     int tamanho_maximo;
  10. };
  11.  
  12. Vetor * vetor_cria (int tamanho_maximo);
  13. void vetor_libera(Vetor * p);
  14. void vetor_insere (Vetor * p, int numero);
  15. void vetor_remove (Vetor * p, int numero);
  16. int vetor_obtem_minimo (Vetor * p);
  17. int vetor_obtem_tamanho (Vetor * p);
  18.  
  19.  
  20. int main(){
  21.     int i, n, j, num;
  22.     scanf("%d",&n);
  23.     Vetor * p = vetor_cria(n);
  24.     char c;
  25.     scanf("%c",&c);
  26.     while(c != 'F'){
  27.         if(c == 'I'){
  28.             scanf("%d",&num);
  29.             vetor_insere(p,num);
  30.         }
  31.         if(c == 'M'){
  32.             printf("%d\n",vetor_obtem_minimo(p));
  33.         }
  34.         if(c == 'R'){
  35.             scanf("%d",&num);
  36.             vetor_remove(p,num);
  37.         }
  38.         scanf("%c",&c);
  39.     }
  40.    
  41.  
  42. }
  43.  
  44. Vetor * vetor_cria (int tamanho_maximo){
  45.     Vetor * v = (Vetor *) malloc(sizeof(Vetor));
  46.     if(v != NULL){
  47.         v->numeros = (int *) malloc(sizeof(int)*tamanho_maximo);
  48.         v->tamanho = 0;
  49.         v->tamanho_maximo = tamanho_maximo;
  50.     }
  51.     return v;
  52. }
  53.  
  54. void vetor_libera(Vetor * p){
  55.     free(p->numeros);
  56.     free(p);
  57. }
  58.  
  59. void vetor_insere (Vetor * p, int numero){
  60.     if(p->tamanho != p->tamanho_maximo){
  61.         p->numeros[p->tamanho] = numero;
  62.         p->tamanho++;
  63.     }
  64. }
  65.  
  66. void vetor_remove (Vetor * p, int numero){
  67.     if(p->tamanho != 0){
  68.         int i,j;
  69.         for(i = 0; i < p->tamanho; i++){
  70.             if(p->numeros[i] == numero){
  71.                 for(j = i; j < p->tamanho-1; j++){
  72.                     p->numeros[j] = p->numeros[j+1];
  73.                 }
  74.                 i--;
  75.                 p->tamanho--;
  76.             }
  77.         }
  78.     }
  79. }
  80.  
  81. int vetor_obtem_minimo (Vetor * p){
  82.     if(p->tamanho == 0){
  83.         return -1;
  84.     }
  85.     int min = p->numeros[0],i;
  86.     for(i = 1; i < vetor_obtem_tamanho(p); i++){
  87.         if(min > p->numeros[i]){
  88.             min = p->numeros[i];
  89.         }
  90.     }
  91.     return min;
  92. }
  93.  
  94. int vetor_obtem_tamanho (Vetor * p){
  95.     return p->tamanho;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement