routingcost.cpp

来自「传感器网络的可靠路由算法」· C++ 代码 · 共 67 行

CPP
67
字号
#include "stdafx.h"
#include "Network.h"



/* global variables */
extern NODE_TYPE nodeT[4000];
extern NETWORK_TYPE net;


//this function is quite simple in that it intuitively gets the cost from end to end 

void BuildCost(int dest)
{
  int index;
  int neighborindex;

  for (index = 0; index < net.NUM_NODES; index++)
  
  {
     int currentnode;
	 double accumulatecost;
	 double accumulatedelay;

	 accumulatecost = 0;
	 accumulatedelay = 0;
	 currentnode = index;
     
 while (currentnode!=dest)
      {for (neighborindex = 0; neighborindex < MAX_NUM_NEIGHBORS; neighborindex ++)

	  if (nodeT[currentnode].rt.routingItem[neighborindex].DenstinationID==dest)
		       {accumulatecost+=linkCost(currentnode,nodeT[currentnode].rt.routingItem[neighborindex].NextHop, dest);
                 accumulatedelay+= linkDelay(currentnode, nodeT[currentnode].rt.routingItem[neighborindex].NextHop);
				 currentnode = nodeT[currentnode].rt.routingItem[neighborindex].NextHop;
	             break;}
      }	      
 	
   for (neighborindex = 0; neighborindex < MAX_NUM_NEIGHBORS; neighborindex ++)
	  
	   if (nodeT[index].rt.routingItem[neighborindex].DenstinationID==dest)
			  {   nodeT[index].rt.routingItem[neighborindex].Cost = accumulatecost;
                    nodeT[index].rt.routingItem[neighborindex].Delay = accumulatedelay;
                    break;}
  }
  
  
}


void PrintCost(int dest)
{
	int index;
	double total ;
	int neighborindex;

	total = 0; 

    for (index = 0; index < net.NUM_NODES; index++)
	{  for (neighborindex = 0; neighborindex < MAX_NUM_NEIGHBORS; neighborindex ++)
	   if (nodeT[index].rt.routingItem[neighborindex].DenstinationID==dest)
			  {   total+= nodeT[index].rt.routingItem[neighborindex].Cost ;
                    break;}
	}
  
printf("The total is %f\n", total);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?