Last active
August 29, 2015 14:09
-
-
Save LeonNardella/0209a939bca73c389521 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Nome: Leon Nardella RA: 1581708 | |
| * Disciplina: Técnicas de Programação Turma: C21 | |
| * | |
| * 06) Crie um programa com o seguinte menu de opções: | |
| * | |
| * <1> Cadastrar Cliente | |
| * <2> Consultar Nome | |
| * <3> Listar Todos | |
| * <4> Sair. | |
| * | |
| * Cada uma destas opções, com exceção de sair, chama uma função que executará | |
| * o que ela indica. Este programa ira cadastrar os seguintes dados do cliente: | |
| * Nome, Idade, Rua. Bairro, Cidade, CEP e Telefone, todos estes campos devem | |
| * fazer parte de uma struct que será utilizada para resolver o problema. O número | |
| * máximo de clientes que podem ser cadastrados no sistema será perguntando no | |
| * início da execução do programa. Caso este limite seja alcançado, no momento | |
| * de um novo cadastro, deverá ser impresso na tela uma mensagem de erro. Na | |
| * opção <2> deverá ser consultado um nome de cliente, caso seja localizado, sua | |
| * ficha e impressa na tela. Na opção <3> é impresso o nome, idade, e telefone | |
| * de todos os clientes cadastrados. | |
| */ | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #define STRING 100 | |
| #define CEP 9 | |
| #define TELEFONE 9 | |
| struct Cliente { | |
| char nome[STRING], | |
| rua[STRING], | |
| bairro[STRING], | |
| cidade[STRING], | |
| cep[CEP], | |
| telefone[TELEFONE]; | |
| int idade; | |
| int emUso; | |
| }; | |
| typedef struct Cliente Cliente; | |
| void leString(char*, int); | |
| void cadastraCliente(Cliente*, int , int*); | |
| void imprimeCliente(Cliente*); | |
| void consultaNome(Cliente*,int); | |
| void listaClientes(Cliente*, int); | |
| int main() | |
| { | |
| int limite, cadastrados = 0; | |
| Cliente* clientes; | |
| do { | |
| printf("Qual eh o limite de cadastros?\n"); | |
| fflush(stdin); | |
| scanf("%d", &limite); | |
| clientes = malloc(limite * sizeof(Cliente)); | |
| if(!clientes) | |
| printf("Nao foi possivel reservar a memoria necessaria!\nTente novamente.\n"); | |
| else | |
| break; | |
| } while(1); | |
| do { | |
| int opcao; | |
| printf("\n\n\n" | |
| " ------ \n" | |
| "| Menu |\n" | |
| "|----------------------- \n" | |
| "| <1> Cadastrar cliente |\n" | |
| "| <2> Consultar nome |\n" | |
| "| <3> Listar todos |\n" | |
| "| |\n" | |
| "| <4> Sair |\n" | |
| " -----------------------\n\n\n"); | |
| printf("Selecione uma opcao: "); | |
| fflush(stdin); | |
| scanf("%d", &opcao); | |
| if(!opcao) | |
| opcao = -1; | |
| switch(opcao) { | |
| case 1: | |
| cadastraCliente(clientes, limite, &cadastrados); | |
| break; | |
| case 2: | |
| consultaNome(clientes, limite); | |
| break; | |
| case 3: | |
| listaClientes(clientes, limite); | |
| break; | |
| case 4: | |
| free(clientes); | |
| exit(0); | |
| default: | |
| printf("\n\n\n" | |
| "Opcao invalida!\n" | |
| "Tente novamene.\n\n\n"); | |
| break; | |
| } | |
| } while(1); | |
| } | |
| void cadastraCliente(Cliente* clientes, int limite, int* cadastrados) | |
| { | |
| if(*cadastrados < limite) { | |
| for(int i = 0; i < limite; i++) { | |
| if(!clientes[i].emUso) { | |
| printf("\n\n\nNome:\t\t "); | |
| leString(clientes[i].nome, STRING); | |
| printf("Idade:\t\t "); | |
| fflush(stdin); | |
| scanf("%d", &clientes[i].idade); | |
| printf("Rua:\t\t "); | |
| fflush(stdin); | |
| leString(clientes[i].rua, STRING); | |
| printf("Bairro:\t\t "); | |
| fflush(stdin); | |
| leString(clientes[i].bairro, STRING); | |
| printf("Cidade:\t\t "); | |
| fflush(stdin); | |
| leString(clientes[i].cidade, STRING); | |
| printf("CEP:\t\t "); | |
| fflush(stdin); | |
| leString(clientes[i].cep, CEP); | |
| printf("Telefone:\t "); | |
| fflush(stdin); | |
| leString(clientes[i].telefone, TELEFONE); | |
| clientes[i].emUso = 1; | |
| (*cadastrados)++; | |
| break; | |
| } | |
| } | |
| } else | |
| printf("\nO limite de cadastros esgotado!\n"); | |
| } | |
| void consultaNome(Cliente* clientes, int limite) | |
| { | |
| int erro=1; | |
| char nome[STRING]; | |
| printf("Digite um nome a ser pesquisado:\t"); | |
| fflush(stdin); | |
| fgets(nome, STRING, stdin); | |
| for(int i = 0; i< limite; i++) { | |
| if(!stricmp(clientes[i].nome, nome)) { | |
| imprimeCliente(&clientes[i]); //pq o & aqui? | |
| erro = 0; | |
| } | |
| } | |
| if(erro) | |
| printf("Nao foi encontrado nenhum registro com este nome!\n"); | |
| else | |
| printf("Cadastro realizado com sucesso!\n"); | |
| } | |
| void imprimeCliente(Cliente* cliente) | |
| { | |
| printf("\n\n\n" | |
| "Nome ........: %s\n" | |
| "Idade .......: %d\n" | |
| "Rua .........: %s\n" | |
| "Bairro ......: %s\n" | |
| "Cidade ......: %s\n" | |
| "CEP .........: %s\n" | |
| "Telefone ....: %s\n", | |
| cliente->nome, | |
| cliente->idade, | |
| cliente->rua, | |
| cliente->bairro, | |
| cliente->cidade, | |
| cliente->cep, | |
| cliente->telefone | |
| ); | |
| } | |
| void listaClientes(Cliente* clientes, int limite) | |
| { | |
| for(int i = 0; i < limite; i++) | |
| if(clientes[i].emUso) | |
| imprimeCliente(&clientes[i]); | |
| } | |
| void leString(char* string, int comprimento) | |
| { | |
| fflush(stdin); | |
| fgets(string, comprimento, stdin); | |
| for(int i = 0; i < comprimento; i++) | |
| if(string[i]=='\n') | |
| string[i]='\x0'; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment