#include <stdio.h> #include <string.h>

int main() { char stu_num[13]; // 学号 int len; // 学号长度 char grade[5]; // 年级 char college[50]; // 学院 char class_num[3]; // 班级 char stu_id[3]; // 班内序号 char again; // 是否继续输入学号

do {
    printf("请输入12位学号(按0结束程序):\n");
    scanf("%s", stu_num);

    len = strlen(stu_num);
    if (len != 12) {
        printf("输入的学号不是12位,请重新输入!\n");
        continue;
    }

    // 年级
    strncpy(grade, stu_num, 4);
    grade[4] = '\0';
    if (strcmp(grade, "2022") && strcmp(grade, "2021") && strcmp(grade, "2020") && strcmp(grade, "2019") && strcmp(grade, "2018")) {
        printf("输入的学号年级不正确,请重新输入!\n");
        continue;
    }

    // 学院
    char college_code[3];
    strncpy(college_code, stu_num + 4, 2);
    college_code[2] = '\0';
    if (!strcmp(college_code, "01")) {
        strcpy(college, "机械与动力工程学院");
    } else if (!strcmp(college_code, "02")) {
        strcpy(college, "电气工程学院");
    } else if (!strcmp(college_code, "03")) {
        strcpy(college, "化工学院");
    } else {
        printf("输入的学号学院代码不正确,请重新输入!\n");
        continue;
    }

    // 班级
    strncpy(class_num, stu_num + 6, 2);
    class_num[2] = '\0';

    // 班内序号
    strncpy(stu_id, stu_num + 8, 2);
    stu_id[2] = '\0';

    printf("%s级,%s,%s班,%s号\n", grade, college, class_num, stu_id);

    do {
        printf("是否继续输入学生的学号(y/n):\n");
        scanf(" %c", &again);
    } while (again != 'y' && again != 'n');

} while (again == 'y');

return 0;

}

C语言程序:根据郑州大学学号获取学生信息

原文地址: https://www.cveoy.top/t/topic/nYXo 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录