Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include<math.h>
- typedef struct player{
- int mez;
- char nev[42];
- int dobottgolok;
- struct player *next;
- }player;
- typedef struct {
- int mez;
- int ido;
- }gol;
- player *uj_elem_mez(char puffer[], player *head) //fajlbol a mezszamot adja a listahoz
- {
- player *p;
- p =(player*)malloc(sizeof(player));
- sscanf(puffer, "%d", &(p->mez));
- p->next=head;
- head=p;
- return head;
- }
- player *uj_elem_nev(char puffer[], player *head) //fajlbol a nevet adja a listahoz
- {
- player *p;
- p =(player*)malloc(sizeof(player));
- sscanf(puffer, "%s", p->nev);
- p->next=head;
- head=p;
- return head;
- }
- player *beolvas(char *fname) //beolvassa a binaris fajlbol a jatekosok tombbe
- {
- FILE *file;
- player *head;
- head=NULL;
- int db=17;
- int i;
- char puffer[50];
- char puffer2[10];
- file=fopen(fname,"rb");
- if (file == NULL)
- {
- printf("Nem sikerult megnyitni a binaris fajlt. :(\n");
- return 1;
- }
- for(i=0;i<db;++i)
- {
- fgets(puffer, 10, file);
- head =uj_elem_mez(puffer, head);
- fgets(head->nev, 42, file);
- head->dobottgolok = 0;
- }
- fclose(file);
- return head;
- }
- gol *golbeolvaso(char *fname2, double *goldarab) //beolvassa a szoveges fileból a golok tombbe
- {
- FILE *file;
- int i;
- char puffer[50];
- int goldb;
- gol *golok;
- file=fopen(fname2,"r");
- fgets(puffer, "%lf", file);
- sscanf(puffer, "%d", &goldb);
- golok = (gol*)malloc(goldb*sizeof(gol));
- for(i=0;i<goldb;++i)
- {
- fgets(puffer, 10, file);
- sscanf(puffer, "%d", &golok[i].ido);
- fgets(puffer, 10, file);
- sscanf(puffer,"%d", &golok[i].mez);
- }
- *goldarab = goldb;
- fclose(file);
- return golok;
- }
- void printlist(player *head) //kiirja a listat stdout-ra
- {
- player *p;
- p = head;
- if (head != NULL)
- {
- while (p != NULL)
- {
- printf("\n%d. szamu jatekos\n", p->mez);
- printf("%s", p->nev);
- printf("%d gol\n", p->dobottgolok);
- p=p->next;
- }
- }
- }
- void golhozzarendelo(gol *golok, player *head, double *goldarab) //a strukturaba irja hogy hany golt dobott
- {
- int i;
- player *p;
- p=head;
- for (i=0;i<*goldarab;i++)
- {
- while (golok[i].mez != p->mez)
- p = p->next;
- (p->dobottgolok) = (p->dobottgolok) + 1;
- p = head;
- }
- }
- double golszerzo(player *head) //megszamolja hanyan dobtak golt a meccs alatt
- {
- player *p;
- p=head;
- double gollovo;
- gollovo = 0;
- while (p->next != NULL)
- {
- if (p->dobottgolok != 0)
- gollovo++;
- p = p->next;
- }
- return gollovo;
- }
- double atlagszamolo(double gollovo, double goldarab) // kiszamolja hogy egy jatekosra hany gol jut
- { // csak azok kozul akik dobtak golt
- double atlag;
- atlag=goldarab/gollovo;
- return atlag;
- }
- void bigger_than_atlag(player *head, double atlag) // kiirja azokat a jatekosokat akik az atlagnal
- { // tobb golt szereztek
- player *p;
- p=head;
- printf("Az atlagnal tobb golt dobo jatekosok:\n");
- while (p->next != NULL)
- {
- if ((p->dobottgolok) >= atlag)
- printf("%s\n", p->nev);
- p=p->next;
- }
- }
- void playerfree(player* head) // visszaadja a playerbol allo listat
- {
- player *p;
- p=head;
- while (p->next != NULL)
- {
- head = p->next;
- free(p);
- p=head;
- }
- }
- int main()
- {
- char *fname = "jatekosok.dat";
- char *fname2 = "golok.txt";
- double gollovo;
- double atlag;
- double goldarab;
- player *head;
- gol *golok;
- char exit;
- head = beolvas(fname);
- golok = golbeolvaso(fname2, &goldarab);
- golhozzarendelo(golok, head, &goldarab);
- //printlist(head); // lista kiiro
- gollovo = golszerzo(head);
- atlag = atlagszamolo(gollovo, goldarab);
- printf("\n%f az atlag\n%f jatekos lott golt, %f gol\n\n", atlag, gollovo, goldarab);
- bigger_than_atlag(head, atlag);
- free(golok);
- playerfree(head);
- scanf("%c", &exit);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement