Buscar

Exercicio AEM Cadastrar 100 nomes e Idades em Linguagem C

Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original

/* 
 * File: main.cpp
 * Author: joao
 *
 * Created on 19 de Abril de 2016, 22:03
 */
#include <cstdlib>
#include<stdio.h>
#include<string.h>
using namespace std;
/*
 *Programa para simples cadastro de nomes e idades
 * 
 */
#define TF 100
struct Registro {
 char nome[40], status;
 int idade;
};
typedef struct Registro reg;
/*---------------------------------------------------------------------------*/
void menuPrincipal();
void cadastrar(reg lista[TF], int *tl);
void listar(reg lista[TF], int tl);
void consultar(reg lista[TF], int tl);
void excluir(reg lista[TF], int tl);
void alterar(reg lista[TF], int tl);
/*---------------------------------------------------------------------------*/
int main(int argc, char** argv) {
 menuPrincipal();
}
/*---------------------------------------------------------------------------*/
void menuPrincipal() {
 char menu[6][16];
 reg lista[TF];
 int i, op, tl = 0;
 strcpy(menu[0], "[ 1 ] Cadastrar");
 strcpy(menu[1], "[ 2 ] Consultar");
 strcpy(menu[2], "[ 3 ] Alterar");
 strcpy(menu[3], "[ 4 ] Excluir");
 strcpy(menu[4], "[ 5 ] Listar");
 strcpy(menu[5], "[ 6 ] Sair");
 do {
 for (i = 0; i < 6; i++) {
 printf("%s\n", menu[i]);
 }
 fflush(stdin);
 scanf("%d", &op);
 switch (op) {
 case 1:
 cadastrar(lista, &tl);
 break;
 case 2:
 consultar(lista, tl);
 break;
 case 3:
 alterar(lista,tl);
 break;
 case 4:
 excluir(lista, tl);
 break;
 case 5:
 listar(lista, tl);
 break;
 case 6:
 break;
 }
 } while (op != 6);
}
/*---------------------------------------------------------------------------*/
void cadastrar(reg lista[TF], int *tl) {
 reg aux;
 printf("Nome:\n");
 scanf("%s", &aux.nome);
 printf("Idade:");
 scanf("%d", &aux.idade);
 aux.status = 1;
 lista[*tl] = aux;
 *tl = *tl + 1;
}
/*---------------------------------------------------------------------------*/
void consultar(reg lista[TF], int tl) {
 char nome[40];
 int op;
 printf("Digite o nome a cunsultar:\n");
 scanf("%s", &nome);
 printf("\n");
 while (tl-- && strcmp(lista[tl].nome, nome) != 0) {
 }
 if (strcmp(lista[tl].nome, nome) == 0 && lista[tl].status != 0) {
 printf("Reg [%2d] - Nome[%s], Idade[%d]", tl, lista[tl].nome, lista[tl].idade);
 } else {
 printf("Registro não encontrado");
 }
 scanf("%d", &op);
}
/*---------------------------------------------------------------------------*/
void alterar(reg lista[TF], int tl) {
 char nome[40];
 int op;
 printf("Digite o nome a cunsultar:\n");
 scanf("%s", &nome);
 printf("\n");
 while (tl-- && strcmp(lista[tl].nome, nome) != 0) {
 }
 if (strcmp(lista[tl].nome, nome) == 0 && lista[tl].status != 0) {
 printf("Nome:\n");
 scanf("%s", &lista[tl].nome);
 printf("Idade:");
 scanf("%d", &lista[tl].idade);
 } else {
 printf("Registro não encontrado");
 }
 scanf("%d", &op);
}
/*----------------------------------------------------------------------------*/
void excluir(reg lista[TF], int tl) {
 char nome[40];
 int op;
 printf("Digite o nome a excluir:\n");
 scanf("%s", &nome);
 printf("\n");
 while (tl-- && strcmp(lista[tl].nome, nome) != 0) {
 }
 if (strcmp(lista[tl].nome, nome) == 0 && lista[tl].status != 0) {
 lista[tl].status = 0;
 } else {
 printf("Registro não encontrado");
 }
 scanf("%d", &op);
}
/*---------------------------------------------------------------------------*/
void listar(reg lista[TF], int tl) {
 int op;
 while (tl--) {
 if (lista[tl].status == 1)
 printf("Nome:[%s], Idade[%d]\n", lista[tl].nome, lista[tl].idade);
 }
 scanf("%d", &op);
}
/*---------------------------------------------------------------------------*/

Teste o Premium para desbloquear

Aproveite todos os benefícios por 3 dias sem pagar! 😉
Já tem cadastro?

Outros materiais