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

📄 vector.h

📁 用模拟退火算法解决旅行商问题
💻 H
字号:
#ifndef VECTOR_H_
#define VECTOR_H_
#include <iomanip>
#include <vector>
#include <fstream>
#include <math.h>
#include <stdlib.h>
#include <string.h>
using namespace  std;
static int NUM; 
static double longth=0;
struct str{
	char N[10];
	double X;
	double Y;
	str& operator=(const str s){        //运算符重载
		strcpy(this->N,s.N);
		this->X=s.X;
		this->Y=s.Y;
		return *this;
	}
};

vector<str> vec;      //用来储存读取的每一行内容
double f();
void print(){
	for(vector<str>::iterator i=vec.begin();i!=vec.end();i++)
		cout<<i->N<<" ";
	cout<<"指标函数:"<<f()<<endl;
}

double d(str p1,str p2)   //计算两个城市之间的距离
{
	double px=pow((p1.X-p2.X),2.0);
	double py=pow((p1.Y-p2.Y),2.0);
	return fabs(sqrt(px+py));
}

void file(){    // 读入.txt文件内容
	//********************定义变量**********************/
	char filename[30];              //文件名
	char s[20];                   //从文件读入的临时字符串。*/
	str temp;
	//*********************END****************************/
	cin>>filename;
	ifstream fin(filename);
	if(!fin)
	{
		cout<<filename<<"文件打开失败\n";
	}
	//*********************主体**************************/
	fin>>s;            //每一行的第一个
	NUM=atoi(s)+1;	
	while(fin>>s)                        //
	{
		if(s==NULL) temp=vec[0];
		else{
			strcpy(temp.N,s);
			fin>>s;
			temp.X=atof(s);
			fin>>s;
			temp.Y=atof(s);
		}
		vec.push_back(temp);
		continue;
	}
	fin.close();
	vec.push_back(vec[0]);
	longth=f();
	cout<<"初始指标函数:"<<longth<<endl;
}

void change(int u,int v){
	str temp2;
	while((u+1)<(v-1)){
		v--;
		u++;
		temp2=vec[u];
		vec[u]=vec[v];
		vec[v]=temp2;
		/*
		temp2=vec[u+1];
		vec[u+1]=vec[v-1];
		vec[v-1]=temp2;*/
	}

}

double f(){     //指标能量函数
	longth=0;
	for(int i=0;i<NUM-1;i++)
	{
		longth=longth+d(vec[i],vec[i+1]);
	}
	return longth;
}
#endif

⌨️ 快捷键说明

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