1、实训报告:github实训报告

2、代码:

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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//基于多级文件目录的文件管理系统
#include<bits/stdc++.h>
#include <windows.h>
#include <stdio.h>
#include <ctime>
#include <cstdio>
#define endl '\n'
using namespace std;

const int N = 1e4 + 10;
const int M = 110;
struct file{
string name;
vector<string> vet; //当前文件中存储的内容
string where;//用来记录当前文件的位置
int year,month,day,hour,minute,seconds;
int _size;
}b[N];
struct directory{
string name;
vector< pair<string,int> > hold;//记录当前文件存储的文件,pair表示当前的存储是文件还是目录
map<string,int> is_file; //当前目录下是否存在某个名字的文件
map<string,int> is_directory; //当前目录下是否存在某个目录
string where;//用来记录当前目录的位置
string pre;//上一个文件,用来回到上一个目录
int year,month,day,hour,minute,seconds;
int _size;
}a[N];
vector< pair<string,int> > fid[N];//存储某个名字所有出现的位置,如果second = 0,表示已经被删除
map<string,int > file_directory;//找到某个文件或者目录存储的位置
int cnt;//表示每个名字对应的位置
int cnt1, cnt2;//表示存储的文件和目录的位置
int pos = 0;//表示当前所在目录的坐标
char place[N];//表示当前位置
//1表示目录,0表示文件

void manage();

//改变字符显示颜色
void COLOR_PRINT(const char* s, int color){
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | color);
printf(s);
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | 7);
}


void get_systime(int tmp_stt, int tmp_cnt){ //tmp_cnt表示要进行操作的文件或目录的位置
time_t rawtime;
struct tm *ptminfo;
time(&rawtime);
ptminfo = localtime(&rawtime);
if(tmp_stt == 1){ //为1表示要对目录时间信息进行更新
a[tmp_cnt].year = ptminfo->tm_year + 1900;
a[tmp_cnt].month = ptminfo->tm_mon + 1;
a[tmp_cnt].day = ptminfo->tm_mday;
a[tmp_cnt].hour = ptminfo->tm_hour;
a[tmp_cnt].minute = ptminfo->tm_min;
a[tmp_cnt].seconds = ptminfo->tm_sec;
}
else{ //为0表示要对文件时间信息进行更新
b[tmp_cnt].year = ptminfo->tm_year + 1900;
b[tmp_cnt].month = ptminfo->tm_mon + 1;
b[tmp_cnt].day = ptminfo->tm_mday;
b[tmp_cnt].hour = ptminfo->tm_hour;
b[tmp_cnt].minute = ptminfo->tm_min;
b[tmp_cnt].seconds = ptminfo->tm_sec;
}
}

//该函数用来获取目录和文件的大小
int get_size(int id, int tmp_cnt){//tmp_cnt为目录或文件所在存储位置
int sum = 0;
if(id == 1){ //获取目录的大小
sum = a[tmp_cnt].is_file.size() + a[tmp_cnt].is_directory.size();
}
else{ //获取文件的大小
for(int i = 0; i < b[tmp_cnt].vet.size(); i++){
sum += b[tmp_cnt].vet[i].size();
}
}
return sum;
}

//实现目录打开功能
void open(string poss){ //poss表示要打开的目录的名字
if(poss == ".."){ //linux命令行,cd ..表示返回上一级目录
if(pos >= 1) pos --;
else{
cout <<"wrong: wrong return!" << endl;
}
}
else{//判断当前目录中是否含有要打开的目录文件
if(a[pos].is_directory.find(poss) == a[pos].is_directory.end()){
cout <<"-bash: cd: " << poss <<" : Not a directory" << endl;
}
else{
pos = a[pos].is_directory[poss];
}
}
manage();
}

