【问题标题】:Palindrome detector using stacks and queues C++使用堆栈和队列 C++ 的回文检测器
【发布时间】:2014-04-24 23:31:44
【问题描述】:

我正在研究 C++ 中的回文检测器,它读取文件并用指示符“*”标记回文行。这就是我所拥有的。

PalindromeDetector::PalindromeDetector(const string& iFile, const string& oFile) {
myInFile = iFile;
myOutFile = oFile;
}

void PalindromeDetector::detectPalindrome() {
    ifstream fin(myInFile.data());
    ofstream fout(myOutFile.data());
    string nLine, palLine;
    while (getline(fin, nLine)){
        if (isPalindrome(nLine)){
            fout << nLine << " ***";
        } else {
            fout << nLine;
        }
    }
    fin.close();
    fout.close();
}

bool PalindromeDetector::isPalindrome(const string& str) {
    Stack<char> charStack(1);
    ArrayQueue<char> charQueue(1);
    char ch1, ch2;
    for ( unsigned i = 0; i < str.size(); i++){
        if (isalnum (str[i])){
            tolower(str[i]);
            try {
                charStack.push(str[i]);
                charQueue.append(str[i]);
            } catch ( StackException& se ){
                charStack.setCapacity(charStack.getCapacity() * 2);
                charQueue.setCapacity(charQueue.getCapacity() * 2);
                charStack.push(str[i]);
                charQueue.append(str[i]);
            }
        } else {
            while ( !charStack.isEmpty() || !charQueue.isEmpty() ){
                ch1 = charStack.pop();
                ch2 = charQueue.remove();
                if ( ch1 != ch2 ){
                    return false;
                }
            }
        }
    }
    return true;
}

到目前为止,我遇到了 2 个问题: 1. 没有正确输出行尾带有“*”的文件;出于某种原因,它在前面做。 2. 它只标记文件每个块中的第一行,而不是回文的行。 我非常感谢您对此的帮助。

【问题讨论】:

    标签: c++ stack queue palindrome


    【解决方案1】:

    你为什么要把isPalidrome弄得这么复杂?

    可以这样做

    bool isPalidrome(const string &s) {
        int left = 0;
        int right = s.length() - 1;
        while (left < right) {
           if (s[left] != s[right]) return false;
           left++; right--;
        }
        return true;
    }
    

    当然你可以添加什么不区分大小写

    编辑

    使用更傻的栈和队列方式

    bool isPalidrome(const string &s) {
       // Put everything on both
        std::stack<char> lifo;
        std::queue<char> fifo;
        unsigned int loop;
        for (loop = 0; loop < s.length); ++loop) {
          lifo.push(s[loop]);
          fifo.push(s[loop]);
        }
        // Note stack and queue the characters are in reverse order from each other
        for (loop = 0; loop < s.length); ++loop) {
           if (lifo.pop() != fifo.pop()) return false;
        }
        return true;
    }
    

    【讨论】:

    • 可能是因为他必须使用提供的类(堆栈和队列等):stackoverflow.com/questions/23255996/…
    • 那么为什么不使用这些类来读取和存储文件信息,而不是在isPalindrome 函数中使用它们呢?
    • @PawełStawarz - 你明白评论了吗
    • @EdHeal 但是为什么我要把每个字符都放在队列和堆栈上呢?我只需要比较字母数字的。
    • @user3020033 - 简单位 - 修改算法以仅考虑字母数字。变量的名称 LIFO 和 FIFO 给出了它。与先进先出相比,后进先出。即比较最后一个和第一个。
    猜你喜欢
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 2013-09-18
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多