题目要求:
编一个通过屏幕选择命令的文件管理系统,每屏要为用户提供足够的选择信息,不需要打入冗长的命令。
代码:
#include <iostream> #include <string> #include <conio.h> using namespace std; struct UFD //用户目录文件 { string File_Name; //文件名 bool R,W,X; // 读写执行 int Len_File; //文件大小 }; struct MFD //主文件夹目录 { string User_Name; //用户名 UFD * Pointer; //用户文件目录指针 }; struct AFD //运行文件目录 { int File_ID;// 文件号 bool R, W, X; int Pointer;//读写指针 }; class FILE_SYSTEM { public: void Inital(); void Start(); private: int Num_User; //用户数量 int Num_File; //每个用户可保存的文件数 int MAX_Open_File; //打开的最大文件数 MFD * Mfd; // 主文件目录 UFD * Ufd; // 用户文件目录 AFD * Afd; //运行文件目录 }; int main() { FILE_SYSTEM FS; FS.Inital(); FS.Start(); return 0; } void FILE_SYSTEM::Inital() { Num_File = 10; Num_User = 10; MAX_Open_File = 5; //UFD Ufd = new UFD[Num_User*Num_File]; //MFD Mfd = new MFD[Num_User]; for(int i = 0 ; i < Num_User; i++) { Mfd[i].Pointer = &(Ufd[i*Num_File]); } //AFD Afd = new AFD[MAX_Open_File]; Mfd[0].User_Name="wzn"; Ufd[0].File_Name="1.txt"; Ufd[0].Len_File=10; Ufd[0].R=true; Ufd[0].W=false; Ufd[0].X =false; Ufd[1].File_Name="2.txt"; Ufd[1].Len_File=15; Ufd[1].R=false; Ufd[1].W=false; Ufd[1].X =true; for(int i = 2; i < Num_File; i++) //其他人没有文件存在 { Ufd[i].File_Name=""; Ufd[i].Len_File=-1; Ufd[i].R=false; Ufd[i].W=false; Ufd[i].X =false; } } void showOrder() { cout << "Order list" << endl; cout << " 1. dir "; cout << " 2. diropen" << endl; cout << " 3. create"; cout << " 4. delete" << endl; cout << " 5. open "; cout << " 6. logout" << endl; cout << " 7. close"; cout << " 8. read" << endl; cout << " 9. write "; cout << " 10. shutdown" << endl; } void FILE_SYSTEM::Start() { showOrder(); int User_ID,i; int Num_Open_File,tmp_len; //当前用户打开文件数 string User_Name,tmp;//当前登录的用户名 int cmd; //用户输入命令 UFD * ufd; char choice; do //打开计算机 { do { cout << "User Name:"; cin >> User_Name; for(User_ID = 0; User_ID < Num_User; User_ID++) { if(Mfd[User_ID].User_Name == User_Name) break; } if(User_ID == Num_User) cout << "No such User name,please try again!" << endl; } while(User_ID == Num_User); cout << "Welcome to login, " << User_Name << "!" << endl; ufd = Mfd[User_ID].Pointer; //初始化文件列表 for(i = 0 ; i < MAX_Open_File; i++) { Afd[i].File_ID = -1; } Num_Open_File = 0; do //命令处理 { //模拟命令行: cout << "["<<User_Name<<"]~"; cin >> cmd; if(cmd == 1) { cout << endl; cout << "Owner of Files:" << User_Name << endl; cout << " State Length FileName" << endl; for(i = 0 ; i < Num_File; i++) { if(ufd[i].Len_File != -1) { cout << " "; if(ufd[i].R == true)cout << "R"; else cout << "-"; if(ufd[i].W == true)cout << "W"; else cout << "-"; if(ufd[i].X == true)cout << "X"; else cout << "-"; cout << " "; cout << ufd[i].Len_File << " "; cout << ufd[i].File_Name << endl; } } cout << endl; } else if(cmd == 2) { cout << endl; cout << "Opened Files of " << User_Name << endl; cout << " " << "State " << "Open File name" << endl; for(i = 0 ; i < MAX_Open_File; i++) { if(Afd[i].File_ID != -1) { cout << " "; if(Afd[i].R == true)cout << "R"; else cout << "-"; if(Afd[i].W == true)cout << "W"; else cout << "-"; if(Afd[i].X == true)cout << "X"; else cout << "-"; cout << " "; cout << ufd[Afd[i].File_ID].File_Name << endl; } } cout << endl; } else if(cmd == 3) { for(i=0; i<Num_File; i++) { if(ufd[i].Len_File == -1) break; } if(i == Num_File) { cout << "No Enough space!" << endl; } else { cout << "File's Information:" << endl; cout << "File 's name:"; cin >> tmp; ufd[i].File_Name = tmp; cout << "Read(y/n):"; choice = getch(); if(choice == 'y')ufd[i].R = true; else ufd[i].R = false; cout << endl; cout << "Write(y/n):"; choice = getch(); if(choice == 'y')ufd[i].W = true; else ufd[i].W = false; cout << endl; cout << "Execute(y/n):"; choice = getch(); if(choice == 'y')ufd[i].X = true; else ufd[i].X = false; cout << endl; cout << "Length:"; cin >> tmp_len; if(tmp_len > 0) ufd[i].Len_File = tmp_len; cout << "New File: " << ufd[i].File_Name << " is Created" << endl; } } else if(cmd == 4) { cout << "Enter Delete file name:"; cin >> tmp; for(i = 0 ; i < Num_File; i++) { if((ufd[i].Len_File != -1) && (ufd[i].File_Name == tmp)) break; } if(i == Num_File) { cout << "NOT FOUND!" << endl; } else { ufd[i].Len_File = -1; //delete the file cout << "The File:" << ufd[i].File_Name << " is deleted!" << endl; } } else if(cmd == 5) { if(Num_Open_File == MAX_Open_File) cout << "Can't Open anymore!" << endl; else { cout << "Open file 's name:"; cin >> tmp; for(i=0; i <Num_File; i++) { if((ufd[i].Len_File != -1)&& (ufd[i].File_Name == tmp)) break; } if(i == Num_File) { cout << "No such file" << endl; } else { int t; Num_Open_File++; for(t = 0; t < MAX_Open_File; i++) if(Afd[t].File_ID == -1) break; Afd[t].File_ID = i; Afd[t].Pointer = 0;//0 cout << "Open mode:" << endl; if(ufd[i].R == 1) { cout << "Read(y/n):"; choice = getch(); if(choice == 'y')Afd[t].R = 1; else Afd[t].R = 0; } else Afd[t].R = 0; if(ufd[i].W == 1) { cout << "Write(y/n):"; choice = getch(); if(choice == 'y')Afd[t].W = 1; else Afd[t].W = 0; } else Afd[t].W = 0; if(ufd[i].X == 1) { cout << "Execute(y/n):"; choice = getch(); if(choice == 'y')Afd[t].X = 1; else Afd[t].X = 0; } else Afd[t].X = 0; cout << "The File:" << tmp << " is opened" << endl; } } } else if(cmd == 6) { cout << "bye~" << User_Name << endl; break; } else if(cmd == 7) { cout << "Close File name:"; cin >> tmp; for(i = 0 ; i < Num_File ; i++) { if((ufd[i].Len_File != -1) && (ufd[i].File_Name == tmp)) break; } if(i == Num_File) { cout << "Not found!" << endl; } else { int t; for(t = 0 ; t < MAX_Open_File ; t++) { if(Afd[t].File_ID == i) break; } if(t == MAX_Open_File) cout << "Not found" << endl; else { Afd[t].File_ID = -1; Num_Open_File--; cout << tmp << " is closed" << endl; } } } else if(cmd == 8) { cout << "Read file Name:"; cin >> tmp; for(i = 0 ; i < Num_File; i++) { if(ufd[i].File_Name == tmp && ufd[i].Len_File != -1) break; } if(i == Num_File) { cout << "Not Found" << endl; } else { int t; for(t = 0 ; t < MAX_Open_File; t++) { if(Afd[t].File_ID == i) break; } if(t == MAX_Open_File) cout << "No Enough Space" << endl; else { if(Afd[t].R == true) { cout << "You can read this file" << endl; } else { cout << "Error:Not have Power of Open" << endl; } } } } else if(cmd == 9) { cout << "enter write file name:"; cin >> tmp; for(i = 0 ; i < Num_File ; i++) if(ufd[i].Len_File!=-1&&ufd[i].File_Name==tmp) break; if(i == Num_File) { cout << "No such file" << endl; } else { int t; for(t = 0 ; t < MAX_Open_File; t++) { if(Afd[t].File_ID == i) break; } if(t == MAX_Open_File) { cout << "The file:" << tmp << "is not open" <<endl; } else { if(Afd[t].W == true) cout << "You can write this file" << endl; else cout << "Error:Not have Power of write" << endl; } } } else if(cmd == 10) { cout << "loging out ...." << endl; cout << "bye~" << User_Name << endl; cout << "Shutting down ..." << endl; break; } else { cout << "Illegal command, please try again" << endl; } } while(cmd != 10 && cmd != 6); } while(cmd != 10); }