All pastes #41459 Raw Edit

Something

public text v1 · immutable
#41459 ·published 2006-02-13 18:53 UTC
rendered paste body
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


struct phonebook {
	char *username,*region;
	int code,time,used;
	struct phonebook *nxt;
};


struct phonebook *root;


void init () {
	root=NULL;
}

struct phonebook *find (const char *username) {
	struct phonebook *l;
	for (l=root; (l!=NULL) && strcmp(l->username,username)!=0; l=l->nxt) {
	}
	return(l);
}

struct phonebook *find_code(const int code) {
    struct phonebook *l;
    for (l=root; (l!=NULL) && (l->code!=code); l=l->nxt) {
    }
    return(l);
}

	
int list_insert(const char *username,const char *region,int time) {
    int a=1;
    struct phonebook *l,*ptr;
	l=find(username);
	if (l!=NULL) {
	    printf("This name allready exists!\n");
	    return(0);
	}
	l=(struct phonebook *)malloc(sizeof(struct phonebook));
	if (l==NULL) {
	    printf("Memory Error!\n");
		return(0);
	}
	do {
	    ptr=find_code(a);
	    if (ptr==NULL) {
	        break;
	    }
        a++;
    }while (ptr!=NULL);
    l->username=strdup(username);
    l->region=strdup(region);
    l->code=a;
    l->time=time;
    l->nxt=root;
    root=l;
    return(1);
}

void add_time (const char username[]) {
	int time;
	struct phonebook *l;
	l=find(username);
	if (l!=NULL) {
		printf("Dose ta min pou thes na prostheseis ");
		scanf("%d",&time);
		l->time+=time;
	}
	else {
		printf("Den uparxei pelatis me auto to onomateponumo!\n");
	}
	printf("\n");
}

void print_phonebook () {
    struct phonebook *l;
	printf("code onomateponumo region time\n");
	for (l=root; l!=NULL; l=l->nxt) {
		printf("%d %s %s %d\n",l->code,l->username,l->region,l->time);
	}
	printf("\n");
}


void remove_name (const char username[]) {
    struct phonebook *l,*p;
	int i;	
	for (p=NULL,l=root; (l!=NULL) && strcmp(l->username,username)!=0; p=l,l=l->nxt) {
	}
	if (l!=NULL) {
	    if (p==NULL) {
	        root=l->nxt;
	    }
        else {
            p->nxt=l->nxt;
        }
        free(l->username);
        free(l->region);
        free(l);
        printf("H kataxorisi diagrafike\n");
	}
	else {
		printf("Den uparxei pelatis me auto to onomateponumo!\n");
	}
	printf("\n");
}

void list_delete (const char username[]) {
    struct phonebook *l,*p;
	int i;	
	for (p=NULL,l=root; (l!=NULL) && strcmp(l->username,username)!=0; p=l,l=l->nxt) {
	}
	if (l!=NULL) {
	    if (p==NULL) {
	        root=l->nxt;
	    }
        else {
            p->nxt=l->nxt;
        }
        free(l->username);
        free(l->region);
        free(l);
	}
}

int list_insert2(const char *username,const char *region,int time) {
    int a=1;
    struct phonebook *l,*ptr;
	l=find(username);
	if (l!=NULL) {
 	   return(0);
	}
	l=(struct phonebook *)malloc(sizeof(struct phonebook));
	if (l==NULL) {
	    printf("Memory Error!\n");
		return(0);
	}
	do {
	    ptr=find_code(a);
	    if (ptr==NULL) {
	        break;
	    }
        a++;
    }while (ptr!=NULL);
    l->username=strdup(username);
    l->region=strdup(region);
    l->code=a;
    l->time=time;
    l->nxt=root;
    root=l;
    return(1);
}

void file_open () {
    int time,code;
    struct phonebook *l;
    char username[64],region[64];
    FILE *phonebook;
    for (l=root; l!=NULL; l=l->nxt) {
	    list_delete(l->username);
    }
    phonebook=fopen("phonebook.txt","r");
    if (phonebook==NULL) {
        printf("phonebook.txt doesn't exist!\n");
        return;
    }
    else {
        while (!feof(phonebook)) {
            fscanf(phonebook,"%d %s %s %d",&code,username,region,&time);
            list_insert2(username,region,time);
        }
        fclose(phonebook);
        printf("Phonebook restored!\n");
    }
}

void file_flush () {
    struct phonebook *l;
    FILE *phonebook;
    phonebook=fopen("phonebook.txt","w");
    for (l=root; l!=NULL; l=l->nxt) {
        fprintf(phonebook,"%d %s %s %d ",l->code,l->username,l->region,l->time);
    }
    fclose(phonebook);
    printf("Phonebook Saved!\n");
}        

int main(int argc,char *argv[]) {
	int s,time,i;
	char username[64],region[64];
	init();
	do {
		printf ("1. Add\n");
		printf ("2. Remove\n");
		printf ("3. Add Time\n");
		printf ("4. Print Phonebook\n");
		printf ("5. Open Phonebook\n");
		printf ("6. Save Phonebook\n");
		printf ("7. Exit\n");
		printf ("> ");
		scanf("%d",&s);
		switch (s) {
			case 1: {
				printf("Dose onomateponumo ");
				scanf("%63s",username);
				printf("Dose region gia ton neo xristi ");
				scanf("%63s",region);
				printf("Dose ton xrono omilias ");
				scanf("%d",&time);
				printf("\n");				
				i=list_insert(username,region,time);
				if (i==1) {
				    printf("H prosthiki oloklirothike epitixos!\n");
				}    
				break;
			}
			case 2: {
			    printf("Dose to onomateponumo tou xristi pou thelete na aferesete ");
			    scanf("%63s",username);
				remove_name(username);
				break;
			}
			case 3: {
			    printf("Dose to onomateponumo tou opoiou thes na auksiseis ton xrono omilias ");
			    scanf("%63s",username);
				add_time(username);
				break;
			}
			case 4: {
			    print_phonebook();
			    break;
			}    
			case 5: {
				file_open();
				break;
			}
			case 6: {
			    file_flush();
			    break;
			}    
		}
  	} while (s!=7);
}