1. 程式人生 > 其它 >結構體陣列實現的簡易學生資訊管理系統

結構體陣列實現的簡易學生資訊管理系統

技術標籤:筆記

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<fstream>
#include<conio.h>
using namespace std;
const int maxx = 4;
struct student {
	int idnum;
	char name[40];
	char sex;
	int age;
}stu[maxx];//最多新增4個學生
void choice();
void dell();
void xiugai();
void baocun();
void display() { cout << "-----------------------學生管理選單----------------------\n"; cout << "1 新增學生" << " " << "2 刪除學生 3 查詢學生資訊 4修改資訊 5儲存 6退出" << endl; } void init() { //初始化函式 for (int i = 0; i < maxx; i++) { stu[i].name[
40] = '\0'; stu[i].sex = '\0'; stu[i].age = 0; } } void add() { int k = 0, j; for (int i = 0; i < maxx; i++) { if (stu[i].sex == '\0') k++; } cout << "剩餘" << k << "個記錄可以新增\n"; cout << "請輸入你要新增的學生個數;\n"; cin >> j; if (j > k) { cout <<
"error number"; cout << endl; display(); choice(); } else for (int i = 0; i < j; i++) { for (int h = 0; h < maxx; h++) { if (stu[h].sex == '\0') { cout << "請輸入第" << i + 1 << "個學生學號 姓名 性別(用m表示男 w表示女)年齡" << endl; cin >> stu[h].idnum >> stu[h].name >> stu[h].sex >> stu[h].age; for (int g = 0; g < maxx; g++) { if (stu[g].idnum == stu[h].idnum && g != h) { cout << "該學號不能新增" << endl; display(); } } if (stu[h].sex != 'w' && stu[h].sex != 'm') { stu[h].sex = '\0'; cout << "性別輸入錯誤\n"; display(); } if (stu[h].age < 1 || stu[h].age >100) { stu[h].sex = '\0'; cout << "年齡輸入錯誤" << endl; } break; } } cout << "\n學生新增成功\n"; } display(); choice(); } void dell() { //刪除功能 int id,count=0; cout << "您想刪除的學生學號:\n"; cin >> id; for (int i = 0; i < maxx; i++) { if (stu[i].idnum == id) { count++; stu[i].idnum = -1; stu[i].name[40] = '\0'; stu[i].sex = '\0'; stu[i].age = 0; cout << "刪除成功" << endl; break; } } if (count == 0) { cout << "該學號不存在\n"; } display(); choice(); } void inqui() { int id , count = 0; cout << "您想查詢的學生學號(輸入0檢視所有學生):\n"; cin >> id; if (id == 0) { for (int i = 0; i < maxx; i++) { if (stu[i].sex != '\0') cout << stu[i].idnum << " " << stu[i].name << " " << stu[i].sex << " " << stu[i].age << " \n"; } } else { for (int i = 0; i < maxx; i++) { if (stu[i].idnum == id) { count++; cout << stu[i].idnum << " " << stu[i].name << " " << stu[i].sex << " " << stu[i].age << " "; } } if (count == 0) cout << "不存在這個學號的學生!\n"; } display(); choice(); } void xiugai() { int count = 0; cout << "您想修改的學生學號:\n"; int id; cin >> id; int i; for (i = 0; i < maxx; i++) { if (stu[i].idnum == id) { count++; cout << "您要修改的學生資訊:"; cout << stu[i].idnum << " " << stu[i].name << " " << stu[i].sex << " " << stu[i].age << " \n"; cout << "請重新輸入資訊 按學號 姓名 性別 年齡輸入;\n"; cin >> stu[i].idnum >> stu[i].name >> stu[i].sex >> stu[i].age; cout << "已經修改"; break; } } if (count == 0) cout << "該學號不存在!\n"; \ display(); choice(); } void baocun() { FILE* mysfile = NULL; mysfile = fopen("ooo.txt", "r"); for (int i = 0; i < maxx; i++) { fscanf(mysfile, "%d", &stu[i].idnum); fscanf(mysfile, "%s",stu[i].name); fscanf(mysfile, "%s", stu[i].sex); fscanf(mysfile, "%d", stu[i].age); } cout << "資訊已經儲存\n"; fclose(mysfile); display(); choice(); } void exit() { system("cls"); exit(1); } void choice() { int a; cout << "請選擇功能:\n"; cin >> a; if (a == 1 || a == 2 || a == 3 || a == 4) { switch (a) { case 1:add(); case 2: dell(); break; case 3:inqui(); break; case 4:xiugai(); break; case 5:baocun(); break; case 6:exit(); break; default: cout << "輸入選擇有錯誤!\n"; break; } } } int main() { init(); display(); choice(); }