發新話題

[問題] 請把以下Java程式轉成C語言

請把以下Java程式轉成C語言

import java.io.*;
import java.util.*;

public class Finalwork {
    public static int order;
    public static int amountOfNum = 256;
    public static int amountOfList = 5120;
    public static int[]numbers = new int[amountOfNum];
    public static boolean[] isExisted = new boolean[amountOfNum];
    public static String[]listOfNum = new String [amountOfList];
   
    public static Random random = new Random();
        
    public static void main(String[] args) {
        produceNumbers();
        produceLists();
        getOrder();
        printResult();
    }
   
    public static void produceNumbers() {
        int check = 0;
        
        for( int cnt=0; cnt<numbers.length; cnt++) {
            do{
                int value = random.nextInt(256);
                check = value;
               
            } while( isExisted[check] == true );
            numbers[cnt] = check;
            isExisted[check] = true;
        }
    }
   
    public  static void produceLists() {
        for( int times = 1; times<=listOfNum.length; times++) {
            for( int amount = 0; amount<=numbers.length; amount++) {
                int n1=random.nextInt(256)%10;
                int n2=random.nextInt(256)%10;

                int temp=numbers[n1];
                numbers[n1]=numbers[n2];
                numbers[n2]=temp;
            }
            
            for( int amount = 0; amount<numbers.length; amount++) {
                if( amount==0 ) {
                    listOfNum[times-1] = numbers[amount]+" ";
                } else{
                    listOfNum[times-1] += numbers[amount]+" ";
                }
            }
            writeToFile( listOfNum[times-1]+"\n\n", times);
        }
    }
   
    public static void getOrder() {
        System.out.println("Please give the order:");
        Scanner scanner = new Scanner(System.in);
        order = scanner.nextInt();
    }
   
    public static void writeToFile( String list, int times) {
        File file = new File("file_of_Finalwork.txt");

        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        try {
            FileWriter fw;
            if( times == 1) {
                fw = new FileWriter(file);
            } else{
                fw = new FileWriter(file,true);
            }
            fw.write(list);
            fw.close();
        } catch (IOException iox) {
            iox.printStackTrace();
        }
    }
   
    public static void printResult() {
        if( order > listOfNum.length ) {
            System.out.println("Your order is out of range.");
        } else{
            String[] number = listOfNum[order].split(" ");
            System.out.println("content of list:\n");

            int line = 1;
            for( int cnt = 0; cnt<number.length; cnt++) {
                int num = Integer.valueOf(number[cnt]);
               
                System.out.print(Integer.toHexString(num)+" ");
               
                if(line == 16){
                    System.out.println("");
                    line = 0;
                }
               
                line++;
            }
        }
    }
}

TOP

發新話題

本站所有圖文均屬網友發表,僅代表作者的觀點與本站無關,如有侵權請通知版主會盡快刪除。