简体   繁体   English

递归函数 - 输出不正确

[英]Recursion functions - Output is incorrect

I am creating a program that uses recursion functions to count the vowels in a sentence and to determine if it is a palindrome. 我正在创建一个程序,它使用递归函数来计算句子中的元音并确定它是否是回文。 The problem I am getting is that it says the sentence entered is not palindrome even if it is.. Any help with this will be greatly appreciated. 我得到的问题是它说输入的句子不是回文,即使它是..任何帮助都将非常感激。 Thank you. 谢谢。

#include<iostream> 
#include <cmath>


using namespace std;

struct Sentence
{
    int CountVowels(string , int);

    public:
    Sentence (string);
    bool isPal(string , int);
    void Print();
    string s;
    int numVowel;
    int length;
    //~Sentence();

};

Sentence :: Sentence (string b)
{
    s = b;
    length = 0;
    numVowel = CountVowels(s, 0);
}

int Sentence :: CountVowels(string myWord, int startindex)
{
    length ++;
    int pandi; 

    if(myWord[startindex])
    {
        if (myWord[startindex] != 'a' && myWord[startindex] != 'e' && myWord[startindex] != 'i' && myWord[startindex] != 'o' && myWord[startindex] != 'u')
        {
            pandi = 0;
        }
    else pandi = 1;
    return pandi + CountVowels(myWord, startindex + 1);
    } 
    return 0;
}

bool Sentence :: isPal(string myWord, int size)
{
    int r = myWord.size() - size;
    int t = size - 1;


    if (size == r || r == t)

        return true;


    if ((myWord[r]) != (myWord[t]))

        return false;


    return isPal(myWord, -- size);
}

void Sentence :: Print()
{
    cout << s [-- length];
    if (length == 0)
    {
        cout << endl;
        return;

    }
    Print ();
}

/*Sentence :: ~Sentence()
{
    cout << "\ntilde delete\n\n";
}*/

int main ()
{
    string userW;

    cout << "Enter a sentence: \n";
    getline(cin, userW);
    userW.erase(remove_if(userW.begin(), userW.end(), [](char c) {return !isalpha(c); }), userW.end());
    Sentence userSent(userW);

    cout << "The number of vowels in the sentence is " << userSent.numVowel << endl;
    cout << "" << endl;

    cout << "The sentence " << userSent.s << " is" << 
    (userSent.isPal(userSent.s, userSent.s.size()) ? " Palindrome\n" : " Not Palindrome\n");


    return 0;
}

UPDATE: I am now trying to remove special characters. 更新:我现在正在尝试删除特殊字符。 So it looks like this 所以它看起来像这样

string userW;

        cout << "Enter a sentence: \n";
        getline(cin, userW);
        userW.erase(remove_if(userW.begin(), userW.end(), [](char c) {return !isalpha(c); }), userW.end());

But I am getting this error: 但是我收到了这个错误:

In function 'int main()':
88:85: error: 'remove_if' was not declared in this scope

Your code is fine except that it does not remove commas, spaces and anything that is not alphabetic from the sentence. 您的代码很好,除了它不会删除逗号,空格和句子中不是字母的任何内容。 Also, you need to do a case-insensitive comparison on the characters. 此外,您需要对字符进行不区分大小写的比较。 This is required, otherwise the examples 这是必需的,否则就是例子

A man, a plan, a canal, Panama 一个男人,一个计划,一条运河,巴拿马

Desserts, I stressed 甜点,我强调

would not be palidromes. 不会是palidromes。

To remove special characters from the user input, you can use lambda 要从用户输入中删除特殊字符,可以使用lambda

string userW; cout << "Enter a sentence: \n"; getline(cin, userW);
userW.erase(remove_if(userW.begin(), userW.end(), [](char c) {return !isalpha(c); }), userW.end());

EDIT you can also try the following to avoid the need for lambda: 编辑你也可以尝试以下以避免需要lambda:

  userW.erase(std::copy_if(userW.begin(), userW.end(), userW.begin(), isalpha), userW.end());

to do a case-insensitive comparison, in the function isPal, you can change this: 要做一个不区分大小写的比较,在函数isPal中,你可以改变这个:

if ((myWord[r]) != (myWord[t])) if((myWord [r])!=(myWord [t]))

into this: 进入这个:

if (tolower(myWord[r]) != tolower(myWord[t]))

I have reviewed your program. 我查看了你的课程。 You are trying to out the string in the function pirnt(). 您正在尝试在函数pirnt()中输出字符串。 Here is the problem ,when you use 这是您使用时的问题

 cout << "The sentence backwards is: " << userSent.Print();

but funtion Print() does not have any return type.(because this is void type). 但功能Print()没有任何返回类型。(因为这是void类型)。 Here you should use 在这里你应该使用

cout << "The sentence backwards is: " ;
 userSent.Print();

and now it works. 现在它的工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM