Hello,I am Xpsc.

努力改变世界,即使微不足道。

导航

使用结构体数组与指针实现的命令行人员信息管理系统(C++)

水平有限,只是想记录一下,如果能够帮到你,那必定是我的荣幸!

GitHub仓库地址:”AzureXCC/ConsoleInfManagerSys: 使用结构体数组和指针实现的命令行人员信息管理系统。 (github.com)

效果图:

 

 

 

 

 

项目结构:

 

 

 源代码:

  1 #include <iostream>
  2 #include "infManager.h"
  3 #include <conio.h>
  4 
  5 
  6 void PrintLine()
  7 {
  8     printf("___________________________________________________________________\n");
  9 }
 10 
 11 struct Person person[PERSON_NUM];
 12 int flag = 1;
 13 
 14 int ReceiveKeyCode()
 15 {
 16     char ch = _getch();
 17     switch (ch)
 18     {
 19     case '0':
 20     {    
 21         PrintLine();
 22         printf("\n请依次键入“姓名”、“班级代号”、“生日日期(xxxx.xx.xx)”、“专业类别”、“成绩等级”、“年龄”,以“空格”或“换行”相隔。\n");
 23         string name = ""; int classN = 0; string bd = ""; string mj = ""; string acm = ""; short age = 0;
 24         cin >> name >> classN >> bd >> mj>>acm  >> age;
 25         int result = AddPerson(person, name,classN,bd,mj,acm,age);
 26         if (result)
 27         {
 28             printf("\n添加失败,可能键入了非法值。\n");
 29         }
 30         break;
 31     }
 32     case '1':
 33         DisplayAllPerson(person);
 34         break;
 35     case '2':
 36     {
 37         printf("\n请键入要删除的人员的姓名:");
 38         string name = "";
 39         cin >> name;
 40         int result = DeletePerson(person,name);
 41         if (result)
 42         {
 43             printf("\n删除失败,可能没有此人。\n");
 44         }
 45         break;
 46     }
 47     case '3':
 48     {
 49         string name = "";
 50         PrintLine();
 51         printf("\n键入名称以查找此人。\n");
 52         cin >> name;
 53         int result = FindPerson(person, name);
 54         if (result)
 55         {
 56             printf("\n查找失败,可能没有此人。\n");
 57         }
 58         break;
 59     }
 60     case '4':
 61     {
 62         string name = "";
 63         string nameNew = ""; int classNew = 0; string bdNew = ""; string mjNew = ""; string acmNew = ""; short ageNew = 0;
 64         PrintLine();
 65         printf("\n键入名称以编辑此人员信息。\n");
 66         cin >> name;
 67         cout << endl << "输入新信息数据,依次键入“姓名”、“班级代号”、“生日日期(xxxx.xx.xx)”、“专业类别”、“成绩等级”、“年龄”,以“空格”或“换行”相隔:";
 68         cin >> nameNew >> classNew >> bdNew >> mjNew >> acmNew >> ageNew;
 69         int result = EditPerson(person, name,nameNew,classNew,bdNew,mjNew,acmNew,ageNew);
 70         if (result)
 71         {
 72             printf("\n编辑失败,可能有非法值或未查询到此人员。\n");
 73         }
 74         break;
 75     }
 76     case '5':
 77     {
 78         PrintLine();
 79         printf("\n确定要清空通讯列表的所有内容吗?【确定】/【否】:\n");
 80         string sss = "";
 81         cin >> sss;
 82         if (sss == "确定")
 83         {
 84             int result = CleanAllInf(person);
 85             if (result)
 86             {
 87                 printf("\n删除失败。\n");
 88             }
 89         }
 90         else
 91         {
 92             printf("\n退出。\n");
 93         }
 94         break;
 95     }
 96     case '6':
 97     {
 98         PrintLine();
 99         printf("\n确定退出系统吗?【确定】/【否】:\n");
100         string sss = "";
101         cin >> sss;
102         if (sss == "确定")
103         {
104             int result = ExitSystem(&flag);
105             flag = 0;
106             if (result)
107             {
108                 printf("\n退出失败。\n");
109             }
110         }
111         else
112         {
113             printf("\n未退出。\n");
114         }
115         break;
116     }
117     default:
118         return -1;
119         break;
120     }
121     return 0;
122 }
123 int main()
124 {
125     DisplayFucList();
126     while (flag)
127     {
128         ReceiveKeyCode();
129     }
130 
131     return 0;
132 }
主文件

 

 1 #pragma once
 2 #include <iostream>
 3 
 4 using namespace std;
 5 #define PERSON_NUM 1000
 6 
 7 struct Person
 8 {
 9     string name = "";
10     int classNum = 0;
11     string birthdayDate = "";
12     string major = "";
13     string achievement = "";
14     short age = 0;
15 
16 };
17 int DisplayFucList();
18 int AddPerson(struct Person* p,string name,int classNum,string bd,string mj,string acm,short age);
19 int DisplayAllPerson(struct Person* ppp);
20 int DeletePerson(struct Person* ppp, string name);
21 int FindPerson(struct Person* ppp, string name);
22 int EditPerson(struct Person* ppp, string name, string nameN, int classNumN, string bdN, string mjN, string acmN, short ageN);
23 int CleanAllInf(struct Person* ppp);
24 int ExitSystem(int* p);
头文件声明

 

  1 #include "infManager.h"
  2 #include <process.h>
  3 
  4 int personNum = 0;
  5 
  6 int DisplayFucList()//显示功能列表
  7 {
  8     cout << endl << "[0] 添加联络人" << endl << endl;
  9     cout << "[1] 显示所有人员的信息" << endl << endl;
 10     cout << "[2] 根据名称删除人员" << endl << endl;
 11     cout << "[3] 根据名称查找人员" << endl << endl;
 12     cout << "[4] 根据名称编辑人员信息" << endl << endl;
 13     cout << "[5] 清空联络录" << endl << endl;
 14     cout << "[6] 退出系统" << endl;
 15     return 0;
 16 }
 17 int AddPerson(struct Person* ppp, string name, int classNum, string bd, string mj, string acm, short age)//添加联络人
 18 {
 19     if (personNum == PERSON_NUM)
 20     {
 21         system("cls");
 22         cout << endl << "人数已达到上限!请尝试删除一些人员或联系技术人员。" << endl;    
 23         DisplayFucList();
 24         return -1;
 25     }
 26     else
 27     {
 28         for (int i = 0; i < PERSON_NUM; i++)
 29         {
 30             if (ppp[i].name == "" && ppp[i].classNum == 0 && ppp[i].birthdayDate == "" && ppp[i].major == "" && ppp[i].achievement == "" && ppp[i].age == 0)
 31             {
 32                 personNum++;
 33                 ppp[i].name = name; ppp[i].classNum = classNum; ppp[i].birthdayDate = bd; ppp[i].major = mj; ppp[i].achievement = acm; ppp[i].age = age;
 34                 i = PERSON_NUM + 10;
 35                 break;
 36             }
 37         }
 38         system("cls");
 39         cout << endl <<"添加成功。" << endl;
 40         DisplayFucList();
 41         return 0;
 42     }
 43     
 44 }
 45 int DisplayAllPerson(struct Person*  ppp)//显示所有人员的信息
 46 {
 47     system("cls");
 48     for (int i = 0; i < PERSON_NUM; i++)
 49     {    
 50         if(ppp[i].name != "")
 51             cout << endl << ppp[i].name << endl << ppp[i].classNum << endl << ppp[i].birthdayDate << endl << ppp[i].major << endl << ppp[i].achievement << endl << ppp[i].age << endl;
 52     }
 53     cout << endl << "显示完毕。" << endl;
 54     DisplayFucList();
 55     return 0;
 56 }
 57 int DeletePerson(struct Person* ppp, string name)//根据名称删除人员
 58 {
 59     int dNum = 0;
 60     for (int i = 0; i < PERSON_NUM; i++)
 61     {
 62         if (ppp[i].name == name)
 63         {
 64             personNum--;
 65             dNum++;
 66             ppp[i].name = ""; ppp[i].classNum = 0; ppp[i].birthdayDate = ""; ppp[i].major = ""; ppp[i].achievement = ""; ppp[i].age = 0;
 67 
 68         }
 69     }
 70     system("cls");
 71     cout << endl << "删除完毕,共删除 " << dNum << " 个人员。" << endl;
 72     DisplayFucList();
 73     return 0;
 74 }
 75 int FindPerson(struct Person* ppp, string name)//根据名称查找人员
 76 {
 77     int fNum = 0;
 78     system("cls");
 79     for (int i = 0; i < PERSON_NUM; i++)
 80     {
 81         if (ppp[i].name == name)
 82         {
 83             fNum++;
 84             cout << endl << ppp[i].name << endl << ppp[i].classNum << endl << ppp[i].birthdayDate << endl << ppp[i].major << endl << ppp[i].achievement << endl << ppp[i].age << endl;
 85         }
 86     }
 87     cout << endl << "检索完毕,共查找到 " << fNum << " 名人员。" << endl;
 88     DisplayFucList();
 89     return 0;
 90 }
 91 int EditPerson(struct Person* ppp, string name, string nameN, int classNumN, string bdN, string mjN, string acmN, short ageN)//根据名称编辑人员信息
 92 {
 93     int dNum = 0;
 94     for (int i = 0; i < PERSON_NUM; i++)
 95     {
 96         if (ppp[i].name == name)
 97         {
 98             dNum++;
 99             ppp[i].name = nameN; ppp[i].classNum = classNumN; ppp[i].birthdayDate = bdN; ppp[i].major = mjN; ppp[i].achievement = acmN; ppp[i].age = ageN;
100 
101         }
102     }
103     system("cls");
104     cout << endl << "编辑完成!共编辑了 " << dNum << " 位人员的信息。" << endl;
105     DisplayFucList();
106     return 0;
107 }
108 int CleanAllInf(struct Person* ppp )//清空联络录
109 {
110     for (int i = 0; i < PERSON_NUM; i++)
111     {
112         ppp[i].name = ""; ppp[i].classNum = 0; ppp[i].birthdayDate = ""; ppp[i].major = ""; ppp[i].achievement = ""; ppp[i].age = 0;
113 
114     }
115     system("cls");
116     cout << endl << "您已清空数据库数据,误操作请联系技术人员,由其将您押送至附近的公安局。" << endl;
117     DisplayFucList();
118     return 0;
119 }
120 int ExitSystem(int * p)//退出系统
121 {
122     *p = 0;
123     cout << endl << "退出了系统!" << endl;
124     return 0;
125 }
头文件实现

 

共同学习,欢迎留言,有问必答。

 

posted on 2022-04-08 15:47  CeruleanXpsc  阅读(55)  评论(0)    收藏  举报