當前位置:首頁 » 編程語言 » c語言轉java工具
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言轉java工具

發布時間: 2022-03-03 02:51:22

⑴ 求將c語言換成JAVA

堆?

應該算比較簡單的數據結構,不過你剛入門

就算轉換成java,你也很難理解.

數據結構和演算法的東西也需要建立在一定代碼能力上的.

java中有個PriorityQueue類, 就是一個由堆結構實現的

優先隊列. 不過你要看懂的話必須懂一些 collection

相關的知識,包括泛型之類的東西

下面是該類的文檔
http://java.sun.com/javase/7/docs/api/java/util/PriorityQueue.html

下面是實現,我刪節了註解

/*
* @(#)PriorityQueue.java 1.16 06/04/21
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

package java.util;
public class PriorityQueue<E> extends AbstractQueue<E>
implements java.io.Serializable {

private static final long serialVersionUID = -7720805057305804111L;

private static final int DEFAULT_INITIAL_CAPACITY = 11;

/**
* Priority queue represented as a balanced binary heap: the two
* children of queue[n] are queue[2*n+1] and queue[2*(n+1)]. The
* priority queue is ordered by comparator, or by the elements'
* natural ordering, if comparator is null: For each node n in the
* heap and each descendant d of n, n <= d. The element with the
* lowest value is in queue[0], assuming the queue is nonempty.
*/
private transient Object[] queue;

/**
* The number of elements in the priority queue.
*/
private int size = 0;

/**
* The comparator, or null if priority queue uses elements'
* natural ordering.
*/
private final Comparator<? super E> comparator;

/**
* The number of times this priority queue has been
* <i>structurally modified</i>. See AbstractList for gory details.
*/
private transient int modCount = 0;

public PriorityQueue() {
this(DEFAULT_INITIAL_CAPACITY, null);
}

public PriorityQueue(int initialCapacity) {
this(initialCapacity, null);
}

public PriorityQueue(int initialCapacity,
Comparator<? super E> comparator) {
// Note: This restriction of at least one is not actually needed,
// but continues for 1.5 compatibility
if (initialCapacity < 1)
throw new IllegalArgumentException();
this.queue = new Object[initialCapacity];
this.comparator = comparator;
}

public PriorityQueue(Collection<? extends E> c) {
initFromCollection(c);
if (c instanceof SortedSet)
comparator = (Comparator<? super E>)
((SortedSet<? extends E>)c).comparator();
else if (c instanceof PriorityQueue)
comparator = (Comparator<? super E>)
((PriorityQueue<? extends E>)c).comparator();
else {
comparator = null;
heapify();
}
}

public PriorityQueue(PriorityQueue<? extends E> c) {
comparator = (Comparator<? super E>)c.comparator();
initFromCollection(c);
}

public PriorityQueue(SortedSet<? extends E> c) {
comparator = (Comparator<? super E>)c.comparator();
initFromCollection(c);
}

private void initFromCollection(Collection<? extends E> c) {
Object[] a = c.toArray();
// If c.toArray incorrectly doesn't return Object[], it.
if (a.getClass() != Object[].class)
a = Arrays.Of(a, a.length, Object[].class);
queue = a;
size = a.length;
}

private void grow(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
int oldCapacity = queue.length;
// Double size if small; else grow by 50%
int newCapacity = ((oldCapacity < 64)?
((oldCapacity + 1) * 2):
((oldCapacity / 2) * 3));
if (newCapacity < 0) // overflow
newCapacity = Integer.MAX_VALUE;
if (newCapacity < minCapacity)
newCapacity = minCapacity;
queue = Arrays.Of(queue, newCapacity);
}

public boolean add(E e) {
return offer(e);
}

public boolean offer(E e) {
if (e == null)
throw new NullPointerException();
modCount++;
int i = size;
if (i >= queue.length)
grow(i + 1);
size = i + 1;
if (i == 0)
queue[0] = e;
else
siftUp(i, e);
return true;
}

public E peek() {
if (size == 0)
return null;
return (E) queue[0];
}

private int indexOf(Object o) {
if (o != null) {
for (int i = 0; i < size; i++)
if (o.equals(queue[i]))
return i;
}
return -1;
}

public boolean remove(Object o) {
int i = indexOf(o);
if (i == -1)
return false;
else {
removeAt(i);
return true;
}
}

boolean removeEq(Object o) {
for (int i = 0; i < size; i++) {
if (o == queue[i]) {
removeAt(i);
return true;
}
}
return false;
}

public boolean contains(Object o) {
return indexOf(o) != -1;
}

public Object[] toArray() {
return Arrays.Of(queue, size);
}

public <T> T[] toArray(T[] a) {
if (a.length < size)
// Make a new array of a's runtime type, but my contents:
return (T[]) Arrays.Of(queue, size, a.getClass());
System.array(queue, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}

public Iterator<E> iterator() {
return new Itr();
}

private final class Itr implements Iterator<E> {
/**
* Index (into queue array) of element to be returned by
* subsequent call to next.
*/
private int cursor = 0;
private int lastRet = -1;

private ArrayDeque<E> forgetMeNot = null;

private E lastRetElt = null;
private int expectedModCount = modCount;

public boolean hasNext() {
return cursor < size ||
(forgetMeNot != null && !forgetMeNot.isEmpty());
}

public E next() {
if (expectedModCount != modCount)
throw new ();
if (cursor < size)
return (E) queue[lastRet = cursor++];
if (forgetMeNot != null) {
lastRet = -1;
lastRetElt = forgetMeNot.poll();
if (lastRetElt != null)
return lastRetElt;
}
throw new NoSuchElementException();
}

public void remove() {
if (expectedModCount != modCount)
throw new ();
if (lastRet != -1) {
E moved = PriorityQueue.this.removeAt(lastRet);
lastRet = -1;
if (moved == null)
cursor--;
else {
if (forgetMeNot == null)
forgetMeNot = new ArrayDeque<E>();
forgetMeNot.add(moved);
}
} else if (lastRetElt != null) {
PriorityQueue.this.removeEq(lastRetElt);
lastRetElt = null;
} else {
throw new IllegalStateException();
}
expectedModCount = modCount;
}
}

public int size() {
return size;
}

public void clear() {
modCount++;
for (int i = 0; i < size; i++)
queue[i] = null;
size = 0;
}

public E poll() {
if (size == 0)
return null;
int s = --size;
modCount++;
E result = (E) queue[0];
E x = (E) queue[s];
queue[s] = null;
if (s != 0)
siftDown(0, x);
return result;
}

private E removeAt(int i) {
assert i >= 0 && i < size;
modCount++;
int s = --size;
if (s == i) // removed last element
queue[i] = null;
else {
E moved = (E) queue[s];
queue[s] = null;
siftDown(i, moved);
if (queue[i] == moved) {
siftUp(i, moved);
if (queue[i] != moved)
return moved;
}
}
return null;
}

private void siftUp(int k, E x) {
if (comparator != null)
siftUpUsingComparator(k, x);
else
siftUpComparable(k, x);
}

private void siftUpComparable(int k, E x) {
Comparable<? super E> key = (Comparable<? super E>) x;
while (k > 0) {
int parent = (k - 1) >>> 1;
Object e = queue[parent];
if (key.compareTo((E) e) >= 0)
break;
queue[k] = e;
k = parent;
}
queue[k] = key;
}

private void siftUpUsingComparator(int k, E x) {
while (k > 0) {
int parent = (k - 1) >>> 1;
Object e = queue[parent];
if (comparator.compare(x, (E) e) >= 0)
break;
queue[k] = e;
k = parent;
}
queue[k] = x;
}

private void siftDown(int k, E x) {
if (comparator != null)
siftDownUsingComparator(k, x);
else
siftDownComparable(k, x);
}

private void siftDownComparable(int k, E x) {
Comparable<? super E> key = (Comparable<? super E>)x;
int half = size >>> 1; // loop while a non-leaf
while (k < half) {
int child = (k << 1) + 1; // assume left child is least
Object c = queue[child];
int right = child + 1;
if (right < size &&
((Comparable<? super E>) c).compareTo((E) queue[right]) > 0)
c = queue[child = right];
if (key.compareTo((E) c) <= 0)
break;
queue[k] = c;
k = child;
}
queue[k] = key;
}

private void siftDownUsingComparator(int k, E x) {
int half = size >>> 1;
while (k < half) {
int child = (k << 1) + 1;
Object c = queue[child];
int right = child + 1;
if (right < size &&
comparator.compare((E) c, (E) queue[right]) > 0)
c = queue[child = right];
if (comparator.compare(x, (E) c) <= 0)
break;
queue[k] = c;
k = child;
}
queue[k] = x;
}

private void heapify() {
for (int i = (size >>> 1) - 1; i >= 0; i--)
siftDown(i, (E) queue[i]);
}
public Comparator<? super E> comparator() {
return comparator;
}

private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException{
// Write out element count, and any hidden stuff
s.defaultWriteObject();

// Write out array length, for compatibility with 1.5 version
s.writeInt(Math.max(2, size + 1));

// Write out all elements in the "proper order".
for (int i = 0; i < size; i++)
s.writeObject(queue[i]);
}

private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in size, and any hidden stuff
s.defaultReadObject();

// Read in (and discard) array length
s.readInt();

queue = new Object[size];

// Read in all elements.
for (int i = 0; i < size; i++)
queue[i] = s.readObject();

// Elements are guaranteed to be in "proper order", but the
// spec has never explained what that might be.
heapify();
}
}

⑵ c語言轉java,拜託各位

你要把這個轉為java程序么?

packageorg.crazyit.app.ServletAPI;

importjava.util.Arrays;
importjava.util.Scanner;

publicclassTest{
publicstaticvoidmain(String[]args){
char[]ax={'1','2'};
char[]ac=reverse(ax);
System.out.println(Arrays.toString(ac));

charsz[]=newchar[128];
System.out.println("請輸入你要轉換的數:");
Scannerscanner=newScanner(System.in);
intn=scanner.nextInt();
System.out.println("請輸入進制:");
intc=scanner.nextInt();
cover_to(n,c,sz);
System.out.println("轉換成"+c+"進制是:"+Arrays.toString(sz));
}
publicstaticvoidcover_to(intnum,intbase,charsz[]){
intt=num;
intr=0;
intn=0;
inti=0;
if(base<2||base>32){
return;
}
while((n=t/base)!=t){
r=t%base;
t=n;
if(r>9)
sz[i++]=(char)('A'+(r-10));
else
sz[i++]=(char)('0'+r);
}
sz[i]=0;
reverse(sz);
}
publicstaticchar[]reverse(char[]arr){
intend=arr.length-1;
intbegin=0;
while(begin<end){
chartemp=arr[begin];
arr[begin]=arr[end];
arr[end]=temp;
begin++;
end--;
}
returnarr;
}
}

⑶ 把C語言代碼轉成Java代碼

package dis;
import java.io.*;
import java.math.*;
public class dis
{
public static void main(String [] s) throws Exception
{
Float a,b,c;
double x2;
double x1;
System.out.println("請輸入a, b, c的值");
BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in));
a=Float.parseFloat(bReader.readLine());
b=Float.parseFloat(bReader.readLine());
c=Float.parseFloat(bReader.readLine());
if (b*b - 4*a*c <= 0)
System.out.println("輸入的系數不對,b2-4ac不大於0!程序退出!");
else
{
x1 = (-b + Math.sqrt(b*b - 4*a*c))/2*a;
x2 = (-b - Math.sqrt(b*b - 4*a*c))/2*a;

System.out.println("x1 = "+ x1);
System.out.println("x2 = "+x2);
}
return;
}

}

⑷ 能把下面的c語言代碼轉換成java代碼嗎

import java.util.Scanner;

public class Project {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int num, key;

key = sc.nextInt();

while (key-- != 0) {

num = sc.nextInt();

int sum = 0;

int temp = 5;

while (num / temp != 0) {

sum = sum + num / temp;

temp = temp * 5;

}

System.out.println(sum);

}

}

}

格式化代碼截圖如下:

⑸ 把C語言程序轉換成Java

