⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 leach.cpp

📁 无线传感器网络leach协议无线传感器网络leach协议无线传感器网络leach协议
💻 CPP
字号:
#include "StdAfx.h"
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include "Leach.h"



using namespace std;


Leach::Leach(double mP, unsigned int N, unsigned int Time)
:P(mP), NodeCount(N), TimeLapse(Time)
{
    
    this->NodeList = new Node[N];
    for (unsigned int i = 0; i < NodeCount; i++)
        NodeList[i].SetNode(NodeCount, i);
}

Leach::~Leach(void)
{
    if (NodeList) delete []NodeList;
}


void Leach::LoadNodes()
{
    //从文件输入坐标
    fstream fs;
    fs.open("input.txt");
    if (fs.is_open())
    {
        for (unsigned int i = 0; i < NodeCount; i++)
        {
            fs >> NodeList[i].px >> NodeList[i].py;
        }

        fs.close();
    }

    

}



void Leach::StartWork()
{
    //初始化随机数种子
    srand((unsigned)time( NULL ));


    //预先处理一下各节点的距离
    for (unsigned int i = 0; i < NodeCount; i++)
    {
        NodeList[i].SetDistance(NodeList);
        NodeList[i].SetE(4.00);
		cout<<i<<"初始能量为:"<<NodeList[i].ne<<endl;
    }

  

    int Remain = NodeCount;//还没有做过簇头的节点数目
    double T;
    int r = 0;
    double s;

    while(Remain)
    {
        r ++;
        cout <<"-------------No." << r << "--------------"<<endl;
        if (Remain > 1)
        {

            // 每一轮, 计算T函数的值
            T = P / (1 - r % (int)(1/P) * P );
            for (unsigned int i = 0; i < NodeCount; i++)
            {
                if (!NodeList[i].IsCluster)
                {
                    //如果节点还没有成为过簇头
                    // 每个节点生产一个随机数
                    s = (double)rand() / RAND_MAX;
                    if (s < T)
                    {
                        //簇头
                        NodeList[i].IsCluster = true;
                        NodeList[i].IsCurrentCluster = true;
                        NodeList[i].OnBecomeCluster();
                        Remain --;
                    }else{
                        NodeList[i].IsCurrentCluster = false;
                    } 
                }else{
                    
                    NodeList[i].IsCurrentCluster = false;
                }
            }
        }else{
            //仅剩的一个没有当过簇头的节点作为簇头
            for (unsigned int i = 0; i < NodeCount; i++)
            {
                if (!NodeList[i].IsCluster){
                    NodeList[i].IsCluster = true;
                    NodeList[i].IsCurrentCluster = true;
                    NodeList[i].OnBecomeCluster();
                    Remain --;
                }else{
                    NodeList[i].IsCurrentCluster = false;
                }
            }
        }

        cout <<endl;
        for (unsigned int i = 0; i < NodeCount && Remain; i++)
        {
            if (!NodeList[i].IsCurrentCluster)
			{
				int clusterHead = NodeList[i].OnNoneCluster();
				//cout<<"节点"<<i<<"向节点"<<clusterHead<<"发送数据包"<<endl;			
				Node r;
				int u = r.consumeE(NodeList[i],NodeList[clusterHead]);
				//cout<<NodeList[i].GetE()<<" "<<NodeList[clusterHead].GetE()<<endl;
			}             
        }

        //Sleep(this->TimeLapse);

    }

    
}

⌨️ 快捷键说明

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