Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct Bibloteca {
- char *nume, *titlu, *domeniu;
- unsigned short an_ap, nr_pag;
- struct Bibloteca *urm;
- }B;
- B *Addbook(B *, char *, char *, unsigned short, unsigned short, char *);
- void Afisare(B *);
- int main() {
- unsigned short an_ap, nr_pag, opt;
- char titlu[25] , autor[25], domeniu[25];
- char temp;
- B *prim = NULL;
- do {
- printf("0. Iesire\n");
- printf("1. Adaugare carte in bibloteca\n");
- printf("2. Afisare lista\n");
- printf("Optiunea dumeanoastra este: "); scanf("%hu", &opt);
- scanf("%c", &temp);
- switch (opt)
- {
- case 0: exit(0); break;
- case 1: system("cls");
- printf("Titlu carte: "); fgets(titlu, 25, stdin);
- printf("Nume autor: "); fgets(autor, 25, stdin);
- printf("Anul aparitei: "); scanf("%hu", &an_ap);
- printf("Numarul de pagini: "); scanf("%hu", &nr_pag);
- scanf("%c", &temp);
- printf("Domeniul: "); fgets(domeniu, 25, stdin);
- system("cls");
- prim = Addbook(prim, titlu, autor, an_ap, nr_pag, domeniu);
- break;
- case 2:
- system("cls");
- Afisare(prim); break;
- default:system("cls");
- printf("A-ti introdus o optiunea invalida !!! \n");
- break;
- }
- } while (1);
- }
- B *Addbook(B *prim, char *title, char *author, unsigned short year_ap, unsigned short pageNumber, char *domain) {
- B * q, *p;
- p = (B *)malloc(sizeof(B));
- p->nume = (char *)malloc(sizeof(author) + 1);
- strcpy(p->nume, author);
- p->titlu = (char *)malloc(sizeof(title) + 1);
- strcpy(p->titlu, title);
- p->an_ap = year_ap;
- p->nr_pag = pageNumber;
- p->domeniu = (char *)malloc(sizeof(strlen(domain) + 1));
- strcpy(p->domeniu, domain);
- p->urm = NULL;
- if (p == NULL || p->nume == NULL || p->titlu == NULL || p->domeniu == NULL) {
- printf("Eroare la alocarea memoriei !!!");
- exit(0);
- }
- if (prim == NULL) return p;
- if (strcmp(p->titlu, title ) > 0) {
- p->urm = prim;
- return p;
- }
- q = prim;
- while (q->urm != NULL && strcmp(q->urm->titlu, title) < 0) q = q->urm;
- p->urm = q->urm;
- q->urm = p;
- return prim;
- }
- void Afisare(B *prim) {
- B *q = prim;
- while (q)
- {
- printf("\nTitlu: %s",q->titlu);
- printf("Autor: %s", q->nume);
- printf("An aparitie: %hu \n", q->an_ap);
- printf("Numar de pagini: %hu \n", q->nr_pag);
- printf("Domeniu: %s\n\n", q->domeniu);
- q = q->urm;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment