All pastes #675254 Raw Edit

Anonymous

public cpp v1 · immutable
#675254 ·published 2007-08-29 22:56 UTC
rendered paste body
-----------------------------------------------------------mysql.h-----------------------------------------------------------#ifndef MYSQL_H#define MYSQL_H#include <mysql++.h>using namespace mysqlpp;class Database{public:	Result executeSQL (char* host, char* dtbse, char* usern, char* passwd, char* stmnt);};Result Database::executeSQL (char* host, char* dtbse, char* usern, char* passwd, char* stmnt){	Connection connection (use_exceptions);	connection.connect("", host, usern, passwd);	connection.select_db(dtbse);	Query qr = connection.query();	qr << stmnt;	return qr.store();}#endif----------------------------------------------------------------------------------------------------------------------texnologia.cpp-----------------------------------------------------------#ifdef HAVE_CONFIG_H#include <config.h>#endif#include <iostream>#include <cstdlib>#include <time.h>#include "mysql.h"//Epistrefei tin diefthinsi apo arrays pelatwn kai ypalilwn (kai texnikwn se sunexia)void *adressbook(int i); //declaration panw apo to include gt xrisimopoieitai i sunartisi mesa se mia methodo#include "texnologia.h"void emulateerror(pelaths *);	//dinei se random xrono se random pelati ena random errorvoid emulatecust(pelaths *);	//kalei se olous tous pelates tin act methodvoid initpelates(pelaths *);	//Arxikopoiei tis times me kapoies realistikes timesusing namespace std;void *adb[2]; //krataei tis diefthinseis tou adressbookint main(int argc, char *argv[]){	pelaths  customers[CUST_NO]; 	ypalilos ypal[EMPL_NO];	adb[0] = customers;	//init ta global pointers pou kratan tis 	adb[1] = ypal;		//diefthinseis ap ta arrays	initpelates(customers);	for (;;)	{		emulateerror(customers); //Ena error		emulatecust(customers);  //act oloi oi pelates	}	return EXIT_SUCCESS;}//epistrefei tin diefthinsi apo 0:pelates 1:ypalilousvoid *adressbook(int i){	return adb[i];}void emulateerror(pelaths customers[CUST_NO]){	time_t	t1,t2;	int	i, tmp;	srand(time(0));	(void)time(&t1); //pernoume tin wra	tmp = rand()%10; //se posa sec tha parei o pelaths (ews 10)	for ((void)time(&t2); (int) t2-t1 < tmp; (void)time(&t2)); //perimenoume na ftasei o xronos	//eftase i ora gia ena error	tmp = rand()%(CUST_NO-1); //dialegoume random customer 	i   = rand()%3+1; //dialegoume random error - excluding NOERROR 	customers[tmp].haveerror = (errortype)i; //topo8etoume to provlima ston pelati}void emulatecust(pelaths customers[CUST_NO]){	int i;	for (i=0; i < CUST_NO; i++)		customers[i].act();}//Einai statiko vazei stixeia stis protes 10 theseisvoid initpelates(pelaths customers[CUST_NO]){	if (CUST_NO != 10) return; //Einai mono gia 10 pelates		customers++->set_attr("Giorgos Palaiologos",	2108192648);	customers++->set_attr("Kostas Manolopoulos",	2105656482);	customers++->set_attr("Feh Mavragani",		2105555554);	customers++->set_attr("Vlasis Mponatsos",	2101000001);	customers++->set_attr("Grigoris Mpithikotsis",	2105468495);	customers++->set_attr("Dimosthenis Liakopoulos",2310521010);	customers++->set_attr("Aleka Papariga",		2104234895);	customers++->set_attr("Vasilis Leventis)",	2107989584);	customers++->set_attr("Solomos Solomou",	2103216549);	customers++->set_attr("Tassos Isaak",		2109876543);	return;}----------------------------------------------------------------------------------------------------------------------texnologia.h-----------------------------------------------------------#ifndef TEXNOLOGIA_H#define TEXNOLOGIA_H#define CUST_NO 10#define EMPL_NO 1#include <string.h>#include "mysql.h"extern void *adressbook(int);using namespace std;//Typos/kathgoria provlimatosenum errortype { NOERROR = 0, PSTN = 1, ISDN = 2, ADSL = 3, INTERNET = 4};//Structure pou krataei ena record gia tin vasi dedomenwntypedef struct{	int	  id;	string 	  onoma;	unsigned  long long tel;	errortype category;	bool	  condition;}record;class ypalilos; //declarationclass pelaths;	//declarationclass pelaths{public:	//Constructors/Destructor	pelaths():haveerror((errortype)0), reported(0), found(NULL){}	pelaths(string on, unsigned long long til):		haveerror((errortype)0), 		reported(0), onoma(on),		phone_num(til),found(NULL){}	~pelaths(){}	//public methodoi	void	  act();				//Kanei ton pelati na drasei, elegxei an exei provlima	string	  get_onoma()const{return onoma;}	//epistrefei to onoma tou pelati	errortype get_error()const{return haveerror;}	//epistrefei ton typo toy provlimatos	void	  set_attr(string on, unsigned long long til)//Orizei onoma k til			{onoma = on; phone_num = til;}	//public idiotites	unsigned long long phone_num;	//arithmos tilefonou pelath		errortype 	   haveerror;	//typos problimatos (0=NOERROR)private://private methodoi	bool 	  findhelp(); 		//psaxnei gia diathesimo upalilo kai ton kalei	void 	  talk(); 		//kanei tin sinomilia	//private idiotites	int	  reported;		//an exei dilothei i vlavi exei to ID	string 	  onoma;		//onoma pelath	ypalilos* found;		//pointer ston upalilo pou vrethike gia eksipiretisi};class ypalilos{/* ypothetoume oti leitourgei enas kai mono ypalilos, tou opoiou to interface tha fenetai  * tha fenetai  stin othoni. Gia skopous realismou, i efthini tis ilopoiisis twn diadikasiwn * afinetai ston xristi tou programmatos. Autos einai ypefthinos na kanei tis diadikasies, * aftos einai ipefthinos na pei An epidiorthothike i vlavi i oxi (aneksartita apo to an * ontws epidiorthothike). O pelaths tha vasizete stis apantiseis tou upalilou gia to * an i vlavi tou epidiorthothike i graftike stin vasi. * O upalilos tha exei diathesimes energies MONO otan ton "paroun tilefono". */public:	//Constructor/Destructor	ypalilos();	~ypalilos(){}	int	answer(pelaths*,int); 		//H "main" tou ypaliliou	bool	getcond()const{return busy;}	//Katastasi upalilou (busy i oxi)private://Private methodoi (diaxirisi vasis, ginete mono ap ton upalilo)	int	db_add();	//giving data to database	void	db_del(int){cout << "del db" <<endl;}	//delete data with ID TODO	void	db_rea(int);	//read record with ID, 0 shows all	void	db_cha(int){cout << "cha db" <<endl;}	//change record with ID TODO	char	menu(int);	//Rwtaei ti na kanei 	void	initrec();	//Midenizei to record se kathe telos klisis	bool 	busy;	//Katastasi ipalilou	record	data;	//Temp record gia diaxeirisi};/*-----------------------------------------------------------------*//*H act() gia enan pelati tha elegxei an yparxei provlima. An yparxei *proxoraei stin anazitisi diathesimou upalilou apo to adressbook. *An vrei diathesimo ipalilo, kalei tin sinartisi gia epikinwnia mazi tou *Apo tin klisi afti, se kathe arnitiki apantisi (eite den apantise, eite *den eksipiretithike) den epireazei tipota. Diaforetika, analogws ti  *"erwtisi" ekane, epireazei analogws */void	pelaths::act(){	if (haveerror == 0) return; //No error, no actions			//Yparxei provlima			//Psaxnoume upalilo gia voithia kai ton kaloume	if (findhelp()) //An vrethei diathesimos ipalilos		talk(); //ton kaloume			//an den vrethike, den kanoume kati	found == NULL;  //teliosame, den exoume ipalilo diathesimo	return;}/*H findhelp() gia enan pelati, anazitei apo to adressbook (pou dilonetai *sto kiriws source) gia kapoion diathesimo upalilo. An vrei, apothikevei *tin diefthinsi tou sto pointer found kai epistrefei true, allios false */bool	pelaths::findhelp(){	ypalilos *tmp = (ypalilos*)adressbook(1); //Zitame to array twn ypalilwn (1)	for (int i = 0; i < EMPL_NO; i++){		if (tmp[i].getcond() == 0){ //Psaxnoume gia busy==0			found = &tmp[i]; //Diefthinsi tou upalilou pou vrethike diathesimos			return 1;		}	}	return 0;}/*H talk() gia enan pelati, ylopoiei tin "epikinwnia" metaksi pelati kai ypalilou. *Analoga tin katastasi twn idiotitwn, kalei me sigkekrimeno thema gia eksipiretisi *1:dilosi vlavis, kai 2: erwtisi gia to pws paei i vlavi an epidiorthothike *Simiosi: Oso yparxei vlavi o pelatis tha pernei se kathe act(), gia na dilonei  *h na elegxei tin katastasi */void pelaths::talk(){	int tmp = found->answer(this, reported); //"Klisi" ston upalilo	//Meta to peras tis klisis...	if (tmp){ //An eixe "thetiki" apantisi, tote analoga me tin erwtisi ananeonoume		if (reported){ //an to provlima itan dilomeno, tote to "nai" simene oti epidiorthothike			haveerror = (errortype)0;			reported = 0;			return;		}		else{ //an den itan, tote simenei oti dilothike kai to tmp==ID tou aitimatos			reported = tmp;			return;		}	} //An itan arnitiki apantisi, den epireazetai tipota	return;}//Constructor initialising to NULLypalilos::ypalilos():busy(0){	cout << "Line idle ... " <<endl;	initrec();}/*H answer(pelaths*) gia enan ypalilo, einai i kiria diadikasia pou ulopoiei to interface *me ton xristi tou programatos kai tin epikinwnia me ton ypalilo. Energopoieitai kata tin *klisi ston ypalilo, dinei dinatotites opws mi apantisi, erwtiseis ston ypalilo, diaxeirisi *tis vasis ktl. Analoga to erwtima, i apantisi simenei: *1: se dilosi vlavis- Oti i vlavi dilothike kai i apantisi dinei ton kodiko tis *2: se erwtisi epilisis- I katafatiki apantisi simenei oti epilithike *Prosoxi kathws oi apantiseis DEN EKSARTONTAI ap tin pragmatikotita, gi afto prepei na didontai *ypefthina kai me prosoxi */int ypalilos::answer(pelaths *cust,const int id){	char 	rep; 	//Epilogi ipalilou	int 	ans = 1;//Default katafatiki apantisi	string	tmp;	//Ksekinaei to interface - Emfanisi klisis kai stoixeiwn	cout 	<< "-Pelatis me noumero: " << cust->phone_num << " Kalei, Apantisi?(y/n) :";	cin 	>> rep;	if (rep == 'n') //Epilogi na min to sikosoume	{		cout << "Connection with Customer " << cust->phone_num << " refused" << endl << "Line idle..." << endl;		initrec();return 0; //Epistrefete arnitiki apantisi	}	busy = 1; 	//An apantisoume, vazoume tin katastasi busy.			//Se periptosi paralilwn diergasiwn tha eixe noima (leme twra..)	//Aftomata kratame sto record to tilefono kai to ID(an idi uparxei)	data.id = id;	data.tel = cust->phone_num;	cout << "Connection with Customer " << cust->phone_num << " established" << endl;	if (id) //An dothike >0 simenei oti o pelatis rwtaei tin katastasi tou aitimatos tou gia ID=id		cout << "O pelatis rwtaei an h vlavi me kwdiko " << id << " exei epilithei" << endl;	else //Allios simenei oti den exei dilosei kapoia vlavi kai oti afto paei na kanei..		cout << "O pelatis thelei na dilwsei nea vlavi" << endl;	//Efoson deiksame tin erwtisi tou pelati, proxwrame ston rolo tou ypalilou...	//Deixnoume tis diathesimes energies (mporei na praksei asxeta me to ti thelei o pelatis)	rep = menu(id);	while (rep != '0'){		switch(rep){		case '1':{//Erwtisi tou onomatos tou kai egrafi sto record			data.onoma = cust->get_onoma();			cout << " -" << data.onoma << endl;			break;}		case '2':{//Erwtisi provlimatos			data.category = cust->get_error();			cout << " -" << data.category << endl;			break;}		case '3':{//Eggrafi record stin vasi			db_add(); //klisi methodou			break;}		case '4':{//Diagrafi aitimatos me ID apo tin vasi			db_del(id);			break;}		case '5':{//Anagnosi aitimatos me ID apo tin vasi			db_rea(id);			break;}		case '6':{//Allagi aitimatos me ID apo tin vasi			db_cha(id);			break;}		case '7':{//Ektiposi trexontos record			cout << "Trexousa kartela\n";			cout << "--------------------------------------" << endl;			cout << "Kwdikos:\t" 	<< data.id	 << endl;			cout << "Onoma:\t\t:" 	<< data.onoma 	 << endl;			cout << "Tilefono:\t"	<< data.tel 	 << endl;			cout << "Katigoria:\t"	<< data.category << endl;			cout << "Katastash:\t"	<< data.condition<< endl;			cout << "--------------------------------------" << endl;			break;}		case '8':{//Katafatiki apantisi ston pelati			if (id == 0) //An den itan dilomeni vlavi dinoume ton kodiko tis vlavis			{				ans == data.id; //reply the ID of record				cout << "Giving ID to customer :" << ans << ". Is this correct? (y/n) " ;				cin >> rep; //Teliki epiveveosi				if (rep == 'n') 				{					cout << "Doste nea ID gia ton pelati ";					cin >> ans;					while (ans <= 0){						cout << "Lathos id. Doste nea ID gia ton pelati ";						cin >> ans;					}				}			}			cout << "Katafatiki apantisi ston pelati " << ans << endl << "Connection closed.";			cout << endl << "Line idle ..." << endl;			initrec();			return ans;			break;			}		case '9':{//Arnitiki apantisi ston pelati			cout << "Replying negative to " << cust->phone_num << endl << "Connection closed" << endl << "Line idle..." << endl;			initrec();return 0;break;			}		}		rep = menu(id); //Show menu with actions	}	//Kleisimo tilefonou	cout << "Connection terminated" << endl << "Line idle..." << endl;	initrec();return 0;}char ypalilos::menu(int id){	//Emfanisi tou menou ston ipalilo	char tmp;	cout << "\tTi tha thelate na kanete?" << endl;	cout << "\t1: Erwtisi onomatos kai simplirosi eggrafis" << endl;	cout << "\t2: Erwtisi provlimatos kai simplirosi eggrafis" << endl;	cout << "\t3: Prosthiki eggrafis stin vasi dedomenwn" << endl;	cout << "\t4: Diagrafi apo vasi dedomenwn (tis eggrafis ID:" << id << ")" << endl;	cout << "\t5: Anagnosi apo vasi dedomenwn (tis eggrafis ID:" << id << ")" << endl;	cout << "\t6: Allagi apo vasi dedomenwn (tis eggrafis ID:" << id << ")" << endl;	cout << "\t7: Emfanisi trexousas eggrafis" << endl;	cout << "\t8: Thetiki apantisi ston pelati" << endl;	cout << "\t9: Arnitiki apantisi ston pelati" << endl;	cout << "\t0: Klisimo tilefonou" << endl;	cin >> tmp;	return tmp;}void ypalilos::db_rea(int myid){/*Emfanizei apo tin vasi, eite ola ta stoixeia eite sigkekrimeno id */	/*gia na apofigoume ta warnings tha xrisimopoiisoume char* anti gia string	 *kai kata sinepia sprintf anti gia std::<< */ 	Database mydb;	char	 myStatement[200]; 		//To statement gia ton server	char*	 server = "localhost";	//I diefthinsi tou server, ta vazoume fixed gia efkolia	char*	 datb	= "texnologia"; //I database pou xrisimopoioume, fixed gia efkolia	char*	 usr	= "root";	//username, root, fixed gia efkolia (me null pw)	char* 	 pwd	= "";		//null pw	Result 	 rs; 			//apotelesmata tou select	Row 	 rw;			//se grammes	Result::iterator itr;		//counter	if (myid) sprintf(myStatement,"select * from problems where id = %d",myid); //An exoume kodiko tiponoume kodiko	else 	  sprintf(myStatement,"select * from problems"); //Alios ta tiponoume ola	rs = mydb.executeSQL(server, datb, usr, pwd,myStatement); //Dinoume to query	//Tuponoume ta apotelesmata mas	cout << "--------------------------------------" << endl;	for ( itr = rs.begin(); itr != rs.end() ; itr++ )	{ 		rw = *itr;		cout << "ID:\t\t" 	<< rw["id"] 	<< endl;		cout << "Onoma:\t\t" 	<< rw["name"] 	<< endl;		cout << "Phone:\t\t"	<< rw["phone"]	<< endl;		cout << "Category:\t"	<< rw["cat"]	<< endl;		cout << "Condition:\t"	<< rw["cond"]	<< endl;		cout << "--------------------------------------" << endl;	}	cout << "Totally, " << rs.size() << " records listed." << endl;		return;}int	ypalilos::db_add(){/*Vriskei neo ID kai kataxorei to record stin vasi */	Database mydb;	char	 myStatement[200]; 			char*	 server = "localhost";	//I diefthinsi tou server, ta vazoume fixed gia efkolia	char*	 datb	= "texnologia"; //I database pou xrisimopoioume, fixed gia efkolia	char*	 usr	= "root";	//username, root, fixed gia efkolia (me null pw)	char* 	 pwd	= "";		//null pw	Result 	 rs; 			//apotelesmata tou select	Row 	 rw;			//se grammes	Result::iterator itr;		//counter	int 	 myid;	//Se periptosi pou ta stoixeia den einai eparkh	if (data.onoma == "" || data.category == 0) {		cout << "ERROR: Aneparkeis plirofories gia eggkrafh\n";		cout << "Veveotheite na zitisete onoma kai provlima prin ksanadokimasete\n";		return 0;	}			strncpy(myStatement,"select * from problems",200); //Diavazoume oles tis egrafes	rs = mydb.executeSQL(server, datb, usr, pwd,myStatement); //Dinoume to query	//Psaxnoume an idi exei eggrafh	for ( itr = rs.begin(); itr != rs.end();itr++) { 		rw = *itr;		if (strcmp(data.onoma.c_str(),rw["id"]) == 0){ //An exei idi epistrefoume to ID tou			cout << "ERROR: Customer already has reported an error, We give his error ID\n";			data.id = atoi(rw["id"].c_str());			return data.id;		}	}		//Psaxnoume gia Diathesimo ID	myid = 1; //apo ena, mexri na vroume keno	for ( itr = rs.begin(); itr != rs.end();)	{ 		rw = *itr;		if (myid==atoi(rw["id"].c_str())){itr = rs.begin(); myid++;} //an yparxei, afksanoume kai psaxnoume ap tin arxi		else	itr++; //allios proxorame,an ftasoume sto telos, vgenei ap to loop kai sinexizoume me to ID pou vrikame	}		data.id = myid; //Grafoume stin kartela to ID pou dosame	sprintf(myStatement,"insert into problems values('%d', '%s','%i','%d','%d')",data.id,data.onoma.c_str(), (int)data.tel , (int)data.category, (int)data.condition); //Grafoume to query	mydb.executeSQL(server, datb, usr, pwd,myStatement); //Dinoume to query ston server	return myid; //Epistrefoume to ID (den exei ousia opws einai twra, exei an kanoume optimize to code)}void	ypalilos::initrec(){/* midenizei to record */	busy = 0;	data.id = 0;	data.onoma = "";	data.tel = 0;	data.category = NOERROR;	data.condition = 0;}#endif