Ứng dụng và tính chất của biến static trong C


 1 #include 
 2 #include 
 3 #include 
 4 #define MAX_LINES 25
 5 #define MAX_COLS 80
 6 
 7 void display_text(int line, int col, char text[]);
 8 void print_spaces(int n);
 9 void insert_blank_lines(int n);
10 
11 int main() {
12     int row, column, i;
13     char message[MAX_COLS] = "Xin chào, tháng Tư!";
14     
15     srand(time(0));
16     
17     for(i = 1; i <= 10; ++i) {
18         row = rand() % MAX_LINES;
19         column = rand() % MAX_COLS;
20         display_text(row, column, message);
21         Sleep(1000);
22     }
23     
24     return 0; 
25 }
26 
27 void print_spaces(int n) {
28     int j;
29     
30     for(j = 1; j <= n; ++j)
31         printf(" ");
32 }
33 
34 void insert_blank_lines(int n) {
35     int j;
36     
37     for(j = 1; j <= n; ++j)
38         printf("\n");
39 } 
40 
41 void display_text(int row, int col, char text[]) {
42     insert_blank_lines(row-1);      
43     print_spaces(col-1);            
44     printf("%s", text);         
45 }
Hàm trên tạo ngẫu nhiên 10 vị trí (hàng và cột), sau đó hiển thị một chuỗi tại mỗi vị trí với khoảng thời gian nghỉ giữa các lần hiển thị là 1 giây.

 1 // Tính giai thừa sử dụng biến static cục bộ
 2 
 3 #include 
 4 long long factorial(int num);
 5 
 6 int main() {
 7     int a, b;
 8 
 9     printf("Nhập số: ");
10     scanf("%d", &b);
11 
12     for (a = 1; a <= b; ++a)
13         printf("%d! = %lld\n", a, factorial(a));
14 
15     return 0;
16 }
17 
18 long long factorial(int num) {
19     static long long prod = 1;
20     prod *= num;
21 
22     return prod;
23 }
Sử dụng biến static để lưu trữ kết quả trung gian của phép nhân khi tính giai thừa.

 1 // Ví dụ về tính chất của biến static
 2 
 3 #include 
 4 int calculate(int x, int y);
 5 
 6 int main() {
 7     int q = 3, r = 2, res1, res2;
 8 
 9     res1 = calculate(q, r);
10     res2 = calculate(q, r);
11     printf("%d, %d\n", res1, res2);
12 
13     return 0;
14 }
15 
16 int calculate(int x, int y) {
17     static int z = 0, w = 3;
18 
19     w += z + 1;
20     z = w + x + y;
21 
22     return z;
23 }
Biến static giúp lưu trữ giá trị giữa các lần gọi hàm.

 1 #include 
 2 long long compute(int value);
 3 
 4 int main() {
 5     int value;
 6     long long result;
 7 
 8     while (scanf("%d", &value) != EOF) {
 9         result = compute(value);
10         printf("Giá trị: %d, Kết quả: %lld\n", value, result);
11     }
12 
13     return 0;
14 }
15 
16 long long compute(int value) {
17     if (value <= 0) {
18         return 0;
19     } else {
20         return 2 * compute(value - 1) + 1;
21     }
22 }
Tính lũy thừa của 2 trừ đi 1 sử dụng đệ quy.

 1 #include 
 2 int combinatorial(int p, int q);
 3 
 4 int main() {
 5     int p, q;
 6 
 7     while(scanf("%d%d", &p, &q) != EOF)
 8         printf("p = %d, q = %d, Kết quả: %d\n", p, q, combinatorial(p, q));
 9     
10     return 0;
11 }
12 
13 int combinatorial(int p, int q) {
14     if (q > p || q < 0) {
15         return 0;
16     }
17     if (q == 0 || p == q) {
18         return 1;
19     } else {
20         return combinatorial(p - 1, q - 1) + combinatorial(p - 1, q);
21     }
22 }
Tính tổ hợp bằng phương pháp đệ quy.

Thẻ: C static-variable Recursion factorial combinatorial

Đăng vào ngày 2 tháng 10 lúc 16:44