救助java代码转c++

RK_MG_YS 2017-05-10 10:00:13





public abstract class Model {

// z=(x,v)

public static double[][] update(double[][] positionvelocity, double h,
Vehicle[] vehicleArray) {

int numbervehicles = Controller.n_veh;

double[][] positionvelocitynew = new double[positionvelocity.length][2];
// speichert auf [i][0]-Positionen und auf [i][1]-Geschwindigkeit
double k1;
double k2;
double k3;
double k4;
double w1;
double w2;
double w3;
double w4;
double[] temp;

for (int i = 0; i < numbervehicles - 1; i++) {

temp = f(positionvelocity[i][0], positionvelocity[i][1],
positionvelocity[i + 1][0], positionvelocity[i + 1][1],
vehicleArray[i]);
k1 = h * temp[0];
w1 = h * temp[1];

temp = f(positionvelocity[i][0], positionvelocity[i][1] + w1 / 2,
positionvelocity[i + 1][0], positionvelocity[i + 1][1],
vehicleArray[i]);
k2 = h * (temp[0] + w1 / 2);
w2 = h * temp[1];

temp = f(positionvelocity[i][0], positionvelocity[i][1] + w2 / 2,
positionvelocity[i + 1][0], positionvelocity[i + 1][1],
vehicleArray[i]);
k3 = h * (temp[0] + w2 / 2);
w3 = h * temp[1];

temp = f(positionvelocity[i][0], positionvelocity[i][1] + w3,
positionvelocity[i + 1][0], positionvelocity[i + 1][1],
vehicleArray[i]);
k4 = h * (temp[0] + w3);
w4 = h * temp[1];

positionvelocitynew[i][0] = positionvelocity[i][0] + k1 / 6 + k2
/ 3 + k3 / 3 + k4 / 6;
positionvelocitynew[i][1] = positionvelocity[i][1] + w1 / 6 + w2
/ 3 + w3 / 3 + w4 / 6;

// Teste ob Auto recht herausf鋒rt:

if (positionvelocitynew[i][0] > (Controller.winwidth + vehicleArray[i]
.getlength())) {
positionvelocitynew[i][0] = positionvelocitynew[i][0]
- Controller.winwidth;
}

// Teste ob Auto r點kw鋜ts f鋒rt
if (positionvelocitynew[i][1] < 0) {
positionvelocitynew[i][1] = 0;
}

}

temp = f(positionvelocity[numbervehicles - 1][0],
positionvelocity[numbervehicles - 1][1],
positionvelocity[0][0], positionvelocity[0][1],
vehicleArray[numbervehicles - 1]);
k1 = h * temp[0];
w1 = h * temp[1];

temp = f(positionvelocity[numbervehicles - 1][0],
positionvelocity[numbervehicles - 1][1] + w1 / 2,
positionvelocity[0][0], positionvelocity[0][1],
vehicleArray[numbervehicles - 1]);
k2 = h * (temp[0] + w1 / 2);
w2 = h * temp[1];

temp = f(positionvelocity[numbervehicles - 1][0],
positionvelocity[numbervehicles - 1][1] + w2 / 2,
positionvelocity[0][0], positionvelocity[0][1],
vehicleArray[numbervehicles - 1]);
k3 = h * (temp[0] + w2 / 2);
w3 = h * temp[1];

temp = f(positionvelocity[numbervehicles - 1][0],
positionvelocity[numbervehicles - 1][1] + w3,
positionvelocity[0][0], positionvelocity[0][1],
vehicleArray[numbervehicles - 1]);
k4 = h * (temp[0] + w3);
w4 = h * temp[1];

positionvelocitynew[numbervehicles - 1][0] = positionvelocity[numbervehicles - 1][0]
+ k1 / 6 + k2 / 3 + k3 / 3 + k4 / 6;
positionvelocitynew[numbervehicles - 1][1] = positionvelocity[numbervehicles - 1][1]
+ w1 / 6 + w2 / 3 + w3 / 3 + w4 / 6;

// Test ob Auto recht herausf鋒rt
if (positionvelocitynew[numbervehicles - 1][0] > (Controller.winwidth + vehicleArray[numbervehicles - 1]
.getlength())) {
positionvelocitynew[numbervehicles - 1][0] = positionvelocitynew[numbervehicles - 1][0]
- Controller.winwidth;
}

// Teste ob Auto r點kw鋜ts f鋒rt
if (positionvelocitynew[numbervehicles - 1][1] < 0) {
positionvelocitynew[numbervehicles - 1][1] = 0;
}

return positionvelocitynew;

}

public static double[] f(double x1, double v1, double x2, double v2,
Vehicle auto) {

double a;
double b;
double T;
double s_ref;
double v_ref;
double length;
double delta;
double deltav;
double salpha;
double sfunk;

a = auto.geta();
b = auto.getb();
T = auto.getdt();
s_ref = auto.gets_ref();
length = auto.getlength();
v_ref = auto.getv_ref();
delta = auto.getdelta();

deltav = v1 - v2;
salpha = x2 - length - x1;

// Falls diese Auto am rechten Rand ist
if (salpha < 0) {
salpha = x2 - length + Controller.winwidth - x1;
}

sfunk = (s_ref + v1 * T + v1 * deltav) / (2 * Math.sqrt(a * b));

double[] result = {
v1,
a
* (1 - Math.pow(v1 / v_ref, delta) - Math.pow(sfunk
/ salpha, 2)) };

return result;
}

}


