SHOW:
|
|
- or go back to the newest paste.
| 1 | #include <iostream> | |
| 2 | #include <fstream> | |
| 3 | #include <conio.h> | |
| 4 | using namespace std; | |
| 5 | ||
| 6 | class symbollist{
| |
| 7 | //Variables | |
| 8 | char val;//собственно, буква | |
| 9 | long long int freq;//сколько раз повторяется | |
| 10 | symbollist *next;//указатель на следующий элемент | |
| 11 | ||
| 12 | //Methods | |
| 13 | public: | |
| 14 | symbollist(){
| |
| 15 | val=0; | |
| 16 | freq=0; | |
| 17 | next=NULL; | |
| 18 | } | |
| 19 | ||
| 20 | symbollist *add(char c){//создает новый элемент в списке
| |
| 21 | symbollist *current=this, *t=new symbollist; | |
| 22 | t->val=c;//записываем новый символ | |
| 23 | while(current->next!=NULL) | |
| 24 | current=current->next; | |
| 25 | current->next=t; | |
| 26 | t->next=NULL; | |
| 27 | return t; | |
| 28 | } | |
| 29 | ||
| 30 | void increase(char c){//ищем символ в списке и увеличиваем freq
| |
| 31 | symbollist *current=this; | |
| 32 | while(current!=NULL){
| |
| 33 | if(c==current->val){
| |
| 34 | current->freq++; | |
| 35 | return ; | |
| 36 | } | |
| 37 | current=current->next; | |
| 38 | }//если не нашли элемент и не ушли из ф-ции при return ; | |
| 39 | this->add(c)->freq++;//создаем новый эл-т | |
| 40 | } | |
| 41 | ||
| 42 | int slength(){//считает кол-во зафиксированных символов
| |
| 43 | symbollist *current=this; | |
| 44 | int i=1; | |
| 45 | while(current->next!=NULL){
| |
| 46 | i++; | |
| 47 | current=current->next; | |
| 48 | } | |
| 49 | return i; | |
| 50 | } | |
| 51 | ||
| 52 | symbollist *operator[](int a){//пока нигде не используется, но вернет указатель на n-ный элемент при записи L[n]
| |
| 53 | symbollist *current=this; | |
| 54 | for(int i=0;i<a;i++) | |
| 55 | if(current->next!=NULL)//если n>кол-во эл-тов то вернем указатель на последний элемент | |
| 56 | current=current->next; | |
| 57 | return current; | |
| 58 | } | |
| 59 | ||
| 60 | void swap(symbollist *current){
| |
| 61 | std::swap(current->freq, current->next->freq); | |
| 62 | std::swap(current->val, current->next->val); | |
| 63 | } | |
| 64 | ||
| 65 | void sort(){//сортировка пузырьком
| |
| 66 | symbollist *current=this, *subptr=this; | |
| 67 | while(current->next!=NULL){
| |
| 68 | subptr=this; | |
| 69 | while(subptr->next!=NULL){
| |
| 70 | if((subptr->freq) < (subptr->next->freq)) | |
| 71 | this->swap(subptr); | |
| 72 | subptr=subptr->next; | |
| 73 | } | |
| 74 | current=current->next; | |
| 75 | } | |
| 76 | } | |
| 77 | ||
| 78 | void printlist(){
| |
| 79 | symbollist *current=this; | |
| 80 | while(current->next!=NULL){
| |
| 81 | cout<<current->val<<" : "<<current->freq<<endl; | |
| 82 | current=current->next; | |
| 83 | } | |
| 84 | } | |
| 85 | }; | |
| 86 | ||
| 87 | int main(int argc, char **argv){
| |
| 88 | symbollist *L=new symbollist; | |
| 89 | ifstream input; | |
| 90 | - | input.open(argv[1], ifstream::binary);//вместо имени файла argv[1] чтобы можно было дропнуть любой файл на этот exeшник и не парится с воодом пути и имени |
| 90 | + | input.open("input.txt", ifstream::binary);//вместо имени файла argv[1] чтобы можно было дропнуть любой файл на этот exeшник и не парится с воодом пути и имени
|
| 91 | char c; | |
| 92 | while(!input.eof()){
| |
| 93 | input.get(c); | |
| 94 | L->increase(c); | |
| 95 | } | |
| 96 | L->sort(); | |
| 97 | L->printlist(); | |
| 98 | getch(); | |
| 99 | return 0; | |
| 100 | } |