【开发细节】用C语言基础写学生管理系统(二)

一、本次目标


完成核心头文件前的所有准备工作,包含从VS2022创建新项目到第一个自定义头文件的编写。

图片名称 GitHub:https://github.com/ITchujian/StudentManagementSystem_2022_C

注:为方便分享本次开发的经历,我把分析过程以及代码书写过程,以文字、图片形式合计放于开发记录中,但是一些非常基础的行为动作我将不会讲解或者阐述。
当前位置:【开发细节】用C语言基础写学生管理系统(二)
可跳转:

二、开发记录


步骤1

单击创建新项目,之后在新的窗口中选择空项目,继续下一步
在这里插入图片描述
在这里插入图片描述

步骤2

这里我们将项目名称改为:StuMS,不勾选将解决方案和项目放在同一目录中,单击创建
在这里插入图片描述
创建完成后,将显示如下界面。
在这里插入图片描述

步骤3

此时我们右击解决方案资源管理器中的源文件,新建一个SMS_2022.cpp程序,注意.cpp文件是C++的源文件,其实VS中仅仅支持创建C++项目而不能创建C的项目,毕竟百分之九十九的情况下,C++是完全兼容我们写的C语言的,这里我就不给自己挖矿去梳理那百分之一的不同了,后续遇到我会顺嘴一提。
在这里插入图片描述
在这里插入图片描述

步骤4

这里我们不着急动手写代码,不要忘记了我们的目的。根据最开篇的设计思路,那我们将引用到不少C中的标准库头文件,如stdio.hio.h等。其次,我们需要定义不少的变量以及少量的结构体。那么,我们不妨将它们自定义到一个头文件中,作为编译程序的预配置文件,那么我们按照上述的方法,在解决方案资源管理器中的头文件里添加新项,选择头文件,名称为preconf.h(pre寓意着先、前,conf寓意着配置)。
在这里插入图片描述

步骤5

接下来,就开始编写该项目,我们的第一行代码:

#pragma once

为了防止不同头文件中的宏名冲突发生的各种问题,C/C++语言为我们提供了两种方案:

例:#pragma once  //方案1
例:#ifndef  __大写头文件名_H__  // 方案2

方案1由于是产生较前,更受程序员们的青睐,而发展到2022年了,两者其实都已经得到了广泛的使用,相比之下,两者各有千秋,我们这里仅仅了解一下就行了,方案1是不受一些极老的编译器所支持的,而方案二老牌势力了,可移植性灰常强,如果你需要了解它们更深,这里提供维基百科百度百科academic

步骤6

为该头文件做一个说明注释,直接引用前面开发规范中的文件头部注释,以下篇幅和其他文章中,注释不再单独强调,首次需做强调是因为写程序一定要处处规范,这是代码的外观艺术美。当然,你也可以说我的千行代码估计是注释出来的了😂~

#pragma once  // 解决宏名引发的各种奇怪问题/Solve various strange problems caused by macro names
/*********************************************************************
 * 转载请注明来源/Reprint please indicate the source
 * @FileName preconf.h
 * @Description 预配置文件/pre-configured header files
 * @History
 * version      author      data       introduction and operations
 *  1.0         初见     2022-01-23             Create
 *  ***         ***      ****-**-**             *******
 */

步骤7

接下来,引用标准库头文件。

#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <math.h>
#include <process.h>
#include <io.h>
#include <direct.h>
 /*********************************************************************
  * @chujian(cn) 引入标准库头文件
  * @chujian(en) Import standard library headers
  */

步骤8

我们要使用的是数据结构中的线性表(linear list),这里稍作了解,我是给它如此下定义的:

实际情形下,物理地址与逻辑地址一一对应顺序存储的有穷序列

生活中,英文字母表(A, B, C, ……, Y, Z)就是一个线性表。
每一个有若干个同类型的数据元素(data element),每一个数据元素可以有若干个不同类型的数据项(item),数据元素咱们可以称之为记录(record),而一个咱们可以称之为文件(File),是不是So easy呢?
在本项目中,我们的学生总信息就是一个表,而这个表中就有很多个学生,每一个学生可以有若干个数据项,在这里,我们为它们一一定义,方便后续开发:

  • 线性表结构体:SqList
  • 数据元素结构体:ElemType
  • 系统设置:SysConfig
    以上名称为提高其复用性,不着急向开发项目的相关方向命名。
    SqList结构体中,应包含:
  • 数据元素所在的结构体
  • 线性表长度,与若干个数据元素对应
  • 分配的存储容量,该容量的单位为sizeof(ElemType)
    ElemType结构体中,应包含:
  • 姓名
  • 学号(现实生活中具备唯一性)
  • 性别
  • 年龄
  • 语文分数
  • 数学分数
  • 英语分数
  • 平均分(自动计算)
  • 总分(自动计算)
    SysConfig结构体,应包含:
  • 存储文件路径
  • 备份文件路径
  • 学生信息表的文件路径
  • 控制台颜色设置

