#include <stdio.h>
#include <stdlib.h>
#include <string.h>

FILE *data;
struct node* L = NULL;
struct nodeMail* M = NULL;
struct nodeNr* N = NULL;

struct nodeMail {
    char email[20];
    nodeMail* next;
};
struct nodeNr {
    char numri[10];
    nodeNr* next;
};
struct node {
    char emri[10], mbiemri[10];
    nodeMail* email=NULL;
    nodeNr* numri=NULL;
    int r, plq;
    node* next;
};
int    meVogel            (struct node* a, struct node* b) {
    if (strcmp(a->emri, b->emri)<0) // >0 => a<b
    {
        //|| (strcmp(a->emri, b->emri)==0 && strcmp(a->mbiemri, b->mbiemri)>0)
        printf("%s < %s\n", a->emri, b->emri);
    }   return 1;
    printf("%s > %s\n", a->emri, b->emri);
    return 0;
}
void   printMail          (struct nodeMail* p) {
    if (p == NULL)
        return;
    printf("%s\n", p->email);
    printMail(p->next);
}
void   printNr            (struct nodeNr* p) {
    if (p == NULL)
        return;
    printf("%s\n", p->numri);
    printNr (p->next);
}
void   printoData         (struct node* p) {
    if (p == NULL)
        return;
    printf("%d - %s %s\n", p->r, p->emri, p->mbiemri);
    printf("\ne-Mail:\n-------\n");
    printMail(p->email);
    printf("\nTelefon:\n--------\n");
    printNr(p->numri);
    printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
    printoData (p->next);
}
void   printoPlq          (struct node* p, int i) {
    if (p == NULL || i==10)
        return;
    printf("%d - %s %s\n", p->r, p->emri, p->mbiemri);
    printf("Kerkime: %d", p->plq);
/*  printf("\ne-Mail:\n-------\n");
    printMail(p->email);
    printf("\nTelefon:\n--------\n");
    printNr(p->numri); */
    printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
    printoPlq (p->next, i+1);
}
struct node*     shto     (node* head, char emri[], char mbiemri[], nodeMail* email, nodeNr* numri, int i) {
    node *temp = (struct node*) malloc (sizeof (struct node));
    strcpy(temp->emri, emri);
    strcpy(temp->mbiemri, mbiemri);
    temp->email = email;
    temp->numri = numri;
    temp->r = i;
    temp->plq = 0;
    temp->next = NULL;
    if (head == NULL)
        head = temp;
    else {
        node* temp1 = head;
        while (temp1->next != NULL)
            temp1 = temp1->next;
        temp1->next = temp;
    }
    return head;
}
struct nodeMail* shtoMail (nodeMail* head, char email[]) {
    nodeMail *temp = (struct nodeMail*) malloc (sizeof (struct nodeMail));
    strcpy(temp->email, email);
    temp->next = NULL;
    if (head == NULL)
        head = temp;
    else
    {
        nodeMail* temp1 = head;
        while (temp1->next != NULL)
            temp1 = temp1->next;
        temp1->next = temp;
    }
    return head;
}
struct nodeNr*   shtoNr   (nodeNr* head, char numri[]) {
    nodeNr *temp = (struct nodeNr*) malloc (sizeof (struct nodeNr));
    strcpy(temp->numri, numri);
    temp->next = NULL;
    if (head == NULL)
        head = temp;
    else
    {
        nodeNr* temp1 = head;
        while (temp1->next != NULL)
            temp1 = temp1->next;
        temp1->next = temp;
    }
    return head;
}
void   lexoData           () {
    char emri[10], mbiemri[10], email[20], numri[10];
    int i=1;

    if ((data=fopen("data.txt", "r"))==NULL) {
        data=fopen("data.txt", "w");
        fclose(data);
        data=fopen("data.txt", "w");
    }
    while (!feof(data)) {
        fscanf(data, "%s %s", emri, mbiemri);

        fscanf(data, "%s", email);
        while (strcmp(email, ";")!=0) {
            M = shtoMail(M, email);
            fscanf(data, "%s", email);              
        } 

        fscanf(data, "%s", numri);
        while (strcmp(numri, "<")!=0) {
            N = shtoNr(N, numri);
            fscanf(data, "%s", numri);
        }

        L = shto(L, emri, mbiemri, M, N, i++);
        M=NULL; N=NULL;
    }
    fclose(data);
}
void   ruajData           (node* head) {
    data=fopen("data.txt", "w");
    nodeMail* h1; nodeNr* h2;
    while (head!=NULL) {
        fprintf(data, "%s %s ", head->emri, head->mbiemri);
        h1 = head->email;
        while (h1!=NULL) {
            fprintf(data, "%s ", h1->email);
            h1=h1->next;
        }
        fprintf(data, "; ");
        h2 = head->numri;
        while (h2!=NULL) {
            fprintf(data, "%s ", h2->numri);
            h2=h2->next;
        }
        fprintf(data, "<");
        head = head->next;
        if (head!=NULL)
            fprintf(data, "\n");
    }
    fclose(data);
}
void   kerko              (node* head, char x[]) {
    int i=1;
    while (head!=NULL) {
        if (strstr(head->emri, x)!=NULL || strstr(head->mbiemri, x)!=NULL) {
            head->plq++;
            printf("%d - %s %s\n", i++, head->emri, head->mbiemri);
            printf("\ne-Mail:\n-------\n");
            printMail(head->email);
            printf("\nTelefon:\n--------\n");
            printNr(head->numri);
            printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
        }
        head = head->next;
    }
}
void   edito              (node* head, int x) {
    int i=1;
    char c[20];
    while (i<x) {
        head = head->next;
        i++;
    }
    printf("%s %s\n", head->emri, head->mbiemri);
    printMail (head->email);
    printNr (head->numri);
    printf("\nEmri i ri: ");
    scanf("%s", head->emri);
    printf("Mbiemri i ri: ");
    scanf("%s", head->mbiemri);
/*  printf("Email: ");
    scanf("%s", c);
    while (strcmp(c, ";")!=0) {
        shtoMail(head->email, c);
        printf("Email: ");
        scanf("%s", c); 
    }
    printf("Numri: ");
    scanf("%s", c);
    while (strcmp(c, ";")!=0) {
        shtoNr (head->numri, c);
        printf("Numri: ");
        scanf("%s", c); 
    } */
}
struct node*     fshi     (node* head, int x[]) {
    int i=1;
    node* p = head;
    node* q = head->next;
    while (x[i]!='\0') {
        while (q!=NULL) {
            if (q->r == x[i]) {
                p->next = q->next;
                free(q);
                q = p->next;
                break;
                }
            p = q;
            q = q->next;            
        }
        i++;
    }
    if (head->r == x[0]){
        p = head->next;
        free(head);
        return p;
    }
    return head;
}
void   renditPlq          (node* p) {
    node* q;
    node* min;
    node *head = (struct node*) malloc (sizeof (struct node));
    head->next = L;
    p = head;

    while (p->next->next!=NULL)
    {
        min = p;
        q = p->next;
        while (q->next!=NULL)
        {
            if (min->next->plq < q->next->plq)
                min = q;
            q = q->next;
        }
        node* a = p->next;
        p->next = min->next;
        min->next = a;
        node* b = a->next;
        a->next = p->next->next;
        p->next->next = b;
        p = p->next;
    }
    L = head->next;
}
void   rendit             (node* p) {
    node* q;
    node* min;
    node *head = (struct node*) malloc (sizeof (struct node));
    head->next = L;
    p = head;

    while (p->next->next!=NULL)
    {
        min = p;
        q = p->next;
        while (q->next!=NULL)
        {
            if (strcmp(min->next->emri, q->next->emri)==1|| (strcmp(min->next->emri, q->next->emri)==0 && strcmp(min->next->mbiemri, q->next->mbiemri)==1))
                min = q;
            q = q->next;
        }
        node* a = p->next;
        p->next = min->next;
        min->next = a;
        node* b = a->next;
        a->next = p->next->next;
        p->next->next = b;
        p = p->next;
    }
    L = head->next;
}
int main() {
    char search[20];
    char a[10], b[10];
    int ndr, vfshi[10], i=0, e, plq[10];
    node* p = L;

    lexoData();
    rendit(L);
    printf("* * * LIBRI I KONTAKTEVE * * *\n\n");
    do {
    printf("1 - Shiko kontaktet\n");
    printf("2 - Kerko kontakt\n");
    printf("3 - Shto kontakt\n");
    printf("4 - Modifiko kontaktet\n");
    printf("5 - Fshi kontakte\n");
    printf("6 - Te preferuarit\n");
    printf("7 - Dil\n");
    printf("Ekzekuto: ");
    scanf("%d", &e);
    printf("\n\n");

        if (e == 1) { // sh
            printoData(L);
        }
else    if (e == 2) { // ke
        printf("Kerko: ");
        scanf("%s", search);
        printf("\n\n");
        kerko (L, search);
    }
else    if (e == 3) { // s
    while (p->next != NULL)
        p = p->next;
    node *temp = (struct node*) malloc (sizeof (struct node));
    printf("Emri: ");
    scanf("%s", temp->emri);
    printf("Mbiemri: ");
    scanf("%s", temp->mbiemri);
    printf("Email: ");
    do {
        scanf("%s", search);
        if (strcmp(search, ";")!=0)
            shtoMail(temp->email, search);
    } while ((search, ";")!=0);
    printf("Numri: ");
    do {
        scanf("%s", search);
        if (strcmp(search, ";")!=0)
            shtoNr (temp->numri, search);
    } while (strcmp(search, ";")!=0);
    temp->next = NULL;
    p->next = temp;
}
else    if (e == 4) { // modif
    printf("Ndrysho: ");
    scanf("%d", &ndr);
    edito(L, ndr);
    rendit(L);
}
else    if (e == 5) { // f
    printf("Fshi: ");
    do {
        scanf("%d", &vfshi[i]);
    } while (vfshi[i++]!=0);
    vfshi[i]='\0';
    L = fshi(L, vfshi);
}
else    if (e == 6) { // te preferu
    renditPlq(L);
    printoPlq(L, 0);
    rendit(L);
}
else                { //
    ruajData(L);
    break;
}
} while (e>0 && e<8);
    return 0;
}
Attachments
Hello Staff and Members.How are you doing? Hope you are OK.

