mercredi 4 mars 2020

random data struct in c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>
#include <stdbool.h>
#include <time.h>
struct Book {
    int BookID;         
    char Title[100];    
    char Author[50];    
    float Price;    
    int YearOfRelease;  
};

struct Book lstBooks[10000]; 
int maxBook = 0; 
int startBookIndex = 10000; 

#define MAX_NUMBER_TITLES 1000
#define MAX_NUMBER_AUTHORS 1000
#define MAX_LENGTH_OF_TITLE 100
#define MAX_LENGTH_OF_AUTHOR 50
char lstTitles[MAX_NUMBER_TITLES][MAX_LENGTH_OF_TITLE];
char lstAuthors[MAX_NUMBER_AUTHORS][MAX_LENGTH_OF_AUTHOR];

void printBookInfo(struct Book b) {
    printf("BookID: %d\n", b.BookID);
    printf("Title: %s\n", b.Title);
    printf("Author: %s\n", b.Author);
    printf("Price: %f\n", b.Price);
    printf("Year of release: %d\n", b.YearOfRelease);
    printf("-------------------------\n");
}

bool checkExistingBook(struct Book b) {

    return false; 

}

void inputBookInfo() {
    struct Book b1;

    if (checkExistingBook(b1) == false) {
        lstBooks[maxBook].BookID = startBookIndex+maxBook;
        strcpy(lstBooks[maxBook].Title, lstTitles[random0_Upper(10)]);
        strcpy(lstBooks[maxBook].Author, lstAuthors[random0_Upper(10)]);
        lstBooks[maxBook].Price = random0_Upper(50);
        lstBooks[maxBook].YearOfRelease = randomLower_Upper(1980, 2020);
        maxBook++; 
    }   
}

bool isCallSRand = false;

int random() {
    if (isCallSRand != true) {
        srand(time(NULL));   
        isCallSRand = true;
    }
    int r = rand(); 
    return r;
}

int random0_Upper(int upper) {
    return (random()%(upper+1));
}

int randomLower_Upper(int lower, int upper) {
    int r = (random() % (upper - lower + 1)) + lower;
    return r;
}




void initData() {
    strcpy(lstTitles[0], "C Programming Language");
    strcpy(lstTitles[1], "The C Programming Language 2nd Edition");
    strcpy(lstTitles[2], "C Programming: A Modern Approach, 2nd Edition 2nd Edition");
    strcpy(lstTitles[3], "Expert C Programming: Deep Secrets 1st Edition, Kindle Edition"); 
    strcpy(lstTitles[4], "C: The Complete Reference, 4th Ed. 4th Edition");
    strcpy(lstTitles[5], "Head First C 1/e Edition");
    strcpy(lstTitles[6], "Computer Fundamentals and Programming in C");
    strcpy(lstTitles[7], "Low-Level Programming: C, Assembly, and Program Execution");
    strcpy(lstTitles[8], "C in a Nutshell: The Definitive Reference 2nd Edition");
    strcpy(lstTitles[9], "Hands-on Network Programming with C: Learn socket programming in C and write secure and optimized network code");
    strcpy(lstTitles[10], "Data Structures Using C 2nd Edition");

    strcpy(lstAuthors[0], "James Gosling");
    strcpy(lstAuthors[1], "Peter Norton");
    strcpy(lstAuthors[2], "Marry Curry");
    strcpy(lstAuthors[3], "Tony Crawford"); 
    strcpy(lstAuthors[4], "Igor Zhirkov");
    strcpy(lstAuthors[5], "Peter Van der Linden");
    strcpy(lstAuthors[6], "Kernighan");
    strcpy(lstAuthors[7], "Adam Riches");
    strcpy(lstAuthors[8], "Dennis Ritchie");
    strcpy(lstAuthors[9], "Jonathan Payne");
    strcpy(lstAuthors[10], "John Smith");

}





int main(int argc, char *argv[]) {



    int j=0;


    initData();

    for (j=0; j<10; j++) 
        inputBookInfo();

    for (j=0; j<10; j++) 
        printBookInfo(lstBooks[j]);


    return 0;
}

This code will generate a random list of books in library,but i am stuck at writing the function "checkExistingBook()"-if the book has been already in library then return false,else return true if the book isn't in library,just in case the random data repeats itself like 2 books about "C Programming Language"




Aucun commentaire:

Enregistrer un commentaire