-1

我有一个家庭作业如下:

“编写一个程序,将非负整数流分离成一个流:首先,可以被 3 整除的整数,其次,等价于 1 mod 3 的整数,最后等价于 2 mod 3。你编程应该通过生成 0 到 100 之间的 20 个随机整数来模拟传入流,并且应该最多使用 4 个整数堆栈来解决问题。”

我对流不是很熟悉,我对流的体验仅限于简单地从文件中读取和写入。所以很自然地,当我在考虑一个解决方案时,我想我可以生成一个文件并向其中写入 20 个随机整数。然后只需将其读入以模拟传入流。

这是我开始感到困惑的地方,因为我应该将流分离为流(???)。我了解分离的数学方面:x%3=0、x%3=1 和 x%3=2,但我应该在这里创建一个新流吗?所以我会把输入流“分离”成一个输出流?

然后他提到最多使用四个堆栈,我看不出我需要在哪里使用堆栈来完成这个问题,除非我应该在将整数放入新流之前使用堆栈来保存整数?

请让我知道我对这个问题的理解是否合乎逻辑和正确。

编辑:我目前的解决方案

import java.io.*;
import java.util.Iterator;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;

/**
 * write a program which will seperate a stream of nonnegative integers into a stream     consisting of, firstly , the
 * integers which are divisible by 3, secondly, the integers equivalent to 1 modula three, and lastly, the integers
 * equivalent to 2 modulo 3.  Your program should simulate the incoming stream by generating 20 random integers between
 * 0 and 100 and should use at most 4 stack of integers to solve the problem.
*/
public class StreamSeperator {
public static void main (String[] args)
{
    /*
     *  Generate file to simulate incoming stream
     */
    int arr[] = new int[20];
    Random rand = new Random();
    for(int i = 0; i<20;i++){
        arr[i] = rand.nextInt(100);
    }
    try{
        File inputFile = new File("stream_sim.txt");

        if(!inputFile.exists()){
            inputFile.createNewFile();
        }
        FileWriter inputWriter = new FileWriter(inputFile.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(inputWriter);
        for(int j=0;j<arr.length-1;j++){
            bw.write(String.valueOf(arr[j]));
            bw.newLine();
        }
        bw.close();
        System.out.println("Sim file generated");
    }catch (IOException e){
        e.printStackTrace();
    }
    /*
     * Read file in and sort into stacks
     */
    Stack<Integer> divisByThree = new Stack<Integer>();
    Stack<Integer> remainderOne = new Stack<Integer>();
    Stack<Integer> remainderTwo = new Stack<Integer>();

    BufferedReader br = null;
    try {

        String sCurrentLine;

        br = new BufferedReader(new FileReader("stream_sim.txt"));

        while ((sCurrentLine = br.readLine()) != null) {
            int _temp = Integer.parseInt(sCurrentLine);
            //divisible by three
            if(_temp%3==0){
                divisByThree.push(_temp);
            }
            //remainder of one
            else if(_temp%3==1){
                remainderOne.push(_temp);
            }
            //remainder of two
            else if(_temp%3==2){
                remainderTwo.push(_temp);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    /*
     * Feed stacks into output stream
     */
    try{
        File outFile = new File("out.txt");
        if(!outFile.exists()){
            outFile.createNewFile();
        }
        FileWriter outWriter = new FileWriter(outFile.getAbsoluteFile());
        BufferedWriter outbw = new BufferedWriter(outWriter);
        Iterator itStack1 = divisByThree.iterator();
        Iterator itStack2 = remainderOne.iterator();
        Iterator itStack3 = remainderTwo.iterator();
        //first stack
        while(itStack1.hasNext()){
            outbw.write(String.valueOf(divisByThree.pop()));
            outbw.newLine();
        }
        //second stack
        while(itStack2.hasNext()){
            outbw.write(String.valueOf(remainderOne.pop()));
            outbw.newLine();
        }
        //thrid stack
        while(itStack3.hasNext()){
            outbw.write(String.valueOf(remainderTwo.pop()));
            outbw.newLine();
        }
        outbw.close();
        System.out.println("Out file generated");
    }catch (IOException e){
        e.printStackTrace();
    }
}

}

4

1 回答 1

0

我对流不是很熟悉

这就是任务的重点——让你更熟悉。

你在正确的轨道上。将问题分解为多个部分:阅读、组织和重定向。

为每个案例创建一个OutputStream并写入与您的标准匹配的值。

于 2013-04-28T14:33:00.600 回答