#include <stdio.h>

/* Demo 02：流程控制与函数 */

int max(int a, int b)
{
    return a > b ? a : b;
}

void change(int x)          // 值传递，不影响外部变量
{
    x = 100;
}

void changeByPtr(int *x)    // 传指针，可修改外部变量
{
    *x = 100;
}

long factorial(int n)       // 递归求阶乘
{
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

void count()                // static 局部变量
{
    static int times = 0;
    times++;
    printf("第 %d 次调用\n", times);
}

int main()
{
    // 分支
    int score = 85;
    if (score >= 90)
        printf("优秀\n");
    else if (score >= 60)
        printf("及格\n");
    else
        printf("不及格\n");

    switch (score / 10)
    {
    case 8:
        printf("switch: 80分档\n");
        break;
    default:
        printf("switch: 其他\n");
    }

    // 循环：九九乘法表
    for (int i = 1; i <= 3; i++)
    {
        for (int j = 1; j <= i; j++)
            printf("%d*%d=%-3d", j, i, i * j);
        printf("\n");
    }

    // 函数
    printf("max(3, 5) = %d\n", max(3, 5));
    printf("5! = %ld\n", factorial(5));

    int a = 1;
    change(a);
    printf("change 后 a = %d\n", a);          // 1
    changeByPtr(&a);
    printf("changeByPtr 后 a = %d\n", a);     // 100

    count();
    count();
    count();   // static 累计到 3

    return 0;
}
