%将以下C++代码改写为MATLAB代码 %注:由于MATLAB不支持结构体重载运算符,本代码中省略了重载运算符的部分 %请注意代码的差异,以及自行在结构体中添加所需的属性和方法 clc; clear; inf=999999; nodeNum=48; %节点数 edgeNum=102; %边数 stationName=["pingguoyuan","gucheng","bajiao amusement park","babaoshan","yuquanlu","wukesong",... "wanshoulu","gongzhufen","military museum","muxidi","nanlishilu","fuxingmen","fuchengmen",... "chegongzhuang","xizhimen","xinjiekou","ping'anli","xisi","lingjinghutong","xidan","zhangchunjie",... "xuanwumen","tian'anmenxi(W)","jishuitan","guloudajie","andingmen","tian'anmendong(E)","wangfujing",... "hepingmen","qianmen","chongwenmen","dongdan","dengshikou","dongsi","zhangzizhonglu","beixinqiao",... "yonghegong lama temple","dongzhimen","dongsishitiao","chaoyangmen","jianguomen","beijing railway station","yonganli",... "guomao","dawanglu","sihui","sihuidong(E)"]; %车站名 for i=1:nodeNum+2 stationName{i}=''; end stationName{1}=''; stationName{2}='pingguoyuan'; stationName{3}='gucheng'; stationName{4}='bajiao amusement park'; stationName{5}='babaoshan'; stationName{6}='yuquanlu'; stationName{7}='wukesong'; stationName{8}='wanshoulu'; stationName{9}='gongzhufen'; stationName{10}='military museum'; stationName{11}='muxidi'; stationName{12}='nanlishilu'; stationName{13}='fuxingmen'; stationName{14}='fuchengmen'; stationName{15}='chegongzhuang'; stationName{16}='xizhimen'; stationName{17}='xinjiekou'; stationName{18}='ping''anli'; stationName{19}='xisi'; stationName{20}='lingjinghutong'; stationName{21}='xidan'; stationName{22}='zhangchunjie'; stationName{23}='xuanwumen'; stationName{24}='tian''anmenxi(W)'; stationName{25}='jishuitan'; stationName{26}='guloudajie'; stationName{27}='andingmen'; stationName{28}='tian''anmendong(E)'; stationName{29}='wangfujing'; stationName{30}='hepingmen'; stationName{31}='qianmen'; stationName{32}='chongwenmen'; stationName{33}='dongdan'; stationName{34}='dengshikou'; stationName{35}='dongsi'; stationName{36}='zhangzizhonglu'; stationName{37}='beixinqiao'; stationName{38}='yonghegong lama temple'; stationName{39}='dongzhimen'; stationName{40}='dongsishitiao'; stationName{41}='chaoyangmen'; stationName{42}='jianguomen'; stationName{43}='beijing railway station'; stationName{44}='yonganli'; stationName{45}='guomao'; stationName{46}='dawanglu'; stationName{47}='sihui'; stationName{48}='sihuidong(E)'; e=struct('position',zeros(1,edgeNum+2),'to',zeros(1,edgeNum+2),'next',zeros(1,edgeNum+2),'length',zeros(1,edgeNum+2),... 'next_to',zeros(1,edgeNum+2)); head=zeros(1,edgeNum+2); %记录起点相同的边中最后一条边 vis=zeros(1,nodeNum+2); %标记节点是否使用 dis=infones(1,nodeNum+2); %记录各节点到要求起点的最短距离 sp=zeros(1,nodeNum+2); add=@(u,v,w)throughadd(u,v,w); throughadd=@(a,b,c)add(a,b,c); classdef node %节点 properties now w through %now代表该点位置 w是起点到此的距离 end methods function obj=node(a,b,c) obj.now=a; obj.w=b; obj.through=ab; end end end

classdef edge %边 properties position to %终点 next %与这条边同起点的下一条边的终点 length %这条边的长度; next_to end end

add=@(u,v,w)throughadd(u,v,w); throughadd=@(a,b,c)add(a,b,c); init=@()cininit(0); cininit=@(t)check(1); SPFA=@(s)spfa(s); fee=@(distance)calcFee(distance); calcFee=@(distance)calcFeeFunc(distance); calcFeeFunc=@(distance)getFee(distance); check=@(d)SPFA(d);

function throughadd(a,b,c) if a+b+c~=0 add(a,b,c); end end

function init() throughadd(1,2,2606); %存边 throughadd(2,3,1921); throughadd(3,4,1953); throughadd(4,5,1479); throughadd(5,6,1810); throughadd(6,7,1778); throughadd(7,8,1313); throughadd(8,9,1172); throughadd(9,10,1166); throughadd(10,11,1291); throughadd(11,12,424); throughadd(12,20,1590); throughadd(20,23,1217); throughadd(23,27,925); throughadd(27,28,852); throughadd(28,32,774); throughadd(32,41,1230); throughadd(41,43,1377); throughadd(43,44,790); throughadd(44,45,1385); throughadd(45,46,1673); throughadd(46,47,1714); throughadd(15,14,909); throughadd(14,13,960); throughadd(13,12,1832); throughadd(12,21,1234); throughadd(21,22,929); throughadd(22,29,851); throughadd(29,30,1171); throughadd(30,31,1634); throughadd(31,42,1030); throughadd(42,41,945); throughadd(41,40,1763); throughadd(40,39,1027); throughadd(39,38,824); throughadd(38,37,2228); throughadd(37,26,794); throughadd(26,25,1237); throughadd(25,24,1766); throughadd(24,15,1899); throughadd(15,16,1025); throughadd(16,17,1100); throughadd(17,18,1100); throughadd(18,19,869); throughadd(19,20,1011); throughadd(20,22,815); throughadd(36,37,866); throughadd(35,36,791); throughadd(34,35,1016); throughadd(34,33,848); throughadd(33,32,945); throughadd(32,31,821); end

function check(d) SPFA(d); end

function Fee=getFee(distance) %计算费用 if distance==0 %先判断这个,不可以在底下用d判

请把以下c++代码改写为matlab代码#includebitsstdc++h#define inf 0x3f3f3f3fusing namespace std;const int nodeNum=48;						节点数 const int edgeNum=102;						边数 const string stationNamenodeNum+2=pingguoyuanguchengbajia

原文地址: https://www.cveoy.top/t/topic/f70U 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录