gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 20:09:33 24 4
gpt4 key购买 nike

我必须取出一个句子,无论它是什么,并将每个单词放入堆栈中,然后向后打印出来。所以“这是一个句子”将变成“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 ("\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

关于Java - 使用字符串句子学习堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58968495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com