C++ Bc. 17 cpp: Porovnání verzí
Bez shrnutí editace |
Bez shrnutí editace |
||
| Řádek 1: | Řádek 1: | ||
#include <string> | |||
#include <iostream> | |||
#include <iomanip> | |||
#include <list> | |||
std::string::size_type | |||
hledej(const std::string& retezec, const std::string& podretezec); | |||
int main() | |||
{ | |||
using namespace std; | |||
string retezec = "aaa bbb ccc ddd"; | |||
list<string> seznam; | |||
seznam.push_back("aa"); | |||
seznam.push_back("a b"); | |||
seznam.push_back("ccc"); | |||
seznam.push_back("fff"); | |||
seznam.push_back("d"); | |||
cout << "retezec: " << retezec << "\n\n"; | |||
// implicitne jsou retezce strng zarovnavany vpravo | |||
cout.setf(ios_base::left, ios_base::adjustfield); | |||
for (list<string>::const_iterator | |||
i=seznam.begin(), e=seznam.end(); i!=e; ++i) | |||
{ | |||
string podretezec = *i; | |||
cout << setw(4) << podretezec << " "; | |||
string::size_type n = hledej(retezec, podretezec); | |||
if (n != string::npos) | |||
cout << n; | |||
else | |||
cout << "nenalezen"; | |||
cout << endl; | |||
} | |||
} | |||
std::string::size_type | |||
hledej(const std::string& retezec, const std::string& podretezec) | |||
{ | |||
using namespace std; | |||
if (podretezec.size() > retezec.size()) return string::npos; | |||
string::size_type N = retezec.size() - podretezec.size(); | |||
for (string::size_type i=0; i<=N; i++) | |||
{ | |||
bool nalezen = true; | |||
for (string::size_type j=0; j<podretezec.size(); j++) | |||
if (retezec[i+j] != podretezec[j]) | |||
{ | |||
nalezen = false; | |||
break; | |||
} | |||
if (nalezen) return i; | |||
} | |||
return string::npos; | |||
} | |||
retezec: aaa bbb ccc ddd | |||
aa 0 | |||
a b 2 | |||
ccc 8 | |||
fff nenalezen | |||
d 12 | |||
[ [[C plus plus Bc. 17 | Zpět]] ] | [ [[C plus plus Bc. 17 | Zpět]] ] | ||
Verze z 12. 3. 2006, 14:25
#include <string>
#include <iostream>
#include <iomanip>
#include <list>
std::string::size_type
hledej(const std::string& retezec, const std::string& podretezec);
int main()
{
using namespace std;
string retezec = "aaa bbb ccc ddd";
list<string> seznam;
seznam.push_back("aa");
seznam.push_back("a b");
seznam.push_back("ccc");
seznam.push_back("fff");
seznam.push_back("d");
cout << "retezec: " << retezec << "\n\n";
// implicitne jsou retezce strng zarovnavany vpravo
cout.setf(ios_base::left, ios_base::adjustfield);
for (list<string>::const_iterator
i=seznam.begin(), e=seznam.end(); i!=e; ++i)
{
string podretezec = *i;
cout << setw(4) << podretezec << " ";
string::size_type n = hledej(retezec, podretezec);
if (n != string::npos)
cout << n;
else
cout << "nenalezen";
cout << endl;
}
}
std::string::size_type
hledej(const std::string& retezec, const std::string& podretezec)
{
using namespace std;
if (podretezec.size() > retezec.size()) return string::npos;
string::size_type N = retezec.size() - podretezec.size();
for (string::size_type i=0; i<=N; i++)
{
bool nalezen = true;
for (string::size_type j=0; j<podretezec.size(); j++)
if (retezec[i+j] != podretezec[j])
{
nalezen = false;
break;
}
if (nalezen) return i;
}
return string::npos;
}
retezec: aaa bbb ccc ddd aa 0 a b 2 ccc 8 fff nenalezen d 12
[ Zpět ]