C语言基础学习——第2天(语句)

00if.c

#include 

#define EPSILON 0.000001

int main(void)
{
    int a = 0;

    printf("pls input your score: ");
    scanf("%d", &a);

    if (a < 0 || a > 100)
    {
        printf("score should be 0 -- 100.\n");
        return 0;
    }

    if (a < 30)
    {
        printf("grade E.\n");
    }
    else if (a < 60)
    {
        printf("grade D.\n");
    }
    else if (a < 75)
    {
        printf("grade C.\n");
    }
    else if (a < 85)
    {
        printf("grade B.\n");
    }
    else
        printf("grade A, excellent!\n");

    printf("\n");

    // 分成及格与不及格
    if (a < 60)
    {
        if (a < 30)
        {
            printf("grade E.\n");
        }
        else
        {
            printf("grade D.\n");
        }
    }
    else
    {
        if (a < 75)
        {
            printf("grade C.\n");
        }
        else if (a < 85)
        {
            printf("grade B.\n");
        }
        else
            printf("grade A, excellent!\n");
    }

    printf("\n");

    float f = 5.00000001;

    // 不推荐直接用 == 来判断浮点数
    if (f == 5.0)
    {
        printf("f == 5.0!\n");
    }
    else
    {
        printf("f != 5.0!\n");
    }

    printf("\n");

    // 自己控制浮点数的判断范围(推荐做法)
    if ((f > 5.0 - EPSILON) && (f < 5.0 + EPSILON))
    {
        printf("f == 5.0!\n");
    }
    else
    {
        printf("f != 5.0!\n");
    }

    return 0;
}


运行结果:
pls input your score: 86
grade A, excellent!

grade A, excellent!

f == 5.0!

f == 5.0!

01while.c

#include 

int main(void)
{
    int i = 0;

    while (i < 5)
    {
        printf("i = %d\n", i);
        i++;
    }

    printf("******* i = %d *******\n", i);

    // do - while 无论如何会执行一次
    do
    {
        printf("in do while i = %d\n", i);
        i++;
    } while (i < 5);

    printf("******* i = %d *******\n", i);

    return 0;
}


运行结果:
i = 0
i = 1
i = 2
i = 3
i = 4
******* i = 5 *******
in do while i = 5
******* i = 6 *******

02goto.c

#include 

int main(void)
{
    int i = 0;

label:
    printf("i = %d\n", i);
    i++;

    if (i < 5)
        goto label;

    printf("******* i = %d *******\n", i);

    return 0;
}


运行结果:
i = 0
i = 1
i = 2
i = 3
i = 4
******* i = 5 *******

03for.c

#include 

int main(void)
{
    int i = 0;

    for (i = 1; i > 5; i++)
        ;

    printf("i = %d\n", i);

    printf("------------------\n");

    for (i = 0; i < 5; i++)
        printf("i = %d\n", i);

    return 0;
}


for (init_expr; cond_expr; update_expr)
    statment  // statment: 可以是 ; or expression; or { ... }


init_expr(初始化表达式, 无论如何会执行一次)
    -> cond_expr (true)
        -> statment --> updata_expr

   -> cond_expr (true)
        -> statment --> updata_expr
    ...

运行结果:
i = 1
------------------
i = 0
i = 1
i = 2
i = 3
i = 4

04random.c

#include 

int main(void)
{
    int num = 0;

    printf("1970.1.1 00:00 -- now, sec: %d\n", time(NULL));

    srand(time(NULL));  // 利用系统时间来改变系统的种子值

    num = rand() % 100; // 产生100以内的随机整数
    printf("num = %d\n", num);

    num = rand() % 100; // 产生100以内的随机整数
    printf("num = %d\n", num);

    return 0;
}


运行结果:
1970.1.1 00:00 -- now, sec: 1439711053
num = 37
num = 18

05break.c

#include 

int main(void)
{
    int i = 0, j = 0;

    while (i < 9)
    {
        printf("i: %d --> ", i);

        if (i == 3)
        {
            i++;
            putchar('\n');
            continue;   // 跳过本次循环
        }

        j = 0;
        while (1)
        {
            printf("%d ", j);
            j++;
            if (j > i)
                break;
        }

        putchar('\n');
        i++;
    }

    return 0;
}


break: 跳出一层循环
continue: 结束本次循环

运行结果:
i: 0 --> 0
i: 1 --> 0 1
i: 2 --> 0 1 2
i: 3 -->
i: 4 --> 0 1 2 3 4
i: 5 --> 0 1 2 3 4 5
i: 6 --> 0 1 2 3 4 5 6
i: 7 --> 0 1 2 3 4 5 6 7
i: 8 --> 0 1 2 3 4 5 6 7 8

06switch.c

#include 

int main(void)
{
    char ch = 0;

    printf("input a char: ");
    ch = getchar();

    switch (ch)
    {
        case 'a':
        case 'A':
            printf("a or A!\n");
            break;

        case 'b':
        case 'B':
            printf("b or B!\n");
            break;

        case 'c':
        case 'C':
            printf("c or C!\n");
            break;

        default:
            printf("other unkown case.\n");
            break;
    }

    return 0;
}

07output_buf.c

#include 

int main(void)
{
    int n = 0;

    printf("hello\n");  // 这行可以打印出来
    printf("world!");   // 这行却暂时打印不出来

    //scanf("%d", &n);

    //fflush(stdout);

    while (1)
    {
        sleep(1);
    }

    return 0;
}


stdin    标准输入
stdout   标准输出
stderr   标准错误输出

