Skip to content

Instantly share code, notes, and snippets.

Created May 11, 2016 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/be83d40789e43b6ee360fa6f27b02aed to your computer and use it in GitHub Desktop.
Save anonymous/be83d40789e43b6ee360fa6f27b02aed to your computer and use it in GitHub Desktop.
#include"Tekst.h"
int main()
{
Tekst T("test2");
T.Analyze();
T.Sort();
T.Save();
system("pause");
return 0;
}
#include "Tekst.h"
Tekst::Tekst(std::wstring name):name_of_file(name)
{
wchar_t z = 'a';
for (int i = 0; i < size; i++, z++)
{
Litera.first = z;
Litera.second = 0;
Alfabet.push_back(Litera);
}
}
Tekst::~Tekst()
{
}
void Tekst::Analyze()
{
ifstream file;
file.imbue(locale("Polish_poland.852"));
wstring namefile = name_of_file + ".txt";
file.open(namefile.c_str());
if (!file.is_open())
{
cout << "Nie udalo sie otworzyc pliku o nazwie: " << name_of_file << endl;
system("pause");
exit(EXIT_FAILURE);
}
wchar_t znak;
while (file >> znak)
{
if (isalpha(znak))
{
znak = tolower(znak);
Alfabet[znak - 97].second++;
}
}
}
void Tekst::Sort()
{
sort(Alfabet.begin(), Alfabet.end(),Compare);
}
void Tekst::Show()
{
for (auto i : Alfabet)
cout << i.first << " " << i.second << endl;
}
void Tekst::Save()
{
ofstream file;
string name = name_of_file + " - analiza.txt";
file.open(name.c_str());
if (!file.is_open())
{
cout << "Nie udalo sie otworzyc pliku o nazwie: " << name_of_file << endl;
cout << "Byla to proba zapisu do pliku" << endl;
system("pause");
exit(EXIT_FAILURE);
}
for (auto i : Alfabet)
{
file << i.first << " " << i.second << endl;
}
}
#pragma once
#include<iostream>
#include<fstream>
#include<string>
#include<cctype>
#include<utility>
#include<vector>
#include<algorithm>
#include<locale>
using namespace std;
class Tekst
{
wstring name_of_file;
enum
{
size = 26
};
struct compare
{
bool operator()(pair<wchar_t, int> a, pair<wchar_t, int> b)
{
return a.second > b.second;
}
}Compare;
pair<wchar_t, int> Litera;
vector<pair<wchar_t, int>> Alfabet;
public:
Tekst() {};
Tekst(wstring name);
~Tekst();
void Analyze();
void Sort();
void Show();
void Save();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment