C第五天

本文通过多个实例演示了C语言的基本用法,包括算术运算、条件判断、循环控制、数组与指针操作等核心概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
int main (void) {
    printf ("请输入表达式:");
    int a, b, c, invalid = 0;
    char op;
    scanf ("%d%c%d", &a, &op, &b);
    switch (op) {
        default:
            printf ("非法运算符!\n");
            invalid = 1;
            break;
        case '+':
            c = a + b;
            break;
        //case '+':
        //    break;
        case '-':
            c = a - b;
            break;
        case '*':
        case 'x':
            c = a * b;
            break;
        case '/':
            c = a / b;
            break;
        case '%':
            c = a % b;
            break;
    }
    if (! invalid)
    printf ("%d%c%d=%d\n", a, op, b, c);
    return 0;

}


#include <stdio.h>
int main (void) {
    int a = 2, b = 4, c = 6, x, y;
    y = (x = a + b, b + c);
    printf ("%d,%d\n", x, y); // 6,10
    y = (x += 10, ++x);
    printf ("%d,%d\n", x, y); //17,17
    return 0;
}


#include <stdio.h>
int main (void) {
    int i, j;
    for (i = 0; i < 10; i++)
        for (j = 0; j < 10; j++)
            if ((3*10+i)*8256 == (j*10+3)*6528)
                printf ("3%d*8256 = %d3*6528\n", i, j);
    return 0;
}


#include <stdio.h>
int main (void) {
    printf ("请输入一个整数:");
    int n;
    scanf ("%d", &n);
    if (n > 0)
        printf ("正数\n");
    else if (n < 0)
        printf ("负数\n");
    else
        printf ("零\n");
    return 0;
}


#include <stdio.h>
int main (void) {
    int i;
    /*
    for (i = 1; i <= 100; i++)
        if (i % 2)
            printf ("%d ", i);
    */
    for (i = 1; i <= 100; i += 2)
        printf ("%d ", i);
    printf ("\n");
    return 0;
}

#include <stdio.h>
#include <math.h>
int main (void) {
    printf ("请输入一个整数:");
    int max;
    scanf ("%d", &max);
    int i = 2;
    while (i <= max) {
        int j = sqrt (i);
        while (j > 1) {
            if (i % j == 0)
                break;
            j--;
        }
        if (j == 1)
            printf ("%d ", i);
        i++;
    }
    printf ("\n");
    return 0;
}

#include <stdio.h>
int main (void) {
    printf ("考试成绩:");
    int score;
    scanf ("%d", &score);
    if (score < 0 || score > 100) {
        printf ("无效成绩!\n");
        return -1;
    }
    switch (score / 10) {
        case 10:
        case 9:
            printf ("A\n");
            break;
        case 8: {
            char c = 'B';
            printf ("%c\n", c);
            break;
        }
        case 7:
            printf ("C\n");
            break;
        case 6:
            printf ("D\n");
            break;
        default:
            printf ("E\n");
            break;
    }
    return 0;
}

#include <stdio.h>
int main (void) {
    printf ("税前工资:");
    double before;
    scanf ("%lf", &before);
    double extra = before - 3500, tax;
    if (extra <= 0)
        tax = 0;
    else if (extra <= 1500)
        tax = extra * 0.03;
    else if (extra <= 4500)
        tax = extra * 0.1 - 105;
    else if (extra <= 9000)
        tax = extra * 0.2 - 555;
    else if (extra <= 35000)
        tax = extra * 0.25 - 1005;
    else if (extra <= 55000)
        tax = extra * 0.3 - 2755;
    else if (extra <= 80000)
        tax = extra * 0.35 - 5505;
    else
        tax = extra * 0.45 - 13505;
    printf ("应缴个税:%.2lf\n", tax);
    printf ("税后工资:%.2lf\n", before - tax);
    return 0;
}

#include <stdio.h>
int main (void) {
    int i = 100;
    while (i < 10) {
        printf ("%d ", i);
        i++;
    }
    printf ("\n");
    i = 100;
    do {
        printf ("%d ", i);
        i++;
    }   while (i < 10);
    printf ("\n");
    return 0;
}

#include <stdio.h>
int main (void) {
    //for (;;) {
    while (1) {
        printf ("请输入一个年份:");
        int year;
        scanf ("%d", &year);
        if (year == 0)
            break;
        if (year < 0) {
            printf ("无效年份!\n");
            continue;
        }
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
            printf ("闰年\n");
        else
            printf ("平年\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值