Ir ao conteúdo

Posts recomendados

Postado

Estou com o seguinte problema, tenho um exercício que não consigo terminar segue o enunciado.

 

Citação

1) Faça um programa em C que manipule uma lista contendo informações sobre uma agenda de telefones (nome, telefone (campos para DDD e número) e data de aniversário (campos para ano, mês e dia)). Esse programa deverá implementar as seguintes rotinas:
a) Cadastrar novo contato;
B) Editar contato;
c) Excluir contato;
d) Mostrar contatos na ordem crescente (ordenar pelo nome);
e) Busca por nome ou data de aniversário (mês ou ano) e imprimir o resultado;
f) Imprimir todas as pessoas da agenda, mostrando todos os campos de dados.

 

O que consegui fazer do main até agora foi isso:

 

#include<iostream>
#include<stdlib.h>
#include<string.h>
#include"Exercicio 1.h"

struct agenda *insere(struct agenda *L, char nome[], int ddd, int tel, int ano, int mes, int dia);

int main(){
	
	struct agenda *ini;
	
	ini = NULL;
	
	int x = 0;
	char nome1[30];
	int ddd1 = 0;
	int tel1 = 0;
	int ano1 = 0;
	int mes1 = 0;
	int dia1 = 0;
	
	
	printf("Lista de Comandos:\n\n");
	printf("1 - Criar Novo Contato.\n");
	printf("2 - Editar Conato.\n");
	printf("3 - Excluir Contato.\n");
	printf("4 - Mostar todos os Contatos em ordem Alfabetica.\n");
	printf("5 - Buscar o Contato pelo Nome.\n");
	printf("6 - Buscar o Contato pelo Aniversario.\n");
	printf("7 - Imprimir todos os contatos.\n\n");
	
	while(1){
		
		printf("Comando: ");
		scanf("%d", &x);
		printf("\n\n");
		
		switch(x){
			
			case 1:
				printf("Inserir novo contato:\n\n");
				
				printf("Nome: ");
				fflush(stdin);
				gets(nome1);
				printf("\n\n");
				
				printf("DDD: ");
				scanf("%d", &ddd1);
				printf("Telefone: ");
				scanf("%d", &tel1);
				
				printf("Data de Nasc.: ");
				scanf("%d %d %d", &dia1, &mes1, &ano1);
				
				ini = insere(ini, nome1, ddd1, tel1, ano1, mes1, dia1);
				break;
							
		}
		
	}
	
}

	struct agenda *insere(struct agenda *L, char nome[], int ddd, int tel, int ano, int mes, int dia){
		
		struct agenda *novo = (struct agenda *) malloc(sizeof(struct agenda));
		
		strcpy(novo->nome,nome);
		
		novo->ddd = ddd;
		novo->tel - tel;
		
		novo->ano = ano;
		novo->mes = mes;
		novo->dia - dia;
		
		novo->prox = L;
		return novo;
		
}

E meus struct com as variáveis da lista:

 

struct agenda{
	
	char nome[30];
	int ddd;
	int tel;
	int ano;
	int mes;
	int dia;
	
	struct agenda* prox;	
	
};

Alguém pode me ajudar a terminar o exercício.

Postado

Estou reescrevendo o código e estou com um problema para colocar dados na minha lista, estou usando o mesmo struct agenda. E o código em cpp é:

 

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "ex1.h"

using namespace std;

typedef struct node{
	
	Agenda data;
	struct node* next;
	
}Node;

typedef struct list{
	
	int size;
	Node* head;
	
}List;

List* createList();							// cria uma lista
void push(List* list, Agenda data);		// adicia a lista 
void printList(List* list);					// imprime a lista
void pop(List* list);						// retira o ultimo nó da lista
bool isEmpty(list* list);					// verifica se a lista não é vazia.
Node* atPos(List* list, int index);			// retorna o no
int indexOf(List* list, Node* node);		// retorna index
void erase(List* list, int index);			// Apaga um no
void insert(List* list, Agenda data, int index);	// insere um no
void xchgNodes(List* list, Node* nodeA, Node* nodeB);	// trocar nos
Node* min(List* list, int index);			// achar o minimo da lista
Node* max(List* list, int index);			// achar o maximo da lista
void incSort(List* list);					// ordenar em ordem crescente
void decSort(List* list);					// ordenar em ordem decrescente


List* createList(){
	
	List* list = (List*) malloc(sizeof(List));
	
	list->size = 0;
	list->head = NULL;
	
	return list;
	
}

void push(list* list, Agenda data){
	
	Node* node = (Node*) malloc(sizeof(Node));
	
	node->data = data;
	node->next = list->head;
	list->head = node;
	list->size++;
	
}

