Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <string.h>
- typedef struct NodoLista
- {
- char nombreAlu[20];
- int nota;
- struct NodoLista * sig;
- } NodoLista;
- typedef struct celda
- {
- char materia[20];
- struct NodoLista * lista;
- } celda;
- typedef struct EstructuraArchivo
- {
- char materia[20];
- char nombreAlu[20];
- int nota;
- } EstructuraArchivo;
- NodoLista * crearNodo(char nombre[20], int nota)
- {
- NodoLista* lista=(NodoLista*)malloc(sizeof(NodoLista));
- strcpy(lista->nombreAlu,nombre);
- lista->nota=nota;
- lista->sig=NULL;
- return lista;
- }
- int buscarPos(celda arrMaterias[10],int cant, char nombreMat[20])
- {
- int pos=0;
- int rta=-1;
- while (pos<cant&&rta==-1)
- {
- if (strcmpi(arrMaterias[pos].materia,nombreMat)==0)
- {
- rta=pos;
- }
- pos++;
- }
- return rta;
- }
- int AgregarEnArreglo(celda materias[30], int cant, char materia[30])
- {
- strcpy(materias[cant].materia,materia);
- materias[cant].lista=NULL;
- cant++;
- return cant;
- }
- NodoLista * buscarUltimo(NodoLista * lista)
- {
- NodoLista * aux =lista;
- while(aux->sig!=NULL)
- {
- aux = aux->sig;
- }
- return aux;
- }
- NodoLista * AF(NodoLista *lista, NodoLista * nuevo)
- {
- if(lista==NULL)
- {
- lista=nuevo;
- }
- else
- {
- NodoLista*ultimo=buscarUltimo(lista);
- ultimo->sig=nuevo;
- }
- return lista;
- }
- int agregarFinal(celda materias[20], int cant, int nota, char materia[30], char alumno[30])
- {
- NodoLista *aux = crearNodo(alumno,nota);
- int pos = buscarPos(materias,cant,materia);
- if(pos==-1)
- {
- cant = AgregarEnArreglo(materias,cant,materia);
- pos = cant-1;
- }
- materias[pos].lista=AF(materias[pos].lista,aux);
- return cant;
- }
- int agregarMuchos(celda materias[10],int cant){
- int nota;
- char alumno[20];
- char materia[20];
- system("cls");
- printf("Ingrese el nombre de la materia a la que desea agregar datos: ");
- fflush(stdin);
- gets(materia);
- printf("Ingrese el nombre del alumno: ");
- fflush(stdin);
- gets(alumno);
- printf("Ingrese la nota de %s: ",alumno);
- fflush(stdin);
- scanf("%d",¬a);
- cant = agregarFinal(materias,cant,nota,materia,alumno);
- return cant;
- }
- void mostrarLista (NodoLista * lista)
- {
- if(lista!=NULL)
- {
- printf("\nAlumno: %s | ", lista->nombreAlu);
- printf("Nota: %d . ", lista->nota);
- mostrarLista(lista->sig);
- }
- }
- void mostrarTodo(celda materias[20],int cant)
- {
- int i=0;
- while (i<cant)
- {
- system("cls");
- printf("Materia: %s\n",materias[i].materia);
- mostrarLista(materias[i].lista);
- getch();
- i++;
- }
- }
- void mostrar(celda materias[20],int cant)
- {
- char mat[20];
- int busca;
- printf("\nIngrese la materia que desee mostrar: \n");
- fflush(stdin);
- scanf("%s",mat);
- busca=buscarPos(materias,cant,mat);
- if (busca!=-1)
- {
- system("cls");
- mostrarLista(materias[busca].lista);
- }
- }
- void escribe(NodoLista *lista,char materia[20],FILE *archivo)
- {
- EstructuraArchivo EA;
- if (lista != NULL)
- {
- strcpy(EA.materia, materia);
- strcpy(EA.nombreAlu, lista->nombreAlu);
- EA.nota = lista->nota;
- fwrite(&EA,sizeof(EstructuraArchivo),1,archivo);
- escribe(lista->sig,materia,archivo);
- }
- }
- void ArchivarListas (celda ArrMaterias[10],int cant)
- {
- FILE *archivo = fopen("ArregloArboles.dat","a+b");
- int i=0;
- NodoLista *lista;
- while(i < cant)
- {
- lista = ArrMaterias[i].lista;
- if (lista!= NULL)
- {
- escribe(lista,ArrMaterias[i].materia,archivo);
- }
- i++;
- }
- fclose(archivo);
- }
- void MostrarArchivoSinOrden()
- {
- FILE *archivo = fopen("ArregloArboles.dat","r b");
- EstructuraArchivo EA;
- while (fread(&EA,sizeof(EstructuraArchivo),1,archivo)!=0)
- {
- printf("Materia: %s.\n",EA.materia);
- printf("Alumno: %s \n",EA.nombreAlu);
- printf("Nota: %d \n\n",EA.nota);
- }
- getch();
- fclose(archivo);
- }
- int agregarDesdeArchivo(celda materias[10],int cant)
- {
- EstructuraArchivo EA;
- int nota;
- char alumno[20];
- char materia[20];
- FILE *archivo = fopen("ArregloArboles.dat","r b");
- while (fread(&EA,sizeof(EstructuraArchivo),1,archivo)!=0 && cant<10)
- {
- strcpy(materia,EA.materia);
- strcpy(alumno,EA.nombreAlu);
- nota=EA.nota;
- cant=agregarFinal(materias,cant,nota,materia,alumno);
- }
- fclose(archivo);
- mostrarTodo(materias,cant);
- return cant;
- }
- int main()
- {
- celda materias[10];
- int cant=0;
- int menu=0;
- while(menu!=7)
- {
- system("cls");
- printf("Menu\n\n1- Agregar notas de alumnos\n2- Mostrar materia especifica desde el arreglo\n3- Mostrar todo lo cargado en el arreglo\n4- Pasar lo reciente del arreglo al archivo\n5- Mostrar solo el archivo\n6- Introducir al arreglo los datos archivados y mostrar el arreglo actualizado\n7- SALIR\n");
- printf("Que desea hacer? ");
- fflush(stdin);
- scanf("%i",&menu);
- switch(menu)
- {
- case 1: cant=agregarMuchos(materias,cant);
- break;
- case 2: mostrar(materias,cant);
- getch();
- break;
- case 3: system("cls");
- mostrarTodo(materias,cant);
- getch();
- break;
- case 4: ArchivarListas(materias,cant);
- system("cls");
- printf("Hecho. Presione cualquier tecla para continuar");
- getch();
- break;
- case 5: system("cls");
- MostrarArchivoSinOrden();
- break;
- case 6: system("cls");
- cant=agregarDesdeArchivo(materias,cant);
- break;
- case 7: printf("Saliendo...");
- break;
- default: printf("Error, intente nuevamente");
- getch();
- };
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment