I. Conversion of reference parameters and pointers
Standard C does not support reference parameters, which need to be converted. The following is an example of the Destroytriplet () function in Bo1-1.cpp and bo1-1.c to illustrate this conversion.
The functions that contain reference parameters in Bo1-1.cpp are as follows:
1 Status destroytriplet (Triplet &T) {2 // operation result: Ternary T is destroyed 3 free (T); 4 t=NULL; 5 return OK; 6 }
The standard C program in bo1-1.c after conversion is as follows:
1 /* */2/* operation result: Ternary T is destroyed */3Free/ * */ 4/* */5 return OK; 6 }
Compared with the above 2 functions can be seen: the C + + function parameter list in the beginning of the parameters of the & to take the first parameter, and then in the function of the parameter before adding * can.
It is important to note that in these two functions, the types of formal parameters are different. In Bo1-1.cpp, the type of T is triplet; in bo1-1.c, the type of T is a pointer to triplet.
For convenience, however, the comments in the C program are not rewritten. In addition, the function is called in a standard C program, and A & should be added before the argument.
The statement called Destroytriplet () in Main1-1.cpp is
1 destroytriplet (T);
The corresponding standard C program main1-1.c calls the Destroytriplet () statement as
1 destroytriplet (&t);
Where the two arguments of the Destroytriplet () are the same, the two argument T types are the same.
In addition, in the conversion process, encountered &* or *& can be "offset", will be *&t converted to T.
Standard C when specifying a struct or enumeration type that is defined, add STRUTC or enum before the type, and C + + does not have to add
As defined in C + + C2-1.h, the struct sqlist is as follows:
1 struct sqlist {2 // Storage space Base 3 int // Current Length 4 int // Current allocated storage capacity in sizeof (Elemtype) 5 };
C + + when specifying the type of a variable or formal parameter, just use SqList. As in Bo2-1.cpp, a function is as follows:
1 int 2 // initial conditions: Sequential linear table L already exists. Operation Result: Returns the number of data elements in L 3 return l.length; 4 }
If the type of the variable L is defined in a standard C program like C2-1.h C + +, the function in bo2-1.c should be as follows:
1 int Listlength (struct sqlist L) {2/ * initial condition: sequential linear table L already exists. Operation Result: Returns the number of data elements in L */3 return l.length; 4 }
Use struct sqlist to describe the variable L. When a struct is used to add structs to its type, it is cumbersome to use an enum type in front of its type.
For convenience, a typedef can be used to define the type. The sqlist is defined in the c2-1.h of the standard C program as follows:
1 struct {2 /* Storage space Base */ 3 int /* Current Length */ 4 int /* */5 }sqlist;
In this way, the corresponding function in the bo2-1.c is
1 int 2 /* Initial conditions: Sequential linear table L already exists. Operation Result: Returns the number of data elements in L */3 return l.length; 4 }
This function is similar to bo2-1.cpp. It can be seen that whenever a typedef is used when defining a struct, there is no need to add a struct when specifying the type of the struct body.
Standard C uses this method to define the struct body. This is similar to using typedef when defining enum types.
Third, the common body of standard C must have variable names, and C + + can omit
As defined in C5-6.h (c + +), GLNode1 is as follows:
1 //Extended linear list storage structure for c5-6.h generalized tables2 enumElemtag{atom,list};//atom==0: Atom, list==1: Child table3typedefstructGLNode1 {4Elemtag tag;//public part, used to differentiate between atomic nodes and table nodes .5Union//Joint parts of atomic nodes and table nodes6 {7Atomtype Atom;//the domain of atomic nodes8GLNode1 *hp;//Table header pointers for table nodes9 };TenGLNode1 *TP;//next, which is equivalent to the linear list, pointing to the next element node . One}*glist1,glnode1;//Generalized table Type GList1 is an extended linear list
Note: The shared body does not have a variable name. The function Glistempty () in Bo5-6.cpp is as follows:
12 // Initial conditions: Generalized table L exists. Operation Result: Determine if the generalized table L is empty 3 if(! l| | l->tag==list&&! l->hp)4 return OK; 5 Else 6 return ERROR; 7 }
The c5-6.h of standard C is as follows:
1 /*Extended linear list storage structure for c5-6.h generalized tables*/2typedefenum{Atom,list} Elemtag;/*atom==0: Atom, list==1: Child table*/3typedefstructGLNode1 {4Elemtag tag;/*public part, used to differentiate between atomic nodes and table nodes .*/5Union/*Joint parts of atomic nodes and table nodes*/6 {7Atomtype Atom;/*the domain of atomic nodes*/8 structGLNode1 *hp;/*Table header pointers for table nodes*/9 }a;Ten structGLNode1 *TP;/*next, which is equivalent to the linear list, pointing to the next element node .*/ One}*glist1,glnode1;/*Generalized table Type GList1 is an extended linear list*/
The internal shared body must have a variable name. The function Glistempty () in bo5-6.c also changes as follows:
12 /* Initial conditions: Generalized table L exists. Operation Result: Determine if the generalized table L is empty */3 if(! l| | l->tag==list&&! l->a.hp)4 return OK; 5 Else 6 return ERROR; 7 }
Four, C + + can be overloaded
That is, in a program, several functions with the same name can exist at the same time. As long as the number or type of their parameters is different.
Standard C cannot be overloaded. When C + + is converted to standard C, the function with the same name must be changed to a different name.
As in Bo9-2.cpp, there are 1 searchbst () Functions (algorithm 9.5 (b)), and there are 1 func9-1.cpp () Functions (algorithm Searchbst (a)) in the 9.5 of files included in Bo9-2.cpp, but the parameters of these two functions of the same name are different.
C + + is able to tell which function is called based on the number of parameters of the function. The standard C does not have this capability, so change the Searchbst () function in bo9-2.c to the SearchBST1 () function.
V. C + + allows variables to be defined before they are used in executing statements
such as the Printuser () function in Algo8-1.cpp:
1 voidPrintuser (Space p[]) {2 //allocated space as indicated by the output P array3 for(intI=0; i<max/e;i++)4 if(P[i])//the pointer is not 0 (point to a consuming block) {5printf"first address of block%d =%u", I,p[i]);//Output Node Information6printf"block Size =%d flag =%d (0: Idle 1: occupied)",p[i]->size,p[i]->tag);7printf"block Tail sign =%d\n", (Footloc (p[i))tag);8 }9}
This form is not allowed in the standard C language. The Printuser () function in the corresponding ALGO8-1.C is:
1 voidPrintuser (Space p[]) {2 /*allocated space as indicated by the output P array*/3 inti;4 for(i=0; i<max/e;i++)5 if(P[i])/*pointer is not 0 (point to an occupied block)*/ {6printf"first address of block%d =%u", I,p[i]);/*Output Node Information*/7printf"block Size =%d flag =%d (0: Idle 1: occupied)",p[i]->size,p[i]->tag);8printf"block Tail sign =%d\n", (Footloc (p[i))tag);9 }Ten}
Vi. C + + allows the form of a function to be used when converting a type
such as the Allocbuddy () function in Algo8-2.cpp, a statement is as follows:
1 pi=pa+int(POW (2, k-i));
In standard C program algo8-2.c, you must change to the following form (note the underlined part):
1 pi=pa+ (int) POW (2, k-i);
Vii. use new application space in C + +
A statement like the main function main () in Bo8-1.cpp is as follows:
1 p=new word[max+2// request space with size of max*sizeof (WORD) bytes
In the bo8-1.c of standard C, change to
1 p= (word*)malloc((max+2) *sizeof /* Request a space of size max*sizeof (WORD) bytes */
Viii. cout and CIN can be used in C + + for input and output statements
They have the advantage of not having to give a format character. This way, you do not have to modify the statement when the type of the variable changes. However, the main1-1.c must be replaced by
1 printf ("%d%d%d\n", t[0],t[1],t[2]); /* when the type of elemtype changes, the format character of printf () should be changed accordingly */
Nine, in C + + "//" After the end of the content of the bank as a comment, and standard C comments must be placed in "/*", "* *" between ten, the standard C program has an extension of. c,c++ program with the extension. cpp
A file that defines a data storage structure whose extension is. h, which cannot be mixed in two languages.
C and C + + format conversions