2016-12-02 00:24:12
The use of abstract data types to implement the array, mainly using C + + template to implement arrays class, the realization of a complete C + + class writing, can be written out of a few people, especially for the copy constructor and overloaded = [] operation, all need a solid basic attack. Then the output of the prime number sequence is cleverly implemented in the test program, which is not the same as the general method of solving the prime number.
//#include "Array.h"#include <stdio.h>Const intDefaultSize = -; template<classType>classarray{Private: Type*elements;//space for array storage intArraySize;//Current Length Public: Array (intSize =defaultsize); Array (Constarray<type>& x);//copy Constructor~array () {Delete[] elements;} Array<Type>&operator=(Constarray<type>& RHS);//Array Replicationtype&operator[] (inti);//take element value intLength ()Const{returnArraySize;}; voidReSize (intsz);}; Template<classType>Array<Type>& Array<type>::operator=(Constarray<type>&RHS) { intn = rhs. ArraySize;//array size to take RHS if(arraysize!=N) {Delete[] elements; Elements=NewType[n];//re-allocating memory for n elements if(elements==NULL) {ArraySize=0; Cerr<<"Storage Allocation Error! "<<Endl; Exit (1); } ArraySize=N; } //copy elements from RHS to this objectType *destptr =elements; Type*srcptr =rhs.elements; while(n--) { *destptr++ = *srcptr++; } return* This;} Template<classType>Array<type>::array (intSZ) { if(SZ <=0) {ArraySize=0; Cerr<<"illegal array size"<<Endl; return; } Elements=NewType[sz]; if(elements==NULL) {ArraySize=0; Cerr<<"Storage Allocation Error! "<<Endl; Exit (1); } ArraySize=sz;} Template<classType>Array<type>::array (Constarray<type>&x) { intn =x.arraysize; ArraySize=N; Elements=NewType[n]; if(elements==NULL) {ArraySize=0; Cerr<<"Storage Allocation Error! "<<Endl; Exit (1); } Type* Srcptr =x.elements; Type* Destptr =elements; while(n--) { *destptr++ = *srcptr++; }}template<classType>Type& Array<type>::operator[] (inti) { if(i<0|| i>arraysize-1) {Cerr<<"array subscript Hyper bounds"<<Endl; Exit (1); } returnelements[i];} Template<classType>voidArray<type>::resize (intSZ) { if(sz>=0&&sz!=ArraySize) {Type*newarray =NewType[sz]; if(newarray==NULL) {Cerr<<"memory allocation failed! "<<Endl; return; } intn = (sz <= ArraySize)? Sz:arraysize;//determine the number of transmitted data according to the new sizeType*srcptr =elements; Type*destptr =NewArray; while(n--) { *destptr++ = *srcptr++; } Delete[] elements; Elements=NewArray; ArraySize=sz; }} #include<iostream>#include<iomanip>using namespacestd;intMainintargcChar**argv) {Array<int> A (Ten);//array output prime number sequence intN; intPrimecount =0, I, J; cout<<"Enter a value>=2 as upper limit for prime numbers:"; CIN>>N; A[primecount++] =2;//2 is prime number for(inti =3; I < n;i++) { if(primecount==a.length ()) {A.resize (Primecount+Ten); } if(i%2==0)//even skip { Continue; } J=3; while(j<=i/2&&i%j!=0) {J+=2; } if(j>i/2) {A[primecount++] =i; } } for(inti =0; I < primecount;i++) {cout<< SETW (5) <<A[i]; if((i+1)%Ten==0) {cout<<Endl; }} cout<<Endl; return 0;}
Exit (0): Run the program normally and exit the program;
Exit (1): Abnormal operation results in exiting the program;
Return (): Returns a function that, if in the main function, exits the function and returns a value.
Detailed said:
1. Return returns the function value, which is the keyword; exit is a function.
2. Return is a language level that represents the return of the call stack, and exit is the system call level, which represents the end of a process.
3. Return is the exit of the function (return); Exit is the exit of the process.
4. Return is provided by the C language, and exit is provided by the operating system (or given in the function library). Exit is a library function, exit (1) indicates that the program exits when an error occurs, and exit (0) indicates a graceful exit. In Stdlib.h, the Exit function is defined in this way: void exit (int status). This system call is used to terminate a process, regardless of where in the program, as long as the exit is executed, the process will run from the end of the process. Referring to exit this system call, it is necessary to mention another system call, _exit,_exit () function is located in Unistd.h, compared to exit (), the function of the _exit () is the simplest, directly terminates the process of running, releasing its memory space used, and destroys the data structure in memory, and exit () is to check the state of the file before the process exits, and write the contents of the file buffer back to the file.
5. Return is used to end the execution of a function, to pass the execution information of the function to other calling functions; the exit function exits the application, deletes the memory space used by the process, and returns a state of the application to the OS or its parent process, which identifies some of the application's operational information. This information is related to the machine and the operating system, usually 0 for normal exit, not 0 for abnormal exit.
6. Calling return and exit in a non-main function is obvious, but the phenomenon of calling return and exit in the main function is blurred, and in most cases the phenomenon is consistent.
The beauty of the algorithm--2.2 Array