Commit 8970cfb7 by karade

Update generator.c

parent 2199259b
Showing with 46 additions and 32 deletions
/**
* File: generator.c
* Author: Risto Heinsar
* Created: 12.03.2015
* Modified 14.03.2023
*
* Description: This is a random data generator
*
* Note: This is a starter code for a lab task
*/
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
...@@ -44,43 +35,62 @@ int main(void) ...@@ -44,43 +35,62 @@ int main(void)
users data[SEATS]; users data[SEATS] = {0};
int i; int i, j;
char seat[4]; char seat[4];
char month[STR_MAX]; char month[STR_MAX];
char date[DOB_MAX]; char date[DOB_MAX];
char email[STR_MAX]; char mail[STR_MAX];
char docNum[DOC_MAX];
char letters[26] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
for (i = 0; i < nameCount; i++) for (i = 0; i < nameCount; i++)
{ {
strcpy((data + i)->lName, lastNames[GetRand(0,firstNamePoolSize - 1)]); strcpy((data + i)->lName, lastNames[GetRand(0,lastNamePoolSize - 1)]);
strcpy((data + i)->fName, firstNames[GetRand(0,lastNamePoolSize - 1)]); strcpy((data + i)->fName, firstNames[GetRand(0,firstNamePoolSize - 1)]);
printf("TEST: %s\n", (data + i)->fName);
do //leitakse random istekoht, kuni funktsioon tagastab et do //leitakse random istekoht, kuni funktsioon tagastab et
{ //kellegil pole seda istkohta otsitakse uus. { //kellegil pole seda istkohta otsitakse uus.
strcpy(seat,seats[GetRand(0,seatPoolSize - 1)]); strcpy(seat,seats[GetRand(0,seatPoolSize - 1)]);
}while (GetUniqueSeat(data, i, *seats, SEATS, seat) > 0); }while (GetUniqueSeat(data, i, seat) > 0);
strcpy((data + i)->seat, seat); //eeldusel et eelmine tsükkel strcpy((data + i)->seat, seat); //eeldusel et eelmine tsükkel
//lõpetab kopeeritakse istekoht struktuurimassiivi //lõpetab kopeeritakse istekoht struktuurimassiivi
strcpy(month, months[GetRand(0,monthPoolSize - 1)]); strcpy(month, months[GetRand(0,monthPoolSize - 1)]);
GenerateDateOfBirth(month, date); GenerateDateOfBirth(month, date); //funktsioonis pannakse numbrid
strcpy((data + i)->dateOfBirth , date); strcpy((data + i)->dateOfBirth , date);//ja kuu kokku et saada
//formaadis sünnikuupäev
strcpy((data + i)->residency, residencies[GetRand(0,residencyPoolSize - 1)]); strcpy((data + i)->residency, residencies[GetRand(0,residencyPoolSize - 1)]);
(data + i)->checkedIn = false; (data + i)->checkedIn = false; //default kõigile
memset(mail, 0, sizeof(mail)); //tühjendab sõne enne uue meili loomist
mail[0] = (data+i)->fName[0];
mail[strlen(mail)] = '.'; //emaili loomine, null terminaatoreid on
mail[strlen(mail)] = '\0';//palju et tagada strcati funktsionaalsus
strcat(mail,(data+i)->lName);
mail[strlen(mail)] = '\0';
strcat(mail, emails[GetRand(0, emailPoolSize - 1)]);
mail[strlen(mail)] = '\0';
printf("%s\n", mail);
strcpy((data+i)->email, mail);
email[0] = (data + i)->fName[0]; for(j = 0; j < 2; j++)
email[strlen(email)] = '.'; {
strcat(email,(data + i)->lName); docNum[j] = letters[GetRand(0,23)];
strcat(email, emails[GetRand(0,emailPoolSize - 1)]); }
email(strlen(email)) = '\0'; for(j = 2; j < 9; j++) //dokumendi numbri genereerimine
printf("%s\n", email); {
docNum[j] = '0' + GetRand(0,9) % 10;
}
docNum[9] = '\0';
strcpy((data+i)->documentNum, docNum);
strcpy(email,""); (data + i)->luggageClass = GetRand(0,3);
} }
...@@ -90,6 +100,7 @@ int main(void) ...@@ -90,6 +100,7 @@ int main(void)
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
void GenerateDateOfBirth(char *month, char *dateOB) void GenerateDateOfBirth(char *month, char *dateOB)
{ {
...@@ -100,10 +111,12 @@ void GenerateDateOfBirth(char *month, char *dateOB) ...@@ -100,10 +111,12 @@ void GenerateDateOfBirth(char *month, char *dateOB)
snprintf(temp, 5, "%d", date); snprintf(temp, 5, "%d", date);
strcat(dob, temp); strcat(dob, temp);
dob[strlen(dob)] = '\0';
dob[strlen(dob)] = '.'; dob[strlen(dob)] = '.';
strcat(dob, month); strcat(dob, month);
dob[strlen(dob)] = '\0';
dob[strlen(dob)] = '.'; dob[strlen(dob)] = '.';
snprintf(temp, 5, "%d", year); snprintf(temp, 5, "%d", year);
...@@ -115,7 +128,7 @@ void GenerateDateOfBirth(char *month, char *dateOB) ...@@ -115,7 +128,7 @@ void GenerateDateOfBirth(char *month, char *dateOB)
} }
int GetUniqueSeat(users *data, int len, const char *seats, int size, char *seat) int GetUniqueSeat(users *data, int len, char *seat)
{ {
int match = 0; //Otsib kas on kellegil juba sama istekoht int match = 0; //Otsib kas on kellegil juba sama istekoht
...@@ -147,9 +160,10 @@ void GenNames(users *data, int num) ...@@ -147,9 +160,10 @@ void GenNames(users *data, int num)
for (i = 0; i < num; i++) for (i = 0; i < num; i++)
{ {
fprintf(fp,"%s %s %s %s %s\n",(data + i)->fName, fprintf(fp,"%s %s %s %s %s %s %s %d\n",(data + i)->documentNum,
(data + i)->lName,(data + i)->dateOfBirth, (data + i)->residency, (data + i)->fName,(data + i)->lName,(data + i)->dateOfBirth,
(data + i)->seat); (data + i)->email,(data + i)->residency,
(data + i)->seat, (data + i)->luggageClass);
} }
fclose(fp); fclose(fp);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment