//Ciekawe napisy - 2014 R
//autor: Marek Galaszewski, I LO Suwalki
#include <algorithm>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
const int COUNT = 1000; // liczba napisów w pliku
vector<string> t; // kontener do przechowywania napisów
ofstream out;
ifstream in;
bool isPrime(int liczba)
{
if ((liczba < 2) || (liczba % 2 == 0)) return false;
for (int i = 3; i * i <= liczba; i += 2)
if (liczba % i == 0) return false;
return true;
}
int countPrime()
{
int count = 0; // licznik napisów pierwszych
for (int i = 0; i < t.size(); i++)
{
int suma = 0;
for (int j = 0; j < t[i].size(); j++)
suma +=t[i][j]; // obliczanie sumy kodów ASCII
count += isPrime(suma); // niejawna konwersja z bool na int
}
return count;
}
void doTabl()
{
in.open("NAPIS.TXT");
string s;
for(int i = 0; i < COUNT; i++)
{
in >> s;
t.push_back(s);
}
in.close();
}
void taskA()
{
out << "a) " << countPrime() << endl << endl;
}
vector<string> tablRosn()
{
vector<string> tablRosnP; //tablica pomocnicza
for(int i = 0; i < t.size(); i++)
{
bool rosnacy = true;
for(int j = 0; j < t[i].size() - 1; j++)
if(t[i][j] >= t[i][j+1])
{
rosnacy = false;
break;
}
if (rosnacy) tablRosnP.push_back(t[i]);
}
return tablRosnP;
}
void taskB()
{
vector<string> tablRosnP = tablRosn();
out << "b)" << endl;
for (int i = 0; i < tablRosnP.size(); i++)
out << tablRosnP[i] << endl;
out << endl;
}
vector<string> tablDupl()
{
vector<string> d;
for (int i = 0; i < t.size(); i++)
{
// funkcja count() liczy ilosc wystapien t[i] w tablicy t
int licznik = count(t.begin(), t.end(), t[i]);
// funkcja find() znajduje pierwsze wystąpienie wartości t[i] w tablicy d
// i zwraca iterator do niej, lub jeśli nie zostanie znaleziona, zwraca iterator end()
// jesli ilosc wystapien t[i] jest > 1 i t[i] nie ma jeszcze w tablicy d:
if ((licznik > 1) && (find(d.begin(),d.end(), t[i]) == d.end()))
d.push_back(t[i]);
}
return d;
}
void taskC()
{
vector<string> d = tablDupl();
out << "c)" << endl;
for (int i = 0; i < d.size(); i++)
out << d[i] << endl;
out << endl;
}
int main()
{
doTabl();
out.open("ZADANIE5.TXT");
taskA();
taskB();
taskC();
out.close();
return 0;
}