资源简介
在Windows平台下,用C++编写的邮局选址问题,可以选择出任意多个地址,内嵌聚类算法,可以运行。
代码片段和文件信息
#include “MailAddress.h“
#include
#include
#include
using namespace std;
MailAddress::MailAddress()
{
getData();
}
MailAddress::~MailAddress()
{
addresses.clear();
}
void MailAddress::getData()
{
char fname[256];
cout << “请输入存放数据的文件名:“< cin >> fname;
cout << endl;
ifstream in(fname ios_base::in);
if (!in)
{
cout << “can not open this file!“ << endl;
exit(0);
}
while (in.get()!=EOF)
{
CData data;
in >> data.x;
in >> data.y;
addresses.push_back(data);
}
in.close();
}
void MailAddress::showVector(vector v)
{
for (int i = 0; i < v.size(); i++)
{
cout << “(“ << v[i].x << ““ << v[i].y << “)“ << endl;
}
}
void MailAddress::showAddress()
{
for (int i = 0; i < addresses.size(); i++)
{
cout << “(“ << addresses[i].x<< ““ << addresses[i].y << “)“ << endl;
}
}
void MailAddress::showDestination()
{
cout << “(“ << dx << ““ << dy << “)“ << endl;
}
void MailAddress::execute(int m_k)
{
k = m_k;
vector *clusters = new vector[k];
CData *means = new CData[k];
int i = 0;
//默认一开始将前K个元组的值作为k个簇的质心(均值)
for ( i = 0; i < k; i++)
{
means[i].x = addresses[i].x;
means[i].y = addresses[i].y;
}
int lable = 0;
//根据默认的质心给簇赋值
for (i = 0; i < addresses.size(); i++)
{
lable = clusterOfTuple(means addresses[i]);
clusters[lable].push_back(addresses[i]);
}
//输出刚开始的簇
for (lable = 0; lable < k; lable++)
{
cout << “第“ << lable + 1 << “个簇:“ << endl;
vector t = clusters[lable];
showVector(t);
cout << endl;
}
double oldVar = -1;
double newVar = getVar(clusters means);
while (abs(newVar - oldVar) >= 1) //当新旧函数值相差不到1即准则函数值不发生明显变化时,算法终止
{
for (i = 0; i < k; i++) //更新每个簇的中心点
{
means[i] = getMeans(clusters[i]);
cout << “means[“ << i << “]:“ << means[i].x << “ “ << means[i].y << endl;
}
oldVar = newVar;
newVar = getVar(clusters means); //计算新的准则函数值
for ( i = 0; i < k; i++)
{
clusters[i].clear(); //清空每个簇
}
//根据新的质心获得新的簇
for ( i = 0; i < addresses.size(); i++)
{
lable = clusterOfTuple(means addresses[i]);
clusters[lable].push_back(addresses[i]);
}
//输出当前的簇
for ( lable = 0; lable < k; lable++)
{
cout << “第“ << lable + 1 << “个簇:“ << endl;
vector t = clusters[lable];
showVector(t);
cout << endl;
}
}
}
//根据质心,决定当前元组属于哪个簇
int MailAddress::clusterOfTuple(CData means[] CData tuple)
{
double dist = getDist(means[0] tuple);
double tmp;
int lable = 0; //标示属于哪一个簇
for (int i = 1; i < k; i++)
{
tmp = getDist(means[i] tuple);
if (tmp < dist)
{
dist = tmp;
lable = i;
}
}
return lable;
}
//获得给定簇集的平方误差
double MailAddress::getVar(vector clusters[] CData means[])
{
double var = 0;
for (int i = 0; i < k; i++)
{
vector t = clusters[i];
for (int j = 0; j < t.size(); j++)
{
var += getDist(t[j] means[i]
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 126976 2015-03-28 21:48 MailAddress\Debug\MailAddress.exe
文件 928880 2015-03-28 21:48 MailAddress\Debug\MailAddress.ilk
文件 1477632 2015-03-28 21:48 MailAddress\Debug\MailAddress.pdb
文件 24 2015-03-24 14:31 MailAddress\input_assign01_01.dat
文件 59 2015-03-24 21:44 MailAddress\input_assign01_03.dat
文件 290 2015-03-24 21:44 MailAddress\input_assign01_05.dat
文件 393 2015-03-24 21:45 MailAddress\input_assign01_06.dat
文件 26 2015-03-28 21:35 MailAddress\input_assign_01_02.dat
文件 149 2015-03-24 21:44 MailAddress\input_assign_01_04.dat
文件 2383 2015-03-28 21:48 MailAddress\MailAddress\Debug\MailAddress.log
文件 404163 2015-03-28 21:48 MailAddress\MailAddress\Debug\MailAddress.obj
文件 1714 2015-03-28 21:48 MailAddress\MailAddress\Debug\MailAddress.tlog\cl.command.1.tlog
文件 37122 2015-03-28 21:48 MailAddress\MailAddress\Debug\MailAddress.tlog\CL.read.1.tlog
文件 3920 2015-03-28 21:48 MailAddress\MailAddress\Debug\MailAddress.tlog\CL.write.1.tlog
文件 1332 2015-03-28 21:48 MailAddress\MailAddress\Debug\MailAddress.tlog\li
文件 2870 2015-03-28 21:48 MailAddress\MailAddress\Debug\MailAddress.tlog\li
文件 636 2015-03-28 21:48 MailAddress\MailAddress\Debug\MailAddress.tlog\li
文件 178 2015-03-28 21:48 MailAddress\MailAddress\Debug\MailAddress.tlog\MailAddress.lastbuildstate
文件 153286 2015-03-28 21:48 MailAddress\MailAddress\Debug\test.obj
文件 286685 2015-03-24 14:53 MailAddress\MailAddress\Debug\test2.obj
文件 379904 2015-03-28 21:48 MailAddress\MailAddress\Debug\vc120.idb
文件 446464 2015-03-28 21:48 MailAddress\MailAddress\Debug\vc120.pdb
文件 24 2015-03-24 14:31 MailAddress\MailAddress\input_assign01_01.dat
文件 59 2015-03-24 21:44 MailAddress\MailAddress\input_assign01_03.dat
文件 290 2015-03-24 21:44 MailAddress\MailAddress\input_assign01_05.dat
文件 393 2015-03-24 21:45 MailAddress\MailAddress\input_assign01_06.dat
文件 26 2015-03-28 21:35 MailAddress\MailAddress\input_assign_01_02.dat
文件 149 2015-03-24 21:44 MailAddress\MailAddress\input_assign_01_04.dat
文件 5346 2015-03-28 21:48 MailAddress\MailAddress\MailAddress.cpp
文件 991 2015-03-24 21:46 MailAddress\MailAddress\MailAddress.h
............此处省略16个文件信息
- 上一篇:停车场管理系统C语言报告
- 下一篇:C++培训课程资料1
相关资源
- C++培训课程资料1
- C++全英文授课课件
- c++音乐播放器
- C++程序设计谭浩强PDF完整版
- Visual C++音频-视频技术开发与实战1-
- VC++计算器源代码
- Boost.Asio C++ Network Programming(2nd) az
- Accelerated C++ 无水印pdf
- MFC电子邮箱
- EffectiveC++ 中文版 & EffectiveC++ 3rd 英文
- VC++实现悬浮窗 迅雷360悬浮球
- 简单HTTP代理服务器-源码c++
- Visual.Assist.X助手及其注册码 VC++
- C++程序设计_谭浩强_PDF电子书
- 全国计算机二级题库 c++ 二级
- MFC网络聊天工具
- VS2015 C++操作WebService
- 数据结构与算法分析C++语言描述第二
- C++ 学生成绩管理系统125548
- C++面向对象程序设计龚晓庆课后习题
- 五子棋大作业C++实现
- 《Visual C++ MFC棋牌类游戏编程》的源代
- VC++实现的软件自动更新模块Updater源码
- 计算器MFC+VC++
- vc++2010学习版Microsoft Visual C++ 2010 Exp
- New C++基础与提高-王桂林 .pdf
- 哈夫曼编码压缩文件,c/c++课程设计
- 学生管理系统 mfc 数据库 c++
- C++ MFC 餐厅点菜管理系统
- c++课程设计学生成绩管理系统含源代
评论
共有 条评论