而SysConfig结构体中的路径与颜色设置会在最开始读取,所以我们应该从main入口第一个加载它,因此还要在代码中给它一个小小的初始化。

如果以代码的形式,是这样的:

typedef struct
{
	char name[20];
	int num;
	char sex[4];
	int age;
	float score_literature;
	float score_math;
	float score_english;
	float average_score;
	float sum_score;
} ElemType;
/*********************************************************************
 * @chujian(cn) 定义学生信息结构体,包含姓名、学号等
 * @chujian(en) Define the student information structure, including name, student ID, etc.
 */

typedef struct
{
	ElemType* elem;
	int length;
	int list_size;
} SqList;
/*********************************************************************
 * @chujian(cn) 顺序存储结构
 * @chujian(en) Dynamic allocation sequential storage structure of linear table
 */

typedef struct
{
	char file_path[128];
	char backup_path[128];
	char list_path[128];
	char sys_color[32];
} SysConfig;
SysConfig config_bin = {};
/*********************************************************************
 * @chujian(cn) 编译外的设置值
 * @chujian(en) Dynamic allocation sequential storage structure of linear table
 */

当然了,我们要定义存储空间的初始值,暂且设为128,存储空间不足时,我们为之增加一个空间增量,每一次不足的话就按这个量和内存需求来增加,定为16。

#define LIST_INIT_SIZE 128
#define LIST_INCREMENT 16
  /*********************************************************************
   * @chujian(cn) 配置存储空间的初始分配量128,增量16,单位是sizeof(ElemType)
   * @chujian(en) The initial allocation of configuration storage space is 128, in increments of 16, and the unit is sizeof(ElemType)
   */

再者,根据设计需求,我们应该还需定义两个类型,Boolean与Status,以及他们之下的状态值。

typedef int Boolean;
#define TRUE 1
#define FALSE 0

typedef int Status;
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOWED -2
/*********************************************************************
 * @chujian(cn) 定义布尔型、函数返回状态值
 * @chujian(en) Define boolean type, function return status value
 */

preconf.hの完整代码:

#pragma once  // 解决宏名引发的各种奇怪问题/Solve various strange problems caused by macro names
/*********************************************************************
 * 转载请注明来源/Reprint please indicate the source
 * @FileName preconf.h
 * @Description 预配置文件/pre-configured header files
 * @History
 * version      author      data       introduction and operations
 *  1.0         初见     2022-01-23             Create
 *  ***         ***      ****-**-**             *******
 */

#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <math.h>
#include <process.h>
#include <io.h>
#include <direct.h>
 /*********************************************************************
  * @chujian(cn) 引入标准库头文件
  * @chujian(en) Import standard library headers
  */

#define LIST_INIT_SIZE 128
#define LIST_INCREMENT 16
  /*********************************************************************
   * @chujian(cn) 配置存储空间的初始分配量100,增量10,单位是sizeof(ElemType)
   * @chujian(en) The initial allocation of configuration storage space is 100, in increments of 10, and the unit is sizeof(ElemType)
   */

typedef int Boolean;
#define TRUE 1
#define FALSE 0

typedef int Status;
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOWED -2
/*********************************************************************
 * @chujian(cn) 通过枚举,定义布尔型、函数返回状态值
 * @chujian(en) Through enumeration, define boolean type, function return status value
 */

typedef struct
{
	char name[20];
	int num;
	char sex[4];
	int age;
	float score_literature;
	float score_math;
	float score_english;
	float average_score;
	float sum_score;
} ElemType;
/*********************************************************************
 * @chujian(cn) 定义学生信息结构体,包含姓名、学号等
 * @chujian(en) Define the student information structure, including name, student ID, etc.
 */

typedef struct
{
	ElemType* elem;
	int length;
	int list_size;
} SqList;
/*********************************************************************
 * @chujian(cn) 顺序存储结构
 * @chujian(en) Dynamic allocation sequential storage structure of linear table
 */

typedef struct
{
	char file_path[128];
	char backup_path[128];
	char list_path[128];
	char sys_color[32];
} SysConfig;
SysConfig config_bin = {};
/*********************************************************************
 * @chujian(cn) 编译外的设置值
 * @chujian(en) Dynamic allocation sequential storage structure of linear table
 */
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

顾平安6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值