Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # include <stdio.h>
- # include <conio.h>
- # include <stdlib.h>
- # include <locale.h>
- #define MAX_nome 500
- int cont=0;
- int u=0;
- enum Pri_ut {sim=1,nao=2};
- typedef struct guard{
- int codig_ut;
- char nome_ut[MAX_nome];
- char CC_ut[8+1];
- enum Pri_ut pri_ut;
- }n_guard;
- typedef struct elemento {
- n_guard utent;
- struct elemento *seg;
- }Elemento;
- //******************************************** Menu ***************************************************
- void menu(){
- system("cls");
- printf("****************************************\n");
- printf("* 1- Inserir Utentes *\n");
- printf("* 2- Listar Utentes *\n");
- printf("* 3- Atender Utentes *\n"); //Remover//
- printf("* 4- Contar Utentes *\n");
- printf("* 5- Alterar Utentes *\n");
- printf("* 0- Terminar Programa *\n");
- printf("****************************************\n");
- }
- //**********************************criacao de um elemento na lista********************************
- Elemento *criarElemento(Elemento *Base,n_guard utent) {
- Elemento *elemento;
- elemento=(Elemento*) malloc(sizeof(Elemento)); //alocar espa?o em memoria para o elemento
- elemento->utent = utent; //atribuir o valor ao elemento
- elemento->seg = NULL; //inicializar o apontador do elemento seguinte para NULL
- return elemento;
- }
- //************************************* INSERIR *********************************************************
- Elemento *opcao1(Elemento *Base) {
- system("cls");
- printf("****************************************************\n");
- printf("* Esta presente na opção de Inserção de Utentes: *\n");
- printf("****************************************************\n");
- if (Base!=NULL) {
- Base->seg=opcao1(Base->seg);
- } else {
- n_guard utent;
- cont++;
- utent.codig_ut=++u;
- printf("Número de utente: %d\n", u);
- // printf("Número de utente: ");
- // fflush(stdin);scanf("%d",&utent.codig_ut);
- printf ("Introduza o nome: ");
- fflush(stdin);gets(utent.nome_ut);
- printf("introduza o numero do CC do utente: ");
- fflush(stdin);gets(utent.CC_ut);
- /* do{
- printf(" É um utente prioritário (s(1)/n(2)): ");
- fflush (stdin);scanf("%d", &utent.pri_ut);
- }while( utent.pri_ut != 1 && utent.pri_ut != 2);*/
- Base=criarElemento(Base,utent);
- }
- return Base;
- }
- //****************************************** LISTAR ****************************************************
- void opcao2(Elemento *Base) {
- printf("***************************************************\n");
- printf("* Listagem de Utentes: *\n");
- printf("***************************************************\n");
- if(Base==NULL){
- printf("Não existe Utentes registados!!");
- }
- if (Base!=NULL) {
- printf("\nO número do Utente é: %d", Base->utent.codig_ut);
- printf("\nO seu nome é: %s",Base->utent.nome_ut);
- printf("\nO número de cartão de cidadão: é: %s\n", Base->utent.CC_ut);
- if (Base->seg != NULL) opcao2(Base->seg);
- }
- printf("\nPrime uma tecla para continuar!");
- getch();
- }
- //******************************************** apagar ****************************************************
- void apagarElemento(Elemento *Base) {
- free(Base); //libertar elemento
- Base=NULL; //colocar o apontador para NULL
- }
- //******************************************** Atender ***************************************************
- Elemento *opcao3(Elemento *Base) {
- system("cls");
- Elemento *aux = Base;
- char n;
- printf("*******************************************************\n");
- printf("* Esta presente na opção de Atendimento de Utentes: *\n");
- printf("*******************************************************\n");
- if(Base==NULL){
- printf("Não existe Utentes registados!!");
- }
- if (Base!=NULL) {
- printf("-----------------------------------\n\n");
- printf("-> Nome: %s \n\n", Base->utent.nome_ut);
- printf("-----------------------------------\n");
- printf("\nPrentende mesmo atender este Utente? (s/n)");
- fflush(stdin);scanf("%c",& n);
- if(n == 's'){
- Base = Base->seg;
- apagarElemento(aux);
- printf("\n\n***** O Utente foi atendido com sucesso! ****\n\n");
- printf("Prima uma tecla para continuar...");
- getch();
- }else if(n == 'n'){
- printf("\t************** O utente continuará nas Urgencias. ************\n");
- printf("Prima uma tecla para continuar...");
- }
- }
- getch();
- return Base;
- }
- //******************************************** CONTAR *****************************************************
- void opcao4(Elemento *Base){
- system("cls");
- int i;
- n_guard utent;
- printf("****************************************************\n");
- printf("* Esta presente na opção de Contagem de Utentes: *\n");
- printf("****************************************************\n");
- // if(Base==NULL){
- // printf("Não existe Utentes registados!!");
- // }
- // if(Base!=NULL ){
- //for (i=1;i<Base->utent.codig_ut;i++){
- printf ("\nExistem %d utentes em fila de espera! \n\n\n", cont);
- //}
- // if (Base->seg != NULL) opcao4(Base->seg,cont);
- // }
- printf("\n Prime uma tecla para continuar!");
- getch();
- }
- //******************************************** Alterar *****************************************************
- void opcao5(Elemento *Base){
- system ("cls");
- n_guard utent;
- char n;
- printf("*****************************************************\n");
- printf("* Esta presente na opção de Alteração de Utentes: *\n");
- printf("*****************************************************\n");
- if(Base==NULL){
- printf("\n\nNão existe Utentes registados!");
- }
- if (Base!=NULL) {
- printf("-----------------------------------\n\n");
- printf("-> Nome: %s \n\n", Base->utent.nome_ut);
- printf("-----------------------------------\n\n");
- printf("\nPrentende mesmo alterar este Utente? (s/n)");
- fflush(stdin);scanf("%c",& n);
- if(n == 's'){
- printf ("\n->Introduza o novo nome: ");
- fflush(stdin);gets(Base->utent.nome_ut);
- printf("->Introduza o novo numero do CC do utente: ");
- fflush(stdin);gets(utent.CC_ut);
- printf("\n\nUtente alterado com sucesso!!");
- }else if(n == 'n'){
- printf("\n\n\n************* O utente continuará com mesmo registos ************");
- }
- }
- printf("\n\nPrima uma tecla para continuar!");
- getch();
- }
- //********************************************* Programa principal **************************************************
- main() {
- system("color F0");
- setlocale (LC_ALL, "Portuguese");
- char op;
- int n=0;
- n_guard utent;
- Elemento *Base=NULL;
- //Ler registos do ficheiro
- FILE*fp;
- //Abrir ficheiros para Leitura
- if((fp = fopen("DADOS.DAT","rb"))==NULL) {
- printf("\nImpossivel abrir o ficheiro para leitura\n");
- getch();
- }else{
- // ler os dados do ficheiro
- while (fread(&(utent),sizeof (n_guard),1,fp)){
- n++;
- }
- //fechar o ficheiro
- fclose (fp);
- }
- do {
- menu();
- fflush(stdin);
- op=getche();
- switch(op){
- case '1':
- system("cls");
- Base = opcao1(Base);
- break;
- case '2':
- system("cls");
- opcao2(Base);
- break;
- case '3':
- system("cls");
- Base = opcao3(Base);
- break;
- case '4':
- opcao4(Base);
- break;
- case '5':
- opcao5(Base);
- break;
- case '0':
- system("cls");
- printf ("Prime uma tecla para terminar o programa");
- break;
- }
- }while(op!='0');
- //escrever registos no ficheiro
- //Abrir Ficheiro para escrita
- if ((fp=fopen("DADOS.DAT","wb"))==NULL){
- printf("Impossível abrir o ficheiro para a inserçao de filmes\n");
- getch();
- }else{
- //Escrever os dados no ficheiro
- if (fwrite(utent,sizeof( n_guard),n,fp)!=n){
- printf("Não foram escritos todos os registos\n");
- getch();
- }
- //fechar ficheiro
- fclose(fp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment