صورت مساله و برنامه رو بزار ببینیم قضیه چیه!
Printable View
صورت مساله و برنامه رو بزار ببینیم قضیه چیه!
چشم اما خواهش می کنم یکی کمکم کنه اینو باید 9 روز دیگه تحویل بدم.نمی خوام که برام بنویسید فقط گام به گام منو راهنمایی کنید بنویسمش.من دانشگاه هر چی برنامه تحویل می دادم دستی بود چون اون موقع سیستم نداشتم بلد نیستم ایراد برنامه مو پیدا کنم.اول کلاس ها رو با دوست نوستم بعد اومدم از مشتق استفاده کنم اما حالا برنامه که قبلا حداقل کار می کرد حالا نمی کنه
برنامه اول:
کد:// link list.cpp : Defines the entry point for the console application.
کد:
//
#include "stdafx.h"
#include "link list.h"
#include <iostream>
#include<conio.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
using namespace std;
class List;
class ListIterator;
class ListNode;
class Graph;
/////////////////////////////////////////
class ListNode
{
friend class List;
friend class Graph;
friend class LA;
int data;
ListNode* link;
}; // end class ListNode
class ListNode2
{
friend class List;
friend class Graph;
friend class LA;
int data1;
float data2;
ListNode2* link;
}; // end class ListNode2
class List
{
friend class ListIterator;
friend class Graph;
friend class LA;
ListNode* first;
ListNode2* First;
public:
List(int element=0)
{
first = new ListNode;
first->data = element;
first->link = NULL;
}
//List(int element1=0,float element2=0)
//{
// First = new ListNode2;
// First->data1 = element1;
// First->data2 = element2;
// First->link = NULL;
//}
////////////////////////////////////////////end fo constructor
void InsertFirst(int element)
{
ListNode* t;
t = new ListNode;
t->data = element;
t->link = first;
first=t;
}
//////////////////////////////////////// end of InsertFirst
/* void InsertIn( int element,ListNode* befor)
{
ListNode* t;
t = new ListNode;
if(first)
{
t->data = element;
t->link = befor->link;
befor->link = t;
}
else
t->data = element;
t->link = NULL;
first= t;
} */
//////////////////////////////////////// end of Insert
void InsertEnd( int element)
{ ListNode* t,* s;
t = new ListNode;
if(!first)
{
t->data = element;
t->link = NULL;
}
else
{ s = first;
while(s->link != NULL)
s = s->link;
t->data = element;
s->link = t;
t->link = NULL;
}
}
//////////////////////////////////// end of Insert
void deleteNode(int element)
{
/*ListNode* t,* x;
t=first;
if(t->data == element)
{
first = t->link;
delete t;
return;
}
while(t->data != element)
{
t = x;
t=t->link ;
}
x->link = t->link;*/
ListNode* t,* h;
t = first;
if(t->data == element)
{
first = t->link;
delete t;
return;
}
h=t;
while(t != NULL)
{
if(t->data == element)
{
h->link = t->link ;
delete t;
return;
}
h=t;
t=t->link ;
}
cout<<"\n element" <<" "<<element<<" "<<"Not found"<<endl;
}
/////////////////////////////////////////end of deleteNode
List Inaction(List t)
{
}
///////////////////////////////////////// end of Inaction
int count()
{
ListNode *t;
int count=0;
for(t=first;t!=NULL;t=t->link)
{
count++;
}
return count-1;
}
//////////////////////////////////////////// end of count
void ConcatList(List x)
{
ListNode* t;
if(!first)
{
first = x.first;
return;
}
if(x.first)
{
for(t = first;t->link;t = t->link);
t->link = x.first;
/*t=first;
while(t!=NULL)
{
t = t->link;
}
t->link = x.first;*/
}
}
//////////////////////////////////////////////end of ConcatList
void Display()
{
ListNode* current;
current = first;
while(current != NULL)
{
cout<<current->data<<endl;
current = current->link;
}
}
/////////////////////////////////////////end of display
~List()
{
ListNode* t,* h;
t = first;
while(t != NULL)
{
h = t;
t = t->link ;
delete h;
}
}
};//end class link list
////////////////////////////////////////////////////////////////////////////////////////
class Graph
{
friend class LA;
List* headNodes;
int v,ID,num,temp,number,count;
public:
Graph(int v=0)
{
headNodes = new List[v+1];
}
///////////////////////////////////////end of constractor
void getANDset(int vertex)
{
List AllNodes;
cout<<"Please repeat follow steps for each node"<<endl<<endl;
for(int i=0;i<vertex;i++)
{
cout<<"1. enter vertex ID:";
cin>>ID;
AllNodes.InsertEnd(ID);
cout<<"2. enter number of neghbour vertice:";
cin>>num;
for(int j=0;j<num;j++)
{
cout<<"enter neghbour vertex"<<" "<<(j+1)<<" "<<":";
cin>>number;
temp = number;
headNodes[ID].InsertFirst(temp);
}//end for j
}//end for i
AllNodes.Display();
//return AllNodes;
}
//////////////////////////////////////////end of getANDset
int CountNeghbourNodes(int v)
{ int count=0;
ListNode* t;
t = headNodes[v].first;
for(t; t->link; t = t->link)
{
count++;
}
return count;
}
///////////////////////////////////////end of CountNeghbourNode
void DisplayNeighbourNodes(int v)
{
ListNode* t;
t = headNodes[v].first;
cout<<"[";
for(t; t->link; t=t->link)
{cout<< t->data <<" ";}
cout<<"]";
}
///////////////////////////////////////end of DisplayNeighbourNodes
void Display(int v)
{ int i;
for(i=1;i<=v;i++)
{
cout<<"node "<<i<<" adjacent: ";
DisplayNeighbourNodes(i);
cout<<endl;
}
}
////////////////////////////////////////
//void Display(List x)
//{ ListNode* s;
// s= x.first;
// while(s!= NULL)
// {
// cout<<s->data;
// s=s->link;
// }
// }
//}
///////////////////////////////////////end of Display
ListNode operator = (ListNode* x)
{
ListNode* t;
t->data = x->data;
t->link = x->link ;
}
////////////////////////////////////////////////////end of operator =
List operator = (List x)
{ int k=x.count();// felan
List t;
ListNode* s;
t =(x.first)->data;
s = x.first;
for(int i=1; i<k; i++)
{
s = s->link;
t.InsertEnd(s->data);
}
return t;
}
////////////////////////////////////////////end of operator =
List AllNodesList()
{ int v=2;
List AllNodes;
for(int i=1;i<v;i++)
{
AllNodes.InsertEnd(i);
}
return AllNodes;
}
///////////////////////////////////////////end of ggg
//void noneNeighborNodes(int v)
//{
//
//}
ListNode* NeighborNodes(int v)
{
ListNode* t;
t = headNodes[v].first;
return t ;
}
/////////////////////////////////////////////////
void Didplay(ListNode* s)
{
while(s!=NULL)
{
cout<<s->data;
s=s->link;
}
}
////////////////////////////////////////////
};//end class Graph
////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
int main()
{
int vertex,num,i,v;
List l,s,m;
l.InsertFirst(3);
l.InsertFirst(5);
l.InsertFirst(9);
l.InsertEnd(7);
l.Display();
v=l.count();
cout<<v;
cin>>num;
Graph g(num);
g.getANDset(num);
g.Didplay(g.NeighborNodes(3));
getch();
return 0;
}
برنامه دوم:
کد:#include "stdafx.h"
#include "LA.h"
#include <iostream>
#include<conio.h>
using namespace std;
class ListNode
{
public:
int data1;
float data2;
bool action;
ListNode* link;
ListNode(int &a,float &b,bool &c,ListNode* p=NULL):data1(a),data2(b),action(c),link(p){}
};
class List :public ListNode
{
protected:
ListNode* newNode(int &a,float &b,bool &c,ListNode* p)
{
ListNode* q=new ListNode(a,b,c,p);
return q;
}
public:
ListNode* first;
List(int element1=0,float element2=0,bool element3=true,ListNode* p=NULL):ListNode(element1,element2,element3,p){}
///////////////////////
void InsertFirst(int element1,float element2=0,bool element3=true)
{
ListNode* p=newNode(element1,element2,element3,first);
first=p;
}
//////////////////////
void deleteNode(int element)
{
ListNode* t,* h;
t = first;
if(t->data1 == element)
{
first = t->link;
delete t;
return;
}
h=t;
while(t != NULL)
{
if(t->data1 == element)
{
h->link = t->link ;
delete t;
return;
}
h=t;
t=t->link ;
}
cout<<"\n element" <<" "<<element<<" "<<"Not found"<<endl;
}
//////////////////////
int Count()
{
ListNode *t;
int count=0;
for(t=first;t!=NULL;t=t->link)
{
count++;
}
return count-1;
}
//////////////////////
void Display()
{
ListNode* current;
current = first;
while(current != NULL)
{
cout<<current->data1<<endl;
current = current->link;
}
}
/////////////////////
~List()
{
ListNode* t,* h;
t = first;
while(t != NULL)
{
h = t;
t = t->link ;
delete h;
}
}
};
/////////////////////////////////////////////////////////////////////////
class Graph:public List
{
protected:
int num,number,temp,ID,NV;
List* headNodes;
public:
Graph(int v=0)
{
headNodes = new List[v+1];
}
/////////////////
int CountNeighborNodes(int v)
{
int count=0;
ListNode* t;
t = headNodes[v].first;
for(t; t->link; t = t->link)
{
count++;
}
return count;
}
void DisplayNeighbourNodes(int v)
{
ListNode* t;
t = headNodes[v].first;
cout<<"[";
for(t; t->link; t=t->link)
{cout<< t->data1 <<" ";}
cout<<"]";
}
/////////////////////
void Display()
{
int i;
for(i=1;i<=NV;i++)
{
cout<<"node "<<i<<" adjacent: ";
DisplayNeighbourNodes(i);
cout<<endl;
}
}
////////////////////
ListNode operator = (ListNode* x)
{
ListNode* t;
t->data1 = x->data1;
t->data2 = x->data2;
t->action = x->action;
t->link = x->link ;
}
///////////////////////////////////////////////////////end of operator =
List operator = (List x)
{ int k =x.Count();// felan
List t((x.first)->data1,(x.first)->data2);
ListNode* s;
s = x.first;
for(int i=1; i<k; i++)
{
s = s->link;
t.InsertFirst(s->data1,s->data2,s->action);
}
return t;
}
///////////////////////////////////////////////////////end of operator =
ListNode* NeighborNodes(int v)
{
ListNode* t;
t = headNodes[v].first;
return t ;
}
///////////////////////////////////////////////////////end of NeighborNodes with poniter
void Display(ListNode* s)
{
while(s!=NULL)
{
cout<<s->data1;
s=s->link;
}
}
///////////////////////////////////////////////////////end of Display with poniter
List AllNodesList()
{
List AllNodes;
for(int i=1;i<NV;i++)
{
AllNodes.InsertFirst(i);
}
return AllNodes;
}
//////////////////////////////////////////////////////end of AllNodesList
List NoneNeighborNodes(int v)
{
List NNeighbor;
ListNode* s;
NNeighbor = AllNodesList();
s = NeighborNodes(v);
for(s;s!=NULL;s=s->link)
{
NNeighbor.deleteNode(s->data1);
}
return NNeighbor;
}
///////////////////////////////////////////////////////end of NoneNeighborNodes
};
class LA:public Graph
{
public:
LA(int ID)
{
int k;
ListNode* t;
t=first;
List alphaANDp;
alphaANDp = NoneNeighborNodes(ID);
k=NoneNeighborNodes(ID).Count();
for(t;t->link;t=t->link)
t->data2= 1/k;
}
List operator = (List x)
{
List t;
ListNode* s;
int k = x.Count();
s = x.first;
for(int i=1;i<=k;i++)
{
t.InsertFirst(s->data1,s->data2,s->action);
s = s->link;
}
return t;
}
////////////////////////////////////////////////////end of operator =
void Reward(int ID,int actionselect,float a)
{
ListNode* t;
List temp;
temp = headNodes[ID];
t = temp.first;
for(t;t->link;t=t->link)
{
if(t->data1 == ID)
t->data2 = t->data2 + a*(1-t->data2);
else
t->data2 = t->data2 * (1-a);
}
}
////////////////////////////////////////////////////end of Reward
void Penalty(int ID,int actionselect,float b)
{
int r;
ListNode* t;
r = NoneNeighborNodes(ID).Count();
t = headNodes[ID].first;
for(t;t->link;t=t->link)
{
if(t->data1 == ID)
t->data2 = t->data2 * (1-b);
else
t->data2 = b/(1-r) + t->data2 * (1-b);
}
}
////////////////////////////////////////////////////end of Penalty
int GetNextNode(int ID)
{
float A=0;
ListNode* t;
t = headNodes[ID].first;
while(t!=NULL)
{
if((t->data2) >= A && t->action == true)
{
A = t->data2;
ID = t->data1;
}
}
return ID;
}
};
int main()
{
return 0;
}
ممکنه یکی جواب منو بده
من کد زیر رو نوشتم اما خطا می ده
کد:int Count()
{
List *t;
t=first
int count=0;
for(t;t!=NULL;t=t->link)
{
count++;
}
اینم خطاشهکد:'=':cannot convert from ListNode* to List*
من هنوز متنظرم ممنون میشم یکی راهنمایی کنه.
خب برای اینکه دارین یه Node رو تو یه لیست پیوندی قرار می دین .نقل قول:
می خواین کدتون چیکار کنه ؟
می خوام یه لیست رو پیمایش کنه و تعداد اونو برگردونه.
بله حرف شما درسته ! به نظر شما باید در کلاس Listode تابع هایی برای get مقادیر محافظت شده بنویسم اگه اینطوره برای اینکه لینک رو برگردونه چی باید بنویسم و تو کد بالا چطور ازش استفاده کنم؟من کد زیر رو تو کلاس پایه نوشتم درسته به نظرتون؟نقل قول:
خب برای اینکه دارین یه Node رو تو یه لیست پیوندی قرار می دین .
کد:class ListNode
{
protected:
int data1;
float data2;
bool action;
ListNode* link;
public:
int getdata1()const{return data1};
float getdata2()const{return data2};
bool getaction()const{return action};
ListNode* getlink()const{return link};
};
سلام
من قبلاً برای یک تاپیک دیگر یک نمونه list نوشتم، یک نگاهی بیاندازید شاید کمکی کند:
تاپیک:
[ برای مشاهده لینک ، با نام کاربری خود وارد شوید یا ثبت نام کنید ]
دانلود:
support.h02.ir/fwlink/?LinkId=1006755647
در مفابل هر خطی که تغییر دادم اسم خودم رو نوشتم. خط هایی هم که اضافه بودند را کامنت کردم. از جمله overload کردن operator =
نیازی نیست که کلاس List از کلاس ListNode مشتق شود (ساختار داده برای نمایش لیست پیوندی)
همچنین نیازی نیست که کلاس Graph از کلاس List مشتق شود (ساختار داده برای نمایش گراف به صورت لیست های مجاورتی )
اما کلاس LA چون سر در نیاوردم که هدفش چیه چیزی نمی تونم بگم.
در تابع اصلی برنامه برای کلاس های لیست و گراف چند خط کد برای تست نمودن عملیات انها نوشتم.
کد:#include<iostream>
#include<conio.h>
usingnamespace std;
class ListNode
{
public:
int data1;
float data2;
bool action;
ListNode* link;
ListNode(int a,float b,bool c,ListNode* p=NULL):data1(a),data2(b),action(c),link(p){} //karim_medusa
};
class List //:public ListNode
{
protected:
ListNode* newNode(int a,float b,bool c,ListNode* p) //karim_medusa
{
ListNode* q=new ListNode(a,b,c,p);
return q;
}
public:
ListNode* first;
List(int element1=0,float element2=0,bool element3=true,ListNode* p=NULL)//:ListNode(element1,element2,element3,p)
{
if(element1==0 && element2==0 && element3==true && p==NULL) //karim_medusa
first=NULL; //karim_medusa
else//karim_medusa
first=newNode(element1,element2,element3,p); //karim_medusa
}
///////////////////////
void InsertFirst(int element1,float element2=0,bool element3=true)
{
ListNode* p=newNode(element1,element2,element3,first);
first=p;
}
//////////////////////
void deleteNode(int element)
{
ListNode* t=NULL,* h=NULL; // karim_medusa
t = first;
if(first==NULL) //karim_medusa
{
cout<<"delete a ction error : The list is empty!"<<endl; //karim_medusa
return; //karim_medusa
}
if(t->data1 == element)
{
first = t->link;
delete t;
return;
}
h=t;
while(t != NULL)
{
if(t->data1 == element)
{
h->link = t->link ;
delete t;
return;
}
h=t;
t=t->link ;
}
cout<<"delete action error : element" <<" "<<element<<" "<<"Not found"<<endl;
}
//////////////////////
int Count()
{
ListNode *t=NULL;
int count=0;
for(t=first;t!=NULL;t=t->link)
count++;
return count; //karim_medusa
}
//////////////////////
void Display()
{
ListNode* current=NULL; //karim_medusa
current = first;
while(current != NULL)
{
cout<<current->data1<<" ";
current = current->link;
}
}
/////////////////////
~List()
{
ListNode* t=NULL,* h=NULL;
t = first;
while(t != NULL)
{
h = t;
t = t->link ;
delete h;
}
}
};
class Graph:public List
{
protected:
int num,number,temp,ID,NV;
public:
List* headNodes; //karim_medusa
Graph(int v=0)
{
if(v>0) //karim_medusa
headNodes = new List[v];
else
headNodes =NULL; //karim_medusa
NV=v; //karim_medusa
}
/////////////////
int CountNeighborNodes(int v)
{
int count=0;
//ListNode* t;
//t = headNodes[v].first;
count= headNodes[v].Count(); //karim_medusa
//for(t; t!=NULL; t = t->link)
//count++;
return count;
}
void DisplayNeighbourNodes(int v)
{
//ListNode* t;
//t = headNodes[v].first;
cout<<"[";
headNodes[v].Display(); //karim_medusa
//for(t; t!=NULL; t=t->link)
// cout<< t->data1 <<" ";
cout<<"]";
}
/////////////////////
void Display()
{
int i;
for(i=0;i<NV;i++) //karim_medusa
{
cout<<"node "<<i<<" adjacent: ";
DisplayNeighbourNodes(i);
cout<<endl;
}
}
//////////////////////
//ListNode operator = (ListNode* x)
//{
// ListNode* t;
// t->data1 = x->data1;
// t->data2 = x->data2;
// t->action = x->action;
// t->link = x->link ;
//}
/////////////////////////////////////////////////////////end of operator =
//List operator = (List x)
//{
// int k =x.Count();// felan
// List t((x.first)->data1,(x.first)->data2);
// ListNode* s;
// s = x.first;
// for(int i=1; i<k; i++)
// {
// s = s->link;
// t.InsertFirst(s->data1,s->data2,s->action);
// }
// return t;
//}
///////////////////////////////////////////////////////end of operator =
ListNode* NeighborNodes(int v)
{
ListNode* t;
t = headNodes[v].first;
return t ;
}
///////////////////////////////////////////////////////end of NeighborNodes with poniter
//void Display(ListNode* s)
//{
//while(s!=NULL)
//{
// cout<<s->data1;
// s=s->link;
//}
//}
///////////////////////////////////////////////////////end of Display with poniter
List* AllNodesList() // karim_medusa
{
List* AllNodes=new List(); // karim_medusa
for(int i=0;i<NV;i++) // karim_medusa
AllNodes->InsertFirst(i); // karim_medusa
return AllNodes;
}
//////////////////////////////////////////////////////end of AllNodesList
List* NoneNeighborNodes(int v)
{
List *NNeighbor; //karim_medusa
ListNode* s;
NNeighbor = AllNodesList();
s = NeighborNodes(v);
for(s;s!=NULL;s=s->link)
NNeighbor->deleteNode(s->data1); //karim_medusa
return NNeighbor;
}
///////////////////////////////////////////////////////end of NoneNeighborNodes
};
class LA:public Graph
{
public:
LA(int ID)
{
int k;
ListNode* t;
t=first;
List *alphaANDp; //karim_medusa
alphaANDp = NoneNeighborNodes(ID);
k=NoneNeighborNodes(ID)->Count(); //karim_medusa
for(t;t!=NULL;t=t->link) //karim_medusa
t->data2= 1/k;
}
//List operator = (List x)
//{
// List t;
// ListNode* s;
// int k = x.Count();
// s = x.first;
// for(int i=1;i<=k;i++)
// {
// t.InsertFirst(s->data1,s->data2,s->action);
// s = s->link;
// }
// return t;
//}
////////////////////////////////////////////////////end of operator =
void Reward(int ID,int actionselect,float a)
{
ListNode* t;
List temp;
temp = headNodes[ID];
t = temp.first;
for(t;t->link;t=t->link)
{
if(t->data1 == ID)
t->data2 = t->data2 + a*(1-t->data2);
else
t->data2 = t->data2 * (1-a);
}
}
////////////////////////////////////////////////////end of Reward
void Penalty(int ID,int actionselect,float b)
{
int r;
ListNode* t;
r = NoneNeighborNodes(ID)->Count(); //karim_medusa
t = headNodes[ID].first;
for(t;t->link;t=t->link)
{
if(t->data1 == ID)
t->data2 = t->data2 * (1-b);
else
t->data2 = b/(1-r) + t->data2 * (1-b);
}
}
////////////////////////////////////////////////////end of Penalty
int GetNextNode(int ID)
{
float A=0;
ListNode* t;
t = headNodes[ID].first;
while(t!=NULL)
{
if((t->data2) >= A && t->action == true)
{
A = t->data2;
ID = t->data1;
}
}
return ID;
}
};
int main()
{
//List class Test
List testList; // first is NULL
List testList2(1,2,false,NULL); // first not NULL
cout <<"testlist.count() : "<<testList.Count()<<endl;
cout <<"testlist2.count() : "<<testList2.Count()<<endl;
testList.deleteNode(2);
testList.InsertFirst(1);
testList.InsertFirst(2);
testList.InsertFirst(3);
cout <<"test list count : "<<testList.Count()<<endl;
cout<<"--------------testList.Display()--------------"<<endl;
testList.Display();
testList.deleteNode(3); // first node
cout<<"--------------testList.Display()--------------"<<endl;
testList.Display();
//Graph class Test
int n;
int data;
cout<<"enter number of graph nodes:"<<endl;
cin>>n;
Graph graph(n);
for(int i=0;i<n;i++)
{
cout<<"enter neighbor nodes for node ("<<i<<") <<enter -1 to end>> :"<<endl;
cin>>data;
while(data>=0 && data<n)
{
graph.headNodes[i].InsertFirst(data);
cin>>data;
}
}
cout<<"graph.CountNeighborNodes(0):"<<graph.CountNeighborNodes(0)<<endl;
cout<<"--------------graph.Display()--------------"<<endl;
graph.Display();
cout<<"--------------graph.NoneNeighborNodes(0)->Display()--------------"<<endl;
graph.NoneNeighborNodes(0)->Display();
getch();
return 0;
}
نمونه خروجی کد های تست به صورت زیر است :
کد:testlist.count() : 0
testlist2.count() : 1
delete a ction error : The list is empty!
test list count : 3
--------------testList.Display()--------------
3 2 1 --------------testList.Display()--------------
2 1 enter number of graph nodes:
6
enter neighbor nodes for node (0) <<enter -1 to end>> :
1 2 3 -1
enter neighbor nodes for node (1) <<enter -1 to end>> :
0 -1
enter neighbor nodes for node (2) <<enter -1 to end>> :
0 3 5 -1
enter neighbor nodes for node (3) <<enter -1 to end>> :
0 4 5 -1
enter neighbor nodes for node (4) <<enter -1 to end>> :
3 -1
enter neighbor nodes for node (5) <<enter -1 to end>> :
2 3 -1
graph.CountNeighborNodes(0):3
--------------graph.Display()--------------
node 0 adjacent: [3 2 1 ]
node 1 adjacent: [0 ]
node 2 adjacent: [5 3 0 ]
node 3 adjacent: [5 4 0 ]
node 4 adjacent: [3 ]
node 5 adjacent: [3 2 ]
--------------graph.NoneNeighborNodes(0)->Display()--------------
5 4 0
کد:
class L//:public Graph
{
protected:
float k;
int flag; //karim_medusa
List* alpahANDp;
public:
L(Graph *graph,int ID) //karim_medusa
{
flag=0; //karim_medusa
alpahANDp=NULL; //karim_medusa
ListNode* t=NULL; //karim_medusa
alpahANDp=graph->NoneNeighborNodes(ID); //karim_medusa
t=alpahANDp->first;
k=alpahANDp->Count(); // karim_medusa
for(t;t!=NULL;t=t->link) //karim_medusa
t->data2=1/k;
cout<<"la constractor "<<ID;
}
//----------------------------------------
int selectNextNode(int ID)
{
int NN;
if(flag==0)
{
NN=selectRandomAction(ID);
flag=1;
return NN;
}
else
NN=selectBestAction(ID);
return NN;
}
//------------------------------------------
int selectRandomAction(int ID)
{
ListNode* t=NULL; //karim_medusa
//List *NNB=NULL;
int NewID,k,s;
//NNB=NoneNeighborNodes(ID);
t=alpahANDp->first;
s=alpahANDp->Count(); //karim_medusa
k=GenRandom(s);
while(k!=0 && t!=NULL) //karim_medusa
{
k--;
t=t->link;
}
if(t!=NULL)//karim_medusa
{
NewID=t->data1;
return NewID;//karim_medusa
}
return -1;//karim_medusa
}
//---------------------------------------
int selectBestAction(int ID)
{
int NewID=-1; //karim_medusa
float a=0;
ListNode* t=NULL; //karim_medusa
//List *NNB;
//NNB=NoneNeighborNodes(ID);
t=alpahANDp->first;
while(t!=NULL)
{
if(t->data1>=a && t->action==true)
{
a = t->data2;
NewID = t->data1;
}
t=t->link; //karim_medusa
}
return NewID;
}
//-----------------------------------------
int GenRandom(int lenght)
{
int Rand;
srand(time(0));
return Rand=1+rand()%lenght;
}
//------------------------------------------
void rewardANDpenalty(int ID,int act,int response,float a,float b)
{
int r;
//List *NNB;
ListNode* t=NULL; //karim_medusa
//NNB=NoneNeighborNodes(ID);
t=alpahANDp->first; //karim_medusa
r=alpahANDp->Count(); //karim_medusa
if(response==0)
{
if(t->data1==act)
t->data2 += a*(1-t->data2);
else
t->data2 = t->data2 *(1-a);
}
else
{
if(t->data1==act)
t->data2=t->data2 *(1-b);
else
t->data2=b/(r-1) + t->data2*(1-b);
}
}
};