Guest User

Untitled

a guest
Mar 13th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <string.h>
  5.  
  6. typedef struct NodoLista
  7. {
  8.     char nombreAlu[20];
  9.     int nota;
  10.     struct NodoLista * sig;
  11. } NodoLista;
  12.  
  13. typedef struct celda
  14. {
  15.     char materia[20];
  16.     struct NodoLista * lista;
  17. } celda;
  18.  
  19. typedef struct EstructuraArchivo
  20. {
  21.     char materia[20];
  22.     char nombreAlu[20];
  23.     int nota;
  24. } EstructuraArchivo;
  25.  
  26.  
  27. NodoLista * crearNodo(char nombre[20], int nota)
  28. {
  29.     NodoLista* lista=(NodoLista*)malloc(sizeof(NodoLista));
  30.     strcpy(lista->nombreAlu,nombre);
  31.     lista->nota=nota;
  32.     lista->sig=NULL;
  33.     return lista;
  34. }
  35.  
  36. int buscarPos(celda arrMaterias[10],int cant, char nombreMat[20])
  37. {
  38.     int pos=0;
  39.     int rta=-1;
  40.     while (pos<cant&&rta==-1)
  41.     {
  42.         if (strcmpi(arrMaterias[pos].materia,nombreMat)==0)
  43.         {
  44.             rta=pos;
  45.         }
  46.         pos++;
  47.     }
  48.     return rta;
  49. }
  50.  
  51. int AgregarEnArreglo(celda materias[30], int cant, char materia[30])
  52. {
  53.     strcpy(materias[cant].materia,materia);
  54.     materias[cant].lista=NULL;
  55.     cant++;
  56.     return cant;
  57. }
  58.  
  59. NodoLista * buscarUltimo(NodoLista * lista)
  60. {
  61.     NodoLista * aux =lista;
  62.     while(aux->sig!=NULL)
  63.     {
  64.         aux = aux->sig;
  65.     }
  66.     return aux;
  67. }
  68.  
  69. NodoLista * AF(NodoLista *lista, NodoLista * nuevo)
  70. {
  71.     if(lista==NULL)
  72.     {
  73.         lista=nuevo;
  74.     }
  75.     else
  76.     {
  77.         NodoLista*ultimo=buscarUltimo(lista);
  78.         ultimo->sig=nuevo;
  79.     }
  80.     return lista;
  81. }
  82.  
  83. int agregarFinal(celda materias[20], int cant, int nota, char materia[30], char alumno[30])
  84. {
  85.     NodoLista *aux = crearNodo(alumno,nota);
  86.     int pos = buscarPos(materias,cant,materia);
  87.  
  88.     if(pos==-1)
  89.     {
  90.         cant = AgregarEnArreglo(materias,cant,materia);
  91.         pos = cant-1;
  92.     }
  93.     materias[pos].lista=AF(materias[pos].lista,aux);
  94.  
  95.     return cant;
  96. }
  97.  
  98. int agregarMuchos(celda materias[10],int cant){
  99.     int nota;
  100.     char alumno[20];
  101.     char materia[20];
  102.  
  103.     system("cls");
  104.     printf("Ingrese el nombre de la materia a la que desea agregar datos: ");
  105.     fflush(stdin);
  106.     gets(materia);
  107.     printf("Ingrese el nombre del alumno: ");
  108.     fflush(stdin);
  109.     gets(alumno);
  110.     printf("Ingrese la nota de %s: ",alumno);
  111.     fflush(stdin);
  112.     scanf("%d",&nota);
  113.     cant = agregarFinal(materias,cant,nota,materia,alumno);
  114.  
  115.     return cant;
  116. }
  117.  
  118. void mostrarLista (NodoLista * lista)
  119. {
  120.     if(lista!=NULL)
  121.     {
  122.         printf("\nAlumno: %s | ", lista->nombreAlu);
  123.         printf("Nota: %d . ", lista->nota);
  124.         mostrarLista(lista->sig);
  125.     }
  126. }
  127.  
  128. void mostrarTodo(celda materias[20],int cant)
  129. {
  130.     int i=0;
  131.  
  132.     while (i<cant)
  133.     {
  134.         system("cls");
  135.         printf("Materia: %s\n",materias[i].materia);
  136.         mostrarLista(materias[i].lista);
  137.         getch();
  138.         i++;
  139.     }
  140. }
  141.  
  142. void mostrar(celda materias[20],int cant)
  143. {
  144.     char mat[20];
  145.     int busca;
  146.  
  147.     printf("\nIngrese la materia que desee mostrar: \n");
  148.     fflush(stdin);
  149.     scanf("%s",mat);
  150.     busca=buscarPos(materias,cant,mat);
  151.     if (busca!=-1)
  152.     {
  153.         system("cls");
  154.         mostrarLista(materias[busca].lista);
  155.     }
  156. }
  157.  
  158. void escribe(NodoLista *lista,char materia[20],FILE *archivo)
  159. {
  160.     EstructuraArchivo EA;
  161.  
  162.     if (lista != NULL)
  163.     {
  164.         strcpy(EA.materia, materia);
  165.         strcpy(EA.nombreAlu, lista->nombreAlu);
  166.         EA.nota = lista->nota;
  167.         fwrite(&EA,sizeof(EstructuraArchivo),1,archivo);
  168.         escribe(lista->sig,materia,archivo);
  169.     }
  170. }
  171.  
  172. void ArchivarListas (celda ArrMaterias[10],int cant)
  173. {
  174.     FILE *archivo = fopen("ArregloArboles.dat","a+b");
  175.     int i=0;
  176.     NodoLista *lista;
  177.  
  178.     while(i < cant)
  179.     {
  180.         lista = ArrMaterias[i].lista;
  181.         if (lista!= NULL)
  182.         {
  183.             escribe(lista,ArrMaterias[i].materia,archivo);
  184.         }
  185.         i++;
  186.     }
  187.     fclose(archivo);
  188. }
  189.  
  190. void MostrarArchivoSinOrden()
  191. {
  192.     FILE *archivo = fopen("ArregloArboles.dat","r b");
  193.     EstructuraArchivo EA;
  194.  
  195.     while (fread(&EA,sizeof(EstructuraArchivo),1,archivo)!=0)
  196.     {
  197.          printf("Materia: %s.\n",EA.materia);
  198.          printf("Alumno: %s \n",EA.nombreAlu);
  199.          printf("Nota: %d \n\n",EA.nota);
  200.     }
  201.     getch();
  202.     fclose(archivo);
  203. }
  204.  
  205. int agregarDesdeArchivo(celda materias[10],int cant)
  206. {
  207.     EstructuraArchivo EA;
  208.     int nota;
  209.     char alumno[20];
  210.     char materia[20];
  211.     FILE *archivo = fopen("ArregloArboles.dat","r b");
  212.  
  213.     while (fread(&EA,sizeof(EstructuraArchivo),1,archivo)!=0 && cant<10)
  214.     {
  215.         strcpy(materia,EA.materia);
  216.         strcpy(alumno,EA.nombreAlu);
  217.         nota=EA.nota;
  218.         cant=agregarFinal(materias,cant,nota,materia,alumno);
  219.     }
  220.     fclose(archivo);
  221.     mostrarTodo(materias,cant);
  222.  
  223.     return cant;
  224. }
  225.  
  226.  
  227. int main()
  228. {
  229.     celda materias[10];
  230.     int cant=0;
  231.     int menu=0;
  232.  
  233.     while(menu!=7)
  234.     {
  235.         system("cls");
  236.         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");
  237.         printf("Que desea hacer? ");
  238.         fflush(stdin);
  239.         scanf("%i",&menu);
  240.         switch(menu)
  241.         {
  242.             case 1: cant=agregarMuchos(materias,cant);
  243.                     break;
  244.             case 2: mostrar(materias,cant);
  245.                     getch();
  246.                     break;
  247.             case 3: system("cls");
  248.                     mostrarTodo(materias,cant);
  249.                     getch();
  250.                     break;
  251.             case 4: ArchivarListas(materias,cant);
  252.                     system("cls");
  253.                     printf("Hecho. Presione cualquier tecla para continuar");
  254.                     getch();
  255.                     break;
  256.             case 5: system("cls");
  257.                     MostrarArchivoSinOrden();
  258.                     break;
  259.             case 6: system("cls");
  260.                     cant=agregarDesdeArchivo(materias,cant);
  261.                     break;
  262.             case 7: printf("Saliendo...");
  263.                     break;
  264.             default: printf("Error, intente nuevamente");
  265.                      getch();
  266.         };
  267.     }
  268.     return 0;
  269. }
Add Comment
Please, Sign In to add comment