Programação Orientada à Procedimentos II Programação Orientada à Procedimentos I Processamento Paralelo Lógica de Programação Introdução à Computação Informática Básica

 

 

Informática Básica
Introdução à Computação
Lógica de Programação
Programação Orientada à Procedimentos I
Programação Orientada à Procedimentos II

Processamento Paralelo


Uma Possível Solução ...




1 - Criar um banco de dados o qual armazene os campos nome, endereco e telefone. O sitema deverá ter as opções (criar arquivo, cadastrar, imprimir registros). A opção criar arquivo, deverá perguntar ao usuário em qual diretorio e qual o nome do arquivo a ser criado. A opção cadastrar deverá cadastrar registros até que o usuario digite o campo nome em branco. Todas opções deverão verificar a existência do arquivo e possíveis erros de abertura do mesmo.

/*OBS: Nao permite que o numero de caracteres nos campos ultrapasse 
o permitido 
*/

#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "ctype.h"
#include "dir.h"

#define END 70
#define NOM 50
#define TEL 15


void exibir(char path[]);
void definir(char path[]);
void cadastrar(char path[]);
void get_data(char [], int);

//*********** funcao main ********
void main()
{
 char key;
 char caminho[100];
 int OK = 0;

 do
 {
   clrscr();
   printf("\n\tOpcoes:");
   printf("\n\n\t[1] Definir arquivo");
   printf("\n\t[2] Cadastrar");
   printf("\n\t[3] Exibir");
   printf("\n\t[S] Sair");

   printf("\n\n\t\tDigite tua opcao: ");
   key = getche();

   //verifica se foi definido o arquivo
   if(!OK && (key == '2' || key == '3') )
   {
     printf("\n\n\n\t\t\tDefina o arquivo 1§!!!");
     getch();
     continue;
   }

   switch(key)
   {
     case '1': 
                definir(caminho); 
                OK = 1;
                break;
     case '2': 
                cadastrar(caminho); 
                break;
     case '3': 
                exibir(caminho); 
                break;
     default: 
                printf("\n\n\n\t\t\tOpcao inexistente!!!");
   }
	 
 }while(toupper(key) != 'S');
}

//********************** funcao definir ****************
void definir(char path[])
{
 char *status = NULL;
 char key;
 int OK = 0;

 do
 {
   printf("\n\n\n\t\tDigite o path com o nome do arquivo: ");
   printf("\n\t\tEx: c:\\temp\\lixo.dat.\n\t>>");
   fflush(stdin);
   gets(path);
   status = searchpath(path); //verifica se o arquivo existe

   if (status) // arquivo existente
   {
	 printf("\n\n\t\tDeseja sobrescrever? (S/N) ");
	 key = getche();
	 if (toupper(key) == 'S') //usuario confirma sobrescricao
	 {
	   if (fopen(path,"w") != NULL)
	   {
	     printf("\n\n\n\t\tArquivo criado com sucesso!!!");
	     getch();
	   }
	   OK = 1;
	 }
	 else
	 {
	  do
	  {
	   printf("\n\n\t\tDeseja (D)igitar novamente ou (A) 
		 ler/acrescentar dados \nao arquivo? (D/A) ");
	   key = getche();
	  }while(toupper(key) != 'A' && toupper(key) != 'D');

	  if (toupper(key) == 'A')
	  {
	     OK = 1;//Ok. Sai da rotina
	  }
	 }
	status = NULL;
   }
   else
   {
	   if (fopen(path,"w") != NULL)
	   {
	     printf("\n\n\n\t\tArquivo criado com sucesso!!!");
	     getch();
	   }

	   OK = 1;  //Ok. Sai da rotina
   }
 }while(!OK);

}

//********************** funcao exibir ****************
void exibir(char path[])
{
  FILE *out;
  char nome[NOM+1], end[END+1], tel[TEL+1];
  int REG = 0;

  if((out = fopen(path,"r")) == NULL)
  {
	printf("\n\n\n\t\t\tArquivo nao pode ser aberto!!!");
	getch();
  }
  else
  {

       while(fgets(nome, NOM, out) != NULL)
       {
	 clrscr();
	 printf("\n\n\tRegistro: %d ", REG+1);
	 printf("\n\n\tNome: ");
	 fflush(stdout);
	 puts(nome);

	 fgets(end, END, out);
	 printf("\n\n\tEndereco: ");
	 fflush(stdout);
	 puts(end);

	 fgets(tel, TEL, out);
	 printf("\n\n\tTelefone: ");
	 fflush(stdout);
	 puts(tel);

	 getch();
	 REG++;
       }
	fclose (out);
  }

}

//******************** funcao cadastrar ****************
void cadastrar(char path[])
{
  FILE *in;
  char key;
  char nome[NOM+1], end[END+1], tel[TEL+1];


  if((in = fopen(path,"a")) == NULL)
  {
	printf("\n\n\n\t\t\tArquivo nao pode ser aberto!!!");
	getch();
  }
  else
  {
       do
       {
	 clrscr();
	 printf("\n\n\tNome: ");
	 fflush(stdin);
	 get_data(nome, NOM); 
	// tem a mesma funcao do gets; o segundo parametro
	// eh a quantidade caractes a ser recebido

	 printf("\n\n\tEndereco: ");
	 fflush(stdin);
	 //gets(end);
	 get_data(end,END);

	 printf("\n\n\tTelefone: ");
	 fflush(stdin);
	 //gets(tel);
	 get_data(tel, TEL);

	 printf("\n\n\t\tConfirma os dados? (s/n) ");
	 key = getche();
	 if (toupper(key) == 'S')//GRAVACAO DOS DADOS
	 {
		 fputs(nome, in);
		 fputs("\n", in);
		 fputs(end, in);
		 fputs("\n", in);
		 fputs(tel, in);
		 fputs("\n", in);
	 }
	 printf("\n\n\t\tDeseja continuar? (s/n) ");
	 key = getche();

       }while(toupper(key) != 'N');

       fclose (in);

  }

}

//********************** get_data ****************
//evita que a quantidade de caracteres ultrapasse o permitido
void get_data(char data[], int size)
{
  int i = 0;
  char key;

  do
  {
    key = getch();
    //verifica se o enter ou o backspace foi pressionado
    if (key != '\r' && key != '\b' && i<size)
    {
      printf("%c",key);
      data[i] = key;
      i++;
    }
  
	  //detecta o backspace e realiza a mesmas funcoes de um backspace.
    if (key == '\b' && i>0)
    {
	  printf("\b \b"); //apaga o caracter
	  i--;
    }

  }while (key != '\r');
  data[i] = '\0';
}