服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++实现宿舍管理查询系统

C++实现宿舍管理查询系统

2022-10-21 13:54DialogD C/C++

这篇文章主要为大家详细介绍了C++实现宿舍管理查询系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++实现宿舍管理查询系统的具体代码,供大家参考,具体内容如下

C++使用IO流关联.txt文件

各模块之间的调用关系如下:

C++实现宿舍管理查询系统

函数的调用关系反映了演示程序的层次结构:

C++实现宿舍管理查询系统

代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdlib>
#include<string>
using namespace std;
#define MAXSIZE 100     //顺序表的最大长度
typedef struct {
    string name;        //姓名
    string id;          //学号
    string dormid;      //宿舍号
}Student;
typedef struct {
    Student r[MAXSIZE + 1];     //r[0]做单元哨兵
    int length;//长度
}SqList;
 
 
//用直接插入排序存入到student.txt文件中
void InsertSort(SqList &stu)
{
    int i, j;
    for (i = 2; i <= stu.length; i++)
        if (stu.r[i].id < stu.r[i - 1].id)
        {
            stu.r[0] = stu.r[i];
            stu.r[i] = stu.r[i - 1];
            for (j = i - 2; stu.r[0].id < stu.r[j].id; j--)
                stu.r[j + 1] = stu.r[j];
            stu.r[j + 1] = stu.r[0];
        }
    ofstream outfile("student.txt", ios::out);
    if (!outfile) {                             //如果文件打开失败
        cout << "文件打开失败" << endl;
        exit(1);
    }
    //outfile << "学号" << setw(8) << "姓名" << setw(8) << "宿舍号" << endl;
    outfile << stu.length << endl;
 
    for (i = 1; i <= stu.length; i++) {
        outfile << stu.r[i].id << setw(8) << stu.r[i].name << setw(8) << stu.r[i].dormid << endl;
    }
    cout << "学生信息数:" << stu.length << endl;
    outfile.close();
    cout << stu.length;
}
 
//创建学生信息(只能创建一次,不然会被刷新)
void InitList(SqList &stu)
{
 
    int i;
    cout << "学号" << setw(8) << "姓名" << setw(8) << "宿舍号" << endl;
    for (i = 1; i <= stu.length; i++) {
        cin >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
    }
    InsertSort(stu);
}
 
//增加学生信息
void Addstudent(SqList &stu)
{
    int n;
    int i = stu.length + 1;
    cout << "输入增加学生人数" << endl;
    cin >> n;
    cout << "学号" << setw(8) << "姓名" << setw(8) << "宿舍号" << endl;
    stu.length = stu.length + n;
    for (i; i <= stu.length; i++)
        cin >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
    InsertSort(stu);
}
 
//查询学生信息
void Findstudent(SqList &stu)
{
    string a, b, c;
    string name, id, dormid;
    cout << "1.学号查询  2.姓名查询  3.宿舍号查询" << endl;
    cout << "请输入你的查询选择(1~3)" << endl;
    int i;
    int n;
    cin >> n;
    if (n < 1 && n>3)
    {
        cout << "您输入有误,请重新输入:" << endl;
        Findstudent(stu);
    }
    if (1 == n)
    {
        cout << "请输入学生学号:" << endl;
        cin >> id;
 
        ifstream infile("student.txt", ios::in);//定义输入文件流对象,以输入方式打开磁盘文件"student.txt"
        infile >> stu.length;
        for (i = 1; i <= stu.length; i++) {
            infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
        }
        infile.close();
        for (i = 1; i <= stu.length; i++)
        {
            infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
            if (stu.r[i].id == id)
                cout << stu.r[i].id << setw(8) << stu.r[i].name << setw(8) << stu.r[i].dormid << endl;
        }
 
        infile.close();//关闭磁盘文件
 
    }
    if (2 == n)
    {
        cout << "请输入学生姓名:" << endl;
        cin >> name;
 
        ifstream infile("student.txt", ios::in);//定义输入文件流对象,以输入方式打开磁盘文件"student.txt"
        infile >> stu.length;
        for (i = 1; i <= stu.length; i++) {
            infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
        }
        infile.close();
        for (i = 1; i <= stu.length; i++)
        {
            infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
            if (stu.r[i].name == name)
                cout << stu.r[i].id << setw(8) << stu.r[i].name << setw(8) << stu.r[i].dormid << endl;
        }
 
        infile.close();//关闭磁盘文件
    }
    if (3 == n)
    {
        cout << "请输入学生宿舍号:" << endl;
        cin >> dormid;
 
        ifstream infile("student.txt", ios::in);//定义输入文件流对象,以输入方式打开磁盘文件"student.txt"
        infile >> stu.length;
        for (i = 1; i <= stu.length; i++) {
            infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
        }
        infile.close();
        for (i = 1; i <= stu.length; i++)
        {
            infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
            if (stu.r[i].dormid == dormid)
                cout << stu.r[i].id << setw(8) << stu.r[i].name << setw(8) << stu.r[i].dormid << endl;
        }
    }
 
}
 
