All pastes #673705 Raw Edit

Mine

public cpp v1 · immutable
#673705 ·published 2007-08-28 16:52 UTC
rendered paste body
---------------------------------------------------------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 methodusing 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	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;	(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();}------------------------------------------------------------------------------------------------------------------texnologia.h---------------------------------------------------------#ifndef TEXNOLOGIA_H#define TEXNOLOGIA_H#define CUST_NO 10#define EMPL_NO 1extern 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(){return onoma;}	//epistrefei to onoma tou pelati	errortype 	get_error(){return haveerror;}	//epistrefei ton typo toy provlimatos	//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(){return busy;}	//Katastasi upalilou (busy i oxi)private://Private methodoi (diaxirisi vasis, ginete mono ap ton upalilo)	void	db_add(){cout << "add db" <<endl;}	//giving data to database TODO	void	db_del(int){cout << "del db" <<endl;}	//delete data with ID TODO	void	db_rea(int){cout << "rea db" <<endl;}	//read record with ID, 0 shows all TODO	void	db_cha(int){cout << "cha db" <<endl;}	//change record with ID TODO	char	menu(int);	//Rwtaei ti na kanei 	//attributes	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;	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;	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){	data.id = 0;	data.onoma = "";	data.tel = 0;	data.category = NOERROR;	data.condition = 0;}/*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, 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;		return 0; //Epistrefete arnitiki apantisi	}	busy = 1; 	//An apantisoume, vazoume tin katastasi busy.			//Se periptosi paralilwn diergasiwn tha eixe noima (leme twra..)	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 << data.id << endl << data.onoma << endl << data.tel << endl << data.category << endl << data.condition << endl;			break;}		case '8':{//Katafatiki apantisi ston pelati			cout << "Replying positively to " << cust->phone_num << endl << "Connection closed" << endl << "Line idle..." << endl;			busy = 0;			if (id == 0) 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 == 'y') return ans;break;			cout << "Give the correct ID to say : " ;			while (ans <= 0) cin >> ans; //Elegxos gia akuri timi			return ans;break;			}		case '9':{//Arnitiki apantisi ston pelati			cout << "Replying negative to " << cust->phone_num << endl << "Connection closed" << endl << "Line idle..." << endl;			busy = 0;return 0;break;			}		}		rep = menu(id); //Show menu with actions	}	//Kleisimo tilefonou	cout << "Connection terminated" << endl << "Line idle..." << endl;	busy = 0;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;}#endif