环境
操作系统:Windows11
编译器:GCC
源代码
函数较多,自定义头文件,主文件引入即可
头文件:functions.h
头文件所对应的源文件:functions.c
主文件:学生信息管理系统.c
functions.h
#ifndef _FUNCTINOS_H_
#define _FUNCTINOS_H_
#define MAX_STU 100
typedef struct Students{
char name[9], sex[5], StuID[21], class[31];
int age;
float score;
}stu;
#define LEN sizeof(stu)
stu students[MAX_STU];
//函数申明
void menu(); //打印菜单
void enter_info(); //录入信息
void delete_info(); //删除信息
void select_info(); //检索信息
void show_all(); //查看所有信息
void order_score(); //根据成绩排序
void insert_info(); //插入信息
void modify_info(); //修改数据
#endif
functions.c
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include "functions.h"
//打印菜单
void menu(){
char *options[] = {"1.录入学生信息", "2.删除学生信息", "3.修改学生信息", "4.查询学生信息", "5.插入学生信息", "6.根据成绩排序", "7.查看所有记录", "8.清屏 ", "9.退出管理系统"};
printf("\n\n\n");
Sleep(1200);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 3);
printf("\t\t===============================================\n");
printf("\t\t||\t\t学生信息管理系统\t ||\n");
printf("\t\t||-------------------------------------------||\n");
for(int i=0; i<sizeof(options)/sizeof(options[0]); i++){
printf("\t\t||\t\t");
printf("%s", options[i]);
printf("\t\t ||\n");
}
printf("\t\t===============================================\n");
}
//读取数据
int read_info(){
int index=0;
FILE *fp = fopen("informations.txt", "r");
if(fp == NULL){
printf("没有文件,请先添加!\n");
return 0;
}
while(!feof(fp))
if(fread(&students[index], LEN, 1, fp) == 1)
index++;
fclose(fp);
fp = NULL;
return index;
}
//录入信息
void enter_info(){
FILE *fp;
int index;
//如果没有文件,则先创建一个
fp = fopen("informations.txt", "a+");
if(fp == NULL){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
printf("创建文件失败!\n");
return;
}
//录入数据
index = read_info();
if(index>100){
system("color 0c");
printf("文件已满,无法录入!\n");
return;
}
printf("姓名:");
scanf("%s", students[index].name);//printf("\n");
printf("性别:");
scanf("%s", students[index].sex);//printf("\n");
if(strcmp(students[index].sex, "男")!=0 && strcmp(students[index].sex, "女")!=0){
printf("应输入“男”或“女”!\n");
return;
}
printf("年龄:");
scanf("%d", &students[index].age);//printf("\n");
if(students[index].age<0 || students[index].age>100){
printf("年龄应在0~100之间!\n");
return;
}
printf("学号:");
scanf("%s", students[index].StuID);//printf("\n");
for (int i = 0; i < index; ++i)
{
if(strcmp(students[i].StuID, students[index].StuID) == 0){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
printf("学号不能重复!\n");
return;
}
}
printf("班级:");
scanf("%s", students[index].class);//printf("\n");
printf("此次项目的成绩:");
scanf("%f", &students[index].score);//printf("\n");
fp = fopen("informations.txt", "a+");
fwrite(&students[index], LEN, 1, fp);
fclose(fp);
fp = NULL;
}
//检索信息
void select_info(){
char id[21];
int index = read_info();
if(index == 0){
printf("文件中没有任何信息,请先添加!\n");
return;
}
//根据学号检索信息
printf("学号:");
scanf("%s", id);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
for(int i=0; i<index; i++){
if(strcmp(id, students[i].StuID) == 0){
printf("姓名:%s ", students[i].name);
printf("性别:%s ", students[i].sex);
printf("年龄:%d ", students[i].age);
printf("学号:%s ", students[i].StuID);
printf("班级:%s ", students[i].class);
printf("答辩成绩:%.1f\n", students[i].score);
return;
}
}
printf("查无此人!\n");
return;
}
//删除信息
void delete_info(){
FILE *fp;
char id[21];
int index = read_info();
if(index == 0){
printf("文件中没有数据,请先添加!\n");
return;
}
//根据学号删除
printf("学号:");
scanf("%s", id);
for(int i=0; i<index; i++){
if(strcmp(id, students[i].StuID) == 0)
break;
else{
if(i == index-1){
printf("文件中没有该学号的相关信息!\n");
return;
}
}
}
for(int i=0; i<index; i++){
if(strcmp(id, students[i].StuID) == 0){
for(int j=i; j<index; j++){
students[j] = students[j+1];
}
}
}
//将删除后的信息更新到文件中
fp = fopen("informations.txt", "w+");
for(int i=0; i<index-1; i++)
fwrite(&students[i], LEN, 1, fp);
fclose(fp);
fp = NULL;
printf("删除成功!\n");
return;
}
//插入信息
void insert_info(){
FILE *fp;
int position;
int index = read_info();
if(index==0){
printf("文件中没有任何数据,请先添加!\n");
return;
}
if(index>100){
printf("文件已满,请删除后再插入!\n");
return;
}
printf("插入位置:");
scanf("%d", &position);
for(int i=index; i>=position; i--){
students[i] = students[i-1];
}
//录入信息
printf("姓名:");
scanf("%s", students[position-1].name);//printf("\n");
printf("性别:");
scanf("%s", students[position-1].sex);//printf("\n");
if(strcmp(students[position-1].sex, "男")!=0 && strcmp(students[position-1].sex, "女")!=0){
printf("应输入“男”或“女”!\n");
return;
}
printf("年龄:");
scanf("%d", &students[position-1].age);//printf("\n");
if(students[position-1].age<0 || students[position-1].age>100){
printf("年龄应在0~100之间!\n");
return;
}
printf("学号:");
scanf("%s", students[position-1].StuID);//printf("\n");
for (int i = 0; i < index; ++i)
{
if(i==position-1)
continue;
else{
if(strcmp(students[i].StuID, students[index].StuID) == 0){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
printf("学号不能重复!\n");
return;
}
}
}
printf("班级:");
scanf("%s", students[position-1].class);//printf("\n");
printf("此次项目的成绩:");
scanf("%f", &students[position-1].score);//printf("\n");
fp = fopen("informations.txt", "w+");
for(int i=0; i<index+1; i++)
fwrite(&students[i], LEN, 1, fp);
fclose(fp);
fp = NULL;
printf("插入成功!\n");
return;
}
//根据成绩排序
void order_score(){
int index = read_info();
stu temp;
if(index == 0){
printf("文件中没有任何数据,请先添加!\n");
return;
}
for(int i=0; i<index-1; i++)
for(int j=0; j<index-1-i; j++)
if(students[j].score < students[j+1].score){
temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;
}
//输出结果
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
printf("%-9s %-7s %-5s %-21s %-30s %-5s\n",
"姓名", "性别", "年龄", "学号", "班级", "成绩");
for (int i = 0; i < index; i++){
printf("%-9s ", students[i].name);
printf("%-7s ", students[i].sex);
printf("%-5d ", students[i].age);
printf("%-21s ", students[i].StuID);
printf("%-30s ", students[i].class);
printf("%-.1f", students[i].score);
printf("\n");
}
}
//打印文件中所有信息
void show_all(){
int index = read_info();
if(index == 0){
printf("文件中没有任何数据,请先添加!\n");
return;
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
printf("%-9s %-7s %-5s %-21s %-30s %-5s\n",
"姓名", "性别", "年龄", "学号", "班级", "成绩");
for (int i = 0; i < index; i++){
printf("%-9s ", students[i].name);
printf("%-7s ", students[i].sex);
printf("%-5d ", students[i].age);
printf("%-21s ", students[i].StuID);
printf("%-30s ", students[i].class);
printf("%-.1f", students[i].score);
printf("\n");
}
return;
}
//修改数据
void modify_name(stu *p){
printf("修改后的姓名:");
scanf("%s", p->name);
}
void modify_sex(stu *p){
printf("修改后的性别:");
scanf("%s", p->sex);
}
void modify_age(stu *p){
printf("修改后的年龄:");
scanf("%d", &(p->age));
if(p->age<0 || p->age>100){
printf("年龄的范围应在0~100之间!\n");
return;
}
}
int modify_StuID(stu *p, stu *students, int index, int demo){
printf("修改后的学号:");
scanf("%s", p->StuID);
for (int i = 0; i < index; ++i)
{
if(i == demo)
continue;
else
if(strcmp(p->StuID, (students+i)->StuID) == 0){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
printf("学号不能重复\n");
return -1;
break;
}
}
printf("\n");
return 0;
}
void modify_class(stu *p){
printf("修改后的班级:");
scanf("%s", p->class);
}
void modify_score(stu *p){
printf("修改后的成绩:" );
scanf("%f", &(p->score));
}
void modify_info(){
FILE *fp;
int index = read_info();
char StuID[21];
int demo, status, number;
if(index == 0){
printf("文件中没有任何数据,请先添加!\n");
return;
}
printf("请输入学号:");
scanf("%s", StuID);
for(int i=0; i<index; i++){
if(strcmp(StuID, students[i].StuID) == 0){
demo = i;
break;
}
}
printf("1.修改姓名 2.修改性别 3.修改年龄 4.修改学号 5.修改班级 6.修改成绩\n");
scanf("%d", &number);
switch(number){
case 1:modify_name(&students[demo]); break;
case 2:modify_sex(&students[demo]); break;
case 3:modify_age(&students[demo]); break;
case 4:status=modify_StuID(&students[demo], students, index, demo); break;
case 5:modify_class(&students[demo]); break;
case 6:modify_score(&students[demo]); break;
default:return;break;
}
if(status == -1){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
printf("学号重复!\n");
return;
}
//将信息更新到文件中
fp = fopen("./informations.txt", "w+");
for(int i=0; i<index; i++)
fwrite(&students[i], sizeof(stu), 1, fp);
printf("修改成功!\n");
fclose(fp);
fp = NULL;
}
学生信息管理系统.c
#include "functions.h"
#include <stdio.h>
#include <windows.h>
int main(int argc, char const *argv[])
{
int command;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
printf("\t\t\t 欢迎使用学生信息管理系统\n");
printf("\t 作者:BrankYeen,博客:https://www.cnblogs.com/brankyeen\n");
while(1){
menu();
while(1){
fflush(stdin);
printf("输入命令:");
scanf("%d", &command);
if(command>=1 && command<=9)
break;
else
printf("请输入1~9的整数!\n");
}
switch(command){
case 1:enter_info();break;
case 2:delete_info();break;
case 3:modify_info();break;
case 4:select_info();break;
case 5:insert_info();break;
case 6:order_score();break;
case 7:show_all();break;
case 8:system("cls");break;
case 9:return 0;break;
}
}
return 0;
}
编译运行
cmd进入文件所在目录
gcc -fexec-charset=GBK 学生信息管理系统.c -o 学生信息管理系统.exe -include functions.h functions.c
标签:index,students,scanf,学生,int,printf,信息管理系统,C语言,void
From: https://www.cnblogs.com/brankyeen/p/17498362.html