Facebook
From Putrid Hedgehog, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 256
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <conio.h>
  5.  
  6.  
  7.  
  8. struct lista {
  9.         char imie[20];
  10.         char nazwisko[20];
  11.         int wiek;
  12.         struct lista *next;
  13. };
  14.  
  15. void dodaj(struct lista *dane) {
  16.         struct lista *tmp;
  17.         tmp=malloc(sizeof(struct lista));
  18.        
  19.         char tmp1[20];
  20.         char tmp2[20];
  21.        
  22.         printf("\n\nPodaj imie \n");
  23.         fgets(tmp1,20,stdin);
  24.         strcpy(tmp->imie, tmp1);
  25.         fflush(stdin);
  26.        
  27.         printf("Podaj nazwisko \n");
  28.         fgets(tmp2,20,stdin);
  29.         strcpy(tmp->nazwisko, tmp2);
  30.         fflush(stdin);
  31.        
  32.         printf("Podaj wiek \n");
  33.         scanf("%i",&tmp->wiek);
  34.         fflush(stdin);
  35.         tmp->next=NULL;
  36.         while(dane->next != NULL){
  37.        
  38.         dane=dane->next;
  39. }
  40.         dane->next=tmp;
  41.        
  42. }
  43.  
  44.  
  45. void zapisz(struct lista *dane,FILE *plik){
  46.         plik=fopen("test.txt","w");
  47.             while(dane->next != NULL)
  48.     {
  49.         fprintf(plik,"\n%s",dane->imie);
  50.         fprintf(plik,"%s",dane->nazwisko);
  51.         fprintf(plik,"%i",dane->wiek);
  52.             dane=dane->next;
  53. }
  54.         fprintf(plik,"\n%s",dane->imie);
  55.         fprintf(plik,"%s",dane->nazwisko);
  56.         fprintf(plik,"%i",dane->wiek);
  57.  
  58. fclose(plik);
  59.        
  60. }
  61.  
  62.  
  63.  
  64. void show(struct lista *dane)
  65. {
  66.      while(dane->next != NULL)
  67.     {
  68.     printf("%s\n", dane->imie);
  69.         printf("%s\n", dane->nazwisko);
  70.         printf("%i\n", dane->wiek);
  71.     dane=dane->next;
  72. }
  73.     printf("%s\n", dane->imie);
  74.         printf("%s\n", dane->nazwisko);
  75.         printf("%i\n", dane->wiek);
  76. }
  77.  
  78.  
  79. int main() {
  80.         FILE *pliczek;
  81.         struct lista *dane;
  82.         dane = malloc(sizeof(struct lista));
  83.         char znak;
  84.        
  85. strcpy(dane->imie,"Test");     
  86. strcpy(dane->nazwisko,"Testowe");
  87.         dane->wiek=20;
  88.         dane->next=NULL;
  89.         start:
  90.         printf("-------------------------------------------------------------\nCo chcesz zrobic?\n\n1. Dodaj nowy element\n2. Wypisz elementy\n3.Zapisz elementy do pliku\n4.Zakoncz program\n");
  91.         znak=getch();
  92.        
  93.         if(znak=='1') {
  94.                 fflush(stdin);
  95.                 dodaj(dane);
  96.  
  97.                 goto start;
  98.         }
  99.         if(znak=='2') {
  100.                 fflush(stdin);
  101.                 show(dane);
  102.                 goto start;
  103.         }
  104.         if(znak=='3') {
  105.         fflush(stdin);
  106.         zapisz(dane,pliczek);
  107.         goto start;
  108.         }
  109.         if(znak=='4')
  110.                 return 0;
  111.        
  112. }