Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /* Beugró példafeladat:
- Írjunk függvényt, amely visszaadja az első kocsmában kapható sörök átlagárát.
- */
- typedef struct
- {
- double x, y;
- }pont;
- typedef struct _sorlista_elem // 1.b
- {
- char nev[3 + 1]; // Név max. 3 karakter + \0
- unsigned ar;
- struct _sorlista_elem* kov;
- } sorlista_elem;
- typedef struct _publista_elem // 1.b
- {
- char nev[30 + 1]; //+ Név max. 30 karakter + \0
- pont p;
- struct _publista_elem* kov;
- sorlista_elem* sorok; // + kapható sörök
- } publista_elem;
- publista_elem* uj_publista_elem(char* nev, double x, double y) // 1.b
- {
- publista_elem* uj_pub;
- uj_pub = (publista_elem*)malloc(1 * sizeof(publista_elem));
- if (uj_pub) {
- int i;
- for (i = 0; nev[i] != '\0' && i < 30; i++)
- uj_pub->nev[i] = nev[i];
- uj_pub->nev[i] = '\0';
- uj_pub->p.x = x;
- uj_pub->p.y = y;
- uj_pub->kov = NULL;
- uj_pub->sorok = NULL;
- }
- return uj_pub;
- }
- void beszuras(publista_elem* lista, publista_elem* ujpub) // 1.b
- {
- publista_elem* akt_pub = lista;
- while (akt_pub->kov->kov)
- {
- akt_pub = akt_pub->kov;
- }
- ujpub->kov = akt_pub->kov;
- akt_pub->kov = ujpub;
- }
- double tav(publista_elem* pp) // 1.b
- {
- return (pp->p.x * pp->p.x) + (pp->p.y * pp->p.y);
- }
- void beszuras_rendezve(publista_elem* lista, publista_elem* ujpub) // 1.b
- {
- publista_elem* akt_pub = lista;
- while (akt_pub->kov->kov != NULL && strcmp(akt_pub->kov->nev, ujpub->nev) < 0) {
- akt_pub = akt_pub->kov;
- }
- ujpub->kov = akt_pub->kov;
- akt_pub->kov = ujpub;
- }
- sorlista_elem* uj_sorlista_elem(char* nev, unsigned ar) // 1.b
- {
- sorlista_elem* uj_sor;
- uj_sor = (sorlista_elem*)malloc(1 * sizeof(sorlista_elem));
- if (uj_sor) {
- int i;
- for (i = 0; nev[i] != '\0' && i < 3; i++)
- uj_sor->nev[i] = nev[i];
- uj_sor->nev[i] = '\0';
- uj_sor->ar = ar;
- uj_sor->kov = NULL;
- }
- return uj_sor;
- }
- publista_elem* pub_keres(publista_elem* lista, char* pub_nev) // 1.c
- {
- return NULL;
- }
- void sort_beszur(publista_elem* hely, sorlista_elem* sor) // 1.d
- {
- }
- int sort_hozzaad(publista_elem* lista, char* pub_nev, sorlista_elem* sor) // 1.b
- {
- publista_elem* akt_pub = pub_keres(lista, pub_nev); // 1.c
- if (akt_pub)
- {
- sort_beszur(akt_pub, sor); // 1.d
- return 1;
- }
- else return 0;
- }
- void listaz_sorok(sorlista_elem* lista) // 1.b
- {
- sorlista_elem* akt_sor = lista;
- while (akt_sor)
- {
- printf("%s\t", akt_sor->nev);
- printf("%d\n", akt_sor->ar);
- akt_sor = akt_sor->kov;
- }
- }
- publista_elem *elso(publista_elem *lista){
- publista_elem *elso=lista->kov;
- return elso;
- }
- double atlagar(publista_elem* lista)
- {
- int c=0;
- double atlagar, sum = 0;
- sorlista_elem* akt_sor = lista->sorok;
- while (akt_sor) {
- sum += akt_sor->ar;
- c++;
- akt_sor = akt_sor->kov;
- }
- atlagar = sum / c;
- return atlagar;
- }
- void listaz(publista_elem* lista) // 1.b
- {
- publista_elem* akt_pub = lista->kov; //Elsőt kihagy
- while (akt_pub->kov) // Utolsót kihagy
- {
- printf("%s\n", akt_pub->nev);
- printf("X: %lf\n", akt_pub->p.x);
- printf("Y: %lf\n", akt_pub->p.y);
- printf("===================\n");
- listaz_sorok(akt_pub->sorok);
- printf("===================\n\n");
- akt_pub = akt_pub->kov;
- }
- }
- publista_elem *uccso(publista_elem *lista){
- publista_elem *mozgo=lista->kov;
- while(mozgo->kov->kov){
- mozgo=mozgo->kov;
- }
- return mozgo;
- }
- int hanysor(publista_elem *lista){
- sorlista_elem *mozgo=lista->sorok;
- int c=0;
- while(mozgo){
- c++;
- mozgo=mozgo->kov;
- }
- return c;
- }
- publista_elem* legolcsobb_sor(publista_elem* lista, char* sor) // 1.e
- {
- return NULL;
- }
- int bin_fajl_ir(char* filenev, publista_elem t[], unsigned n)
- {
- FILE* out;
- unsigned i;
- out = fopen(filenev, "wb"); //w = write, b = binary (r=read, t=text)
- if (!out)
- {
- printf("Error opening file: %s", filenev);
- return 0;
- }
- for (i = 0; i < n; i++)
- {
- fwrite(t[i].nev, sizeof(char), 30 + 1, out);
- fwrite(&(t[i].p.x), sizeof(double), 1, out);
- fwrite(&(t[i].p.y), sizeof(double), 1, out);
- }
- if (fclose(out) != 0)
- {
- printf("Error closing file: %s", filenev);
- return 0;
- }
- return 1;
- }
- int bin_fajl_olvas() // 1.f
- {
- return 0;
- }
- int main()
- {
- publista_elem* pub;
- publista_elem* lista_fej = uj_publista_elem("----", 0, 0);
- publista_elem* lista_veg = uj_publista_elem("~~~~", 10000, 10000);
- publista_elem tomb[41] = {
- { "360 Bar", 47.498370, 19.040888 },
- { "A38", 47.498340, 19.040812 },
- { "Action Bar", 47.498348, 19.040882 },
- { "Andersen Pub", 47.498364, 19.040887 },
- { "AZNAP", 47.498325, 19.040872 },
- { "BADGIRLZ", 47.498317, 19.040869 },
- { "BarCraft", 47.498398, 19.040888 },
- { "Becketts Irish Pub", 47.498358, 19.040850 },
- { "Bestia", 47.498355, 19.040829 },
- { "Boutiq’Bar", 47.498387, 19.040886 },
- { "Columbus Club", 47.498379, 19.040863 },
- { "Csipesz Bar", 47.498336, 19.040816 },
- { "Doboz", 47.498369, 19.040883 },
- { "EXTRA", 47.498344, 19.040815 },
- { "Fekete Kutya", 47.498355, 19.040863 },
- { "Filo Bar", 47.498314, 19.040817 },
- { "Grandio", 47.498383, 19.040851 },
- { "Hello Baby Bar", 47.498337, 19.040879 },
- { "Holdudvar", 47.498397, 19.040817 },
- { "HOPS Beer Bar Budapest", 47.498350, 19.040857 },
- { "Ibolya Espresso", 47.498353, 19.040831 },
- { "Instant", 47.498359, 19.040828 },
- { "Irish Cat Pub", 47.498362, 19.040815 },
- { "ISkola 2.0", 47.498393, 19.040838 },
- { "Klub Vittula", 47.498393, 19.040867 },
- { "Kraft", 47.498366, 19.040877 },
- { "Liebling", 47.498316, 19.040843 },
- { "London Stone Pub", 47.498310, 19.040839 },
- { "Marionett", 47.498314, 19.040890 },
- { "Most!", 47.498398, 19.040814 },
- { "Neked csak Dezso", 47.498397, 19.040877 },
- { "One More Please", 47.498377, 19.040899 },
- { "Piritos", 47.498379, 19.040838 },
- { "Platz", 47.498367, 19.040843 },
- { "Raqpart", 47.498397, 19.040848 },
- { "Schatzi", 47.498328, 19.040890 },
- { "Spiccproject", 47.498393, 19.040832 },
- { "Trafik Klub", 47.498382, 19.040813 },
- { "WARMUP", 47.498337, 19.040882 },
- { "WNDRLND", 47.498374, 19.040806 },
- { "ZONA", 47.498347, 19.040864 }
- };
- bin_fajl_ir("kocsma.bin", tomb, 41);
- lista_fej->kov = lista_veg;
- beszuras_rendezve(lista_fej, uj_publista_elem("Tuti", 47.498354, 19.040815));
- beszuras_rendezve(lista_fej, uj_publista_elem("ZP", 47.498342, 19.040825));
- beszuras_rendezve(lista_fej, uj_publista_elem("Szoda", 47.498315, 19.04082));
- beszuras_rendezve(lista_fej, uj_publista_elem("B7Cl", 47.498372, 19.040867));
- beszuras_rendezve(lista_fej, uj_publista_elem("I PuB", 47.498366, 19.040844));
- beszuras_rendezve(lista_fej, uj_publista_elem("Carlo", 47.498387, 19.040854));
- beszuras_rendezve(lista_fej, uj_publista_elem("Jaffa", 47.498311, 19.040827));
- bin_fajl_olvas("kocsma.bin", lista_fej);
- sort_hozzaad(lista_fej, "Tuti", uj_sorlista_elem("DAB", 315));
- sort_hozzaad(lista_fej, "Tuti", uj_sorlista_elem("BSD", 285));
- sort_hozzaad(lista_fej, "A38", uj_sorlista_elem("HEI", 425));
- sort_hozzaad(lista_fej, "A38", uj_sorlista_elem("BSD", 325));
- listaz(lista_fej);
- pub = legolcsobb_sor(lista_fej, "BSD"); // 1.e
- if (pub) printf("%f\t%f\n", pub->p.x, pub->p.y);
- listaz_sorok(tomb);
- double c = atlagar(elso(lista_fej));
- printf("\n");
- printf("%f", c);
- int d;
- d=hanysor(uccso(lista_fej));
- printf("%d", d);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement