C语言程序:解析郑州大学学生学号信息
#include <stdio.h>
#include <string.h>
int main() {
char student_id[13]; // 学号
char grade[5]; // 年级
char college_code[3]; // 学院代码
char class_num[3]; // 班级
char num_in_class[3]; // 班内序号
char colleges[50][3] = { '01', '02', '03', '04', '05', '06', '07', '08', '09', '63', '10', '11', '21', '22', '23', '24', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '45', '46', '47', '48', '49', '51', '52', '53', '54', '84', '90' }; // 学院代码表
char college_names[50][20] = { '机械与动力工程学院', '电气工程学院', '化工学院', '土木工程学院', '水利科学与工程学院', '力学与安全工程学院', '管理工程学院', '材料科学与工程学院', '建筑学院', '口腔医学院', '地球科学与技术学院', '生态与环境学院', '数学与统计学院', '物理学院', '化学学院', '信息工程学院', '商学院', '旅游管理学院', '政治与公共管理学院', '信息管理学院', '法学院', '文学院', '外国语与国际关系学院', '历史学院', '马克思主义学院', '新闻与传播学院', '教育学院', '书法学院', '美术学院', '生命科学学院', '音乐学院', '体育学院(校本部)', '基础医学院', '公共卫生学院', '药学院', '护理与健康学院', '国际学院', '交换生' }; // 学院名称表
while (1) {
printf("请输入学号(输入0结束程序):");
scanf("%s", student_id);
if (strcmp(student_id, "0") == 0) { // 输入0结束程序
break;
}
if (strlen(student_id) != 12) { // 判断输入是否为12位
printf("输入格式错误,请重新输入\n");
continue;
}
strncpy(grade, student_id, 4); // 复制年级
grade[4] = '\0'; // 加上字符串结束符
if (strcmp(grade, "2022") != 0 && strcmp(grade, "2021") != 0 && strcmp(grade, "2020") != 0 && strcmp(grade, "2019") != 0 && strcmp(grade, "2018") != 0) { // 判断年级是否正确
printf("输入的年级错误,请重新输入\n");
continue;
}
strncpy(college_code, student_id + 4, 2); // 复制学院代码
college_code[2] = '\0'; // 加上字符串结束符
int found = 0; // 是否找到学院代码
for (int i = 0; i < 40; i++) {
if (strcmp(college_code, colleges[i]) == 0) {
found = 1;
printf("%s\n", college_names[i]); // 输出学院名称
break;
}
}
if (!found) {
printf("输入的学院代码错误,请重新输入\n");
continue;
}
strncpy(class_num, student_id + 6, 2); // 复制班级
class_num[2] = '\0'; // 加上字符串结束符
strncpy(num_in_class, student_id + 8, 2); // 复制班内序号
num_in_class[2] = '\0'; // 加上字符串结束符
printf("%s级,%s,%s班,%s号\n", grade, college_names[i], class_num, num_in_class);
char yn;
while (1) {
printf("是否继续输入学生的学号(y/n):");
scanf(" %c", &yn);
if (yn == 'y' || yn == 'n') {
break;
}
}
if (yn == 'n') {
break;
}
}
return 0;
}
原文地址: https://www.cveoy.top/t/topic/nZ9U 著作权归作者所有。请勿转载和采集!