Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- #define ARRAYLAENGDE 50
- #define antalloeb 1000
- #define antal_maaneder 2
- #define antal_dageprmaaned 31
- #define antal_unikkeloebere 40
- // (hele opg) Main - Laver motionstruct for opgaven
- typedef struct
- {
- char navn[ARRAYLAENGDE],
- ugedag[ARRAYLAENGDE],
- maaned[ARRAYLAENGDE];
- int dato,
- timestart,
- minutstart,
- distance,
- timeantal,
- minutantal,
- sekundantal;
- }struMotion;
- // (hele opg) Main - funktion der læser hver linje ind i hver sit struct - gemmes i array aMotion
- void fLaesdata(const char *filnavn, struMotion aMotion[])
- {
- FILE *ifp;
- char aNavn[ARRAYLAENGDE], aUgedag[ARRAYLAENGDE],aMaaned[ARRAYLAENGDE];
- int iDato,
- iTimestart,
- iMinutstart,
- iDistance,
- iTimeantal,
- iMinutantal,
- iSekundantal,
- i = 0;
- struMotion lokalLoeber;
- ifp = fopen(filnavn, "r");
- while (fscanf(ifp, " %s %s %s %d, 2011 %d:%d %d %d:%d:%d", aNavn, aUgedag, aMaaned,
- &iDato, &iTimestart, &iMinutstart, &iDistance,
- &iTimeantal, &iMinutantal, &iSekundantal) == 10)
- {
- strcpy(lokalLoeber.navn, aNavn);
- strcpy(lokalLoeber.ugedag, aUgedag);
- strcpy(lokalLoeber.maaned, aMaaned);
- lokalLoeber.dato = iDato;
- lokalLoeber.timestart = iTimestart;
- lokalLoeber.minutstart = iMinutstart;
- lokalLoeber.distance = iDistance;
- lokalLoeber.timeantal = iTimeantal;
- lokalLoeber.minutantal = iMinutantal;
- lokalLoeber.sekundantal = iSekundantal;
- aMotion[i] = lokalLoeber;
- i++;
- }
- fclose(ifp);
- }
- // 1.1 - Udregner hastigheden for den i'te loeber
- void fRegnhast(struMotion aMotion, double *dHast)
- {
- int a = 60, b = 3600;
- double tidTim = 0.0, tidMin = 0.0,
- tidSek = 0.0, tidTimer = 0.0, KprM = 1000;
- tidTim = (double)aMotion.timeantal;
- tidMin = ((double)aMotion.minutantal / a );
- tidSek = ((double)aMotion.sekundantal / b);
- tidTimer = tidTim + (tidMin + tidSek);
- *dHast = ((((double)aMotion.distance)/KprM) / tidTimer);
- }
- // 1.2 printer i'te struct i array
- void fPrintloeb(struMotion loeb)
- {
- printf ("navn: %s\nugedag: %s\ndato: %s %d, 2011\nstarttidspunkt: %d:%d\ndistance: %d\ntid: %d:%d:%d\n\n",
- loeb.navn, loeb.ugedag, loeb.maaned,
- loeb.dato, loeb.timestart, loeb.minutstart,
- loeb.distance, loeb.timeantal,
- loeb.minutantal, loeb.sekundantal);
- }
- // 2.1 laver ugedags-struct
- typedef struct
- {
- int mandag,
- tirsdag,
- onsdag,
- torsdag,
- fredag,
- loerdag,
- soendag;
- } struUgedag;
- // 2.2 laver lokal struct med værdier 0.
- void fClean_structUgedag(struUgedag *stru)
- {
- int z = 0;
- (*stru).mandag = z;
- (*stru).tirsdag = z;
- (*stru).onsdag = z;
- (*stru).torsdag = z;
- (*stru).fredag = z;
- (*stru).loerdag = z;
- (*stru).soendag = z;
- }
- // 2.3 funktion der tager distancen for specifik ugedag, og placerer i kopistruct
- void fUgedag(struMotion aMotion, struUgedag *aUgeloeb)
- {
- int x = 0;
- x = aMotion.distance;
- if (strcmp(aMotion.ugedag, "Mon") == 0)
- (*aUgeloeb).mandag = x;
- else if (strcmp(aMotion.ugedag, "Tue") == 0)
- (*aUgeloeb).tirsdag = x;
- else if (strcmp(aMotion.ugedag, "Wed") == 0)
- (*aUgeloeb).onsdag = x;
- else if (strcmp(aMotion.ugedag, "Thu") == 0)
- (*aUgeloeb).torsdag = x;
- else if (strcmp(aMotion.ugedag, "Fri") == 0)
- (*aUgeloeb).fredag = x;
- else if (strcmp(aMotion.ugedag, "Sat") == 0)
- (*aUgeloeb).loerdag = x;
- else if (strcmp(aMotion.ugedag, "Sun") == 0)
- (*aUgeloeb).soendag = x;
- }
- //4.1 funktion der cleaner struct
- void fClean_structKopi(struMotion *aMotionSort)
- {
- int z = 0;
- strcpy((*aMotionSort).navn, "");
- strcpy((*aMotionSort).ugedag, "");
- strcpy((*aMotionSort).maaned, "");
- (*aMotionSort).dato = z;
- (*aMotionSort).timestart = z;
- (*aMotionSort).minutstart = z;
- (*aMotionSort).distance = z;
- (*aMotionSort).timeantal = z;
- (*aMotionSort).minutantal = z;
- (*aMotionSort).sekundantal = z;
- }
- // 4.2 funktion der regner hvornår det i'te loeb er sluttet
- void fSluttidspunkt(struMotion aMotion,int *iMotionsluttime, int *iMotionslutmin)
- {
- int a = 60;
- int b = 1;
- if ((aMotion.minutstart + aMotion.minutantal) < a)
- {
- *iMotionsluttime = aMotion.timestart + aMotion.timeantal;
- *iMotionslutmin = aMotion.minutstart + aMotion.minutantal;
- }
- else
- {
- *iMotionslutmin = ((aMotion.minutstart + aMotion.minutantal) - a);
- *iMotionsluttime = ((aMotion.timestart + aMotion.timeantal) + b);
- }
- }
- // 4.3 funktion der kopierer i aMotionkopi, hvis loebet er indenfor de angivne tider
- void fTidstjek(struMotion aMotion, int iStarttime, int iStartmin, int iSluttime,
- int iSlutmin, int iMotionsluttime, int iMotionslutmin, struMotion *aMotionSort, int *iTjek)
- {
- strcpy((*aMotionSort).navn, aMotion.navn);
- strcpy((*aMotionSort).ugedag, aMotion.ugedag);
- strcpy((*aMotionSort).maaned, aMotion.maaned);
- (*aMotionSort).dato = aMotion.dato;
- (*aMotionSort).timestart = aMotion.timestart;
- (*aMotionSort).minutstart = aMotion.minutstart;
- (*aMotionSort).distance = aMotion.distance;
- (*aMotionSort).timeantal = aMotion.timeantal;
- (*aMotionSort).minutantal = aMotion.minutantal;
- (*aMotionSort).sekundantal = aMotion.sekundantal;
- if ((iStarttime < aMotion.timestart) && (iSluttime > iMotionsluttime))
- {
- *iTjek = 1;
- }
- else if ((iStarttime == aMotion.timestart) && (iSluttime > iMotionsluttime))
- {
- if (iStartmin < aMotion.minutstart)
- {
- *iTjek = 1;
- }
- }
- else if ((iStarttime < aMotion.timestart) && (iSluttime == iMotionsluttime))
- {
- if (iSlutmin > iMotionsluttime)
- {
- *iTjek = 1;
- }
- }
- else if ((iStarttime == aMotion.timestart) && (iSluttime == iMotionsluttime))
- {
- if ((iStartmin < aMotion.minutstart) && (iSlutmin > iMotionslutmin))
- {
- *iTjek = 1;
- }
- }
- }
- // 4.4 funktion der cleaner aMotionSort
- void fClean_structKopia(struMotion *aMotionSort)
- {
- int z = 0;
- strcpy((*aMotionSort).navn, "");
- strcpy((*aMotionSort).ugedag, "");
- strcpy((*aMotionSort).maaned, "");
- (*aMotionSort).dato = z;
- (*aMotionSort).timestart = z;
- (*aMotionSort).minutstart = z;
- (*aMotionSort).distance = z;
- (*aMotionSort).timeantal = z;
- (*aMotionSort).minutantal = z;
- (*aMotionSort).sekundantal = z;
- }
- // 4.5 funktion der bytter 2 structs plads i array aSort
- void fSwap(struMotion *aSort, struMotion *bSort)
- {
- struMotion kopi;
- kopi = *aSort;
- *aSort = *bSort;
- *bSort = kopi;
- }
- // 5.1
- void fKopier(struMotion a, struMotion *b)
- {
- strcpy((*b).navn, a.navn);
- (*b).distance += a.distance;
- (*b).timeantal += a.timeantal;
- (*b).minutantal += a.minutantal;
- (*b).sekundantal += a.sekundantal;
- }
- // printer fortsæt menuvalgmuligheder
- void fFortsaet();
- // 1.0 delproblem 1 sættes igang
- void fprob1()
- {
- int i = 0;
- double x = 0.0, dHast[antalloeb];
- struMotion aMotion[antalloeb];
- // kører laesdata, for at kalde arrayet
- fLaesdata("runs", aMotion);
- // søger og printer den valgte hastighed
- printf("skriv hvilken hastighed der søges efter, dette er angivet i km/t:");
- scanf("%lf", &x);
- printf("Foelgende har loebet hurtigere end den indtastede hastighed:\n \n");
- // Kør loop, der tjekker om x <= end hastighed, hvis ja, så print.
- for (i = 0; i < antalloeb; i++)
- {
- fRegnhast(aMotion[i], &dHast[i]);
- if (x <= dHast[i]) {
- fPrintloeb(aMotion[i]);
- }
- }
- fFortsaet();
- }
- // 2.0 delproblem 2 sættes igang
- void fprob2()
- {
- int i = 0, z = 0;
- struMotion aMotion[antalloeb];
- struUgedag aUgeloeb;
- aUgeloeb.mandag = z;
- aUgeloeb.tirsdag = z;
- aUgeloeb.onsdag = z;
- aUgeloeb.torsdag = z;
- aUgeloeb.fredag = z;
- aUgeloeb.loerdag = z;
- aUgeloeb.soendag = z;
- //struUgedag struUgekopi;
- struUgedag aUgelokal;
- // funktion 2.2, der sætter structens værdier til 0.
- fClean_structUgedag(&aUgelokal);
- // kører laesdata, for at kalde arrayet
- fLaesdata("runs", aMotion);
- for (i = 0; i < antalloeb; i++)
- {
- fUgedag(aMotion[i], &aUgeloeb);
- aUgelokal.mandag += aUgeloeb.mandag;
- aUgelokal.tirsdag += aUgeloeb.tirsdag;
- aUgelokal.onsdag += aUgeloeb.onsdag;
- aUgelokal.torsdag += aUgeloeb.torsdag;
- aUgelokal.fredag += aUgeloeb.fredag;
- aUgelokal.loerdag += aUgeloeb.loerdag;
- aUgelokal.soendag += aUgeloeb.soendag;
- fClean_structUgedag(&aUgeloeb);
- }
- //mandag
- if (aUgelokal.mandag > aUgelokal.tirsdag && aUgelokal.mandag > aUgelokal.onsdag &&
- aUgelokal.mandag > aUgelokal.torsdag && aUgelokal.mandag > aUgelokal.fredag &&
- aUgelokal.mandag > aUgelokal.loerdag && aUgelokal.mandag > aUgelokal.soendag)
- {
- printf("Mandag er dagen hvor der blev loebest mest\n\n", aUgelokal.mandag);
- }
- //tirsdag
- else if (aUgelokal.tirsdag > aUgelokal.mandag && aUgelokal.tirsdag > aUgelokal.onsdag &&
- aUgelokal.tirsdag > aUgelokal.torsdag && aUgelokal.tirsdag > aUgelokal.fredag &&
- aUgelokal.tirsdag > aUgelokal.loerdag && aUgelokal.tirsdag > aUgelokal.soendag)
- {
- printf("Tirsdag er dagen hvor der blev loebest mest\n\n", aUgelokal.mandag);
- }
- //onsdag
- else if (aUgelokal.onsdag > aUgelokal.tirsdag && aUgelokal.onsdag > aUgelokal.mandag &&
- aUgelokal.onsdag > aUgelokal.torsdag && aUgelokal.onsdag > aUgelokal.fredag &&
- aUgelokal.onsdag > aUgelokal.loerdag && aUgelokal.onsdag > aUgelokal.soendag)
- {
- printf("Onsdag er dagen hvor der blev loebest mest\n\n", aUgelokal.mandag);
- }
- //torsdag
- else if (aUgelokal.torsdag > aUgelokal.mandag && aUgelokal.torsdag > aUgelokal.tirsdag &&
- aUgelokal.torsdag > aUgelokal.onsdag && aUgelokal.torsdag > aUgelokal.fredag &&
- aUgelokal.torsdag > aUgelokal.loerdag && aUgelokal.torsdag > aUgelokal.soendag)
- {
- printf("Torsdag er dagen hvor der blev loebest mest\n\n", aUgelokal.mandag);
- }
- //fredag
- else if (aUgelokal.fredag > aUgelokal.tirsdag && aUgelokal.fredag > aUgelokal.onsdag &&
- aUgelokal.fredag > aUgelokal.torsdag && aUgelokal.fredag > aUgelokal.mandag &&
- aUgelokal.fredag > aUgelokal.loerdag && aUgelokal.fredag > aUgelokal.soendag)
- {
- printf("Fredag er dagen hvor der blev loebest mest\n\n", aUgelokal.mandag);
- }
- //loerdag
- else if (aUgelokal.loerdag > aUgelokal.mandag && aUgelokal.loerdag > aUgelokal.onsdag &&
- aUgelokal.loerdag > aUgelokal.torsdag && aUgelokal.loerdag > aUgelokal.fredag &&
- aUgelokal.loerdag > aUgelokal.tirsdag && aUgelokal.loerdag > aUgelokal.soendag)
- {
- printf("Loerdag er dagen hvor der blev loebest mest\n\n", aUgelokal.mandag);
- }
- //soendag
- else
- {
- printf("Soendag er dagen hvor der blev loebest mest \n\n", aUgelokal.mandag);
- }
- fFortsaet();
- }
- // 3.0 - delproblem 3 sættes igang
- void fprob3()
- {
- int z = 0;
- int y = 0;
- int iDagmax = 0;
- int iDag = 0;
- int iMaaned;
- int iTaelloeb[antal_maaneder][antal_dageprmaaned];
- struMotion aMotion[antalloeb];
- char sTjekunikke[antal_maaneder][antal_dageprmaaned][antal_unikkeloebere][100];
- // nulstiller iTaelloeb, således arrayet er tomt og klar til count.
- for (int q = 0; q < antal_maaneder; q++)
- {
- for (int c = 0; c < antal_dageprmaaned; c++)
- {
- iTaelloeb[q][c] = 0;
- }
- }
- // laeser filen ind i aMotion.
- fLaesdata("runs", aMotion);
- // looper aMotion-struct, og taeller antal loeb pr. dag, mens samme
- // loeber kun bliver registreret én gang pr dag.
- for (int i = 0; i < antalloeb; i++)
- {
- int iTjekmaaned = 0;
- if (strcmp(aMotion[i].maaned, "September") == 0)
- {
- iTjekmaaned = 1;
- }
- int iTjek = 0;
- for (int j = 0; j < antal_unikkeloebere; j++)
- {
- if (strcmp(aMotion[i].navn, sTjekunikke[iTjekmaaned][aMotion[i].dato][j]) == 0)
- {
- iTjek = 1;
- break;
- }
- }
- if (iTjek == 0)
- {
- int iGemtloeb = iTaelloeb[iTjekmaaned][aMotion[i].dato];
- strcpy(sTjekunikke[iTjekmaaned][aMotion[i].dato][iGemtloeb], aMotion[i].navn);
- iTaelloeb[iTjekmaaned][aMotion[i].dato]++;
- }
- }
- // tjekker den hoejest værdi af antal loeb i periode
- int iHoejest = 0;
- int k = 0;
- int l = 0;
- for (int q = 0; q < antal_maaneder; q++)
- {
- for (int c = 1; c < antal_dageprmaaned; c++)
- {
- if (iTaelloeb[q][c] > iHoejest)
- {
- iHoejest = iTaelloeb[q][c];
- k = q;
- l = c;
- }
- }
- }
- if (k = 0)
- printf("Der blev loebet flest loeb d. %d. August, hvor der blev loebet %d loeb\n \n", l, iHoejest);
- else
- printf("Der blev loebet flest loeb d. %d. September, hvor der blev loebet %d loeb\n \n", l, iHoejest);
- fFortsaet();
- }
- // 4.0 - delproblem 4 sættes igang
- void fprob4()
- {
- struMotion aMotion[antalloeb];
- struMotion aMotionSort;
- struMotion aSort[antalloeb];
- int z = 0;
- int iStarttime = 0,
- iStartmin = 0,
- iSluttime = 0,
- iSlutmin = 0,
- iMotionsluttime = 0,
- iMotionslutmin = 0;
- fLaesdata("runs", aMotion);
- fClean_structKopi(&aMotionSort);
- for (int i = 0; i < antalloeb; i++)
- {
- fClean_structKopia(&aSort[i]);
- }
- printf("Indtast oenskede klokkesletsstart\n");
- scanf("%d:%d", &iStarttime, &iStartmin);
- printf("Indtast oenskede klokkesletsstop\n");
- scanf("%d:%d", &iSluttime, &iSlutmin);
- int iTael = 0;
- int iTjek = 0;
- // tjekker hvilke loeb der er i intervallet
- for (int i = 0; i < antalloeb; i++)
- {
- fSluttidspunkt(aMotion[i], &iMotionsluttime, &iMotionslutmin);
- fTidstjek(aMotion[i], iStarttime, iStartmin, iSluttime, iSlutmin,
- iMotionsluttime, iMotionslutmin, &aMotionSort, &iTjek);
- if (iTjek == 1)
- {
- aSort[iTael] = aMotionSort;
- iTael++;
- }
- iTjek = 0;
- fClean_structKopi(&aMotionSort);
- }
- // Sorterer aSort
- for (int i = 0; i < antalloeb; i++)
- {
- for (int j = 0; j < (iTael-1); j++)
- {
- if (aSort[j].distance < aSort[(j+1)].distance)
- fSwap(&aSort[j], &aSort[(j+1)]);
- }
- }
- printf("Foelgende loeber har loebet indenfor det valgte tidsinterval:\n");
- // printer loebene der er i intervallet, sorteret efter distance
- for (int i = 0; i < iTael; i++)
- {
- printf("%s %s %s %d, 2011 %d:%d %d %d:%d:%d\n", aSort[i].navn, aSort[i].ugedag, aSort[i].maaned, aSort[i].dato,
- aSort[i].timestart, aSort[i].minutstart, aSort[i].distance, aSort[i].timeantal, aSort[i].minutantal,
- aSort[i].sekundantal);
- }
- fFortsaet();
- }
- // 5.0 - delproblem 5 sættes igang
- void fprob5()
- {
- int c = 0;
- int i = 0;
- int j = 0;
- struMotion aMotion[antalloeb];
- struMotion aMotionkopi[antalloeb];
- // nulstiller aMotionkopi, således arrayet er tomt og klar til count.
- for (i = 0; i < antalloeb; i++)
- {
- fClean_structKopia(&aMotionkopi[i]);
- }
- /*printf("navn: %s\ndistance: %d\ntimeantal: %d\nminutantal: %d\nsekundantal: %d\n", aMotionkopi[0].navn, aMotionkopi[0].distance,
- aMotionkopi[0].timeantal, aMotionkopi[0].minutantal, aMotionkopi[0].sekundantal);*/
- // laeser filen ind i aMotion.
- fLaesdata("runs", aMotion);
- int p = 0;
- int iTaelNavn = 0;
- int iTjek = 0;
- int iResult = 0;
- // looper aMotion-struct, og taeller antal loeb pr. dag, mens samme
- // loeber kun bliver registreret én gang pr dag.
- for (i = 0; i < antalloeb; i++)
- {
- iTjek = 0;
- for (p = 0; p < iResult; p++)
- {
- if (strcmp(aMotion[i].navn, aMotionkopi[p].navn) == 0)
- {
- iTjek = p;
- break;
- }
- }
- if (iTjek == 0)
- {
- aMotionkopi[i].distance += aMotion[iResult].distance;
- iResult++;
- }
- else
- {
- aMotionkopi[i].distance += aMotion[iTjek].distance;
- }
- }
- // timeantal: %d\nminutantal: %d\nsekundantal: %d\n navn: %s\n
- // aMotionkopi[9].navn, aMotionkopi[9].timeantal, aMotionkopi[9].minutantal, aMotionkopi[9].sekundantal
- printf("distance: %d\n", aMotionkopi[0].distance);
- printf("navn: %s\ndistance: %d\ntimeantal: %d\nminutantal: %d\nsekundantal: %d\n", aMotion[0].navn, aMotion[0].distance,
- aMotion[0].timeantal, aMotion[0].minutantal, aMotion[0].sekundantal);
- fFortsaet();
- }
- // Laver protokoller af menu og errorcall, som laves efter main
- void ferrorcall();
- void fmenu();
- int main(void)
- {
- // menu-visning
- printf("Menu:\n");
- printf("Delproblem 1\n");
- printf("Delproblem 2\n");
- printf("Delproblem 3\n");
- printf("Delproblem 4\n");
- printf("Delproblem 5\n");
- printf("indtast talvaerdi fra 1 til 5 efter hvilket delproblem der skal loeses:\n");
- printf("- for quit, skriv 'q'");
- // kalder menuen
- fmenu();
- printf("Programmet lukker nu...\n");
- return 0;
- }
- void fFortsaet()
- {
- printf("indtast nu nummer paa det naeste problem der skal loeses, eller quit program:\n");
- }
- // laver et kald af fejlindtastning
- void ferrorcall()
- {
- printf("Ugyldig vaerdig indtastet, prøv igen\n");
- }
- // funktion-menu, kører i loop, indtil 'q' bliver indtastet
- void fmenu()
- {
- // initialisering af variable
- char x;
- // menu-kør
- do
- {
- printf("\n");
- // scan valg af delproblem
- scanf(" %c", &x);
- if (x == '1')
- {
- fprob1();
- }
- else if (x == '2')
- {
- fprob2();
- }
- else if (x == '3')
- {
- fprob3();
- }
- else if (x == '4')
- {
- fprob4();
- }
- else if (x == '5')
- {
- fprob5();
- }
- else if (x == 'q');
- else
- ferrorcall();
- }
- while(x != 'q');
- }
Add Comment
Please, Sign In to add comment