Results 1 to 2 of 2
Hybrid View
-
10th December 2013 16:44 #1
:
:
1. .
2.
3. .
, .
. :
. .Code:#include<iostream> #include<fstream> #include<string> #include<cstdlib> #include<ctime> using namespace std; int br_both=0; const int N=20; struct opashka{ int key; opashka *first; opashka *next; opashka *last; } *start, *first = NULL, *last = NULL; void shellsort (opashka *first, int n); void push_opashka(int a) //dobavqne na elementite v opashkata { opashka *p=last; last=new opashka; last->key=a; last->next=NULL; if (p!=NULL) { p->next=last;} if (first==NULL) {first=last;} } int pop_opashka(int n) //Izvlichane na element ot opashka { if (first) { n=first->key; opashka *p=first; first=first->next; delete p; return 1; } else return 0; } void Vavejdane_file() //chete i popylva spisaka ot fail { int a; ifstream file_1; file_1.open("file1.txt",ios::in); //otvarq faila za chetene if(file_1!=NULL) //proverka za prazen fail { while(!file_1.eof()) // chete dokato stigne kraiq mu { file_1>>a; push_opashka(a); //prochetenata stoinost se predawa na a } file_1.close(); cout<<endl<<" Faila e vaveden."<<endl<<endl; } else //pri prazen fail ili greshka pri otvarqne cout<<"Greshka pri otvarqne na fila! Molq vavedete otnovo ili syzdaite avtomatichno fail"<<endl; } void Suzdavane_file() // Syzdava fail s random chisla ako nqma gotov fail { int chislo = 0; srand(time(NULL)); ofstream file; file.open("file1.txt"); if (file.is_open()) { for(int i=0;i<N;i++) //Dobavqne na random chislata v faila { chislo = rand()%200; //random chislata sa v interval 0-199 file << chislo << endl; } file.close(); } else cout << "Faila ne moje da bude otvoren"; } void Pisane_file() //pishe vav fail { opashka *temp=first; //ukazatel ot tipa struktura ofstream file_2; if(first) // pishe samo ako opashkata ne prazna { file_2.open("2.txt",ios::out); // otvarq fajla za pisane, kato go trie ako e sashtestvuval if(file_2) { while(temp->next) // varti elementite dokato stigne predposledniq { file_2<<temp->key<<"\n"; // pishe vav faila stoin. na elementa + nov red temp=temp->next; // mesti anonsa vyrhu sledwashtiq element } file_2<<temp->key; //za posledniq element file_2.close(); cout<<endl<<" Uspeshno syhranenen fail 2.txt !"<<endl<<endl; } else cout<<endl<<endl<<"Ne moje da se otvori fail 2.txt"<<endl<<endl; } else //ako e prazen spisaka vadi saobshtenie { cout<<endl<<endl<<"Niama nishto za pisane!"<<endl; cout<<endl<<"Molq zaredete vhodniq fail."<<endl<<endl; } } int find_num(opashka *st, int n) // n- nomera na koi element trqbva da stigne, vrashta stoinosta na tozi element { int br=0; while(br!=n) { st=st->first; br++; } return st->key; } void replace_num(opashka *first, int n, int m) //smenq dvete stoinostti na elementite { opashka *p1=first ,*p2=first; int br1=0,br2=0,tmp; while(br1!=n) { br1++; p1=p1->next; } while(br2!=m) { br2++; p2=p2->next; } tmp=p1->key; p1->key=p2->key; p2->key=tmp; } int count_opashka(opashka *first)//broi kolko elementa ima opashkata { int count_br=0; while(first) { count_br++; first=first->next; } return count_br; } void call_shell_sort(opashka *first) //s otdelna funkciq zashtoto shell-a iska broq na elementite,koito sa ot drugata funkciq { cout<<"\n Sorting is ready "<<endl; int n=count_opashka(first); shellsort(first,n); } void replace_elem(opashka *&first,int b, int r) //prisvoqva na nomera stoinost { opashka *p=first; int i=0; while(i!=b) { p=p->next; i++; } p->key=r; //r - stoinostta koqto se slaga } void shellsort (opashka *first, int n) { int h, i, j, k,g; for (h = n; h /= 2;) { for (i = h; i < n; i++) { k = find_num(first,i); for (j = i; j >= h && k <find_num(first,(j-h)); j -= h) { g=find_num(first,(j-h)); replace_elem(first,j,g); br_both ++; } replace_elem(first,j,k); } } } void Display_opashka() // pokazwa sururjanieto na opashkata { for (opashka *p = first; p; p = p->next) { cout << p->key << " "; } } int main(int argc, char *argv[]) { int izbor,z=0; do { if(z>0) cout<<endl<<" Molq vavedete korekten izbor !!!"<<endl<<endl; cout<<"<#><#><#><#><# MENU <#><#><#><#><#>"<<endl; cout<<"<#><#><#><#><#> <#><#><#><#><#><#><#> <#>"<<endl; cout<<"<#><#> <#><#>"<<endl; cout<<"<#><#> <#><#>"<<endl; cout<<"<#><#> [1] - Vavedi vhodqshtiq fail v programata ( 1.txt ) <#><#>"<<endl; cout<<"<#><#> [2] - Syzdaifail sys sluchaini chisla <#><#>"<<endl; cout<<"<#><#> [3] - Zapishi tekushto sastoqnie v izhodqsht fail <#><#>"<<endl; cout<<"<#><#> [4] - Izvejdane na opashkata <#><#>"<<endl; cout<<"<#><#> [5] - Shell sort <#><#>"<<endl; cout<<"<#><#>[*] - <#><#>"<<endl; cout<<"<#><#>[*] - <#><#>"<<endl; cout<<"<#><#> [0] - Izhod <#><#>"<<endl; cout<<"<#><#> <#><#>"<<endl; cout<<"<#><#> ---------------------------------------------------- <#><#>"<<endl; cout<<endl<<" Vashiqt izbor: "; cin>>izbor; z++; }while(izbor<0 || izbor>=6); cout<<endl; z=0; opashka *temp=first; switch (izbor) { case 0: exit(0); break; case 1: Vavejdane_file(); return 1; break; case 2: Suzdavane_file(); return 1; break; case 3: Pisane_file(); cout<<endl; return 1; break; case 4: Display_opashka(); cout<<endl; return 1; break; case 5: call_shell_sort(first); cout<<endl; return 1; break; } }
|MSI B550-A PRO|Ryzen 7 5800X@4700Mhz all Core+ Liquid Freezer II 280 |VGA:RX 6800 PowerColor |Crucial 4x8 Gb-3600 Mhz 18-22-22-42|Adata XPG 8200 Pro + WD Black 1Tb|FD Define R5-Black|bequiet! Straight power-10 750W Gold| PHL-276E8V | *
-
12th December 2013 10:37 #2
find_num
, first last / !/ - , .Code:int find_num(opashka *st, int n) // n- nomera na koi element trqbva da stigne, vrashta stoinosta na tozi element { int br=0; while(br!=n) { st=st->next; br++; } return st->key; }Last edited by hateras; 12th December 2013 at 11:30.
ASRock B550M Pro 4; Ryzen R5 3600; 2x16 GiB G.SKILL Aegis 3200; 1TB Samsung QVO 960 + 3TB Seagate IronWolf; Zalman Z1




Reply With Quote
Lenovo ThinkPad 15 IdeaPad 15
5th May 2023, 22:16 in