Advertisement
Turbi

Kocsmak

Nov 29th, 2020
1,021
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.30 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. /* Beugró példafeladat:
  7. Írjunk függvényt, amely visszaadja az első kocsmában kapható sörök átlagárát.
  8. */
  9.  
  10. typedef struct
  11. {
  12.     double x, y;
  13. }pont;
  14.  
  15. typedef struct _sorlista_elem               // 1.b
  16. {
  17.     char nev[3 + 1]; // Név max. 3 karakter + \0
  18.     unsigned ar;
  19.     struct _sorlista_elem* kov;
  20. } sorlista_elem;
  21.  
  22. typedef struct _publista_elem               // 1.b
  23. {
  24.     char nev[30 + 1]; //+ Név max. 30 karakter + \0
  25.     pont p;
  26.     struct _publista_elem* kov;
  27.     sorlista_elem* sorok;  // + kapható sörök
  28. } publista_elem;
  29.  
  30. publista_elem* uj_publista_elem(char* nev, double x, double y)  // 1.b
  31. {
  32.     publista_elem* uj_pub;
  33.     uj_pub = (publista_elem*)malloc(1 * sizeof(publista_elem));
  34.     if (uj_pub) {
  35.         int i;
  36.         for (i = 0; nev[i] != '\0' && i < 30; i++)
  37.             uj_pub->nev[i] = nev[i];
  38.         uj_pub->nev[i] = '\0';
  39.         uj_pub->p.x = x;
  40.         uj_pub->p.y = y;
  41.         uj_pub->kov = NULL;
  42.         uj_pub->sorok = NULL;
  43.     }
  44.     return uj_pub;
  45. }
  46.  
  47. void beszuras(publista_elem* lista, publista_elem* ujpub)   // 1.b
  48. {
  49.     publista_elem* akt_pub = lista;
  50.     while (akt_pub->kov->kov)
  51.     {
  52.         akt_pub = akt_pub->kov;
  53.     }
  54.     ujpub->kov = akt_pub->kov;
  55.     akt_pub->kov = ujpub;
  56. }
  57.  
  58. double tav(publista_elem* pp)               // 1.b
  59. {
  60.     return (pp->p.x * pp->p.x) + (pp->p.y * pp->p.y);
  61. }
  62.  
  63. void beszuras_rendezve(publista_elem* lista, publista_elem* ujpub)  // 1.b
  64. {
  65.     publista_elem* akt_pub = lista;
  66.  
  67.     while (akt_pub->kov->kov != NULL && strcmp(akt_pub->kov->nev, ujpub->nev) < 0) {
  68.         akt_pub = akt_pub->kov;
  69.     }
  70.     ujpub->kov = akt_pub->kov;
  71.     akt_pub->kov = ujpub;
  72. }
  73.  
  74. sorlista_elem* uj_sorlista_elem(char* nev, unsigned ar) // 1.b
  75. {
  76.     sorlista_elem* uj_sor;
  77.     uj_sor = (sorlista_elem*)malloc(1 * sizeof(sorlista_elem));
  78.     if (uj_sor) {
  79.         int i;
  80.         for (i = 0; nev[i] != '\0' && i < 3; i++)
  81.             uj_sor->nev[i] = nev[i];
  82.         uj_sor->nev[i] = '\0';
  83.         uj_sor->ar = ar;
  84.         uj_sor->kov = NULL;
  85.     }
  86.     return uj_sor;
  87. }
  88.  
  89. publista_elem* pub_keres(publista_elem* lista, char* pub_nev)   // 1.c
  90. {
  91.     return NULL;
  92. }
  93.  
  94. void sort_beszur(publista_elem* hely, sorlista_elem* sor)       // 1.d
  95. {
  96. }
  97.  
  98. int sort_hozzaad(publista_elem* lista, char* pub_nev, sorlista_elem* sor)   // 1.b
  99. {
  100.     publista_elem* akt_pub = pub_keres(lista, pub_nev); // 1.c
  101.     if (akt_pub)
  102.     {
  103.         sort_beszur(akt_pub, sor);          // 1.d
  104.         return 1;
  105.     }
  106.     else return 0;
  107. }
  108. void listaz_sorok(sorlista_elem* lista)         // 1.b
  109. {
  110.     sorlista_elem* akt_sor = lista;
  111.     while (akt_sor)
  112.     {
  113.         printf("%s\t", akt_sor->nev);
  114.         printf("%d\n", akt_sor->ar);
  115.         akt_sor = akt_sor->kov;
  116.     }
  117. }
  118.  
  119. publista_elem *elso(publista_elem *lista){
  120.     publista_elem *elso=lista->kov;
  121.     return elso;
  122. }
  123.  
  124. double atlagar(publista_elem* lista)
  125. {
  126.     int c=0;
  127.     double atlagar, sum = 0;
  128.     sorlista_elem* akt_sor = lista->sorok;
  129.     while (akt_sor) {
  130.         sum += akt_sor->ar;
  131.         c++;
  132.         akt_sor = akt_sor->kov;
  133.     }
  134.     atlagar = sum / c;
  135.     return atlagar;
  136. }
  137.  
  138. void listaz(publista_elem* lista)               // 1.b
  139. {
  140.     publista_elem* akt_pub = lista->kov; //Elsőt kihagy
  141.     while (akt_pub->kov) // Utolsót kihagy
  142.     {
  143.         printf("%s\n", akt_pub->nev);
  144.         printf("X: %lf\n", akt_pub->p.x);
  145.         printf("Y: %lf\n", akt_pub->p.y);
  146.         printf("===================\n");
  147.         listaz_sorok(akt_pub->sorok);
  148.         printf("===================\n\n");
  149.         akt_pub = akt_pub->kov;
  150.     }
  151. }
  152.  
  153. publista_elem *uccso(publista_elem *lista){
  154.     publista_elem *mozgo=lista->kov;
  155.     while(mozgo->kov->kov){
  156.         mozgo=mozgo->kov;
  157.     }
  158.     return mozgo;
  159. }
  160.  
  161. int hanysor(publista_elem *lista){
  162.     sorlista_elem *mozgo=lista->sorok;
  163.     int c=0;
  164.     while(mozgo){
  165.         c++;
  166.         mozgo=mozgo->kov;
  167.     }
  168.     return c;
  169. }
  170.  
  171.  
  172. publista_elem* legolcsobb_sor(publista_elem* lista, char* sor)  // 1.e
  173. {
  174.     return NULL;
  175. }
  176.  
  177. int bin_fajl_ir(char* filenev, publista_elem t[], unsigned n)
  178. {
  179.     FILE* out;
  180.     unsigned i;
  181.     out = fopen(filenev, "wb"); //w = write, b = binary (r=read, t=text)
  182.     if (!out)
  183.     {
  184.         printf("Error opening file: %s", filenev);
  185.         return 0;
  186.     }
  187.  
  188.     for (i = 0; i < n; i++)
  189.     {
  190.         fwrite(t[i].nev, sizeof(char), 30 + 1, out);
  191.         fwrite(&(t[i].p.x), sizeof(double), 1, out);
  192.         fwrite(&(t[i].p.y), sizeof(double), 1, out);
  193.     }
  194.  
  195.     if (fclose(out) != 0)
  196.     {
  197.         printf("Error closing file: %s", filenev);
  198.         return 0;
  199.     }
  200.     return 1;
  201. }
  202.  
  203.  
  204. int bin_fajl_olvas()        // 1.f
  205. {
  206.     return 0;
  207. }
  208.  
  209.  
  210. int main()
  211. {
  212.     publista_elem* pub;
  213.     publista_elem* lista_fej = uj_publista_elem("----", 0, 0);
  214.     publista_elem* lista_veg = uj_publista_elem("~~~~", 10000, 10000);
  215.     publista_elem tomb[41] = {
  216.     { "360 Bar", 47.498370, 19.040888 },
  217.     { "A38", 47.498340, 19.040812 },
  218.     { "Action Bar", 47.498348, 19.040882 },
  219.     { "Andersen Pub", 47.498364, 19.040887 },
  220.     { "AZNAP", 47.498325, 19.040872 },
  221.     { "BADGIRLZ", 47.498317, 19.040869 },
  222.     { "BarCraft", 47.498398, 19.040888 },
  223.     { "Becketts Irish Pub", 47.498358, 19.040850 },
  224.     { "Bestia", 47.498355, 19.040829 },
  225.     { "Boutiq’Bar", 47.498387, 19.040886 },
  226.     { "Columbus Club", 47.498379, 19.040863 },
  227.     { "Csipesz Bar", 47.498336, 19.040816 },
  228.     { "Doboz", 47.498369, 19.040883 },
  229.     { "EXTRA", 47.498344, 19.040815 },
  230.     { "Fekete Kutya", 47.498355, 19.040863 },
  231.     { "Filo Bar", 47.498314, 19.040817 },
  232.     { "Grandio", 47.498383, 19.040851 },
  233.     { "Hello Baby Bar", 47.498337, 19.040879 },
  234.     { "Holdudvar", 47.498397, 19.040817 },
  235.     { "HOPS Beer Bar Budapest", 47.498350, 19.040857 },
  236.     { "Ibolya Espresso", 47.498353, 19.040831 },
  237.     { "Instant", 47.498359, 19.040828 },
  238.     { "Irish Cat Pub", 47.498362, 19.040815 },
  239.     { "ISkola 2.0", 47.498393, 19.040838 },
  240.     { "Klub Vittula", 47.498393, 19.040867 },
  241.     { "Kraft", 47.498366, 19.040877 },
  242.     { "Liebling", 47.498316, 19.040843 },
  243.     { "London Stone Pub", 47.498310, 19.040839 },
  244.     { "Marionett", 47.498314, 19.040890 },
  245.     { "Most!", 47.498398, 19.040814 },
  246.     { "Neked csak Dezso", 47.498397, 19.040877 },
  247.     { "One More Please", 47.498377, 19.040899 },
  248.     { "Piritos", 47.498379, 19.040838 },
  249.     { "Platz", 47.498367, 19.040843 },
  250.     { "Raqpart", 47.498397, 19.040848 },
  251.     { "Schatzi", 47.498328, 19.040890 },
  252.     { "Spiccproject", 47.498393, 19.040832 },
  253.     { "Trafik Klub", 47.498382, 19.040813 },
  254.     { "WARMUP", 47.498337, 19.040882 },
  255.     { "WNDRLND", 47.498374, 19.040806 },
  256.     { "ZONA", 47.498347, 19.040864 }
  257.     };
  258.    
  259.     bin_fajl_ir("kocsma.bin", tomb, 41);
  260.  
  261.     lista_fej->kov = lista_veg;
  262.     beszuras_rendezve(lista_fej, uj_publista_elem("Tuti", 47.498354, 19.040815));
  263.     beszuras_rendezve(lista_fej, uj_publista_elem("ZP", 47.498342, 19.040825));
  264.     beszuras_rendezve(lista_fej, uj_publista_elem("Szoda", 47.498315, 19.04082));
  265.     beszuras_rendezve(lista_fej, uj_publista_elem("B7Cl", 47.498372, 19.040867));
  266.     beszuras_rendezve(lista_fej, uj_publista_elem("I PuB", 47.498366, 19.040844));
  267.     beszuras_rendezve(lista_fej, uj_publista_elem("Carlo", 47.498387, 19.040854));
  268.     beszuras_rendezve(lista_fej, uj_publista_elem("Jaffa", 47.498311, 19.040827));
  269.  
  270.     bin_fajl_olvas("kocsma.bin", lista_fej);
  271.  
  272.     sort_hozzaad(lista_fej, "Tuti", uj_sorlista_elem("DAB", 315));
  273.     sort_hozzaad(lista_fej, "Tuti", uj_sorlista_elem("BSD", 285));
  274.     sort_hozzaad(lista_fej, "A38", uj_sorlista_elem("HEI", 425));
  275.     sort_hozzaad(lista_fej, "A38", uj_sorlista_elem("BSD", 325));
  276.  
  277.     listaz(lista_fej);
  278.  
  279.     pub = legolcsobb_sor(lista_fej, "BSD");     // 1.e
  280.     if (pub) printf("%f\t%f\n", pub->p.x, pub->p.y);
  281.     listaz_sorok(tomb);
  282.     double c = atlagar(elso(lista_fej));
  283.     printf("\n");
  284.     printf("%f", c);
  285.     int d;
  286.     d=hanysor(uccso(lista_fej));
  287.     printf("%d", d);
  288.     return 0;
  289. }
  290.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement