如何转换java至C ++? [英] How to convert from java to C++?

查看:59
本文介绍了如何转换java至C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.util.Random;
import java.util.Scanner;

public class Main {
	public static Color[][] memory;
	public static Color[] cache;

	public static void main(String[] args) {
		shmain();
	}

	public static void shmain() {

		int N, M, K;
		Scanner s = new Scanner(System.in);

		int AMAT = 0, hit_time = 0, miss_rate = 0, miss_penalty = 1, common_strikes = 0;

		do {
			System.out.println("Enter N (should be power of 2):");
			N = s.nextInt();
		} while (isPowerOfTwo(N));

		do {
			System.out.println("Enter M (should be power of 2):");
			M = s.nextInt();
		} while (isPowerOfTwo(M));

		memory = new Color[N][M];

		do {
			System.out.println("Enter K (should be power of 2 and less than  NxM / 4) :");
			K = s.nextInt();
		} while (isPowerOfTwo(K) || K > (N * M / 4));

		cache = new Color[K];
		Random rand = new Random();

		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++) {
				memory[i][j] = new Color();
				memory[i][j].c = Math.abs(rand.nextInt() % 2);
				memory[i][j].m = Math.abs(rand.nextInt() % 2);
				memory[i][j].y = Math.abs(rand.nextInt() % 2);
				memory[i][j].k = Math.abs(rand.nextInt() % 2);
			}
		}

		for (int i = 0; i < K; i++) {
			cache[i] = new Color();
		}

		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++) {
				for (int c = 0; c < K; c++) {
					common_strikes++;
					if (cache[c] == null) {
						miss_rate++;
					} else if (memory[i][j] == cache[c]) {
						hit_time++;
					} else {
						miss_rate++;
						changeCache(N, M, K, i, j);
					}
				}
			}
		}

		System.out
				.println("Hit times: " + hit_time + " " + " \nmiss times: " + miss_rate + " \nnall checks:" + common_strikes
						+ "\nhit rate: " +((double)hit_time/(double)common_strikes) + "\nmiss rate: " +((double)miss_rate/(double)common_strikes)
						);
	}

	public static void changeCache(int n, int m, int k, int mi, int mj) {

		if (m == k) {
			for (int i = 0; i < k; i++) {
				cache[i] = memory[mi][i];
			}
		} else if (m > k) {

			if (mj + k <= m) {
				int z = 0;
				for (int j = mj; j < mj + k; j++) {
					if (j <= k)
						cache[z] = memory[mi][j];
					z++;
				}
			}

			if (mj + k > m) {
				int z = 0;
				for (int i = mj; i < m; i++) {
					cache[z] = memory[mi][i];
					z++;
				}
				z = 0;
				
			}
		} else {

			Color[] mass_odnom = new Color[n * m];
			int ka = 0;
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					mass_odnom[ka] = memory[i][j];
					k++;
				}
			}

			ka = 0;
			for (int i = mj * mi + mj; i < mj * mi + mj + k; i++) {
				cache[k] = mass_odnom[i];
				ka++;
			}

		}

	}

	public static boolean isPowerOfTwo(int x) {
		return !((x != 0) && ((x & (~x + 1)) == x));
	}
}





我的尝试:



请帮帮我,亲爱的人们

怎么解决这个代码?





What I have tried:

Help me please, dear kind people
how to solve this code?

cache = new Color[K];
		Random rand = new Random();

		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++) {
				memory[i][j] = new Color();
				memory[i][j].c = Math.abs(rand.nextInt() % 2);
				memory[i][j].m = Math.abs(rand.nextInt() % 2);
				memory[i][j].y = Math.abs(rand.nextInt() % 2);
				memory[i][j].k = Math.abs(rand.nextInt() % 2);
			}
		}

		for (int i = 0; i < K; i++) {
			cache[i] = new Color();
		}

		for (int i = 0; i < N; i++) {
			for (int j = 0; j < M; j++) {
				for (int c = 0; c < K; c++) {
					common_strikes++;
					if (cache[c] == null) {
						miss_rate++;
					} else if (memory[i][j] == cache[c]) {
						hit_time++;
					} else {
						miss_rate++;
						changeCache(N, M, K, i, j);
					}
				}
			}
		}

		System.out
				.println("Hit times: " + hit_time + " " + " \nmiss times: " + miss_rate + " \nnall checks:" + common_strikes
						+ "\nhit rate: " +((double)hit_time/(double)common_strikes) + "\nmiss rate: " +((double)miss_rate/(double)common_strikes)
						);
	}

推荐答案

j2c [ ^ ]能够将java代码转换为大致相当的C ++代码。必须对转换后的代码进行测试,以确保其按预期工作。
j2c[^] is able to convert java code to roughly equivalent C++ code. The converted code will have to be tested to ensure that it works as desired.


这篇关于如何转换java至C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