學(xué)生管理系統(tǒng)思路
1. 結(jié)構(gòu)體
2. 動態(tài)分配內(nèi)存
3. 文件操作
.h
.c
函數(shù)
1.輸入管理員密碼
添加數(shù)據(jù)
2.界面
****************
1. 增加
學(xué)號
姓名
年齡
2. 刪除
查找
刪除
3. 更改
查找
更改
4. 查找
姓名
學(xué)號
5. 退出
是否保存更改
****************
代碼
1.文件

文件
2.Constant.h文件
#ifndef Constant_h
#define Constant_h
//聲明學(xué)生的信息
typedef struct student{
long idNumber;
char name[20];
int score;
}StudentInfo;
//管理員密碼
#define kPassword 1234
//密碼輸入次數(shù)
#define kWrongTime 4
//定義一個無效的數(shù)字
#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靜態(tài)變量,只能被定義一次
//靜態(tài)變量并不是說其就不能改變值,不能改變值的量叫常量。,其擁有的值是可變的 ,而且它會保持最新的值,說其靜態(tài),是因為它不會隨著函數(shù)的調(diào)用和退出而發(fā)生變化,即上次調(diào)用的時候,如果我們給靜態(tài)變量賦予某個值的話,下次函數(shù)調(diào)用時,這個值保持不變
static int totoINumber = 0;
//登錄函數(shù),返回值,true:登陸成功,false:登錄失敗
bool sign(void);
//展示內(nèi)容函數(shù)
void show(char *content);
//選擇操作函數(shù)
int choice(int max);
//添加數(shù)據(jù)函數(shù)
void addStudent(StudentInfo *studentDate);
//輸出信息函數(shù)
void list(StudentInfo *studentDate);
//退出函數(shù)
void end(void);
#endif /* Manager_h */
4.Manager.m文件
#include "Manager.h"
#include <stdlib.h>
//登錄函數(shù)
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("錯誤次數(shù)過多 退出!");
return false;
}
}
}
}
//展示內(nèi)容函數(shù)
void show(char *content){
printf("*****************\n");
printf("%s\n",content);
printf("*****************\n");
}
//選擇操作函數(shù)
int choice(int max){
int operation = kInvalid;
float input = 0.0;
do {
if (operation != kInvalid) {
printf("輸入無效 請重新輸入");
exit(0);
}
scanf("%f",&input);
//確保是整數(shù)
operation = (int)input;
} while (operation <= 0 || operation > 5);
return operation;
}
//添加數(shù)據(jù)函數(shù)
void addStudent(StudentInfo *studentDate){
printf("請輸入姓名:");
scanf("%s",studentDate->name);
printf("請輸入分?jǐn)?shù):");
scanf("%d",&(studentDate->score));
//自動為這個學(xué)生添加編號
studentDate->idNumber = totoINumber + 1;
//添加學(xué)生
studentDate[totoINumber] = *studentDate;
totoINumber++;
//寫入到文本中
FILE* file = NULL;
file = fopen(kFilePath, "a");
if (file == NULL) {
printf("error");
}
fprintf(file, "編號:%d 姓名:%s 分?jǐn)?shù):%d\n",totoINumber,studentDate->name,studentDate->score);
fclose(file);
}
//輸出信息函數(shù)
void list(StudentInfo *studentDate){
FILE* file = NULL;
char string[1000] = " ";//保證數(shù)組足夠大
file = fopen(kFilePath, "r");
if (file == NULL) {
printf("error");
}
while (fgets(string, 1000, file) != NULL) {//只要某一行不是空值就讀取它
printf("%s\n",string);
}
fclose(file);
}
//退出函數(shù)
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[]) {
//定義數(shù)組保存學(xué)生信息
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:
//刪除
//刪除文件部分內(nèi)容的大概步驟:新建一個臨時文件,把原文件內(nèi)容向臨時文件里拷貝,遇到要刪除的內(nèi)容就跳過。結(jié)束后關(guān)閉文件,用remove("原文件名");把原文件刪除,用rename("臨時文件名","原文件名");把臨時文件名改為原文件名
break;
case 3:
//更改
//第1種、將文件中數(shù)據(jù)讀入內(nèi)存中,修改后,清空源文件,存入新數(shù)據(jù)
// 第2種、以讀寫的方式打開文件,將文件指針移動到要修改的地方,寫入新數(shù)據(jù)。新數(shù)據(jù)將會覆蓋掉舊數(shù)據(jù)
//第3種、以讀寫的方式打開文件,將文件指針定位到需要修改數(shù)據(jù)的末尾,然后刪除需要修改的數(shù)據(jù)(通過循環(huán)n次執(zhí)行fputc(8,fp),直到清空需要修改的數(shù)據(jù)為止,8為退格鍵對應(yīng)的ascii),然后計算需要加入的新數(shù)據(jù)長度,通過fputc(32,fp)來添加空格到文件中(32為空格鍵的ascii). 然后根據(jù)指針位置,填入數(shù)據(jù)覆蓋掉這些空格
break;
case 4:
//查詢
list(student);
break;
case 5:
//退出
//保存更改的程序
end();
exit(0);
break;
default:
break;
}
}
return 0;
}
6.運行結(jié)果
*****************
請輸入管理員密碼
*****************
123
*****************
密碼錯誤 請重新輸入
*****************
1234
*****************
1.增加
2.刪除
3.更改
4.查詢
5.退出
*****************
1
請輸入姓名:xulei
請輸入分?jǐn)?shù):99
*****************
1.增加
2.刪除
3.更改
4.查詢
5.退出
*****************
1
請輸入姓名:wurui
請輸入分?jǐn)?shù):99
*****************
1.增加
2.刪除
3.更改
4.查詢
5.退出
*****************
4
編號:1 姓名:xulei 分?jǐn)?shù):99
編號:2 姓名:wurui 分?jǐn)?shù):99
*****************
1.增加
2.刪除
3.更改
4.查詢
5.退出
*****************
5
是否退出(y/n)
y
Program ended with exit code: 0

創(chuàng)建的文本