Recent exchanges with senior subjects, especially these two days focused on the practice. Dynamic allocation of storage space and input and output files is undoubtedly very important.
Here are some examples of C + + that are borrowed today:
Simulation of dynamic memory
structmemo{intstart; intfinish; Memo*next;};voidArrange (memo*Memo) {Memo* Inspos = head, *curpos, *Minpos; while(Inspos->next! = NULL) {//Direct Insert SortCurPos = Minpos =Inspos; while(Curpos->next = NULL) {//Find the node with the lowest starting address if(curpos->next->start<minpos->next->start) Minpos=CurPos; CurPos= CurPosNext; } //Insert Minimum nodeCurPos = MinposNext; MinposNext = CurPosNext; CurPosNext = InsposNext; InsposNext =CurPos; Intpos= InsposNext; } //merging contiguous blocks of memory for(CurPos = head, next; CurPos, next!=null; curpos = CurPosnext) { //The current block is contiguous with the next one, performing the merge while(Curpos->finish +1= = Curpos->next->start) {Minpos=curpos->Next; CurPos->finish=minpos->finish; CurPos->next=minpos->Next; DeleteMinpos; if(Curpos->next==null)return; }}
Another very useful program: count the frequency of words used in a file
#include <iostream>#include<iomanip>#include<fstream>using namespacestd; structword{Chardata[ -]; intcount;}; voidGetword (Ifstream &FP,CharCh[]);//read a word from a file voidDoubleSpace (word* &list,int&size);//Expand Array Space intMain () {Ifstreaminch("Txt.txt"); Charch[ -]; intSize =Ten, Len =0, I, J; Word* result =Newword[Ten]; if(!inch) {cout <<"File Open Error"<<endl;return 1;} while(true){//read the file until the endGetword (inch, ch);//read in a word ch if(ch[0]==' /') Break; for(i =0; i < Len; ++i)if(strcmp (Result[i].data, ch) >=0) Break; if(I < len && strcmp (result[i].data, ch) = =0) {Result[i].count+=1; Coutinue; } if(Size = =len) doubleSpace (result, size); for(j = len++; j > i;--j) result[j] = result[j-1]; strcpy (Result[i].data, ch); Result[i].count=1; } for(i =0; i < Len; ++i) cout<< STW ( -) << elft << result[i].data << SETW (5) << Result[i].count <<Endl; inch. Close (); CIN>> ch[0]; return 0;}
About dynamic memory and input and output files