I have a problem in which I need help.I have done a programm for this assignment:

Topic :The book of contacts
Build a programm in C ,which saves in a text document the list of contacts /persons.
Every contact should save an information for:
1.Name
2.Surname
3.the list of email(he can have a few addresses)
4.the list of the number of phones.
Implement an appropriate structure to save this informations in a text document.
Your programm should :
1.Print in an efficient way the sorted list of contacts\
2.Print in an efficient way all the contacts whose name and surname starts with a set of characters given from the user.
3.Add in an efficient way a new contact.
4.Modify an existing contact.
5.Delete one or more contacts simultaneously
6.Print the 10 most prefered contacts (the contacts that are requested mostly from the users)
The solution should be with an optimal performance.


IT IS IMPORTANT THAT THE PROGRAMM BE WITH OPTIMAL PERFORMANCE!!!

HERE ABOVE IS MY PROGRAMM...PLZ IF U CAN HELP ME WITH THE CORRECT PROGRAMM,IT WOULD HELP ME PLZ..THE DEADLINE OF THIS ASSIGNMENT IS THIS FRIDAY...

I HAVE SEEN IN DANIWEB FOR THE BOOK OF CONTACS BUT THESE ARE OTHER REQUIREMENTS..ARE MORE COMPLEX

Open the TXT file and you have the question there.Hey buddy there is an error in your code.

Edited 2 Years Ago by ballhysaledio: asaa

This article has been dead for over six months. Start a new discussion instead.