Experimental data structure, a

Title 1-- three ways of passing arguments to functions in languages ​​C / C ++

C language offers two ways of passing arguments to functions: pass pass address values. In C ++, it has expanded reference. Through this project, make sure have mastered the principles of the three ways to prepare for the follow-up study.

Exchange values ​​of two variables, please use one of three processes, respectively, to complete the preparation of the main program:

// (1) by value 
void MYSWAP (int X, Y int) 
{ 
    int T; 
    T = X; 
    X = Y; 
    Y = T; 
} 

// (2) pass the address 
void myswap (int * p1, int * p2 ) 
{ 
    int T; 
    T = * P1; 
    * P1 = * P2; 
    * P2 = T; 
} 

// (. 3) incorporated by parameter 
void MYSWAP (int & X, int & Y) 
{ 
    int T; 
    T = X; 
    X = Y; 
    Y = T; 
} 
int main () 
{ 
    int a, B; 
    the printf ( "Please enter the two integers to be exchanged:"); 
    Scanf ( "% D% D", & a, & B); 
    __________________; // three programs were written in the appropriate form on the call myswap 
    printf ( "the result of the swap function call is:% d and D% \ n-", a, B);  
    return 0;
}

  

Item 2 - multi-file organization

  Learning data structure, the goal is to prepare a program of considerable scale. All code in a file approach can not be applied at this stage of the demand.

  Through this project, confirmed that the ability to use multi-file organizational procedures. Convenient chapters later, we have a data structure defined algorithm library, a reference library of algorithms and can be practiced.

  The simplest multi-file organization, a project has three files:

   (1) .h header file: defined data types, declarations custom functions, macros defined

   (2) .cpp source document 1: used to implement custom function declared in the header file

   (3) .cpp source file 2: Define the main () function, used to call the relevant function, problem solving to achieve the goal.

head.h

. 1  #ifndef HEAD_H
 2  #define HEAD_H
 . 3  
. 4 #include <stdio.h>
 . 5  #define MaxStud 50       // number of students up to 50 
. 6  #define MaxCour 300 
 . 7  struct stud1
 . 8  {
 . 9      int NO;          // Science No. 
10      char name [ 10 ];   // name 
. 11      int BNO;         // class No. 
12 is  };
 13 is  struct stud2
 14  {
 15      intNO;          // Science No. 
16      int CNO;         // course code 
. 17      int deg;         // score 
18 is  };
 . 19  Double studavg ( struct stud2 S2 [], int m, int I);
 20 is  Double couravg ( struct stud2 S2 [] , int m, int I); 
 21 is  void allavg ( struct stud1 S1 [], int n-, struct stud2 S2 [], int m);
 22 is  #endif
View Code

head.cpp

#include " head.h " 
Double studavg ( struct stud2 S2 [], int m, int i)    // school student number average of i 
{
     int J, n-= 0 ;               // n-i is the number of school the number of students selected courses 
    Double SUM = 0 ;            // school students out number i 
    for (J = 0 ; J <m; J ++ )
         IF (S2 [J] == .no i)     // Science No. i when statistics 
        { 
            n- ++ ; 
            SUM + = S2 [J] .deg; 
        }
    return (SUM / n-); 
} 
Double couravg ( struct stud2 S2 [], int m, int i)    // find the number average of course i 
{
     int J, n-= 0 ;               // n-i is the number of number Coursetaking 
    Double SUM = 0 ;            // number i is out of the course 
    for (J = 0 ; J <m; J ++ ) 
    { 
        IF (S2 [J] == .cno i)    // program number i is statistics 
        { 
            n- ++ ; 
            SUM + = S2 [J] .deg;
        } 
    } 
    Return (SUM / n-); 
} 
void allavg ( struct stud1 S1 [], int n-, struct stud2 S2 [], int m)     // find average student and curriculum average 
{
     int I, J; 
    the printf ( " student average: \ n- " ); 
    the printf ( "   Science No. name average \ n- " ); 
    I = 0 ;
     the while (I < n-) 
    { 
        J = S1 [I] .no; 
        the printf ( " % 10s 4D%% g \ n" , S1 [I] .no, S1 [I] .name, studavg (S2, m, J)); 
        I ++ ; 
    } 
    the printf ( " Course Average: \ n- " );
     for (I = . 1 ; I <= . 6 ; I ++ ) 
        the printf ( " course D%:% G \ n- " , I, couravg (S2, m, I)); 
}
View Code

main.cpp

#include " head.h " 
int main () 
{ 
    int n-= . 7 ;         // number of students recording 
    int m = 21 is ;        // student performance Recording 
    struct stud1 S1 [MaxStud] = 
    { 
        { . 1 , " Bin " , 9901 }, 
        { 8 , " LIU " , 9902 }, 
        { 34 , " Ying " , 9901 }, 
        { 20, " Chen " , 9902 }, 
        { 12 , " Wang Qi " , 9901 }, 
        { 26 , " Dong strong " , 9902 }, 
        { 5 , " Wang Ping " , 9901 } 
    }; 
    struct stud2 S2 [MaxCour] =    // specified number of courses from 1-6, the same student achievement records stored in a row 
    { 
        { 1 , 1 , 67 }, 
        { 1 , 2 ,98},
        {1,4,65},
        {8,1,98},
        {8,3,90},
        {8,6,67},
        {34,2,56},
        {34,4,65},
        {34,6,77},
        {20,1,68},
        {20,2,92},
        {20,3,64},
        {12,4,76},
        {12,5,75},
        {12,6,78},
        {26,1,67},
        {26,5,78},
        {26,6,62},
        {5,1,94},
        {5,2,92},
        {5,6,89}
    };
    allavg(s1,n,s2,m);
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/likunhong/p/11581266.html