//该函数用来实现新建目录功能
void mkdir(string poss){ //poss表示所要新建的目录的名字
if(a[pos].is_directory.find(poss) != a[pos].is_directory.end()){
cout <<"warning: duplicate directories!"<< endl;
}
else{
cnt1 ++;
string tmp_pos = a[pos].where;
tmp_pos += "/";
tmp_pos += a[pos].name;
if(file_directory.find(poss) == file_directory.end()){
cnt ++;
file_directory[poss] = cnt;
fid[cnt].push_back({tmp_pos,1});
}
else{
int x = file_directory[poss];
fid[cnt].push_back({tmp_pos,1});
}
a[cnt1].name = poss;
a[cnt1].where = tmp_pos;
get_systime(1,cnt1); //获取新建目录时的系统时间
a[pos].hold.push_back({poss,1});
a[pos].is_directory[poss] = cnt1;
a[cnt1].pre = a[pos].name;
}
manage();
}
//该函数用来实现新建文件功能
void touch(string name, int stt){ //name表示所要新建的文件名
string tmp_pos = a[pos].where;
tmp_pos += "/";
tmp_pos += a[pos].name;
if(file_directory.find(name) == file_directory.end()){//查找和判断
cnt ++;
file_directory[name] = cnt;
fid[cnt].push_back({tmp_pos,1});
}
else{
int x = file_directory[name];
fid[x].push_back({tmp_pos,1});
}
if(a[pos].is_file.find(name) != a[pos].is_file.end()){
cout <<"warning: duplicate files!" << endl;
manage();
}
cnt2 ++;//更新相应数据结构
b[cnt2].where =tmp_pos;
b[cnt2].name = name;
get_systime(0,cnt2); //获取新建文件时的系统时间
a[pos].hold.push_back({name,0});
a[pos].is_file[name] = cnt2;
if(stt == 1)
manage();
else return;
}

void echo(string st){//st表示想要写到文件中的信息
string s;
s.clear();
string postion;
postion.clear();
int i = 0;
for(i = 1; i < st.size(); i++){
if(st[i] == '"'){
break;
}
s += st[i];
}
for(i = i + 2; i < st.size(); i++){
postion += st[i];
}
if(a[pos].is_file.find(postion) == a[pos].is_file.end()){
//查找所想要写入的文件是否存在,不存在进行创建文件和写入
if(a[pos].is_directory.find(postion) == a[pos].is_directory.end()){
touch(postion, 0);
b[cnt2].vet.push_back(s);
cout << b[cnt2]._size << endl;
}
else{
cout <<"wrong: Illegal naming!" << endl;
}
}
else{ //存在的话,直接查询文件位置,然后进行信息写入即可
int x = a[pos].is_file[postion];
b[x].vet.push_back(s);
}
manage();
}
//该函数实现正向读取文件内容
void cat(string poss){ //poss表示
if(a[pos].is_file.find(poss) == a[pos].is_file.end()){ //如果找不到文件就报错
cout <<"cat " << poss <<": No such file or directory" << endl;
}
else{//找到文件直接正向读取文件信息即可
int x = a[pos].is_file[poss];
for(int i = 0; i < b[x].vet.size(); i++){
cout << b[x].vet[i] << endl;
}
}
manage();
}
//该函数实现反向读取文件内容
void tac(string poss){
if(a[pos].is_file.find(poss) == a[pos].is_file.end()){
cout <<"cat " << poss <<": No such file or directory" << endl;
}
else{
int x = a[pos].is_file[poss];
for(int i = b[x].vet.size() - 1; i >= 0; i--){
cout << b[x].vet[i] << endl;
}
}
manage();
}

