C 语言学习-04【结构化程序设计】

1、单分支结构语句

用单分支结构进行奇偶判断:

c 复制代码
#include <stdio.h>

int main() {
    int num;
    printf("Please enter an integer: ");
    scanf("%d", &num);
    if (num % 2 != 0) {
        printf("%d is odd! \n", num);
    }
    if (num % 2 == 0) {
        printf("%d is even! \n", num);
    }
    return 0;
}
  • 运行结果:

2、双分支结构语句

用双分支结构进行奇偶判断:

c 复制代码
#include <stdio.h>

int main() {
    int num;
    printf("Please enter an integer: ");
    scanf("%d", &num);
    if (num % 2 != 0) {
        printf("%d is odd! \n", num);
    } else {
        printf("%d is even! \n", num);
    }
    return 0;
}
  • 运行结果:

3、多分支结构语句

用多分支结构进行学生成绩的判断:

c 复制代码
#include <stdio.h>

int main() {
    int score;
    printf("Please enter score: ");
    scanf("%d", &score);
    if (score >= 90) {
        printf("A\n");
    } else if (score >= 80) {
        printf("good\n");
    } else if (score >= 70) {
        printf("medium\n"); 
    } else if (score >= 60) {
        printf("pass\n");
    } else {
        printf("fail");
    }
    return 0;
}
  • 运行结果:

4、分支语句的嵌套

判断某学生成绩 score 是否及格:

c 复制代码
#include <stdio.h>

int main() {
    int score;
    printf("Please enter score: ");
    scanf("%d", &score);
    if (score >= 60) {
        if (score >= 90) {
            printf("A\n");
        } else {
            printf("pass\n");
        }
    } else {
        printf("fail\n");
    }
    return 0;
}
  • 运行结果:

5、switch 选择语句

输入一个简单的四则运算表达式,包含两个实数和一个运算符,输出计算结果:

c 复制代码
#include <stdio.h>

int main() {
    char op;
    double x, y;
    printf("Please enter an expression: ");
    scanf("%lf %c %lf", &x, &op, &y);
    switch (op) {
    case '+':
        printf("The expression results in: %.2f\n", x + y);
        break;
    case '-':
        printf("The expression results in: %.2f\n", x - y);
        break;
    case '*':
        printf("The expression results in: %.2f\n", x * y);
        break;
    case '/':
        printf("The expression results in: %.2f\n", x / y);
        break;
    default:
        printf("Operator error! \n");
    }
    return 0;
}
  • 运算结果:

6、while 循环结构与执行流程

用 while 循环语句计算输入的 10 个整数和奇数的和:

c 复制代码
#include <stdio.h>

int main() {
    int num;
    int i = 1, sum = 0;
    int count = 0;
    printf("Please enter 10 integers at a time: ");
    while (i <= 10) {
        scanf("%d", &num);
        if (num % 2 != 0) {
            sum += num;
            count++;
        }
        i++;
    }
    if (count == 0) {
        printf("There are no odd numbers in the data!");
    } else {
        printf("Odd numbers are: %d\n", count);
        printf("Odd sum: %d", sum);
    }
    return 0;
}
  • 运算结果:

7、for 循环语句与执行流程

打印出所有"水仙花数":

c 复制代码
#include <stdio.h>

int main() {
    int a, b, c;
    int i;
    for (i = 100; i < 1000; i++) {
        a = i % 10;
        b = (i / 10) % 10;
        c = i / 100;
        if (a * a * a + b * b * b + c * c * c == i) {
            printf("%d\t", i);
        }
    }
    return 0;
}
  • 运行结果:

    用嵌套 for 循环方式计算 1 到 100 的素数和:
c 复制代码
#include <stdio.h>

int main() {
    int sum = 0, i = 0, j = 0;
    int count;
    printf("The prime numbers from 1 to 100 are: ");
    for (i = 2; i <= 100; i++) {
        count = 0;
        for (j = 2; j <= i / 2; j++) {
            if (i % j == 0) {
                count++;
                break;
            }
        }
        if (count == 0) {
            printf("%d ", i);
            sum += i;
        }
    }
    printf("\nThe sum of primes from 1 to 100 is: %d", sum);
    return 0;
}
  • 运算结果:

    images%5Cimage-20241107233301176.png&pos_id=img-piAAi8Jj-1731048018050)

8、do-while 循环结果与执行流程

计算两个数的最大公约数

c 复制代码
#include <stdio.h>

