Java - 使用字符串句子的学习堆栈



我必须取一个句子,不管它是什么,把每个单词放到一堆,然后倒着打印出来。所以"这是一个句子"会变成"sihT si a ecnetnes"。我已经用"a b c"等示例和其他东西跟踪了代码,但无法弄清楚我缺少什么来让它工作。

我的问题 -

  1. 无法让最后一个单词通过堆栈,因为它基于空格 (-1(,一旦给出循环退出。

  2. 如何获取堆栈并将其打印出来,其中sihT首先显示而不是最后一个显示,然后是句子的其余部分。

输出: 短语 - 这是一个句子 短语长度 - 18 计数 - 0 空间 - 4

短语 - 是一个句子 短语长度 - 13 计数 - 1 空间 - 2

短语 - 一个句子 短语长度 - 10 计数 - 2 空间 - 1

短语 - 句子 短语长度 - 8 计数 - 3 空间 - -1

a si sihT

计数 3 输入短语或退出 (XXX(:

法典:

    public class StackClassDriver
    {
       public static void main(String[] args)
       {
    //local constants
    final String QUIT = "XXX"; // Sentinel Value for Quitting
    //local variables
    String Phrase;   // User Input for sentence
    int Count;   // Initalize count to zero
    int Space;  // Initalize First Space
    char Test;
    CharStackClass Stack = new CharStackClass();
    /**************************/
    System.out.print ("Enter Sentence: ");
    Phrase = Keyboard.readString ();
    //WHILE (phrase is not the quit value)
    while (!Phrase.equalsIgnoreCase(QUIT))
    {
        //find position of the first space
        Space = Phrase.indexOf(" ");
        // Initalize count to 1
        Count = 0;
        //WHILE(a space was found)
        while (Space != -1)
        {
            // Test output
            System.out.print ("nn");
            System.out.println ("Phrase - " + Phrase);
            System.out.println("Phrase Length - " + Phrase.length());
            System.out.println ("Count - " + Count);
            System.out.println ("Space - " + Space);
            for (int Pos = 0; Pos <= Space; Pos++)
            {
                //convert first word to char
                Test = Phrase.charAt(Pos);
                Stack.push(Test);
            }
            //remove the first word and space from the phrase
            Phrase = Phrase.substring(Space + 1);
            //Add 1 to count
            Count ++;
            //find position of the first space
            Space = Phrase.indexOf(" ");
        }//END WHILE
        // Test output
        System.out.print ("nn");
        System.out.println ("Phrase - " + Phrase);
        System.out.println("Phrase Length - " + Phrase.length());
        System.out.println ("Count - " + Count);
        System.out.println ("Space - " + Space);
        System.out.print ("nn");
        while (!Stack.isEmpty())
        //for (int Pos = 0; Pos < Count; Pos++)
        {
            //return value at top of stack
            Test = Stack.peek( );
            Test = Stack.pop ();
            System.out.print (Test);
        }
        //Clear Screen
        System.out.print("nnnn");
        System.out.println("Count " + Count);
        //Input phrase or quit value
        System.out.print(Util.setLeft (27, "Enter a Phrase or Quit (XXX): "));
        Phrase = Keyboard.readString();
        }//END WHILE
      }
      }
      public class CharStackClass
      {
      private char stack [];
      private int stackSize;
      private int top;
      public CharStackClass()
      {
      //local constants
      //local variables
     /**************************/
     stackSize = 50;
     stack = new char[stackSize];
     top = 0;
     }
     public CharStackClass(int size)
     {
     //local constants
    //local variables
    /**************************/
    stackSize = size;
    stack = new char[stackSize];
    top = 0;
    }
    public void push(char num)
    {
    //local constants
    //local variables
    /**************************/
    stack[top] = num;
    top++;
    }
    public char pop()
    {
    //local constants
    //local variables
    char temp;
    /**************************/
    top--;
    temp = stack[top];
    return temp;
    }
    public char peek()
    {
    //local constants
    //local variables
    /**************************/
    return stack[top -1];
    }
    public boolean isEmpty()
    {
    //local constants
    //local variables
    /**************************/
    return top == 0;
    }
    public boolean isFull()
    {
    //local constants
    //local variables
    /**************************/
    return top == stackSize;
    }

    }

你可以简化很多。考虑这个伪代码

pos = 0
while not end of string
    if string[pos] is space
        while stack is not empty
            print stack.pop()
        end-while
        print ' '  // space between words
    end-if
    else
        stack.push(string[pos])
    end-else
    pos = pos + 1
end-while
// at this point, there might be another word on the stack,
// because there was no space at the end of the sentence
while stack is not empty
    print stack.pop()
end-while

最新更新