Differentiation: Function Definition ---> function prototype description ---> function call
Function nesting: one function calls another function, which is called a nested function call;
Recursive call of a function: A function calls itself directly or indirectly and becomes a recursive call of the function;
========================================================== ========================================================== ==========
A few small test questions:
1. Compile a program to calculate the Npower of X;
# Include <stdio. h>
Int NCI (int x, int N)
{
Int I;
Int A = 1;
For (I = 0; I <n; I ++)
A = A * X;
Return;
}
Int main (){
Int X, N, S;
S = NCI (2, 3 );
Printf ("x de n ci Fang is % d \ n", S );
}
2. Compile a function to calculate the sum of elements on the diagonal line;
# Include <stdio. h>
Int Xn (int A [3] [3])
{
Int S = 0, I, J;
For (I = 0; I <3; I ++ ){
For (j = 0; j <3; j ++)
If (I = J)
S = S + A [I] [J];
}
Return S;
}
Int main (){
Int sum;
Int A [3] [3] = {1, 2, 3}, {4, 5, 6}, {7, 8, 9 }};
Sum = xn ();
Printf ("sum is % d", sum );
}
3. Compile a function, int invert (char STR []), and reverse the content of a string;
# Include <stdio. h>
Int invert (char STR []) {
Int I;
Int temp;
Int n = strlen (STR );
For (I = 0; I <(n + 1)/2); I ++ ){
Temp = STR [I];
STR [I] = STR [n-i-1];
STR [n-i-1] = temp;
}
Puts (STR );
}
Int main (){
Char STR [100];
Gets (STR );
Invert (STR );
}
4. Write a function to delete n characters starting from the specified position M. If the string is successfully deleted, the function range is deleted. Otherwise, an empty value is returned;
# Include <stdio. h>
Int Delete (char STR [], int M, int N)
{
Int I;
Int U;
U = strlen (STR );
For (I = 0; I <n; I ++)
{
STR [M + I-1] = STR [M + I + n-1];
}
STR [u-n] = '\ 0 ';
Puts (STR );
}
Int main (){
Int M = 3;
Int N = 4;
Char STR [100];
Gets (STR );
Delete (STR, m, n );
}
5. Compile a program to delete non-numeric characters from the input string on the keyboard and convert them to integer output;
# Include <stdio. h>
# Include <string. h>
Int delete_change (char STR [])
{
Int I, Res = 0;
For (I = 0; STR [I]! = '\ N'; I ++ ){
If (STR [I]> = '0' & STR [I] <= '9 ')
Res = res * 10 + STR [I]-'0 ';
}
Return res;
}
Int main (){
Int I = 0;
Char STR [100];
While (1 ){
STR [I] = getchar ();
If (STR [I] = '\ n ')
Break;
Else I ++;
}
Printf ("delete_change number is % d", delete_change (STR ));
Return 0;
}
6. Write a sum (int n) function using recursive methods, and evaluate 1 ~ The sum of N;
Way1: Non-recursive mode:
# Include <stdio. h>
# Include <string. h>
Int add (int n ){
Int sum = 0;
Int I;
For (I = 1; I <= N; I ++)
{
Sum = sum + I;
}
Return sum;
}
Int main (){
Int X;
X = 5;
Printf ("1 ~ N add is % d ", add (x ));
}
Way2: Recursion
# Include <stdio. h>
# Include <string. h>
Int add (int n ){
Int sum = 0;
If (n <= 1)
Sum = 1;
Else
Sum = N + Add (n-1 );
Return sum;
}
Int main (){
Int X;
X = 4;
Printf ("1 ~ N add is % d ", add (x ));
}
7. Compile a program to test the position of the character in string S2 that appears for the first time in string S1. A function must be defined to return the position where this character appears. If S1 contains no characters in S2, -1 is returned. (TBD)
# Include <stdio. h>
# Include <string. h>
Int length (char str1 [], char str2 []) {
Int I, J;
Int N;
N = strlen (str1 );
For (I = 0; str2 [I]; I ++)
For (j = 0; str1 [I + J] = str2 [J]; j ++)
If (J + 1 = N)
Return I + 1;
Return-1;
}
Int main (){
Char S1 [100];
Char S2 [100];
Int Len;
Printf ("str1 is: \ n ");
Scanf ("% s", S1 );
Printf ("str2 is: \ n ");
Scanf ("% s", S2 );
Len = length (S1, S2 );
Printf ("str2 in str1 location is % d: \ n", Len );
}