Autor Tema: Ayuda con codigo fuente  (Leído 2418 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado lordmicro

  • PIC10
  • *
  • Mensajes: 6
Ayuda con codigo fuente
« en: 01 de Junio de 2011, 04:29:28 »
Hola que tal, necesito un pequeño tirón con un programa que estoy haciendo.Es un menu en el q se introducen,se pueden modificar,consultar o borrar registros. En uno de los menus está la opcion de mostrar la nota media de la clase y es aquí donde no sé muy bien como hacerlo.Os adjunto el codigo y a ver q opinais.

#include<conio.h>
#include<process.h>
#include<string.h>
#include<ctype.h>

void intro(void);
void borra(void);
void consu(void);
void error(void);
void media(void);


struct{
      char nombre[8];
      char apellido[20];
      char email[25];
      char telefono[12];
      float nota[50];

}datos;
FILE *fichero;
char opc,nom[8];
int tama;
int count;  /*variable contador alumnos*/
main()
{
   for (;;){
   clrscr();
   gotoxy(38,5);printf("menu");
   gotoxy(29,8);printf("1_Editar Alumno");
   gotoxy(29,10);printf("2_Borrar Alumno");
   gotoxy(29,12);printf("3_Consultar Alumno");
   gotoxy(29,14);printf("4_Consultar Media");
   gotoxy(29,16);printf("5_Finalizar");
   gotoxy(19,19);printf("introduce el numero de la opcion deseada :");
   opc=getch();
   switch(opc){
      case '1': intro();break;
      case '2': borra();break;
      case '3': consu();break;
      case '4': media();break;
      case '5': exit(0);
      default:break;
   }
  }
 }

/*grabar datos*/
void intro(void)
{
float nota[50];
count=0;
clrscr();
if((fichero=fopen("agenda.dat","ab"))==NULL)error();
for(;;){
clrscr();
gotoxy(20,6);printf("para finalizar pulse la tecla Enter.");
gotoxy(20,9);printf("Nombre :");
gotoxy(20,11);printf("Apellidos :");
gotoxy(20,13);printf("Email :");
gotoxy(20,15);printf("Telefono :");
gotoxy(20,17);printf("Nota :");
gotoxy(34,9); gets(datos.nombre);
if(strlen(datos.nombre)==0) break;
gotoxy(34,11); gets(datos.apellido);
gotoxy(34,13); gets(datos.email);
gotoxy(34,15); gets(datos.telefono);
gotoxy(34,17); scanf("%f",&datos.nota);--->esta es la variable que tiene q ir sumandose cada vez q añada un nuevo registro.
/*----------contador alumnos-----------*/
if(datos.nota>=0){
count=count+1;
gotoxy(20,21);printf("contador alumnos:%d",count);
}
/*------------------------------------*/

gotoxy(20,18); printf("datos correctos (S/N):");
opc=toupper(getch());
if(opc=='S')
   fwrite(&datos,sizeof(datos),1,fichero);
}
fclose(fichero);
}


/*borrar registros*/
void borra(void)
{
   clrscr();
   tama=sizeof(datos);
   if((fichero=fopen("agenda.dat","r+b"))==NULL)
         error();
   gotoxy(15,12);printf("introduce nombre a borrar:");
   gets(nom);
   clrscr();
   while(!feof(fichero)){
      fread(&datos,sizeof(datos),1,fichero);
      if(strcmp(datos.nombre,nom)==0){
      gotoxy(20,7);printf("Nombre :%s",datos.nombre);
      gotoxy(20,9);printf("Apellidos :%s",datos.apellido);
      gotoxy(20,11);printf("Email :%s",datos.email);
      gotoxy(20,13);printf("Telefono :%s",datos.telefono);
      gotoxy(20,15);printf("Nota :%d",&datos.nota);
      gotoxy(20,20);printf(" estas seguro que quieres borrarlo (S/N):");
      opc=toupper(getch());
      clrscr();
      if(opc=='S'){
         strcpy(datos.nombre,"*");
         fseek(fichero,tama,SEEK_CUR);
         fwrite(&datos,sizeof(datos),1,fichero);
         break;
         }
       }
     }
     fclose(fichero);
   }

/*consultar registros*/
void consu(void)
{
   clrscr();
   if((fichero=fopen("agenda.dat","rb"))==NULL)
         error();
   gotoxy(15,12);printf("introduce nombre a consultar:");
   gets(nom);
   clrscr();
   while(!feof(fichero)){
   fread(&datos,sizeof(datos),1,fichero);
   if(strcmp(datos.nombre,nom)==0){
      gotoxy(20,7);printf("Nombre :%s",datos.nombre);
      gotoxy(20,9);printf("Apellidos :%s",datos.apellido);
      gotoxy(20,11);printf("Email :%s",datos.email);
      gotoxy(20,13);printf("Telefono :%s",datos.telefono);
      gotoxy(20,15);printf("Nota :%d",&datos.nota);
      getch();
      break;
     }
    }
   fclose(fichero);
  }

/* consultar media*/---->aqui es donde se mostraria la nota media de los registros introducidos.

   void media(void)
   {


  }



/*error de apertura de archivo*/
void error(void)
{
   clrscr();
   printf("error!.No puedo abrir el fichero.");
   getch();
   exit(0);
 }

Desconectado Mr.Hell

  • PIC10
  • *
  • Mensajes: 8
Re: Ayuda con codigo fuente
« Respuesta #1 en: 11 de Agosto de 2011, 14:31:45 »
Para saber la nota media hay que sacar un promedio. La fórmula para obtener el promedio es: (n<1> + n<2> + n<3> +...+ n<total>)/<total>
Siendo <total> la cantidad de notas a promediar

Ejemplo Visual C++ 2005:

double Promedio(double Notas[] //Aca se guardan las notas obtenidas del archivo, en FLOAT
                         int Cantidad //Esta variable contiene la cantidad de notas guardadas en "Notas[]". Este valor no puede ser mayor al tamaño de la variable, dado que sino el valor obtenido seria erróneo.
);
Código: [Seleccionar]
double Promedio(double Notas[], int Cantidad)
{
int i = 0;
double Promedio = 0;
for(i=0; i<Cantidad; i++)
Promedio += Notas[i];
Promedio /= Cantidad;
return Promedio;
}
Esta funcion te devuelve el promedio en una variable de tipo DOUBLE.

Aca está esta modificación, que muestra el resultado en pantalla usando PRINTF
Código: [Seleccionar]
void Promedio(double Notas[], int Cantidad)
{
int i = 0;
double Promedio = 0;
for(i=0; i<Cantidad; i++)
Promedio += Notas[i];
Promedio /= Cantidad;
printf("Promedio: %.2f", Promedio);
}
Ambas funciones fueron probadas en Visual C++ 2005

Espero haberte ayudado.
« Última modificación: 11 de Agosto de 2011, 14:37:01 por Mr.Hell »