求助大佬帮帮忙,把这段java代码改成c++,或者抓重点说一下,感激不尽
...全文
536 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
RK_MG_YS 2017-05-12
  • 打赏
  • 举报
回复
引用 2楼幻夢之葉 的回复:
public abstract class Model -------> class Model { public: } double[][] positionvelocit -------> double positionvelocit[][] or double** positionvelocit double[][] positionvelocitynew = new double[positionvelocity.length][2]; -------> double *positionvelocitynew = new double[positionvelocity.length]; for(int i = 0; i < positionvelocity.length; i++) { positionvelocitynew[i] = new double[2]; } 这里总的来说区别在于数组形式有些差别,C++ 动态多维数组,其实就是指针的指针的……,new的东西,需要delete销毁 函数不可以返回数组 还有就算帮你改完这里也没有用,你这里面还用到好多其他类,所以还得把其他的所有类都修改一遍。 你真要改就学学C++语法,或者花钱叫人改
谢谢,谢谢
赵4老师 2017-05-11
  • 打赏
  • 举报
回复
不要做A语言代码修改为B语言代码的无用功。 也不要做用A语言代码直接调用B语言代码库这样复杂、这样容易出错的傻事。 只需让A、B语言代码的输入输出重定向到文本文件,或修改A、B语言代码让其通过文本文件输入输出。 即可很方便地让A、B两种语言之间协调工作。 比如: A将请求数据写到文件a.txt,写完后改名为aa.txt B发现aa.txt存在时,读取其内容,调用相应功能,将结果写到文件b.txt,写完后删除aa.txt,改名为bb.txt A发现bb.txt存在时,读取其内容,读完后删除bb.txt 以上A可以替换为任何一种开发语言或开发环境,B可以替换为任何一种与A不同的开发语言或开发环境。 除非A或B不支持判断文件是否存在、文件读写和文件更名。 但是谁又能举出不支持判断文件是否存在、文件读写和文件更名的开发语言或开发环境呢? 可以将临时文件放在RamDisk上提高效率减少磨损磁盘。 数据的结构很复杂的话,文本文件的格式问题可参考json或xml 共享临时文本文件这种进程之间的通讯方法相比其它方法的优点有很多,下面仅列出我现在能想到的: ·进程之间松耦合 ·进程可在同一台机器上,也可跨机,跨操作系统,跨硬件平台,甚至跨国。 ·方便调试和监视,只需让第三方或人工查看该临时文本文件即可。 ·方便在线开关服务,只需删除或创建该临时文本文件即可。 ·方便实现分布式和负载均衡。 ·方便队列化提供服务,而且几乎不可能发生队列满的情况(除非硬盘空间满) ·…… “跨语言、跨机,跨操作系统,跨硬件平台,跨国,跨*.*的”苦海无边, 回头是“使用共享纯文本文件进行信息交流”的岸!
幻夢之葉 2017-05-11
  • 打赏
  • 举报
回复
public abstract class Model -------> class Model { public: } double[][] positionvelocit -------> double positionvelocit[][] or double** positionvelocit double[][] positionvelocitynew = new double[positionvelocity.length][2]; -------> double *positionvelocitynew = new double[positionvelocity.length]; for(int i = 0; i < positionvelocity.length; i++) { positionvelocitynew[i] = new double[2]; } 这里总的来说区别在于数组形式有些差别,C++ 动态多维数组,其实就是指针的指针的……,new的东西,需要delete销毁 函数不可以返回数组 还有就算帮你改完这里也没有用,你这里面还用到好多其他类,所以还得把其他的所有类都修改一遍。 你真要改就学学C++语法,或者花钱叫人改

5,530

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 模式及实现
社区管理员
  • 模式及实现社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