标准输出是行缓冲. 以下情况导致缓冲区刷新:
1. 遇到换行字符('\n').
2. 遇到下一次标准输入.
3. 缓冲区满.
4. 强制刷新(fflush(stdout)).

08input_buf.c

#include 

int main(void)
{
    int ret = 0, n = 0;
    char ch = 0;

    printf("pls input a integer: ");// 在这个地方,你可以输入一个数字或者一个字符试一下,比较结果有何不同
    ret = scanf("%d", &n);          // 如果scanf拿到东西, 则 ret != 0
    printf("n = %d, ret = %d\n", n, ret);

    ret = 0; // 清0
    while(getchar() != '\n') // 清空输入缓冲区(去缓冲区取, 直到取出'\n'就跳出while)
        ;

    printf("pls input a char: ");
    ret = scanf("%c", &ch);         // 如果不清缓冲区的话,ch拿到的是'\n'换行符
    printf("ch = %c, ret = %d\n", ch, ret);

    return 0;
}


scanf 按照格式到标准输入缓冲区中拿东西.
只要标准输入缓冲区中的数据和 scanf 指定的
格式不匹配, 则 scanf 直接返回.
scanf 的返回值用于判断 scanf 有没有拿到东西.

exercise1.c

#include 

int main(void)
{
    double ret = 0.0;
    int divider = 0;

    for (divider = 1; divider <= 10; divider++)
    {
        if (divider % 2 == 0)
            ret -= (double)1 / divider;
        else
            ret += 1 / (double)divider;
    }

    printf("ret = %lf\n", ret);

    return 0;
}


计算 1 - 1/2 + 1/3 - 1/4 + ... - 1/10 的值

exercise2.c

#include 

int main(void)
{
    int num = 0, divider = 0, flag = 0, cnt = 0;

    for (num = 2; num <= 1000; num++)
    {
        flag = 1;   // 素数的标志
        for (divider = 2; divider <= num/2; divider++)// 一半就可以了
        {
            if (num%divider == 0)
            {
                flag = 0;   // 不是素数
                break;
            }
        }
#if 1
        if (flag == 1)
        {
            cnt++;
            printf("%d is a prime number.\n", num);
        }
#else
        if (divider == num / 2 + 1)
        {
            cnt++;
            printf("%d is a prime number.\n", num);
        }
#endif
    }

    printf("*******************\n");
    printf("2 - 1000 has %d prime numbers.\n", cnt);

    return 0;
}


求出 2 - 1000 的素数

exercise3.c

#include 

int main(void)
{
    unsigned int num = 0, temp = 0;
    unsigned int i = 0, k = 0;

    printf("please input the number:");
    scanf("%u", &num);

    temp = num;
    while(temp != 0)
    {
        i = temp % 10;
        temp = temp / 10;
        k = k * 10 + i;
    }

    if(num == k)
        printf("%d is a palindromic number\n", num);
    else
        printf("%d is not a palindromic number\n", num);

    return 0;
}


输入一个无符号整数, 判定该整数是否是回文数
ex: 1, 11, 121, 12321

exercise4.c

#include 

int main(void)
{
    int gj = 0, mj = 0, xj = 0;

    for(gj = 0; gj < 100/5; gj++)
    {
        for(mj = 0; mj < 100/3; mj++)
        {
            xj = 100 - gj - mj;
            if((xj % 3 == 0) && ((gj * 5 + mj * 3 + xj / 3) == 100))
            {
                printf("gj = %d, mj = %d, xj = %d\n", gj, mj, xj);
            }
        }
    }

    return 0;
}


100元买100只鸡, 公鸡5元一只, 母鸡3元一只, 小鸡1元三只, 请输出所有的组合.

运行结果:
gj = 0, mj = 25, xj = 75
gj = 4, mj = 18, xj = 78
gj = 8, mj = 11, xj = 81
gj = 12, mj = 4, xj = 84

exercise5.c

#include 

int main(void)
{
    unsigned int a = 0, b = 0, gcd = 0;

    printf("please input a and b: ");
    scanf("%u %u", &a, &b);

    for(gcd = a > b ? b : a; gcd > 0; gcd--)
        if((a % gcd == 0) && (b % gcd == 0))
            break;

    printf("the greatest common divisor of %d and %d is %d\n", a, b, gcd);

    return 0;
}


求两个数的最大公约数.

exercise6.c

#include 

int main(void)
{
    unsigned int num = 0, i = 0, ret = 0;

    for(num = 1; num <= 1000; num++)
    {
        for(i = 1; i <= num/2; i++) // 一半就可以了
        {
            if(num % i == 0)
                ret += i;
        }
        if(ret == num)
            printf("the perfect number is %d\n", num);

        ret = 0; // 及时清零
    }

    return 0;
}


输出1 - 1000以内的完数(其真因子之和等于其自身)
    6: 1, 2, 3; 6 = 1 + 2 + 3

运行结果:
the perfect number is 6
the perfect number is 28
the perfect number is 496

exercise7.c

#include 

int main(void)
{
    unsigned int num = 0, sum = 0, i = 0, j = 0 ;

    for(num = 2; num < 1000; num++)
    {
        j = num;
        sum = 0;
        while(j > 0)
        {
            i = j % 10;
            j = j / 10;
            sum += i * i * i;
        }
        if(sum == num)
            printf("the narcissistic number is %d\n", num);
    }

    return 0;
}


1 - 1000以内的水仙花数
153 = 1 ^ 3 + 5 ^ 3 + 3 ^ 3

运行结果:
the narcissistic number is 153
the narcissistic number is 370
the narcissistic number is 371
the narcissistic number is 407

你可能感兴趣的:(c语言)