Commit 53ad12b3 by karade

Upload New File

parent a2152ecc
Showing with 162 additions and 0 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "passenger_pool.h"
#include "generator.h"
int main(void)
{
srand(time(NULL));
/* Names are assigned from the pools in the header*/
const char *firstNames[] = FIRST_NAME_POOL;
const char *lastNames[] = LAST_NAME_POOL;
const char *seats[] = PLANE_SEATS_POOL;
const char *months[] = MONTHS_POOL;
const char *residencies[] = RESIDENCY_POOL;
const char *emails[] = EMAIL_POOL;
/* Find the counts for the pool sizes */
int firstNamePoolSize = sizeof(firstNames) / sizeof(char *);
int lastNamePoolSize = sizeof(lastNames) / sizeof(char *);
int seatPoolSize = sizeof(seats) / sizeof(char *);
int monthPoolSize = sizeof(months) / sizeof(char *);
int residencyPoolSize = sizeof(residencies) / sizeof(char *);
int emailPoolSize = sizeof(emails) / sizeof(char *);
printf("\nEnter how many passengers to generate:");
int nameCount;
scanf("%d", &nameCount);
users data[SEATS];
int i;
char seat[4];
char month[STR_MAX];
char date[DOB_MAX];
char email[STR_MAX];
for (i = 0; i < nameCount; i++)
{
strcpy((data + i)->lName, lastNames[GetRand(0,firstNamePoolSize - 1)]);
strcpy((data + i)->fName, firstNames[GetRand(0,lastNamePoolSize - 1)]);
do //leitakse random istekoht, kuni funktsioon tagastab et
{ //kellegil pole seda istkohta otsitakse uus.
strcpy(seat,seats[GetRand(0,seatPoolSize - 1)]);
}while (GetUniqueSeat(data, i, *seats, SEATS, seat) > 0);
strcpy((data + i)->seat, seat); //eeldusel et eelmine tsükkel
//lõpetab kopeeritakse istekoht struktuurimassiivi
strcpy(month, months[GetRand(0,monthPoolSize - 1)]);
GenerateDateOfBirth(month, date);
strcpy((data + i)->dateOfBirth , date);
strcpy((data + i)->residency, residencies[GetRand(0,residencyPoolSize - 1)]);
(data + i)->checkedIn = false;
email[0] = (data + i)->fName[0];
email[strlen(email)] = '.';
strcat(email,(data + i)->lName);
strcat(email, emails[GetRand(0,emailPoolSize - 1)]);
email(strlen(email)) = '\0';
printf("%s\n", email);
strcpy(email,"");
}
GenNames(data, nameCount);
return EXIT_SUCCESS;
}
void GenerateDateOfBirth(char *month, char *dateOB)
{
char dob[20] = {0};
char temp[15] = {0};
int date = GetRand(1, 31);
int year = GetRand(1920,2022);
snprintf(temp, 5, "%d", date);
strcat(dob, temp);
dob[strlen(dob)] = '.';
strcat(dob, month);
dob[strlen(dob)] = '.';
snprintf(temp, 5, "%d", year);
strcat(dob, temp);
dob[strlen(dob)] = '\0';
strcpy(dateOB, dob);
}
int GetUniqueSeat(users *data, int len, const char *seats, int size, char *seat)
{
int match = 0; //Otsib kas on kellegil juba sama istekoht
int i;
for (i = 0; i < len; i++)
{
if (strcmp(seat,(data + i)->seat) == 0)
{
match++;
}
}
return match;
}
void GenNames(users *data, int num)
{
FILE *fp = fopen("passengers.txt", "w");
if(fp == NULL)
{
puts("Output file did not open!");
exit(EXIT_FAILURE);
}
int i;
for (i = 0; i < num; i++)
{
fprintf(fp,"%s %s %s %s %s\n",(data + i)->fName,
(data + i)->lName,(data + i)->dateOfBirth, (data + i)->residency,
(data + i)->seat);
}
fclose(fp);
}
int GetRand(int min, int max)
{
return (rand() % (max - min + 1)) + min;
}
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