lunes, 9 de julio de 2012

FILAS Y APUNTADORES II

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct cola{
int cedula;
char nombre[20];
struct cola *sig;
}*princ, *fin;
int j=0;
void acolar(){
struct cola *nuevo;
nuevo=(struct cola *)malloc(sizeof(struct cola));
printf("INTRODUCIR CÉDULA:\n ");
scanf("%d",&nuevo->cedula);
printf("INTRODUCIR NOMBRE:\n");
scanf("%s",nuevo->nombre);
nuevo->sig=NULL;
if(princ==NULL){
princ=nuevo;
fin=nuevo;
}else{
fin->sig=nuevo;
fin=nuevo;
}
j++;
}
void mostrar(){
struct cola *aux;
aux=princ;
if(aux==NULL){
printf("COLA VACIA\n");
}else{
while(aux!=NULL){
printf("CÉDULA: %d\nNOMBRE: %s\n\n", aux->cedula,aux->nombre);
aux=aux->sig;
}
}
}
void desacolar(){
struct cola *aux;
struct cola *aux1;
int cont;
aux=princ;
aux1=princ;
cont=0;
while(aux!=NULL){
if(aux->cedula==aux1->cedula){
if(cont==0){
princ=princ->sig;
}
}
aux=aux->sig;
aux1=aux;
cont=1;
}
}
main(){
int opc;
do{
printf("\n\n\n\t\tREGISTRO DE PERSONAS ***TELEVISORA TUTV\t\t\t\t\n");
printf("\n\t\t\t1. INGRESAR DATOS A LA COLA\n\t\t\t2. MOSTRAR DATOS DE LA COLA\n\t\t\t3. RETIRAR DATOS EN LA COLA\n\t\t\t4. SALIR\n\t\t\t");
scanf("%d", &opc);
system("cls");
switch (opc){
case 1:
acolar();
break;
case 2:
mostrar();
break;
case 3:
desacolar();
break;
default:
printf("SALIR DEL SISTEMA\n");
break;
}
}while(opc!=4);
}

miércoles, 27 de junio de 2012

funcion para borrar dentro de una fila

La siguiente funcion realiza el borrado dentro de la fila

void borrar(void)
{
 char* q;
 q=crecup();
 if(q==NULL)
 {
 printf("No hay datosn");
 return;
 }
 printf("Este es el dato recuperado y borrado: Dato numero %d: %s\n",rpos+1,q);
 free (q);
}

martes, 26 de junio de 2012

Colas o filas

SE MUESTRA A CONTINUACION LOS METODOS PARA ALMACENAR Y PARA RECUPERAR LOS DATOS EN UNA FILA. TAMBIEN SE MOSTRARA EL METODO MAIN

FUNCION PARA ALMACENAR



void calmac(char *q)
{
  if (spos==MAX) // Control de llenado de la cola
  {
    printf("n La cola está llenan");
    return;
   }
   p[spos]=q;
   spos ++;
 }

FUNCION PARA RECUPERAR



char *crecup(void)
{
 if(rpos==spos)
 {
 printf("n No hay datosn");
 return NULL;
 }
 rpos ++;
 return p[rpos-1];
}



METODO MAIN


int main(void)
{
  char s[80];
  register int t;
  spos=0;             /* inicializacion de indicadores y de cola */
  rpos=0;
  for(t=0;t<MAX;++t) p[t]=NULL;

//Menu
  for(;;){
   printf("\nIntroducir, Listar, Borrar o Salir\n");
   printf("(escribir la acci¢n deseada):\n");
   gets(s);
   *s=toupper(*s);
   switch(*s){
     case 'I': intro(); break;
     case 'L': revisar();break;
     case 'B': borrar(); break;
     case 'S': exit(0);
      }
    }
getch();
 return 0;
}


martes, 19 de junio de 2012

codigo pila en C

Ejemplo de una pila con arreglos

#define MAX 5
#include<stdio.h>
#include<conio.h>
void push(int i);
int pop(void);
int top=0;
int pila[MAX];
main()
{
     
      push(5);
      push(3);
      push(9);
printf("Valor %d\n", pop());
printf("Valor %d\n", pop());
printf("Valor %d\n", pop());
      getch();
     
     
     
      }

void push(int i)
{
     if(top>=MAX)
     {
                 printf("Pila Llena");
                 return;             
                
                
                 }
     pila[top]=i;
     top++;
    
    
     }
int pop(void)
{
    top--;
    if(top<0)
    {
    printf("Pila vacía");
    return(0);
             }
    return pila[top];
   
   
    }

Funciones push y pop

Las siguientes funciones se utilizan para poder implementar una pila

void push(int i)
{
     if(top>=MAX)
     {
                 printf("Pila Llena");
                 return;             
                
                
                 }
     pila[top]=i;
     top++;
    
    
     }


int pop(void)
{
    top--;
    if(top<0)
    {
    printf("Pila vacía");
    return(0);
             }
    return pila[top];
   
   
    }

viernes, 8 de junio de 2012

CODIGO PARA GUARDAR DATOS EN UN ARCHIVO DE TEXTO

#include<conio.h>
#include<stdio.h>
#include<io.h>
#include<stdlib.h>
main()
{
FILE *fp;
char c[80];
int t;
if((fp=fopen("prueba.txt","w"))==NULL)
{
  printf("Error de escritura");
  exit(1);
}
printf("Introduce una cadena y un numero");
fscanf(stdin,"%s %d",c,&t);
fprintf(fp,"%s %d",c,t);
fclose(fp);
if((fp=fopen("prueba.txt","r"))==NULL)
{
  printf("Error de escritura");
  exit(1);
}
fscanf(fp,"%s %d",c,&t);
fprintf(stdout,"%s %d",c,t);
fclose(fp);
getch();
return(0);
}

miércoles, 30 de mayo de 2012

suma de arreglos


#include<stdio.h>
#include<conio.h>

#define MAX 5
int vec[MAX];
int suma(int vec[],int n);
void lee_mat(int vec[]);
void imp_mat(int vec[]);
main()
{

      int i;
      int s;
      printf("Calculo de la sumatoria de los elementos de un arreglo\n\n");
      lee_mat(vec);  
      printf("\nValores capturados en el arreglo\n\n");
      imp_mat(vec);
      s=suma(vec,MAX);
      printf("\nSumatoria de los elementos del arreglo = %d",s);
      getch();
 
      }

void lee_mat(int vec[MAX])
{
     int i;

     for(i=0;i<5;i++)
      {
            printf("Introduce el valor %d=>",i+1);
            scanf("%d",&vec[i]);
       }

 
     }
 
void imp_mat(int vec[MAX])
{
     int i;

     for(i=0;i<5;i++)
      {
            printf("%d\n",vec[i]);
       
       }
 
     }

int suma(int vec[],int n)
{

     int suma=0;
     while(n>0)
     {
               n--;
               suma+=vec[n];
             
               }
     return suma;

   
     }