Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.34 KB | None | 0 0
  1. // TP1 2015_2016.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. typedef enum _STATUS { ERROR,OK } STATUS;
  9. typedef enum _BOOLEAN { FALSE=0, TRUE=1 } BOOLEAN;
  10.  
  11. #define DATA(node) ((node)->data)
  12. #define NEXT(node) ((node)->next)
  13. #define MAX_NOME 20
  14.  
  15. //Definição da estrutura smartphone
  16. typedef struct _SMARTPHONE {
  17.     char marca[MAX_NOME];
  18.     char modelo[MAX_NOME];
  19.     float preco;
  20.     BOOLEAN dualsim; // TRUE = é dual sim
  21. }SMARTPHONE;
  22. //Definição da estrutura nó
  23. typedef struct _List_Node {
  24.     void *data;
  25.     struct _List_Node * next;
  26. }List_Node;
  27. typedef List_Node* List;
  28.  
  29. //Declaração das funções
  30. List_Node *new_node(void *data);
  31. List_Node *insert_Inicio(List L, void *data);
  32. STATUS ReadListFromFile(List *pL,char *filename);
  33.  
  34. BOOLEAN emptyList(List L);
  35. void ShowValues(List L);
  36. int contarMarcaDual(List L);
  37.  
  38. //Programa principal
  39. int main(int argc,char *argv[]) {
  40.     List L = NULL;
  41.     int d = 0;
  42.  
  43.     if (!ReadListFromFile(&L, "produtos.txt")) return EXIT_FAILURE;
  44.  
  45.     ShowValues(L);
  46.     d=contarMarcaDual(L);
  47.     printf("Tem %d com dualsim.\n",d);
  48.  
  49.     getchar();
  50.     getchar();
  51.     EXIT_SUCCESS;
  52.  
  53.  
  54. }
  55.  
  56. List_Node * new_node(void *data){
  57.     List_Node *new_no;
  58.     if ((new_no = (List_Node*)malloc(sizeof(List_Node))) != NULL) {
  59.         DATA(new_no) = data;
  60.         NEXT(new_no) = NULL;
  61.     }
  62.     return new_no;
  63. }
  64. List_Node *insert_Inicio(List L, void *data) {
  65.     List_Node *new_no;
  66.     if ((new_no = new_node(data)) != NULL)
  67.         NEXT(new_no) = L;
  68.  
  69.     return new_no;
  70. }
  71. STATUS ReadListFromFile(List *pL, char *filename) {
  72.     FILE *fp;
  73.     SMARTPHONE phone, *ptr;
  74.  
  75.     if ((fp = fopen(filename, "r")) != NULL) {
  76.         while (!feof(fp)) {
  77.             fscanf(fp, "%[^;];%[^;];%f;%d\n", phone.marca, phone.modelo, &phone.preco, &phone.dualsim);
  78.  
  79.             if (((ptr = (SMARTPHONE*)malloc(sizeof(SMARTPHONE))) != NULL && (*pL = insert_Inicio(*pL, ptr)) != NULL)) {
  80.                 strcpy(ptr->marca, phone.marca);
  81.                 strcpy(ptr->modelo, phone.modelo);
  82.                 ptr->preco = phone.preco;
  83.                 ptr->dualsim = phone.dualsim;
  84.             }
  85.             else
  86.                 return ERROR;
  87.         }
  88.         fclose(fp);
  89.         return OK;
  90.     }
  91.     else
  92.         return ERROR;
  93. }
  94. void ShowValues(List L) {
  95.     int i = 0;
  96.     char straux[100] = { '\0' };
  97.     List_Node *paux = L;
  98.  
  99.     if (emptyList(paux) == TRUE) {
  100.         printf("\nLista de livros vazia.\n");
  101.         return;
  102.     }
  103.     printf("Conteudo da Lista de Livros: \n");
  104.     while (paux != NULL){
  105.         if ((((SMARTPHONE*)paux->data)->dualsim) == 0)
  106.             strcpy(straux, "Nao");
  107.         else
  108.             strcpy(straux, "Sim");
  109.  
  110.         printf("[%d]:Marca: %s | Modelo: %s | Preco: %.2f | Dualsim: %s \n", i + 1, ((SMARTPHONE *)DATA(paux))->marca, ((SMARTPHONE *)DATA(paux))->modelo, ((SMARTPHONE *)DATA(paux))->preco, straux);
  111.         paux = NEXT(paux);
  112.         i++;
  113.     }
  114.     printf(" FIM \n\n");
  115. }//-----------------------------------------------------------------------
  116. BOOLEAN emptyList(List L) {
  117.     return (L == NULL) ? TRUE : FALSE;
  118. }//-----------------------------------------------------------------------
  119. int contarMarcaDual(List L) {
  120.     List_Node *pTemp= L;
  121.     int contador=0;
  122.     char marca_aux[10];
  123.  
  124.     printf("Insira a marca: ");
  125.     scanf(" %[^\n]", marca_aux);
  126.  
  127.     while (pTemp != NULL) {
  128.  
  129.         if ( (strcmp(((SMARTPHONE *)DATA(pTemp))->marca, marca_aux)) == 0 && ((SMARTPHONE *)DATA(pTemp))->dualsim == TRUE)
  130.             contador++;
  131.  
  132.  
  133.         pTemp = NEXT(pTemp);
  134.     }
  135.     return contador;
  136. }
  137. List criarListaPar(List L) {
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement