Advertisement
acongiux

Untitled

Jun 27th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.77 KB | None | 0 0
  1. /*
  2. SINTESE
  3.     Objetivo:   Armazenar os dados de venda dos agasalhos de uma loja
  4.     Entrada:    Codigo do agasalho, a cor do agasalho e o tamanho de cada agasalho cadastrado.
  5.     Saida:      Lista em ordem alfabetica das cores com os dados dos agasalhos cadastrados
  6. */
  7.  
  8.  
  9. #include <stdio.h>     
  10. #include <stdlib.h>             //alocacao
  11. #include <string.h>             //string
  12. #include <ctype.h>              //toupper
  13. #include <conio.h>     
  14.  
  15. #define MAX_COR 40              //tam da cor
  16. #define ARQUIVO "backup.bin"    //noem arquivo
  17. #define MAX_AGASALHO 200
  18.  
  19. typedef struct {
  20.    
  21.     int codigo;
  22.     char cor [MAX_COR];
  23.     char tamanho;
  24.    
  25. } ficha;
  26.  
  27.  
  28. /*PROTOTIPOS*/
  29.  
  30. void validaCodigo (long int *pCodigo);
  31. void corrigeString (char *pString);
  32. void validaCor (char *pCor);
  33. char validaTamanho (char tamanho);  
  34.  
  35. void arquivarRegistro (ficha *pAgasalho);
  36. void listar ();
  37.  
  38.  
  39.  
  40. int main(void) {
  41.     //Declaracoes
  42.    
  43.     ficha agasalho;
  44.     int opcaoMenu;
  45.    
  46.    
  47.     //Instrucoes
  48.     system ("TITLE  AGASALHOS");
  49.    
  50.     do{
  51.         system ("cls");
  52.         printf("\n*********************************\n");
  53.         printf("*\t\tMENU\t\t*");
  54.         printf("\n*********************************\n");
  55.         printf("*\t[1] - NOVO REGISTRO\t*\n");
  56.         printf("*\t[2] - LISTAR REGISTROS\t*\n");
  57.         printf("*\t[3] - SAIR\t\t*\n");
  58.         printf("*********************************\n");
  59.        
  60.    
  61.         printf("\t\nInforme o numero de acordo com o que se deseja:\t\n");
  62.         scanf ("%d", &opcaoMenu);
  63.        
  64.        
  65.         switch (opcaoMenu){
  66.            
  67.             case 1:
  68.                     system ("cls");
  69.                     printf ("\nInsira o codigo (max. 10 numeros) do agasalho: \n");
  70.                     scanf  ("%d", &agasalho.codigo);
  71.                     validaCodigo (&agasalho.codigo);   
  72.                     fflush (stdin);
  73.                     printf ("CODIGO: %d", agasalho.codigo);
  74.                     printf ("\n______________________________________________________\n");
  75.                
  76.    
  77.    
  78.                    
  79.                     printf ("Informe o tamanho do agasalho: \n\t(P) pequeno \t(M) medio \tG (grande): \n");
  80.                     scanf  ("%c", &agasalho.tamanho);
  81.                     fflush (stdin);
  82.                     agasalho.tamanho = toupper (agasalho.tamanho);
  83.                     agasalho.tamanho = validaTamanho (agasalho.tamanho);
  84.                     printf ("TAMANHO: %c\n", agasalho.tamanho);
  85.                     printf ("\n______________________________________________________\n");
  86.                    
  87.                    
  88.                     printf("Informe a cor do agasalho: \n");
  89.                     fgets (agasalho.cor, MAX_COR, stdin);
  90.                     validaCor (agasalho.cor);
  91.                     corrigeString (agasalho.cor);
  92.                     printf ("COR: %s", strlwr (agasalho.cor));
  93.                    
  94.                     arquivarRegistro (&agasalho);
  95.                    
  96.                     break;
  97.             case 2:
  98.                     system ("cls");
  99.                     listar ();
  100.                     printf ("\n\n\t\t Aperte enter para continuar...\n\n\n");
  101.                     getche();
  102.                     break;
  103.             case 3:
  104.                     system ("cls");
  105.                     printf("\nPROGRAMA FINALIZADO!\n");
  106.                     break;
  107.             default:
  108.                     printf("\n\tNUMERO INVALIDO!\n");
  109.                     break;
  110.         }
  111.     } while (opcaoMenu != 3);
  112.  
  113.  
  114.     return 0;
  115. }
  116.  
  117. /*SINTESE
  118.  
  119.     Objetivo:   Validar codigo do agasalho
  120.     Parametro:  Endereco de agasalho.codigo
  121.     Retorno:    Sem retorno
  122. */
  123.  
  124. void validaCodigo (long int *pCodigo){
  125.    
  126.    
  127.     while (*pCodigo < 0 || *pCodigo > 1000000000 ){
  128.     //  system("cls");
  129.         printf ("\n\tCODIGO INVALIDO! \nInforme um codigo MAIOR que ZERO e MENOR que 10 digitos: ");
  130.         scanf("%ld", pCodigo);
  131.     }
  132. }
  133.  
  134. /*SINTESE
  135.  
  136.     Objetivo:   Validar tamanho do agasalho
  137.     Parametro:  Endereco de tamanho.codigo
  138.     Retorno:    Tamanho validado
  139. */
  140.  
  141.  
  142. char validaTamanho (char tamanho){
  143.    
  144.     while ((tamanho != 'P' && tamanho != 'M' && tamanho != 'G') || tamanho == '\n'){
  145.         //system ("cls");
  146.         printf("\n\tTAMANHO INVALIDO! \nInforme novamente o tamanho do agasalho: \n\t(P) pequeno \t(M) medio \tG (grande): \n");
  147.         scanf  ("%c", &tamanho);
  148.         fflush (stdin);
  149.         tamanho = toupper (tamanho);
  150.  
  151.     }
  152. }
  153.  
  154. /*SINTESE
  155.  
  156.     Objetivo:   Corrigir string
  157.     Parametro:  string
  158.     Retorno:    Sem retorno
  159. */
  160.  
  161. void corrigeString (char *pString){
  162.    
  163.     if (pString [strlen(pString)-1] == '\n'){
  164.         pString [strlen(pString)-1] = '\0';
  165.     }
  166. }
  167.  
  168.  
  169.  
  170. /*SINTESE
  171.  
  172.     Objetivo:   Validar cor
  173.     Parametro:  agasalho.cor
  174.     Retorno:    Sem retorno
  175. */
  176. void validaCor (char *pCor){
  177.    
  178.     while (strlen (pCor) == 0 || pCor [0] == '\n'){
  179.         //system ("cls");
  180.         printf ("\n\tCOR INVALIDA! \nInforme a cor novamente: ");
  181.         fgets (pCor, MAX_COR, stdin);
  182.         pCor = strlwr (pCor);
  183.         corrigeString (pCor);
  184.        
  185.     }
  186. }
  187.  
  188.  
  189. //------------------------------------------------------------------------------------------------------------------------------ARQUIVOS-----------------------
  190. /*SINTESE
  191.  
  192.     Objetivo:   Cadastrar os dados dos agasalhos usando arquivos
  193.     Parametro:  codigo, cor e tamanho
  194.     Retorno:    Sem retorno
  195. */
  196.  
  197. void arquivarRegistro (ficha *pAgasalho){
  198.    
  199.     FILE *arquivo;
  200.     arquivo = fopen (ARQUIVO, "ab");
  201.    
  202.    
  203.     if  (arquivo == NULL){
  204.        
  205.         printf ("\nARQUIVO NAO FOI CRIADO!\n");
  206.         exit (1);
  207.     }else {
  208.        
  209.        
  210.         fwrite (pAgasalho, sizeof (ficha), 1, arquivo);
  211.        
  212.        
  213.         /*
  214.         fwrite (&pAgasalho->codigo, sizeof ( int), 1, arquivo);
  215.         fwrite (&pAgasalho->tamanho, sizeof (char), 1, arquivo);
  216.         fwrite (pAgasalho->cor, sizeof (char), MAX_COR, arquivo);
  217.         */
  218.        
  219.        
  220.         system ("cls");
  221.         printf("\nARQUIVO CRIADO COM SUCESSO!\n");
  222.     }
  223.  
  224.     fclose (arquivo);  
  225.    
  226. }
  227.  
  228. void listar (){
  229.    
  230.    
  231.     ficha agasalho;
  232.     FILE *arquivo;
  233.     arquivo = fopen (ARQUIVO, "rb");
  234.    
  235.    
  236.     if (arquivo == NULL){
  237.    
  238.         system ("cls");
  239.         printf ("ARQUIVO NAO ENCONTRADO!\n");
  240.         exit(1);
  241.     }else {
  242.        
  243.         system ("cls");
  244.         printf ("\t CODIGO \t\tTAMANHO \t\t COR");
  245.        
  246.         /*
  247.         while (fread (&agasalho.codigo, sizeof ( int), 1, arquivo) != 0){
  248.            
  249.             fread (&agasalho.tamanho, sizeof (char), 1, arquivo);
  250.             fread (agasalho.cor, sizeof (char), MAX_COR, arquivo);
  251.            
  252.         */ 
  253.         while (! feof (arquivo)){
  254.            
  255.             fread (&agasalho, sizeof (ficha), 1, arquivo);
  256.              
  257.              
  258.             printf ("\t %d ", agasalho.codigo);
  259.             printf ("\t\t%s ", agasalho.tamanho);
  260.             printf ("\t\t %s", agasalho.cor);
  261.            
  262.         }  
  263.            
  264.            
  265.         }
  266.    
  267.    
  268.     fclose(arquivo);
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement