Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // TP1 2015_2016.cpp : Defines the entry point for the console application.
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef enum _STATUS { ERROR,OK } STATUS;
- typedef enum _BOOLEAN { FALSE=0, TRUE=1 } BOOLEAN;
- #define DATA(node) ((node)->data)
- #define NEXT(node) ((node)->next)
- #define MAX_NOME 20
- //Definição da estrutura smartphone
- typedef struct _SMARTPHONE {
- char marca[MAX_NOME];
- char modelo[MAX_NOME];
- float preco;
- BOOLEAN dualsim; // TRUE = é dual sim
- }SMARTPHONE;
- //Definição da estrutura nó
- typedef struct _List_Node {
- void *data;
- struct _List_Node * next;
- }List_Node;
- typedef List_Node* List;
- //Declaração das funções
- List_Node *new_node(void *data);
- List_Node *insert_Inicio(List L, void *data);
- STATUS ReadListFromFile(List *pL,char *filename);
- BOOLEAN emptyList(List L);
- void ShowValues(List L);
- int contarMarcaDual(List L);
- //Programa principal
- int main(int argc,char *argv[]) {
- List L = NULL;
- int d = 0;
- if (!ReadListFromFile(&L, "produtos.txt")) return EXIT_FAILURE;
- ShowValues(L);
- d=contarMarcaDual(L);
- printf("Tem %d com dualsim.\n",d);
- getchar();
- getchar();
- EXIT_SUCCESS;
- }
- List_Node * new_node(void *data){
- List_Node *new_no;
- if ((new_no = (List_Node*)malloc(sizeof(List_Node))) != NULL) {
- DATA(new_no) = data;
- NEXT(new_no) = NULL;
- }
- return new_no;
- }
- List_Node *insert_Inicio(List L, void *data) {
- List_Node *new_no;
- if ((new_no = new_node(data)) != NULL)
- NEXT(new_no) = L;
- return new_no;
- }
- STATUS ReadListFromFile(List *pL, char *filename) {
- FILE *fp;
- SMARTPHONE phone, *ptr;
- if ((fp = fopen(filename, "r")) != NULL) {
- while (!feof(fp)) {
- fscanf(fp, "%[^;];%[^;];%f;%d\n", phone.marca, phone.modelo, &phone.preco, &phone.dualsim);
- if (((ptr = (SMARTPHONE*)malloc(sizeof(SMARTPHONE))) != NULL && (*pL = insert_Inicio(*pL, ptr)) != NULL)) {
- strcpy(ptr->marca, phone.marca);
- strcpy(ptr->modelo, phone.modelo);
- ptr->preco = phone.preco;
- ptr->dualsim = phone.dualsim;
- }
- else
- return ERROR;
- }
- fclose(fp);
- return OK;
- }
- else
- return ERROR;
- }
- void ShowValues(List L) {
- int i = 0;
- char straux[100] = { '\0' };
- List_Node *paux = L;
- if (emptyList(paux) == TRUE) {
- printf("\nLista de livros vazia.\n");
- return;
- }
- printf("Conteudo da Lista de Livros: \n");
- while (paux != NULL){
- if ((((SMARTPHONE*)paux->data)->dualsim) == 0)
- strcpy(straux, "Nao");
- else
- strcpy(straux, "Sim");
- 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);
- paux = NEXT(paux);
- i++;
- }
- printf(" FIM \n\n");
- }//-----------------------------------------------------------------------
- BOOLEAN emptyList(List L) {
- return (L == NULL) ? TRUE : FALSE;
- }//-----------------------------------------------------------------------
- int contarMarcaDual(List L) {
- List_Node *pTemp= L;
- int contador=0;
- char marca_aux[10];
- printf("Insira a marca: ");
- scanf(" %[^\n]", marca_aux);
- while (pTemp != NULL) {
- if ( (strcmp(((SMARTPHONE *)DATA(pTemp))->marca, marca_aux)) == 0 && ((SMARTPHONE *)DATA(pTemp))->dualsim == TRUE)
- contador++;
- pTemp = NEXT(pTemp);
- }
- return contador;
- }
- List criarListaPar(List L) {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement