0
如何轉換以下到C++從Java轉換到C++
public class Palindrome
{
public static void main(String args[])
{
String phrase;
Palindrome program = new Palindrome();
/*gets a phrase from the user and reads it*/
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a phrase =>");
System.out.flush();
phrase = keyboard.readLine();
/*determines if the phrase is a palindrome and prints result*/
if(!program.isPalindrome(phrase))
System.out.println("The phrase is NOT a palindrome.");
else System.out.println("The phrase is a palindrome.");
}
public static boolean isPalindrome(String testString)
{
Stack s = new Stack();
Queue q = new Queue();
String temp = testString.toLowerCase();
for (int i = 0; i < temp.length(); i++) {
Character c = new Character(temp.charAt(i));
if (!Character.isWhitespace(c.charValue())) {
s.push(c);
q.enqueue(c);
}
}
while (!s.isEmpty()) {
Character a = (Character)s.pop();
Character b = (Character)q.dequeue();
if (!a.equals(b))
return false;
}
return true;
}
}
public class Queue
{
private LinkedList myList;
public Queue()
{
myList = new LinkedList();
}
/**
* This method should return null if the Queue is empty
*/
public Object deQueue()
{
return myList.removeHead();
}
public void enQueue(Object addObj)
{
myList.addTail(addObj);
}
/**
* This method should return true if the queue has no more
* elements in it
*/
public boolean isEmpty()
{
myList.resetIndex();
return (myList.getIndexedNode() == null);
}
}
public class Stack
{
private LinkedList myList;
public Stack()
{
myList = new LinkedList();
}
/**
* This method should return null if the Stack is empty
*/
public Object pop()
{
return myList.removeHead();
}
public void push(Object addObj)
{
myList.addHead(addObj);
}
/**
* This method should return true if the stack has no more
* elements in it
*/
public boolean isEmpty()
{
myList.resetIndex();
return (myList.getIndexedNode() == null);
}
}
獲取一本關於C.編輯的書:儘管如此,這裏有很多Java代碼,大多數人都不會去碰它。你最好的選擇是找到可以做到的軟件,或者學習足夠的C來自己做。 – AndyG 2011-03-23 02:48:02
您可以編寫與列出的Java代碼相同的C++代碼。 – 2011-03-23 02:48:24
SO不是「免費寫我的代碼」服務。如果您有特定的編程問題,請提問。 – 2011-03-23 02:48:57