ページ 1 / 1
C++の回文のプログラミングが八方塞がりです。助けて下さい
Posted: 2014年10月05日(日) 12:47
by star
コード:
#include <string>
#include <algorithm>
#include <iostream>
#include <conio.h>
#include <cctype>
using namespace std;
int main()
{
{
system("cls");
cout << "\nEnter a palindrome." << endl;
std::string str;
getline(std::cin, str);
if( equal(str.begin(), str.end(), str.rbegin()) )
std::cout << "is a palindrome.\n";
else
std::cout << "is not a palindrome.\n";
cout << "\nGo again (Y/N)?" << endl;
} while (toupper (_getch())!= 'N');
return 0;
}
これだと
Enter a palindrome.
a
is a palindrome.
Go again(Y/N)?
となるのですが、Yが反応しません。どうすればYタイプでループになるでしょうか?
更に、a is a palindrome.と答えを一行にするにはどこを直せば良いのでしょうか?
まだ全然初心者で皆目見当がつかず泣きそうです
宜しくお願いします
Re: C++の回文のプログラミングが八方塞がりです。助けて下さい
Posted: 2014年10月05日(日) 13:31
by みけCAT
star さんが書きました:どうすればYタイプでループになるでしょうか?
ループしたい範囲のループにすればいいでしょう。
star さんが書きました:更に、a is a palindrome.と答えを一行にするにはどこを直せば良いのでしょうか?
入力も_getch()を使い、出力メッセージを修正すればいいでしょう。
コード:
#include <string>
#include <algorithm>
#include <iostream>
#include <conio.h>
#include <cctype>
using namespace std;
std::string read_one_line()
{
std::string out = "";
for (;;)
{
int input = _getch();
char buffer[4] = {};
if (input == '\n' || input == '\r') break;
buffer[0] = input;
std::cout << buffer;
out += buffer;
}
return out;
}
int main()
{
do
{
system("cls");
cout << "\nEnter a palindrome." << endl;
std::string str;
str = read_one_line();
if( equal(str.begin(), str.end(), str.rbegin()) )
std::cout << " is a palindrome.\n";
else
std::cout << " is not a palindrome.\n";
cout << "\nGo again (Y/N)?" << endl;
} while (toupper (_getch())!= 'N');
return 0;
}
Re: C++の回文のプログラミングが八方塞がりです。助けて下さい
Posted: 2014年10月05日(日) 13:35
by star
ありがとうございます!
これで入力すると、文章の入力後に削除が出来ず、 poor Dan is in a droop などの文章では動いてくれないのですがどうしてでしょうか?
大変失礼な質問で申し訳ありません
この二つに対応させる方法を教えて頂ければ幸いです
Re: C++の回文のプログラミングが八方塞がりです。助けて下さい
Posted: 2014年10月05日(日) 14:08
by かずま
これでいいですか?
コード:
#include <string>
#include <algorithm>
#include <iostream>
#include <conio.h>
#include <cctype>
using namespace std;
int main()
{
do {
system("cls");
cout << "\nEnter a palindrome." << endl;
string str;
getline(cin, str);
system("cls");
cout << "\nEnter a palindrome.\n" << str << " ";
if (equal(str.begin(), str.end(), str.rbegin()))
cout << "is a palindrome.\n";
else
cout << "is not a palindrome.\n";
cout << "\nGo again (Y/N)?" << endl;
} while (toupper(_getch()) != 'N');
return 0;
}
Re: C++の回文のプログラミングが八方塞がりです。助けて下さい
Posted: 2014年10月05日(日) 14:14
by star
ご返信有難うございます。
しかしながら
文章はやはり判別させられないのでしょうか?
何度も何度も恐縮です。
Re: C++の回文のプログラミングが八方塞がりです。助けて下さい
Posted: 2014年10月05日(日) 14:23
by star
コード:
#include <iostream>
#include <string>
using namespace std;
bool isPalindrome(string s);
int main()
{
string test;
cout << "Type in a word or phrase to see if it is a palindrome: ";
getline(cin, test);
if (isPalindrome(test))
cout << test << " : is a palindrome!" << endl;
else
cout << test << " : is not a palindrome!" << endl;
return 0;
}
bool isPalindrome(string s)
{
if (s.length() <=1)
return true;
return (s[0] == s[s.length() - 1] && isPalindrome(s.substr(1, s.length() - 2)));
}
このコードを同じように
Go again(Y/N)?Yとしてループさせられたら大変ありがたいです
更に、a is a palindrome.と答えを一行にするにはどこを直せば良いのでしょうか?
このコードだと文章にも単語にも恐らく対応しているのでこれが最後の質問になると思います
どうか宜しくお願いします。
Re: C++の回文のプログラミングが八方塞がりです。助けて下さい
Posted: 2014年10月05日(日) 14:29
by みけCAT
star さんが書きました:ご返信有難うございます。
しかしながら
文章はやはり判別させられないのでしょうか?
何度も何度も恐縮です。
こうでしょうか?(かずまさんのコードを改造しました)
コード:
#include <string>
#include <algorithm>
#include <iostream>
#include <conio.h>
#include <cctype>
using namespace std;
std::string convert_for_palindrome(const std::string& in) {
std::string out = "";
for (std::string::const_iterator it = in.begin(); it != in.end(); it++) {
if (!isblank(*it)) out += tolower(*it);
}
return out;
}
int main()
{
do {
system("cls");
cout << "\nEnter a palindrome." << endl;
string str, str2;
getline(cin, str);
system("cls");
cout << "\nEnter a palindrome.\n" << str << " ";
str2 = convert_for_palindrome(str);
if (equal(str2.begin(), str2.end(), str2.rbegin()))
cout << "is a palindrome.\n";
else
cout << "is not a palindrome.\n";
cout << "\nGo again (Y/N)?" << endl;
} while (toupper(_getch()) != 'N');
return 0;
}
Re: C++の回文のプログラミングが八方塞がりです。助けて下さい
Posted: 2014年10月05日(日) 14:33
by みけCAT
star さんが書きました:このコードを同じように
Go again(Y/N)?Yとしてループさせられたら大変ありがたいです
更に、a is a palindrome.と答えを一行にするにはどこを直せば良いのでしょうか?
ループさせるコードを加え、入力の処理と出力メッセージを変更しました。
コード:
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
std::string read_one_line()
{
std::string out = "";
for (;;)
{
int input = _getch();
char buffer[4] = {};
if (input == '\n' || input == '\r') break;
if(input == '\b')
{
if (out.size() > 0)
{
out.erase(out.end() - 1);
std::cout << "\b \b";
}
}
else
{
buffer[0] = input;
std::cout << buffer;
out += buffer;
}
}
return out;
}
bool isPalindrome(string s);
int the_main()
{
string test;
cout << "Type in a word or phrase to see if it is a palindrome: ";
test = read_one_line();
if (isPalindrome(test))
cout << " is a palindrome." << endl;
else
cout << " is not a palindrome." << endl;
return 0;
}
bool isPalindrome(string s)
{
if (s.length() <=1)
return true;
return (s[0] == s[s.length() - 1] && isPalindrome(s.substr(1, s.length() - 2)));
}
int main() {
do
{
the_main();
cout << "\nGo again (Y/N)?" << endl;
} while (toupper(_getch()) != 'N');
return 0;
}
Re: C++の回文のプログラミングが八方塞がりです。助けて下さい
Posted: 2014年10月05日(日) 14:46
by star
>>>>みけCATさん
こうでしょうか?(かずまさんのコードを改造しました)
isblankの定義コードを教えて頂ければ幸いです
Re: C++の回文のプログラミングが八方塞がりです。助けて下さい
Posted: 2014年10月05日(日) 14:50
by みけCAT
star さんが書きました:このコードだと文章にも単語にも恐らく対応しているのでこれが最後の質問になると思います
自分の記憶または掲示板のデータベースのいずれかがおかしくなったようで、
これに対する質問を書き込んだつもりが書き込まれていないようなので、再度書き込みます。
"poor Dan is in a droop"という入力をNo: 6のコードに引用符なしで入力すると、
コード:
poor Dan is in a droop : is not a palindrome!
という出力が出ました。
これで正しければ、自分のNo: 7のコードは間違っていることになりますが、本当にこれでいいのでしょうか?
オフトピック
さらに、ループのみを追加し、出力の形式が異なるコードも投稿したはずなのですが…不思議ですね。
Re: C++の回文のプログラミングが八方塞がりです。助けて下さい
Posted: 2014年10月05日(日) 14:57
by star
ご尽力感謝いたします
ですがごめんなさい、まるで理由に見当が付きません
何故文章だとダメなのでしょうか?
Re: C++の回文のプログラミングが八方塞がりです。助けて下さい
Posted: 2014年10月05日(日) 14:59
by みけCAT
star さんが書きました:何故文章だとダメなのでしょうか?
まず、starさんがこのプログラムで判定したい「回文」とは何ですか?
厳密な定義を教えてください。
Re: C++の回文のプログラミングが八方塞がりです。助けて下さい
Posted: 2014年10月05日(日) 15:02
by みけCAT
star さんが書きました:isblankの定義コードを教えて頂ければ幸いです
調べたけどよくわからなかったので、代わりにisblank関数を使わないコードを用意しました。
コード:
#include <string>
#include <algorithm>
#include <iostream>
#include <conio.h>
#include <cctype>
using namespace std;
bool is_blank(int num)
{
return num == ' ' || num == '\t';
}
std::string convert_for_palindrome(const std::string& in) {
std::string out = "";
for (std::string::const_iterator it = in.begin(); it != in.end(); it++) {
if (!is_blank(*it)) out += tolower(*it);
}
return out;
}
int main()
{
do {
system("cls");
cout << "\nEnter a palindrome." << endl;
string str, str2;
getline(cin, str);
system("cls");
cout << "\nEnter a palindrome.\n" << str << " ";
str2 = convert_for_palindrome(str);
if (equal(str2.begin(), str2.end(), str2.rbegin()))
cout << "is a palindrome.\n";
else
cout << "is not a palindrome.\n";
cout << "\nGo again (Y/N)?" << endl;
} while (toupper(_getch()) != 'N');
return 0;
}
Re: C++の回文のプログラミングが八方塞がりです。助けて下さい
Posted: 2014年10月05日(日) 15:10
by star
みけCAT さんが書きました:star さんが書きました:何故文章だとダメなのでしょうか?
まず、starさんがこのプログラムで判定したい「回文」とは何ですか?
厳密な定義を教えてください。
mom,dad, civic, "poor Dan is in a droop", "a man a plan a canal, Panama", 101, 2002等の
前後両方から同じ読み方が出来る単語、数字、若しくは文章だそうです
そしてこのプログラムでは回文, Palindrome だった時に
a man a plan a canal, Panama is a palindromeと
違う時には
ass is not a palindromeと表示されるそうです
Re: C++の回文のプログラミングが八方塞がりです。助けて下さい
Posted: 2014年10月05日(日) 15:12
by みけCAT
star さんが書きました:みけCAT さんが書きました:star さんが書きました:何故文章だとダメなのでしょうか?
まず、starさんがこのプログラムで判定したい「回文」とは何ですか?
厳密な定義を教えてください。
mom,dad, civic, poor Dan is in a droop, a man a plan a canal, Panama, 101, 2002等の
前後両方から同じ読み方が出来る単語、数字、若しくは文章だそうです
なるほど、Panamaなども回文ですか。
そうすると、今までの単純な文字列処理では判定できなさそうなので、考え方をガラっと変える必要がありそうです。
返信を書いているうちに修正されていました。
空白だけでなく、カンマやピリオドも取り除いて判定する感じでしょうか?
文字列処理で判定したいばあいは、文字列レベルでの「厳密な」定義を示してください。
また、任意の文字列ではなく、「単語、数字、若しくは文章」として成立する文字列でないといけないのでしょうか?
英語だけでなく、日本語、ロシア語、そのたあらゆる言語に対応しないといけないのでしょうか?
Re: C++の回文のプログラミングが八方塞がりです。助けて下さい
Posted: 2014年10月05日(日) 15:17
by かずま
これでいいですか?
コード:
#include <string>
#include <algorithm>
#include <iostream>
#include <conio.h>
#include <cctype>
#include <iterator>
using namespace std;
bool isPalindrome(const string& str)
{
if (str.length() <= 1) return true;
string s;
copy_if(str.begin(), str.end(), back_inserter(s), isalnum);
transform(s.begin(), s.end(), s.begin(), toupper);
return (s[0] == s[s.length() - 1] && isPalindrome(s.substr(1, s.length() - 2)));
}
int main()
{
do {
system("cls");
cout << "\nEnter a palindrome." << endl;
string str;
getline(cin, str);
system("cls");
cout << "\nEnter a palindrome.\n" << str << " ";
if (isPalindrome(str))
cout << "is a palindrome.\n";
else
cout << "is not a palindrome.\n";
cout << "\nGo again (Y/N)?" << endl;
} while (toupper(_getch()) != 'N');
return 0;
}
Re: C++の回文のプログラミングが八方塞がりです。助けて下さい
Posted: 2014年10月05日(日) 15:21
by みけCAT
みけCAT さんが書きました:文字列処理で判定したいばあいは、文字列レベルでの「厳密な」定義を示してください。
例
・空文字列は回文
・任意の半角英数字1文字からなる文字列は回文
・回文の前後にそれぞれ同じ半角英字(大文字と小文字の違いは無視する)または半角数字1文字をつなげた文字列は回文
・回文の前または後に半角英数字以外の任意の半角の1文字をつなげた文字列は回文
・これらの条件にあてはまらない文字列は回文ではない
Re: C++の回文のプログラミングが八方塞がりです。助けて下さい
Posted: 2014年10月05日(日) 17:44
by かずま
かずま さんが書きました:これでいいですか?
コード:
bool isPalindrome(const string& str)
{
if (str.length() <= 1) return true;
string s;
copy_if(str.begin(), str.end(), back_inserter(s), isalnum);
transform(s.begin(), s.end(), s.begin(), toupper);
return (s[0] == s[s.length() - 1] && isPalindrome(s.substr(1, s.length() - 2)));
}
あまりにも非効率的なので、最後の return文を次のように変更してください。
コード:
for (size_t i = 0, j = s.size(); i < j--; i++)
if (s[i] != s[j]) return false;
return true;
Re: C++の回文のプログラミングが八方塞がりです。助けて下さい
Posted: 2014年10月05日(日) 22:12
by star
かずまさん、みけCATさん
みなさん、長時間有難うございました。
この最後のコードのおかげで無事に解決しました。
本当に感謝します。