ricop522

Untitled

Mar 30th, 2015
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define TAM 100
  4.  
  5. void inserir(int vet[], int *qtd);
  6. int pesquisar (int vet [], int *qtd, int valor);
  7. void imprimir(int vet[], int qtd);
  8.  
  9. int main()
  10. {
  11.         int tam=0,i,op, valor, qtd;
  12.         int v[TAM];
  13.         do{
  14.                 printf("MENU\n\n");
  15.                 printf("1- Inserir um valor. \n");
  16.                 printf("2- Pesquisar um valor. \n");
  17.                 printf("3- Imprimir o vetor. \n");
  18.                 printf("Digite sua opcao [0 para sair]. \n");
  19.                 scanf("%i",&op);
  20.                 switch(op){
  21.                     case 1:
  22.                         inserir(v, &tam);
  23.                         break;
  24.  
  25.                     case 2:
  26.                         scanf ("%d", &valor);
  27.  
  28.                         if (pesquisar(v, &tam, valor))
  29.                         {
  30.                             printf ("Valor ja existe no vetor.\n");
  31.                         }else
  32.                         {
  33.                             printf ("Valor nao existe no vetor.\n");
  34.                         }
  35.                         break;
  36.                     case 3:
  37.                         imprimir(v, tam);
  38.                         break;
  39.                     default :
  40.                         if(op!=0){
  41.                                 printf("Ops, opcao invalida! \n\n\n");
  42.                         }
  43.                         break;
  44.  
  45.                 }
  46.         }while(op!=0);
  47.         system ("pause");
  48.         return 0;
  49. }
  50.  
  51. void inserir(int vet[], int *qtd)
  52. {
  53.         int inserir_denovo = 1;
  54.         while (inserir_denovo != 2) {
  55.              if(*qtd < TAM){
  56.                int valor;
  57.                printf("Digite o valor a ser inserido:\n");
  58.                scanf("%i", &valor);
  59.  
  60.                if(pesquisar(vet, *qtd, valor))
  61.                {
  62.                    printf ("O valor inserido ja existe no vetor.\n");
  63.                }
  64.                else
  65.                {
  66.                   vet[*qtd] = valor;
  67.                   ++(*qtd);
  68.                }
  69.         }
  70.  
  71.             else {
  72.               printf("Ops, o vetor esta cheio!\n");
  73.         }
  74.  
  75.  
  76.         printf("Deseja continuar inserindo? '1'para SIM ou '2' para NAO\n");
  77.         scanf("%d", &inserir_denovo);
  78.     }
  79.  
  80. }
  81.  
  82. int pesquisar (int vet [], int *qtd, int valor)
  83. {
  84.         int i;
  85.  
  86.         printf ("Informe o valor a ser pesquisado: \n"); scanf ("%d", &valor);
  87.         for (i=0;i<qtd;i++)
  88.         {
  89.                 if (vet[i] == valor)
  90.                     return 1;
  91.         }
  92.         return 0;
  93.  
  94. }
  95.  
  96. void imprimir(int vet[], int qtd)
  97. {
  98.         int i;
  99.         printf("Vetor: \n");
  100.         for(i=0; i<qtd; ++i)
  101.         {
  102.                 printf("%d ", vet[i]);
  103.         }
  104.         printf("\n\n");
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment