Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define ITEMS_COUNT 16
- typedef unsigned short int DBID;
- typedef enum {
- TYPE_PERSON,
- TYPE_COMPANY
- } type;
- typedef struct {
- char name[64];
- char surname[64];
- } Person;
- typedef struct {
- char name[128];
- } Company;
- typedef struct {
- type type;
- DBID id;
- union {
- Person person;
- Company item;
- };
- } Item;
- Item itemList[ITEMS_COUNT];
- static void populateItems(void) {
- for(size_t i = 0; i != ITEMS_COUNT; ++i) {
- Item* it = &itemList[i];
- it->it = i + 1;
- it->type = randomType();
- if(TYPE_PERSON == it->type) {
- randomPersonName(it->person.name, sizeof it->person.name);
- randomPersonSurname(it->person.surname, sizeof it->person.surname);
- } else {
- randomPersonSurname(it->company.name, sizeof it->company.name);
- }
- }
- }
- static type randomType(void) {
- return (type) randr(0, 1);
- }
- static char* randomCompanyName() {
- static const char* nameList[] = {
- "Microsoft",
- "Valve",
- "Vamonos pest",
- "Hello folks!",
- "Just a program inc"
- };
- const int listSize = sizeof nameList / sizeof nameList[0];
- const char *name = nameList[randr(0, listSize - 1)];
- strncpy(buf, name, size);
- buf[size - 1] = '\0';
- return buf;
- }
- static char* randomPersonName(char* buf, size_t size) {
- static const char* nameList[] = {
- "John",
- "Mike",
- "Emily",
- "Jessica",
- "Steven"
- };
- const int listSize = sizeof nameList / sizeof nameList[0];
- const char *name = nameList[randr(0, listSize - 1)];
- strncpy(buf, name, size);
- buf[size - 1] = '\0';
- return buf;
- }
- static char* randomPersonSurname(char* buf, size_t size) {
- static const char* nameList[] = {
- "Ross",
- "Litt",
- "Hellyeah"
- };
- const int listSize = sizeof nameList / sizeof nameList[0];
- const char *name = nameList[randr(0, listSize - 1)];
- strncpy(buf, name, size);
- buf[size - 1] = '\0';
- return buf;
- }
- static unsigned int randr(unsigned int min, unsigned int max)
- {
- double scaled = (double) rand() / (RAND_MAX + 1.0);
- return (max - min + 1) * scaled + min;
- }
- static void dumpItems(void) {
- for(size_t i = 0; i != ITEMS_COUNT; ++i) {
- Item *it = &itemList[i];
- printf("Row %d: { dbID: %d, ", i + 1, id->id);
- if(TYPE_PERSON == id->type) {
- printf("name: %s, surname: %s", id->person.name, id->person.surname);
- } else {
- printf("company name: %s", id->company.name);
- }
- print("}\n");
- }
- }
- int main(int argc, char* argv[]) {
- srand(time(NULL));
- populateItems();
- dumpItems();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement