📜  在线将java代码转换为c++ - Java代码示例

📅  最后修改于: 2022-03-11 14:52:07.415000             🧑  作者: Mango

代码示例10
" + coded);
        // decode it back
        System.out.println(" Decoded: " + adfgvx.decode(coded));
        //---------------------------------------------
        // end of automatic tests
        //---------------------------------------------
        
        // now proceed with user input
        Scanner scan = new Scanner(System.in);
        System.out.print("\n    Enter a Key: ");
        String key = scan.nextLine();
        Adfgvx user = new Adfgvx(key);
        System.out.print("Enter a message: ");
        String msg = scan.nextLine();
        // dump the grid
        user.dumpGrid();
        System.out.println("  Key is: " + user.key);
        System.out.println("Original: " + msg);
        String cypher = user.encode(msg);
        System.out.println("   Coded: " + cypher);
        System.out.println(" Decoded: " + user.decode(cypher));
    }

    /**
     * An internal class to hold the data (the character of each column)
     * it implements comparable so the column could be sorted by alpahbetical order
     */
    private class Column implements Comparable {

        // the letter A D F G V X at the head of the column
        private char header;
        // all the letters in the column
        private char[] letters;
        // use when we cumulate the digits in the letters array
        private int index;

        /**
         * Constructor that receives the letter as header
         */
        Column(char header) {
            this.header = header;
        }
        /**
         * To set the number of elements in the column
         */
        void setSize(int size) {
            // build array to receive all elements
            letters = new char[size];
            // reset that we are at element 0
            index = 0;
        }
        /** 
         * To return, while decoding, the number of characters to insert in the Column
         */
        int getSize() {
            return letters.length;
        }
        /**
         * To add a letter to the column
         */
        void add(char c) {
            letters[index++] = c;
        }
        /** 
         * To get a single letter
         */
        char getChar(int n) {
            return letters[n];
        }
        /**
         * To return as a String the letters in the column
         */
        public String toString() {
            return new String(letters);
        }
        
        /**
         * To sort the columns by header
         */
        public int compareTo(Column other) {
            return header - other.header;
        }
    }

}