main.c

creato da RIGUZZI FABRIZIO ultima modifica 25/02/2010 22:38

main.c — C source code, 0Kb

Contenuto del file

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


int main()
{
	char command[1000];
	GHashTable  * phones;
	char * name;
	int * value,number,res;
	phones=g_hash_table_new(g_str_hash,g_str_equal);

	

while(1)
{
	fgets(command,1000,stdin);
	if (command[0]=='a')
	{
		//add
		name=(char *)malloc(sizeof(char)*1000);
		sscanf(command+2,"%s%d",name,&number);
		value=(int *)malloc(sizeof(int ));
		*value=number;
		g_hash_table_insert(phones, name , value);

	}
	else
	{
		if (command[0]=='f')
		{
			//retrieve
			name=(char *)malloc(sizeof(char)*1000);
			sscanf(command+2,"%s",name);
			value=g_hash_table_lookup(phones,name);
			if (value!=NULL)
				printf("Number %d\n\n",*value);
			else
				printf("Name not found\n\n");
		}
		else
			break;
	}
}
	return 1;

}
Azioni sul documento