int main() {
    int m, n, r, t;
    int m1, n1;
    printf("Please enter the first number: ");
    scanf("%d", &m);
    printf("Please enter the second number: ");
    scanf("%d", &n);
    m1 = m;
    n1 = n;
    if (m < n) {
        t = m;
        m = n;
        n = t;
    }
    do {
        r = m % n;
        m = n;
        n = r;
    } while (r != 0);
    printf("The greatest common divisor of %d and %d is %d\n", m1, n1, m);
    return 0;
}
  • 运行结果:

9、循环结构嵌套

九九乘法表的打印:

c 复制代码
#include <stdio.h>

int main() {
    int i = 0, j = 0;
    printf("*");
    for (i = 1; i <= 9; i++) {
        printf("%8d", i);
    }
    printf("\n");
    for (i = 1; i <= 9; i++) {
        printf("%d", i);
        for (j = 1; j <= i; j++) {
            printf("%3d*%d=%2d", i, j, i * j);
        }
        printf("\n");
    }
    return 0;
}
  • 运行结果:

10、辅助语句 break 和 continue

输入大于 2 的整数,判断该数是否为素数:

c 复制代码
#include <stdio.h>

int main() {
    int m, i, flag;
    flag = 1;
    printf("Please enter an integer greater than 2: ");
    scanf("%d", &m);
    for (i = 2; i < m; i++) {
        if (m % i == 0) {
            flag = 0;
            break;
        }
    }
    if (flag) {
        printf("%d is prime number! \n", m);
    } else {
        printf("%d is not prime number! \n", m);
    }
    return 0;
}
  • 运行结果为

    判断 1800 年到 2024 年之间的所有闰年并输出:
c 复制代码
#include <stdio.h>

int main() {
    int year;
    for (year = 1800; year <= 2024; year++) {
        if (!(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)) {
            continue;
        }
        printf("%d ", year);
        if (year % 10 == 0) {
            printf("\n");
        }
    }
    return 0;
}
  • 运行结果:

11、改良的计算器

改良版实现四则运算功能的计算器:

c 复制代码
#include <stdio.h>

int main() {
    double displayed_value;
    double new_entry;
    char command_character;
    printf("Improved computer program\n");
    printf("Tip: (number> )Enter number, (command> )Enter Operator +、-、*、/\n");
    printf("number> ");
    scanf("%lf", &displayed_value);
    getchar();
    printf("command> ");
    scanf("%c", &command_character);
    while (command_character != 'Q') {
        switch (command_character) {
        case 'c':
        case 'C':
            scanf("%lf", &displayed_value);
            break;
        case '+':
            printf("number> ");
            scanf("%lf", &new_entry);
            displayed_value += new_entry;
            break;
        case '-':
            printf("number> ");
            scanf("%lf", &new_entry);
            displayed_value -= new_entry;
            break;
        case 'x':
        case 'X':
        case '*':
            printf("number> ");
            scanf("%lf", &new_entry);
            displayed_value *= new_entry;
            break;
        case '/':
            printf("number> ");
            scanf("%lf", &new_entry);
            while (new_entry == 0) {
                printf("Divisor cannot be 0, please re-enter! \n");
                printf("number> ");
                scanf("%lf", &new_entry);
            }
            displayed_value /= new_entry;
            break;
        default:
            printf("Invalid input, please re-enter the command type! \n");
        }
        printf("Value: %lf\n", displayed_value);
        getchar();
        printf("command> ");
        scanf("%c", &command_character);
    }
    return 0;
}
  • 运算结果:
相关推荐
HC182580858321 小时前
“倒时差”用英语怎么说?生活英语口语学习柯桥外语培训
学习·生活
学习路上_write1 小时前
FPGA/Verilog,Quartus环境下if-else语句和case语句RT视图对比/学习记录
单片机·嵌入式硬件·qt·学习·fpga开发·github·硬件工程
非概念1 小时前
stm32学习笔记----51单片机和stm32单片机的区别
笔记·stm32·单片机·学习·51单片机
iiiiiankor2 小时前
C/C++内存管理 | new的机制 | 重载自己的operator new
java·c语言·c++
小辛学西嘎嘎2 小时前
C/C++精品项目之图床共享云存储(3):网络缓冲区类和main
c语言·开发语言·c++
无敌最俊朗@2 小时前
stm32学习之路——八种GPIO口工作模式
c语言·stm32·单片机·学习
EterNity_TiMe_3 小时前
【论文复现】STM32设计的物联网智能鱼缸
stm32·单片机·嵌入式硬件·物联网·学习·性能优化
L_cl3 小时前
Python学习从0到1 day28 Python 高阶技巧 ⑤ 多线程
学习
前端SkyRain3 小时前
后端Node学习项目-用户管理-增删改查
后端·学习·node.js
提笔惊蚂蚁3 小时前
结构化(经典)软件开发方法: 需求分析阶段+设计阶段
后端·学习·需求分析