void show(string order){ //order表示展示命令
int tmp_tot = 0;
if(order == "all"){ //如果展示命令为all,则展示粗略包含信息
for(int i = 0; i < a[pos].hold.size(); i++){
string st = a[pos].hold[i].first;
char tmp[M];
memset(tmp, 0, sizeof tmp);
for(int j = 0; j < st.size(); j++){
tmp[j] = st[j];
}
if(a[pos].hold[i].second == 0){
COLOR_PRINT(tmp,7);
tmp_tot ++;
cout <<" ";
}
else{
COLOR_PRINT(tmp,1);
tmp_tot ++;
cout <<" ";
}
}
if(tmp_tot == 0) cout <<"warning:is null";
cout << endl;
}
else{ //如果为-l,则展示详细信息,包括建立时间,大小和名称
for(int i = 0; i < a[pos].hold.size(); i++){
string st = a[pos].hold[i].first;
char tmp[M];
memset(tmp, 0, sizeof tmp);
for(int j = 0; j < st.size(); j++){
tmp[j] = st[j];
}
int tmp_pos;
if(a[pos].hold[i].second == 1){
if(a[pos].is_directory.find(st) != a[pos].is_directory.end()) tmp_pos = a[pos].is_directory[st];
else continue;
if(tmp_tot == 0){ //输出目录的相关信息
printf("create time\t\tsize\tname\n");
}
printf("%02d-%02d-%02d %02d:%02d:%02d\t",a[tmp_pos].year,a[tmp_pos].month, a[tmp_pos].day,
a[tmp_pos].hour, a[tmp_pos].minute,a[tmp_pos].seconds);
cout << get_size(1,tmp_pos)<<'\t';
COLOR_PRINT(tmp,7);
tmp_tot ++;
cout << endl;
}
else{ //输出文件的相关信息
if(a[pos].is_file.find(st) != a[pos].is_file.end()) tmp_pos = a[pos].is_file[st];
else continue;
if(tmp_tot == 0){
printf("create time\t\tsize\tname\n");
}
printf("%02d-%02d-%02d %02d:%02d:%02d\t",b[tmp_pos].year,b[tmp_pos].month, b[tmp_pos].day,
b[tmp_pos].hour, b[tmp_pos].minute,b[tmp_pos].seconds);
cout << get_size(0,tmp_pos)<<'\t';
COLOR_PRINT(tmp,1);
tmp_tot ++;
cout << endl;
}
}
if(tmp_tot == 0) cout <<"warning:is null" << endl;
}
manage();
}

void find_where(string name){ //寻找名称为name的文件和目录
int tmp_tot = 0;
if(file_directory.find(name) == file_directory.end()){ //如果找不到,输出提示信息
cout << "can't find!" << endl;
}
else{//找到了,遍历进行输出
int x = file_directory[name];
for(int i = 0; i < fid[x].size(); i++){
if(fid[x][i].second == 1){
cout << fid[x][i].first << endl;
tmp_tot ++;
}
}
//找不到,输出错误提示信息
if(tmp_tot == 0) cout <<"can't find!" << endl;
}
manage();
}

void manage(){
string order1,order2;
COLOR_PRINT("/home/user@LAPTOP-MFH2B9V0:",2);
string tmp_st = a[pos].where;
tmp_st +="/";
tmp_st += a[pos].name;
memset(place, 0, sizeof place);
for(int i = 0; i < tmp_st.size(); i++){
place[i] = tmp_st[i];
}
COLOR_PRINT(place,1);
cout << "$ ";
cin >> order1 >> order2;
if(order1 == "mkdir"){ //新建目录指令
mkdir(order2);
}
else if(order1 == "touch"){//新建文件指令
touch(order2, 1);
}
else if(order1 == "cd"){//打开目录/返回上一级目录
open(order2);
}
else if(order1 == "echo"){//写入信息到文件中去
echo(order2);
}
else if(order1 == "cat"){//正向读取文件
cat(order2);
}
else if(order1 == "tac"){//反向读取文件
tac(order2);
}
else if(order1 == "ls"){//展示目录中所含的文件、子目录信息
show(order2);
}
else if(order1 == "find"){//按名查找
find_where(order2);
}
else{
cout <<"command '" << order1 <<"' not found" << endl; //指令不存在报错
}
manage();
}

signed main(){
//一些初始化操作
cout <<"Welcome to OS_work Page (Design Based on Linux)" << endl;
cout <<endl;
a[0].name = "user";
file_directory["~/home"] = 0;
a[0].where = "~/home";
place[0] = '~';
manage();
return 0;
}