2011-03-23 86 views
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); 
    } 

} 
+0

獲取一本關於C.編輯的書:儘管如此,這裏有很多Java代碼,大多數人都不會去碰它。你最好的選擇是找到可以做到的軟件,或者學習足夠的C來自己做。 – AndyG 2011-03-23 02:48:02

+2

您可以編寫與列出的Java代碼相同的C++代碼。 – 2011-03-23 02:48:24

+3

SO不是「免費寫我的代碼」服務。如果您有特定的編程問題,請提問。 – 2011-03-23 02:48:57

回答

1

在起飛的機會,這是一個有效的問題,我想我d做這樣的事情:

#include <string> 
#include <iostream> 

bool is_palindrome(std::string const &input) { 
    return input == std::string(input.rbegin(), input.rend()); 
} 

int main() { 
    std::cout << "Enter a phrase =>"; 
    std::string input; 
    std::getline(std::cin, input); 

    std::cout << 
     (is_palindrome(input) 
      ? "Phrase is a palindrome" 
      : "Phrase is not a palindrome"); 
    return 0; 
} 

我不確定你爲什麼要爲此使用堆棧或者deque,但是如果你這樣做的話,看起來標準庫中的那些人可以很好地完成這項工作。