学生管理系统思路
1. 结构体
2. 动态分配内存
3. 文件操作
.h
.c
函数
1.输入管理员密码
添加数据
2.界面
****************
1. 增加
学号
姓名
年龄
2. 删除
查找
删除
3. 更改
查找
更改
4. 查找
姓名
学号
5. 退出
是否保存更改
****************
代码
1.文件
文件
2.Constant.h文件
#ifndef Constant_h
#define Constant_h
//声明学生的信息
typedef struct student{
long idNumber;
char name[20];
int score;
}StudentInfo;
//管理员密码
#define kPassword 1234
//密码输入次数
#define kWrongTime 4
//定义一个无效的数字
#define kInvalid -2018
//文件的路径
#define kFilePath "/Users/xulei/Desktop/studentInfo.txt"
#endif /* Constant_h */
3.Manager.h文件
#ifndef Manager_h
#define Manager_h
#include <stdio.h>
#include <stdbool.h>
#include "Constant.h"
//编号,static静态变量,只能被定义一次
//静态变量并不是说其就不能改变值,不能改变值的量叫常量。,其拥有的值是可变的 ,而且它会保持最新的值,说其静态,是因为它不会随着函数的调用和退出而发生变化,即上次调用的时候,如果我们给静态变量赋予某个值的话,下次函数调用时,这个值保持不变
static int totoINumber = 0;
//登录函数,返回值,true:登陆成功,false:登录失败
bool sign(void);
//展示内容函数
void show(char *content);
//选择操作函数
int choice(int max);
//添加数据函数
void addStudent(StudentInfo *studentDate);
//输出信息函数
void list(StudentInfo *studentDate);
//退出函数
void end(void);
#endif /* Manager_h */
4.Manager.m文件
#include "Manager.h"
#include <stdlib.h>
//登录函数
bool sign(void){
int inputPwd = 0;
int count = 0;
show("请输入管理员密码");
while (1) {
scanf("%d",&inputPwd);
if (inputPwd == kPassword) {
return true;
} else {
show("密码错误 请重新输入");
count++;
if (count == kWrongTime) {
show("错误次数过多 退出!");
return false;
}
}
}
}
//展示内容函数
void show(char *content){
printf("*****************\n");
printf("%s\n",content);
printf("*****************\n");
}
//选择操作函数
int choice(int max){
int operation = kInvalid;
float input = 0.0;
do {
if (operation != kInvalid) {
printf("输入无效 请重新输入");
exit(0);
}
scanf("%f",&input);
//确保是整数
operation = (int)input;
} while (operation <= 0 || operation > 5);
return operation;
}
//添加数据函数
void addStudent(StudentInfo *studentDate){
printf("请输入姓名:");
scanf("%s",studentDate->name);
printf("请输入分数:");
scanf("%d",&(studentDate->score));
//自动为这个学生添加编号
studentDate->idNumber = totoINumber + 1;
//添加学生
studentDate[totoINumber] = *studentDate;
totoINumber++;
//写入到文本中
FILE* file = NULL;
file = fopen(kFilePath, "a");
if (file == NULL) {
printf("error");
}
fprintf(file, "编号:%d 姓名:%s 分数:%d\n",totoINumber,studentDate->name,studentDate->score);
fclose(file);
}
//输出信息函数
void list(StudentInfo *studentDate){
FILE* file = NULL;
char string[1000] = " ";//保证数组足够大
file = fopen(kFilePath, "r");
if (file == NULL) {
printf("error");
}
while (fgets(string, 1000, file) != NULL) {//只要某一行不是空值就读取它
printf("%s\n",string);
}
fclose(file);
}
//退出函数
void end(void){
printf("是否退出(y/n)\n");
getchar();
char c = getchar();
if (c == 'y') {
exit(0);
}
}
5.main.c文件
#include <stdio.h>
#include <stdlib.h>
#include "Manager.h"
int main(int argc, const char * argv[]) {
//定义数组保存学生信息
StudentInfo student[20] = {};
//登录
bool result = sign();
if (result == false) {
exit(0);
}
//界面
while (1) {
//显示操作界面
show("1.增加\n2.删除\n3.更改\n4.查询\n5.退出");
//等待用户选择
int operation = choice(5);
switch (operation) {
case 1:
//增加
addStudent(student);
break;
case 2:
//删除
//删除文件部分内容的大概步骤:新建一个临时文件,把原文件内容向临时文件里拷贝,遇到要删除的内容就跳过。结束后关闭文件,用remove("原文件名");把原文件删除,用rename("临时文件名","原文件名");把临时文件名改为原文件名
break;
case 3:
//更改
//第1种、将文件中数据读入内存中,修改后,清空源文件,存入新数据
// 第2种、以读写的方式打开文件,将文件指针移动到要修改的地方,写入新数据。新数据将会覆盖掉旧数据
//第3种、以读写的方式打开文件,将文件指针定位到需要修改数据的末尾,然后删除需要修改的数据(通过循环n次执行fputc(8,fp),直到清空需要修改的数据为止,8为退格键对应的ascii),然后计算需要加入的新数据长度,通过fputc(32,fp)来添加空格到文件中(32为空格键的ascii). 然后根据指针位置,填入数据覆盖掉这些空格
break;
case 4:
//查询
list(student);
break;
case 5:
//退出
//保存更改的程序
end();
exit(0);
break;
default:
break;
}
}
return 0;
}
6.运行结果
*****************
请输入管理员密码
*****************
123
*****************
密码错误 请重新输入
*****************
1234
*****************
1.增加
2.删除
3.更改
4.查询
5.退出
*****************
1
请输入姓名:xulei
请输入分数:99
*****************
1.增加
2.删除
3.更改
4.查询
5.退出
*****************
1
请输入姓名:wurui
请输入分数:99
*****************
1.增加
2.删除
3.更改
4.查询
5.退出
*****************
4
编号:1 姓名:xulei 分数:99
编号:2 姓名:wurui 分数:99
*****************
1.增加
2.删除
3.更改
4.查询
5.退出
*****************
5
是否退出(y/n)
y
Program ended with exit code: 0
创建的文本