//修改学生信息
void Renewstudent(SqList &stu)
{
    int n;
    string id, name, dormid;
    cout << "1.姓名  2.宿舍号" << endl;
    cout << "请输入您的选择(1~2):" << endl;
    cin >> n;
    cout << "请输入需要修改学生的学号" << endl;
    cin >> id;
    if (n != 1 && n != 2)
    {
        cout << "输入有误,请重新输入:" << endl;
        Renewstudent(stu);
    }
    if (1 == n)
    {
        cout << "请输入修改姓名" << endl;
        cin >> name;
        int i = 0;
        ifstream infile("student.txt", ios::in);
        infile >> stu.length;
        for (i = 1; i <= stu.length; i++) {
            infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
        }
        infile.close();
        for (i = 1; i <= stu.length; i++)//先找到再修改
        {
            if (stu.r[i].id == id)
            {
                stu.r[i].name = name;
                InsertSort(stu);
                return;
            }
        }
    }
    if (2 == n)
    {
        int i;
        cout << "请输入修改宿舍号" << endl;
        cin >> dormid;
        ifstream infile("student.txt", ios::in);
        infile >> stu.length;
        for (i = 1; i <= stu.length; i++) {
            infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
        }
        infile.close();
        for (i = 1; i <= stu.length; i++)//先找到再修改
        {
            if (stu.r[i].id == id)
            {
                stu.r[i].dormid = dormid;
                InsertSort(stu);
                return;
            }
        }
    }
 
}
 
//显示宿舍信息
void Showstudent(SqList &stu)
{
    string a, b, c;
    int i;
    cout << "学生的信息如下:" << endl;
    cout << "**********************************" << endl;
    ifstream infile("student.txt", ios::in);
    if (!infile) {                              //如果文件打开失败
        cout << "文件打开失败" << endl;
        exit(1);
    }
    /*infile >> a >> b >> c;//从磁盘文件读入
    cout << a << setw(8) << b << setw(8) << c << endl;//在显示器上显示*/
    infile >> stu.length;
    for (i = 1; i <= stu.length; i++) {
        infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
        cout << stu.r[i].id << setw(8) << stu.r[i].name << setw(8) << stu.r[i].dormid << endl;
    }
    infile.close();
}
 
//删除宿舍信息
void Deletstudent(SqList &stu)
{
    int  i, j;
    string a, b, c, id;
    cout << "请输入删除学生学号" << endl;
    cin >> id;
    ifstream infile("student.txt", ios::in);//定义输入文件流对象,以输入方式打开磁盘文件"student.txt"
    infile >> stu.length;
    for (i = 1; i <= stu.length; i++) {
        infile >> stu.r[i].id >> stu.r[i].name >> stu.r[i].dormid;
    }
    infile.close();
    for (i = 1; i <= stu.length; i++)//先找到再删除
    {
        if (stu.r[i].id == id)
        {
            for (j = i; j<stu.length; j++)
                stu.r[j] = stu.r[j + 1];
            stu.length--;
            InsertSort(stu);
            return;
        }
    }
}
 
//主函数
int main()
{
    SqList stu;
    int n;
    for (;;)
    {
        cout << "**************************宿舍管理查询软件**************************" << endl;
        cout << "1. 创建学生信息" << endl;                        //InitList
        cout << "2. 增加学生信息" << endl;                        //Addstudent    
        cout << "3. 查询学生信息" << endl;                        //Findstudent
        cout << "4. 显示学生信息" << endl;                        //Showstudent
        cout << "5. 修改学生信息" << endl;                        //Renewstudent
        cout << "6. 删除学生信息" << endl;                        //Deletstudent
        cout << "0. 退出系统" << endl;
        cout << "*******************************************************************" << endl;
        cout << "请输入你需要的操作(0~6):" << endl;
        cin >> n;
        switch (n)
        {
        case 1:
            cout << "输入学生人数" << endl;
            cin >> stu.length;
            InitList(stu);
            cout << "###########################################" << endl;
            break;
        case 2:
            Addstudent(stu);
            cout << "###########################################" << endl;
            break;
        case 3:
            Findstudent(stu);
            cout << "###########################################" << endl;
            break;
        case 4:
            Showstudent(stu);
            cout << "###########################################" << endl;
            break;
        case 5:
            Renewstudent(stu);
            cout << "###########################################" << endl;
            break;
        case 6:
            Deletstudent(stu);
            cout << "###########################################" << endl;
            break;
        case 0:
            cout << "您已退出系统!" << endl;
            return 0;
        }
 
    }
    system("pause");
    return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/DialogD/article/details/77758238

延伸 · 阅读

精彩推荐