繁体   English   中英

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

[英]Java - Learning Stacks using a String Sentence

无论是什么句子,我都必须将每个单词放入一个堆栈中,然后将其反向打印出来。 所以“This is a sentence”会变成“sihT si a ecnetnes”。 我已经用“abc”之类的例子和其他东西跟踪了代码,但无法弄清楚我缺少什么才能让它工作。

我的问题 -

  1. 无法通过堆栈获得 go 的最后一句话,因为它基于空格 (-1) 并且一旦给出循环退出。

  2. 如何获取堆栈并将其打印在 sihT 首先显示而不是最后显示的位置,然后是句子的 rest。

Output:短语 - 这是一个句子 短语长度 - 18 计数 - 0 空格 - 4

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

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

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

咻咻咻

计数 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 ("\n\n");
            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 ("\n\n");
        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 ("\n\n");

        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("\n\n\n\n");

        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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM