将Java代码更改为C ++ [英] Change Java code to C++
本文介绍了将Java代码更改为C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hi
我编写了一个java类,现在我需要将它改为C ++代码,但我不是C ++的专家。任何人都可以为我改变代码到C ++吗?
PLZ我需要它!!
Hi
I coded a java class, now I need to change it to C++ code, but I am not expert in C++. Can any body change the code to C++ for me?
plz i need it!!
public class Polynomial {
private int[] coef; // coefficients
private int deg; // degree of polynomial (0 for the zero polynomial)
// a * x^b
public Polynomial(int a, int b) {
coef = new int[b+1];
coef[b] = a;
deg = degree();
}
// return the degree of this polynomial (0 for the zero polynomial)
public int degree() {
int d = 0;
for (int i = 0; i < coef.length; i++)
if (coef[i] != 0) d = i;
return d;
}
// return c = a + b
public Polynomial plus(Polynomial b) {
Polynomial a = this;
Polynomial c = new Polynomial(0, Math.max(a.deg, b.deg));
for (int i = 0; i <= a.deg; i++) c.coef[i] += a.coef[i];
for (int i = 0; i <= b.deg; i++) c.coef[i] += b.coef[i];
c.deg = c.degree();
return c;
}
// return (a - b)
public Polynomial minus(Polynomial b) {
Polynomial a = this;
Polynomial c = new Polynomial(0, Math.max(a.deg, b.deg));
for (int i = 0; i <= a.deg; i++) c.coef[i] += a.coef[i];
for (int i = 0; i <= b.deg; i++) c.coef[i] -= b.coef[i];
c.deg = c.degree();
return c;
}
// return (a * b)
public Polynomial times(Polynomial b) {
Polynomial a = this;
Polynomial c = new Polynomial(0, a.deg + b.deg);
for (int i = 0; i <= a.deg; i++)
for (int j = 0; j <= b.deg; j++)
c.coef[i+j] += (a.coef[i] * b.coef[j]);
c.deg = c.degree();
return c;
}
// return a(b(x)) - compute using Horner's method
public Polynomial compose(Polynomial b) {
Polynomial a = this;
Polynomial c = new Polynomial(0, 0);
for (int i = a.deg; i >= 0; i--) {
Polynomial term = new Polynomial(a.coef[i], 0);
c = term.plus(b.times(c));
}
return c;
}
// do a and b represent the same polynomial?
public boolean eq(Polynomial b) {
Polynomial a = this;
if (a.deg != b.deg) return false;
for (int i = a.deg; i >= 0; i--)
if (a.coef[i] != b.coef[i]) return false;
return true;
}
// use Horner's method to compute and return the polynomial evaluated at x
public int evaluate(int x) {
int p = 0;
for (int i = deg; i >= 0; i--)
p = coef[i] + (x * p);
return p;
}
// differentiate this polynomial and return it
public Polynomial differentiate() {
if (deg == 0) return new Polynomial(0, 0);
Polynomial deriv = new Polynomial(0, deg - 1);
deriv.deg = deg - 1;
for (int i = 0; i < deg; i++)
deriv.coef[i] = (i + 1) * coef[i + 1];
return deriv;
}
// convert to string representation
public String toString() {
if (deg == 0) return "" + coef[0];
if (deg == 1) return coef[1] + "x + " + coef[0];
String s = coef[deg] + "x^" + deg;
for (int i = deg-1; i >= 0; i--) {
if (coef[i] == 0) continue;
else if (coef[i] > 0) s = s + " + " + ( coef[i]);
else if (coef[i] < 0) s = s + " - " + (-coef[i]);
if (i == 1) s = s + "x";
else if (i > 1) s = s + "x^" + i;
}
return s;
}
// test client
public static void main(String[] args) {
Polynomial zero = new Polynomial(0, 0);
Polynomial p1 = new Polynomial(4, 3);
Polynomial p2 = new Polynomial(3, 2);
Polynomial p3 = new Polynomial(1, 0);
Polynomial p4 = new Polynomial(2, 1);
Polynomial p = p1.plus(p2).plus(p3).plus(p4); // 4x^3 + 3x^2 + 1
Polynomial q1 = new Polynomial(3, 2);
Polynomial q2 = new Polynomial(5, 0);
Polynomial q = q1.plus(q2); // 3x^2 + 5
Polynomial r = p.plus(q);
Polynomial s = p.times(q);
Polynomial t = p.compose(q);
System.out.println("zero(x) = " + zero);
System.out.println("p(x) = " + p);
System.out.println("q(x) = " + q);
System.out.println("p(x) + q(x) = " + r);
System.out.println("p(x) * q(x) = " + s);
System.out.println("p(q(x)) = " + t);
System.out.println("0 - p(x) = " + zero.minus(p));
System.out.println("p(3) = " + p.evaluate(3));
System.out.println("p'(x) = " + p.differentiate());
System.out.println("p''(x) = " + p.differentiate().differentiate());
}
}
推荐答案
是的,你会遇到很多错误。以下是一些原因:
- C ++中没有String
类;你可能想用std:string
。
- 没有Math.max
;你可能想用__ max
。
- 当你在C ++中使用new
时,它返回一个指针;这里有很多错误,而且你会泄漏掉wazoo因为delete
永远不会被调用。
- 没有coef.length
;你可能想要__ countof()
。
-这个
在C ++中是一个指针。
底线:你必须用C ++从上到下重新编写它。因为我猜你没有完成所有的先决条件,所以你必须向朋友询问,或者付钱给别人为你做这件事。
Yes, you will have many errors. Here are some of the reasons:
- There is noString
class in C++; you probably want to usestd:string
.
- There is noMath.max
; you probably want to use__max
.
- When you usenew
in C++, it returns a pointer; many errors here, plus you're going to have leaks out the wazoo becausedelete
is never called.
- There is nocoef.length
; you probably want__countof()
.
-this
in C++ is a pointer.
Bottom line: you have to man up and rewrite this in C++ from top to bottom. Since I'm guessing you took a course without having all the prereqs, you will have to ask a friend, or pay someone to do this for you.
在这里你去
Java和C ++的比较 [ ^ ]
XES - 用户指南 - 转换Java源代码C ++ [ ^ ] < br $> b $ b
C ++和Java语法差异备忘单 [ ^ ]
Here you go
Comparison of Java and C++[^]
XES - User Guide - Converting Java Source code C++[^]
C++ and Java Syntax Differences Cheat Sheet[^]
除了打印代码,这对我来说就像C ++。你为什么要转换它?你为什么不试试?
Apart from the printing code, this looks like C++ to me. Why do you need to convert it ? Why don't you just try ?
这篇关于将Java代码更改为C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文