C ++到Java代码转换:numberToBarcode()

时间:2012-03-10 17:14:58

标签: java c++ translation

有人可以帮我翻译一下从C ++到Java的代码吗?我对C ++一无所知。

EDIT2: 感谢响应人员,我将花时间学习C ++。我只是需要一些帮助来翻译事物。这是我完成的翻译。我收到所有numberToBarcode()方法名称的错误。我可以让你们查一下这段代码,看看翻译是否准确吗?

我的代码:

import java.util.Scanner;

public class zipBar {
public static int numberToBarcode(int arg0)
{
    return arg0;
}

public static void main(String[] args) {

    int z;
    int num1, num2, num3, num4, num5, checkNum;
    int tempNum;
    int checkTotal;
    String barcode = "|";

    System.out.println("Enter zip code: ");
    Scanner zip = new Scanner(System.in);
    z = zip.nextInt(); 

    if (z >= 10000 || z < 0)
    {
        System.out.println("Input Error: Input not a valid zip code");

    }
    tempNum = z;

    num5 = tempNum % 10;
    tempNum = tempNum / 10;

    num4 = tempNum % 10;
    tempNum = tempNum / 10;

    num3 = tempNum % 10;
    tempNum = tempNum / 10;

    num2 = tempNum % 10;
    tempNum = tempNum / 10;

    num1 = tempNum % 10;
    tempNum = tempNum / 10;

    checkTotal = num1 + num2 + num3 + num4 + num5;
    checkNum = (10-(checkTotal % 10)) % 10;

    barcode += numberToBarcode(num1);
    barcode += numberToBarcode(num2);  
    barcode += numberToBarcode(num3);  
    barcode += numberToBarcode(num4);  
    barcode += numberToBarcode(num5);  
    barcode += numberToBarcode(checkNum);

    barcode += "|";

    System.out.println("Your zip code's barcode is: " + barcode);

    //return 0;

}

public static void String numberToBarcode(int num){

String barcode = " ";

int dig;
int tempNum;
int bcTotal = 0;

tempNum = num;

if (tempNum >= 10){
    dig = tempNum % 10;
    tempNum /= 10;

    barcode = numberToBarcode(tempNum);
}
else{
    dig = tempNum;
}
tempNum = dig;

if (dig == 0) {
    barcode += "||:::";
}
else{
    if (tempNum / 7 == 1 && bcTotal < 2){
        barcode += "|";
        tempNum -= 7;
        bcTotal++;
    }
    else  
        barcode += ":";   
    if (tempNum / 4 == 1 && bcTotal < 2) {     
        barcode += "|";  
        tempNum -= 4;  
        bcTotal++;  
    } else  
        barcode += ":";  
    if (tempNum / 2 == 1 && bcTotal < 2) {    
        barcode += "|";  
        tempNum -= 2;  
        bcTotal++;  
    } else  
        barcode += ":";  
    if (tempNum / 1 == 1 && bcTotal < 2) {  
        barcode += "|";  
        tempNum -= 1;  
        bcTotal++;  
    } else  
        barcode += ":";  
    if (bcTotal < 2) { 
        barcode += "|";  
        bcTotal++;  
    } else  
        barcode += ":";
}
return barcode;
}

}

C ++代码:

string numberToBarcode(int);  

int main() {  

int zip;  
int num1, num2, num3, num4, num5, checkNum;  
int tempNum;  
int checkTotal;  
string barcode = "|"; 

cout << "Please enter a 5 digit zip code.\n"  
    << " --> ";  
cin >> zip; 


if (zip >= 100000 || zip < 0) {  
    cout << "Error: Not a zip code.\n";  
    return 0;  
}  

tempNum = zip;  

num5 = tempNum % 10;  
tempNum = tempNum / 10;  
num4 = tempNum % 10;  
tempNum = tempNum / 10;  
num3 = tempNum % 10;  
tempNum = tempNum / 10;  
num2 = tempNum % 10;  
tempNum = tempNum / 10;  
num1 = tempNum % 10;  
tempNum = tempNum / 10;  

checkTotal = num1 + num2 + num3 + num4 + num5;  
checkNum = (10-(checkTotal % 10))%10;   


barcode += numberToBarcode(num1);  

4 个答案:

答案 0 :(得分:2)

C ++允许您只提及数据类型(int)而不使用名称,但仅在函数声明中,您必须在实现中定义它。否则,一般语法没有太大区别(虽然这并不意味着您可以直接在Java中使用C ++函数实现)。

这是一个Java翻译:

/** Java translation **/
String numberToBarcode(int arg0);

barcode += numberToBarcode(num1);  
barcode += numberToBarcode(num2);  
barcode += numberToBarcode(num3);  
barcode += numberToBarcode(num4);  
barcode += numberToBarcode(num5);  

barcode += numberToBarcode(checkNum);  

此外,如果这是您第一次遇到这种情况,我必须告诉您,在您的职业生涯中,您可能必须使用或转换为数百种C ++代码库。所以我建议你认真考虑学习一点C ++。这是一项非常好的投资。

答案 1 :(得分:0)

我可以看到的唯一区别是您需要使用String而不是string您可能希望将该方法设为静态。

public static String numberToBarcode(int n) {
   return n as a String
}

所有其他行都是一样的。

答案 2 :(得分:0)

interface BarcodGenerator {
    String numberToBarcode(int no);
}

BarcodeGenerator bg =.... some instance ....

String barcode = "";
barcode += bg.numberToBarcode(num1);
barcode += bg.numberToBarcode(num2);
barcode += bg.numberToBarcode(num3);
barcode += bg.numberToBarcode(num4);
barcode += bg.numberToBarcode(num5);
barcode += bg.numberToBarcode(checkNum);

答案 3 :(得分:0)

String numberToBarcode(int x){
    return ""+x;
} 

String makeBarCode() {
    String barcode="";
    barcode += numberToBarcode(1);   
    barcode += numberToBarcode(2);   
    barcode += numberToBarcode(3);   
    barcode += numberToBarcode(4);   
    barcode += numberToBarcode(5);
    return barcode;
}