void printList(List* list){
	
	Node* pointer = list->head;
	
	if(isEmpty(list)){
		
		printf("Lista vazia\n");
		return;
		
	}
	
	while(pointer != NULL){
		
		printf("Nome: %s \n", pointer->data.nome);
		printf("DDD: %d \n", pointer->data.DDD);
		printf("Telefone: %d \n", pointer->data.tel);
		cout << "Nascimento: " << pointer->data.dia << "/" << pointer->data.mes << "/" << pointer->data.ano << "\n\n";
		pointer = pointer->next;
		
	}
	
	printf("\n");
	
}

bool isEmpty(List* list){
	
	return (list->size == 0);
	
}

void pop(List* list){
	
	if(!isEmpty(list)){
		
		Node* pointer = list->head;
		list->head = pointer->next;
		free(pointer);
		list->size--;
		
	}
	
}

Node* atPos(List* list, int index){
	
	if(index >= 0 && index < list->size){
		
		Node* node = list->head;
		
		int i;
		for(i=0;i<index;i++){
			
			node = node->next;
			
		}
		
		return node;
		
	}
	
	printf("Indice invalido\n");
	return NULL;
	
}

int indexOf(List* list, Node* node){
	
	if(node != NULL){
		
		Node* pointer = list->head;
		
		int index = 0;
		while(pointer != node && pointer != NULL){
			
			pointer = pointer->next;
			index++;			
			
		}
		
		if(pointer != NULL){
			
			return index;
			
		}
		
	}
	
	printf("No não pertencente a lista.\n");
	return -1;
	
}

void insert(List* list, Agenda data, int index){
	
	if(index == 0){
		
		push(list, data);
		
	}else{
		
		Node* current = atPos(list, index);
		
		if(current != NULL){
			
			Node* previous = atPos(list, index - 1);
			
			Node* newNode = (Node*) malloc(sizeof(Node));
			newNode->data = data;
			
			previous->next = newNode;
			newNode->next = current;
			
			list->size++;
			
		}
		
	}
	
}

void erase(List* list, int index){
	
	if(index == 0){
		
		pop(list);
		
	}else{
		
		Node* current= atPos(list, index);
		
		if(current != NULL){
			
			Node* previous = atPos(list, index - 1);
			previous->next = current->next;
			
			free(current);
			list->size--;
			
		}

	}
	
}

void xchgNodes(List* list, Node* nodeA, Node* nodeB){
	
	if(nodeA == nodeB){
		
		return;
		
	}
	
	int indexA = indexOf(list, nodeA);
	int indexB = indexOf(list, nodeB); 
	
	if(indexA == -1 || indexB == -1){
		
		return;
		
	}
	
	if(indexA > indexB){
		
		nodeA = nodeB;
		nodeB = atPos(list, indexA);
		
		indexA = indexB;
		indexB = indexOf(list, nodeB);		
		
	}
	
	Node* pb = atPos(list, indexB - 1);
	
	if(nodeA == list->head){
		
		list->head = nodeB;
		
	}else{
		
		Node* pa = atPos(list, indexA - 1);
		pa->next = nodeB;
		
	}
	
	pb->next = nodeA;
	
	Node* pointer = nodeA->next;
	nodeA->next = nodeB->next;
	nodeB->next = pointer;
	
}

Node* min(List* list, int index){
	
	Node* pointer = atPos(list, index);
	
	if(pointer != NULL){
		
		Node* minNode = pointer;
		
		while(pointer != NULL){
			
			if(pointer->data.ano < minNode->data.ano){
				
				minNode = pointer;

			}
			
			pointer = pointer->next;
			
		}
		
		return minNode;
		
	}
	
	return NULL;
	
}

Node* max(List* list, int index){
	
	Node* pointer = atPos(list, index);
	
	if(pointer != NULL){
		
		Node* maxNode = pointer;
		
		while(pointer != NULL){
			
			if(pointer->data.ano > maxNode->data.ano){
				
				maxNode = pointer;
				
			}
			
			pointer = pointer->next;
					
		}
		
		return maxNode;
		
	}
	
	return NULL;
	
}

void incSort(List* list){
	
	int i;
	
	for(i=0;i<list->size - 1;i++){
		
		xchgNodes(list, atPos(list, i), min(list, 1));
		
	}
	
}

void decSort(List* list){
	
	int i;
	
	for(i=0;i<list->size - 1;i++){
		
		xchgNodes(list, atPos(list, i), max(list, 1));
		
	}
	
}

int main(int argc, char** argv) {
	
	List* l = createList();
	
	Agenda data;
	
	data.nome = "Thiago";
	data.DDD = 41;
	data.tel = 998349123;
	data.dia = 14;
	data.mes = 02;
	data.ano = 1992;
	
	push(l, data);
	
	printList(l);
		
	return 0;
}

A unica variável que não consigo passar é o data.nome, sempre da erro de todas as formas alguém pode me ajudar?

Visitante
Este tópico está impedido de receber novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

LANÇAMENTO!

eletronica2025-popup.jpg


CLIQUE AQUI E BAIXE AGORA MESMO!