开发者

Converting from java to C++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

How do I convert the following to 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*/
        BufferedRe开发者_C百科ader 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);
    }

}


On the off chance that this is a valid question, I think I'd do it something like this:

#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;
}

I'm not sure why you'd use a stack or a deque for this, but if you did, it seems like the ones in the standard library would do the job perfectly well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