package dis;
import java.io.*;
import java.math.*;
public class dis
{
public static void main(String [] s) throws Exception
{
Float a,b,c;
double x2;
double x1;
System.out.println("請輸入a, b, c的值");
BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in));
a=Float.parseFloat(bReader.readLine());
b=Float.parseFloat(bReader.readLine());
c=Float.parseFloat(bReader.readLine());
if (b*b - 4*a*c <= 0)
System.out.println("輸入的系數不對,b2-4ac不大於0!程序退出!");
else
{
x1 = (-b + Math.sqrt(b*b - 4*a*c))/2*a;
x2 = (-b - Math.sqrt(b*b - 4*a*c))/2*a;

System.out.println("x1 = "+ x1);
System.out.println("x2 = "+x2);
}
return;
}

}

⑹ 怎樣把一下的C語言程序改成java程序

修改成Java後的代碼:

publicclassDemo{
publicstaticintgcd(inta,intb,intc){
intmin,i;
if(a>b){
if(b>c){
min=c;
}else{
min=b;
}
}else{
if(a>c){
min=c;
}else{
min=a;
}
}
if(b>c){
if(c>a){
min=a;
}else{
min=c;
}
}else{
if(b>a){
min=a;
}else{
min=b;
}
}
if(a>c){
if(c>b){
min=b;
}else{
min=c;
}
}else{
if(a>b){
min=b;
}else{
min=a;
}
}
for(i=min;i>=1;i--){
if(a%i==0&&b%i==0&&c%i==0){
break;
}
}
returni;
}

publicstaticvoidmain(String[]args){
inta=1,b=0,c=0,i,k=0,j=0,m=0,n=0,z,y;
while(a<9){
b=1;
while(b<9){
c=1;
while(c<9){
if(gcd(a,b,c)==1){
System.out.println(a+";"+b+";"+c);
k++;
z=b*b-4*a*c;
if(z==0){
j++;
}elseif(z>0){
m++;
for(y=1;y<z/2;y++){
if(y*y==z){
System.out.println("解答是有理數。");
}
}
}else{
n++;
}
}
c++;
}
b++;
}
a++;
}
System.out.println("N="+k);
System.out.println("N2="+j);
System.out.println("N3="+m);
System.out.println("N4="+n);
i=gcd(a,b,c);
}
}

⑺ c語言轉Java急用謝謝

這個改造工作量相當大,同學,自己盤算一下吧

⑻ 如何將c語言程序轉換成java語言

c語言生成的是exe的文件,點擊後可以直接運行,但是java需要在java虛擬機上運行,應該不能

⑼ 一段C語言程序轉換為java的寫法謝謝!

按照你的要求,C語言程序轉換成的Java程序如下:

publicclassGGG{

(Stringinput_str){

Stringstr_buff_8="",str_buff_10="",str_return="";

longsum8_1=0,sum8_2=0,sum10_1=0,sum10_2=0;//分別存儲八進制和十進制不同演算法取值的合計for(inti=0;i<input_str.length();i++){//對輸入字元串的每一個位元組進行循環

str_buff_8+=Integer.toOctalString(input_str.charAt(i));//將ASCII碼值轉換成八進制字元串

str_buff_10+=Integer.toString(input_str.charAt(i));//將ASCII碼值轉換成十進制字元串

}

for(inti=1;i<=str_buff_8.length();i++){

sum8_1+=(str_buff_8.charAt(i-1)-'0')*i;

sum8_2+=(str_buff_8.charAt(str_buff_8.length()-i)-'0')*i;

}

for(inti=1;i<=str_buff_10.length();i++){

sum10_1+=(str_buff_10.charAt(i-1)-'0')*i;

sum10_2+=(str_buff_10.charAt(str_buff_10.length()-i)-'0')*i;

}

str_return=""+sum8_1%10+sum8_2%10+sum10_1%10+sum10_2%10;

returnstr_return;

}

publicstaticvoidmain(String[]args){

System.out.println(calculateBillMac("abcdef"));

}

}

運行結果:

2739

這個數據和C語言用字元串"abcdef"測試的結果相同.

⑽ 有沒有工具有以把C語言轉換成為Java語言

沒有,但是如果樓主要用Java的話,你可以選擇性地看看c,因為c比Java強大。