1.电梯模拟。。。乱写的
View Code
main.cpp #include"louzhang.h" using namespace std; CElevator elevator; int main(){ //srand(time(0)); elevator.run(); return 0; } louzhang.h #ifndef LOUZHANG_H #define LOUZHANG_H #include<vector> #include<cstdlib> #include<iostream> #include<cstdio> #include<cstdlib> #include<ctime> #include<algorithm> #include<Windows.h> using namespace std; const int max_floor = 20; const int max_num = 13; const int t = 100; class CPerson{ public: CPerson();//构造 ~CPerson();//析构 //private: int src_floor;//这个人在哪个楼层按了电梯 int dst_floor;//这个人去哪一层 int max_wait;//这个人最大等待时间 int wait_time;//这个人已经等了多长时间 int id;//这个人的ID号,即序号 bool flag;//true is out, and false is in the elevator }; class CElevator{ public: CElevator(); ~CElevator(); void run(); int MAX(int a, int b, int c);//取3个数中最大值 int MIN(int a, int b, int c);//取3个数中最小值 //private: int now_floor;//电梯当前所在楼层 int dst_floor;//电梯所去楼层 int flag;//标识位,1表示向上,-1表示向下,0表示电梯停留 }; #endif louzhang.cpp #include"louzhang.h" extern CElevator elevator; vector < CPerson > Person; vector < CPerson >::iterator it; void CElevator::run(){ int up = 1; int down = max_floor; for(int rand_num = 1; rand_num <= 2 * max_floor; rand_num ++){ CPerson person; person.id = rand_num; Person.push_back(person); printf("第 %d 号乘客在 %d 楼按了电梯......\n", person.id , person.src_floor); if(elevator.flag == 0){ for(int i = 0; i < (int)Person.size(); i++){ if(Person[i].src_floor > elevator.now_floor){ elevator.flag = 1; elevator.dst_floor = MAX(elevator.dst_floor, Person[i].src_floor, Person[i].dst_floor); }else if(Person[i].src_floor < elevator.now_floor){ elevator.flag = -1; elevator.dst_floor = MIN(elevator.dst_floor, Person[i].src_floor, Person[i].dst_floor); }else { if(Person[i].dst_floor >elevator.now_floor){ elevator.flag = 1; elevator.dst_floor = Person[i].dst_floor; }else{ elevator.flag = -1; elevator.dst_floor = Person[i].dst_floor; } break; } } if(elevator.flag == 1){ elevator.now_floor ++; puts("电梯正在关门......"); Sleep(20 * t); puts("电梯在加速......"); Sleep(15 * t); puts("电梯正在上升......"); Sleep(22 * t); puts("电梯正在减速......"); Sleep(14 * t); printf("电梯到达 %d 楼\n\n", elevator.now_floor); for(int i = 0; i < (int)Person.size(); i++){ Person[i].wait_time += 71; if(Person[i].wait_time > Person[i].max_wait ){ Person.erase(Person.begin() + i); i --; } } } else{ elevator.now_floor --; puts("电梯正在关门......"); Sleep(20 * t); puts("电梯在加速......"); Sleep(15 * t); puts("电梯正在下降......"); Sleep(23 * t); puts("电梯正在减速......"); Sleep(23 * t); printf("电梯到达 %d 楼\n\n", elevator.now_floor); for(int i = 0; i < (int)Person.size(); i++){ Person[i].wait_time += 81; if(Person[i].wait_time > Person[i].max_wait ){ Person.erase(Person.begin() + i); i --; } } } }else if(elevator.flag == 1){ if(person.src_floor > elevator.now_floor) elevator.dst_floor = MAX(elevator.dst_floor, person.dst_floor, person.src_floor); int flag = 1; int count = 0; for(int i = 0; i < (int)Person.size(); i++){ if(Person[i].dst_floor == elevator.now_floor && Person[i].flag == false){ if(flag){ puts("电梯正在开门......"); Sleep(20 *t); flag = 0; } count ++; printf("乘客 %d 正从电梯出来......\n", Person[i].id); Sleep(25 *t); Person.erase(Person.begin() + i); i --; } } for(it = Person.begin(); it != Person.end(); it ++){ if((*it).src_floor == elevator.now_floor && (*it).flag == true){ if((*it).dst_floor - (*it).src_floor > 0){ (*it).flag = false; if((*it).dst_floor > up ) up = (*it).dst_floor; if(flag){ puts("电梯正在开门......"); Sleep(20 *t); flag = 0; } count ++; printf("乘客 %d 进入电梯......\n", (*it).id); printf("%d 号乘客目的楼层为 %d \n", (*it).id, (*it).dst_floor); Sleep(25 *t); } } } if(elevator.now_floor != elevator.dst_floor){ elevator.now_floor ++; if(flag == 0){ puts("没有人进电梯了......"); Sleep(40 * t); puts("电梯正在关门......"); Sleep(20 * t); puts("电梯在加速......"); Sleep(15 * t); puts("电梯正在上升......"); Sleep(22 * t); puts("电梯正在减速......"); Sleep(14 * t); for(int i = 0; i < (int)Person.size(); i++){ Person[i].wait_time += (count * 25) + 20 + 111; if(Person[i].wait_time > Person[i].max_wait ){ Person.erase(Person.begin() + i); i --; } } }else{ puts("电梯继续上升......"); Sleep(51 * t); for(int i = 0; i < (int)Person.size(); i++){ Person[i].wait_time += (count * 25) + 20 + 51; if(Person[i].wait_time > Person[i].max_wait ){ Person.erase(Person.begin() + i); i --; } } } printf("电梯到达 %d 楼\n\n", elevator.now_floor); } else elevator.flag = 0; }else { if(person.src_floor <elevator.now_floor) elevator.dst_floor = MIN(elevator.dst_floor, person.dst_floor, person.src_floor); int flag = 1; int count = 0; for(int i = 0; i < (int)Person.size(); i++){ if(Person[i].dst_floor == elevator.now_floor && Person[i].flag == false){ if(flag){ puts("电梯正在开门......"); Sleep(20 *t); flag = 0; } count ++; printf("乘客 %d 从电梯出来......\n", Person[i].id); Sleep(25 * t); Person.erase(Person.begin() + i); i --; } } for(it = Person.begin(); it != Person.end(); it ++){ if((*it).src_floor == elevator.now_floor && (*it).flag == true){ if((*it).dst_floor - (*it).src_floor < 0){ (*it).flag = false; if((*it).dst_floor < down) down = (*it).dst_floor; if(flag){ puts("电梯正在开门......"); Sleep(20 * t); flag = 0; } count ++; printf("乘客 %d 正在进入电梯......\n", (*it).id); printf("%d 号乘客目的楼层为 %d \n", (*it).id, (*it).dst_floor); Sleep(25 * t); } } } if(elevator.now_floor != elevator.dst_floor){ elevator.now_floor --; if(flag == 0){ puts("没有人进电梯了......"); Sleep(40 * t); puts("电梯正在关门......"); Sleep(20 * t); puts("电梯在加速......"); Sleep(15 * t); puts("电梯正在下降......"); Sleep(23 * t); puts("电梯正在减速......"); Sleep(23 * t); for(int i = 0; i < (int)Person.size(); i++){ Person[i].wait_time += (count * 25) + 20 + 121; if(Person[i].wait_time > Person[i].max_wait ){ Person.erase(Person.begin() + i); i --; } } }else{ puts("电梯继续下降......"); Sleep(51 * t); for(int i = 0; i < (int)Person.size(); i++){ Person[i].wait_time += (count * 25) + 20 + 51; if(Person[i].wait_time > Person[i].max_wait ){ Person.erase(Person.begin() + i); i --; } } } printf("电梯到达 %d 楼\n\n", elevator.now_floor); } else elevator.flag = 0; } if(elevator.now_floor == 1) break; } if(elevator.now_floor == 1){ int flag = 1; for(int i = 0; i < (int)Person.size(); i++){ if(Person[i].dst_floor == elevator.now_floor && Person[i].flag == false){ if(flag){ puts("电梯正在开门......"); Sleep(20 * t); flag = 0; } printf("乘客 %d 正从电梯出来......\n", Person[i].id); Sleep(25 * t); Person.erase(Person.begin() + i); i --; } } } } int CElevator::MAX(int a, int b, int c){ return (a > (b > c ? b : c) ? a : (b > c ? b : c) ); } int CElevator::MIN(int a, int b, int c){ return (a < (b < c ? b : c) ? a : (b < c ? b : c) ); } CPerson::CPerson(){ do{ src_floor = rand() % max_floor + 1; }while(src_floor == elevator.now_floor); do{ dst_floor = rand() % max_floor + 1; }while(dst_floor == src_floor || dst_floor == elevator.now_floor); max_wait = 1000; wait_time = 0; flag = true; } CPerson::~CPerson(){ ; } CElevator::CElevator(){ now_floor = 1; dst_floor = 1; flag = 0; } CElevator::~CElevator(){ ; }
2.linux文件目录管理模拟器
View Code
main.cpp #include "louzhang.h" CRun Run; int main(){ puts("welcome to ubuntu 12.04\n\n"); Run.run(); return 0; } louzhang.h #ifndef LOUZHANG_H #define LOUZHANG_H #include<list> #include<vector> #include<ctime> #include<string> #include<cstring> #include<iostream> #include<queue> #include<stack> #include<Windows.h> using namespace std; class CFile{ public: CFile(); ~CFile(); string name; bool type;//true is directory, and false is file string make_time; string permissions;//х╗оч, rwx int size; int num; bool hide;//true is hide, ans false is not hide vector < CFile * > next; CFile *pre; friend bool operator < (const CFile &a, const CFile &b){ return a.make_time < b.make_time; } }; class CRun{ public: void run(); void MKDIR(); void TOUCH(); void FIND(); void CD(); void REMOVE(); void SHOW(CFile *p); void CRun::DFS(CFile *p, string name, CFile **ans); void PWD(); void LS(); }; #endif louzhang.cpp #include "louzhang.h" CFile *root = new CFile;//init a root directory CFile *now = new CFile; #define WRONG system("color 2c");puts("wrong");Sleep(100);system("color 0f"); void CRun::run(){ root->name = "root"; root->type = true; root->permissions = "rwx"; root->size = 0; root->num = 0; now = root; while(now != NULL){ printf("louzhang$: "); string op, name;//op is mkdir, touch, find, rm, cp cin>>op; if(op == "cd"){ CD(); continue; } if(op == "mkdir"){ MKDIR(); continue; } if(op == "touch"){ TOUCH(); continue; } if(op == "find"){ FIND(); continue; } if(op == "rm"){ REMOVE(); continue; } if(op == "pwd"){ PWD(); continue; } if(op == "ls"){ LS(); continue; } if(op == "exit") break; WRONG } } void CRun::LS(){ sort(now->next.begin(), now->next.end() ); char c = getchar(); if(c == ' '){ string ss; cin>>ss; for(int i = 0; i < (int)now->next.size(); i ++){ CFile *tmp = now->next[i]; if(tmp->type == true){ cout<<"d"; }else cout<<"-"; cout<<tmp->permissions<<" "; cout<<tmp->size<<" "; cout<<tmp->num<<" "; cout<<tmp->make_time<<" "; cout<<tmp->name<<endl; } return; } for(int i = 0; i < (int)now->next.size(); i ++){ cout<<now->next[i]->name<<" "; } cout<<endl; } void CRun::REMOVE(){ char s[100]; gets(s); if(now->next.size() == 0){ WRONG return; } char op[10], name[100]; int t = sscanf(s, "%s %s", op, name); if(t == 2){ for(int i = 0; i <(int)now->next.size(); i++){ if(now->next[i]->name == name){ now->next.erase(now->next.begin() + i); return; } } WRONG }else{ strcpy(name, op); for(int i = 0; i < (int)now->next.size(); i++){ if(now->next[i]->name != name) continue; if(now->next[i]->type == false){ now->next.erase(now->next.begin() + i); return; }else{ if(now->next[i]->next.size() == 0){ now->next.erase(now->next.begin() + i); return; } } } WRONG } } void CRun::CD(){ string dir; cin>>dir; if(dir == ".."){ if(now == root) return; now = now->pre; return; } if(dir == "~"){ now = root; return; } for(int i = 0; i < (int)now->next.size(); i++){ if(now->next[i]->name == dir){ if(now->next[i]->type == true){ now = now->next[i]; return; } } } WRONG } void CRun::SHOW(CFile *p){ stack < string > q; q.push(p->name); while(p->pre != NULL){ q.push(p->pre->name); p = p->pre; } while(!q.empty()){ cout<<q.top()<<"/"; q.pop(); } cout<<endl; } void CRun::PWD(){ SHOW(now); } void CRun::DFS(CFile *p, string name, CFile **ans){ if(p->name == name){ *ans = p; SHOW(p); } if(name.find('?') >= 0 && name.find('?') < name.length()){ if(name.length() != p->name.length()) goto loop; int flag = 0; for(int i = 0; name[i]; i ++){ if(name[i] == '?') continue; if(name[i] != p->name[i]){ flag = 1; break; } } if(flag == 0){ *ans = p; SHOW(p); } } loop: if(name.find('*') >= 0 && name.find('*') < name.length()){ int pos = name.find('*'); const char *s1 = name.c_str(); const char *s2 = p->name.c_str(); if(strncmp(s1, s2, pos) == 0){ *ans = p; SHOW(p); } } if(p->next.size() == 0) return; for(int i = 0; i < (int)p->next.size(); i++){ DFS(p->next[i], name, ans); } } void CRun::FIND(){ string name; cin>>name; CFile *ans =new CFile; DFS(root, name, &ans); if(ans->pre == NULL && ans != root ) {WRONG} } void CRun::TOUCH(){ string name; cin>>name; if(now->next.size() != 0){ for(int i = 0; i < (int)now->next.size(); i++){ if(now->next[i]->type == false && name == now->next[i]->name){ WRONG return; } } } CFile *file = new CFile; file->name = name; file->type = false; file->permissions = "rwx"; file->hide = false; file->size = 0; file->pre = now; now->next.push_back(file); if(now == root) return; CFile *tmp = now; while(tmp->pre->pre != NULL){ tmp->pre->num += tmp->num; tmp = tmp->pre; } } void CRun::MKDIR(){ string name; cin>>name; if(now->next.size() != 0){ for(int i = 0; i < (int)now->next.size(); i++){ if(now->next[i]->type == true && name == now->next[i]->name){ WRONG return; } } } CFile *file = new CFile; file->name = name; file->type = true; file->permissions = "rwx"; file->hide = false; file->size = 0; file->pre = now; now->next.push_back(file); now->num ++; if(now == root) return; CFile *tmp = now; while(tmp->pre->pre != NULL){ tmp->pre->num += tmp->num; tmp = tmp->pre; } } CFile::CFile(){ time_t t = time(0); char tmp[64]; strftime( tmp, sizeof(tmp), "%Y/%m/%d %X %A ±¾ÄêµÚ%jÌì ",localtime(&t) ); make_time = tmp; num = 0; pre = NULL; } CFile::~CFile(){ ; }
3.校园平面导航,最扯淡的一个题目了,其实就是最短路
View Code
main.cpp #include "louzhang.h" void Remove(int id); void change(int id1, int id2, int w); void change2(int id1, int id2, int w); int main(){ for(int i = 0; i < 16; i ++){ cin>>s[i].name>>s[i].id>>s[i].infomation; } for(int i = 0; i <= 30; i ++){ cin>>ed[i].u>>ed[i].v>>ed[i].w; } memset(list, NULL, sizeof(list)); for(int i = 1; i <= 30; i ++){ add_edge(ed[i].u, ed[i].v, ed[i].w); add_edge(ed[i].v, ed[i].u, ed[i].w); } while(true){ int select; puts("1. 查看"); puts("2. 查看一个景点信息"); puts("3. 查找两景点最短路 "); puts("4. 增加一个景点"); puts("5. 删除一个景点"); puts("6. 修路改路"); scanf("%d", &select); int flag1 = 0; int flag2 = 0; switch(select){ case 2:{ string name; cin>>name; flag1 = 0; for(int i = 0; i < num_view; i ++){ if(name == s[i].name){ cout<<s[i].infomation<<endl; flag1 = 1; break; } } if(flag1 == 0)puts("wrong"); break; } case 3:{ string name1, name2; cin>>name1>>name2; int id1, id2; flag1 = flag2 = 0; for(int i = 0; i < num_view; i ++){ if(name1 == s[i].name){ id1 = s[i].id; flag1 = 1; } if(name2 == s[i].name){ id2 = s[i].id; flag2 = 1; } } if(!(flag1 && flag2) ){puts("wrong");break;} dijk(id1); if(dist[id2] == inf){ puts("wrong"); break; } dfs(id2); cout<< s[id2 - 1].name<<endl; printf("距离: %d\n",dist[id2]); break; } case 4:{ puts("输入景点名称和信息"); cin>>s[num_view].name; cin>>s[num_view].infomation ; num_view ++; s[num_view - 1].id = num_view ; int num_edge; //cin>>num_edge; num_edge = 1; while(num_edge --){ string name; int id1, id2; puts("和哪个景点相连?"); cin>>name; for(int i = 0; i < num_view; i ++){ if(name == s[i].name){ id1 = s[i].id; } } puts("距离多少"); int w; cin>>w; add_edge(num_view, id1, w); add_edge(id1, num_view, w); } break; } case 5:{ string name; cin>>name; int id; flag1 = 0; for(int i = 0; i < num_view; i ++){ if(name == s[i].name){ flag1 = 1; s[i].name = ""; id = i; for(int j = i; j < num_view; j++){ s[j] = s[j + 1]; } break; } } if(flag1 == 0){puts("wrong");break;} Remove(id + 1); num_view --; break; } case 6:{ string name1, name2; cin>>name1>>name2; int id1, id2; flag1 = flag2 = 0; for(int i = 0; i < num_view; i ++){ if(name1 == s[i].name){ id1 = s[i].id; flag1 = 1; } if(name2 == s[i].name){ id2 = s[i].id; flag2 = 1; } } if(!(flag1 && flag2) ){puts("wrong");break;} int tmp; puts("1 加路"); puts("2 修改"); puts("3 删除路"); cin>>tmp; int w; switch(tmp){ case 1: cin>>w; add_edge(id1, id2, w); add_edge(id2, id1, w); break; case 2: cin>>w; change(id1, id2, w); break; case 3: change2(id1, id2, inf); } break; } case 1: for(int i = 0 ; i < num_view; i ++){ cout<<s[i].name<<" "<<s[i].infomation<<endl; } } } return 0; } void Remove (int id){ for(int i = 1; i <= num_view; i ++){ if(list[i] == NULL)continue; if(i == id){ delete list[i]; continue; } edge *tmp = list[i]; if(tmp->to == id){ tmp->next = tmp->next->next; } } } void change(int id1, int id2, int w){ for(int i = 1; i <= num_view; i++){ if(list[i] == NULL ) continue; if ( i == id1){ edge *tmp = list[i]; while(tmp != NULL){ if(tmp->to == id2) if(tmp->w > w) tmp->w = w; tmp = tmp->next; } continue; } if(i == id2){ edge *tmp = list[i]; while(tmp !=NULL){ if(tmp->to == id1) if(tmp->w > w) tmp->w = w; tmp = tmp->next; } } } } void change2(int id1, int id2, int w){ for(int i = 1; i <= num_view; i++){ if(list[i] == NULL ) continue; if ( i == id1){ edge *tmp = list[i]; while(tmp != NULL){ if(tmp->to == id2) tmp->w = w; tmp = tmp->next; } continue; } if(i == id2){ edge *tmp = list[i]; while(tmp !=NULL){ if(tmp->to == id1) tmp->w = w; tmp = tmp->next; } } } } louzhang.h #include<cstdio> #include<cstring> #include<string> #include<iostream> #include<cstdlib> using namespace std; class CViewpoint{ public: string name; int id; string infomation; }s[1000]; int num_view = 16; struct Edge{ int u, v, w; }ed[1000]; const int maxn=1000; //定义最大值即0x7fffffff const int inf=~0U>>1; int path[maxn];//保存路径 int visit[maxn];//标记是否访问 int dist[maxn];//保存距离,即结果 int shortest[maxn];//保存倒找的结点 //边的结构体 struct edge{ int to,w; edge*next; }*list[maxn]; int n,m; //按最短路长短排序 struct sort__ { int len; int num; //重载运算符 friend bool operator <(const sort__&a,const sort__&b) { if(a.len!=b.len)return a.len<b.len;else a.num<b.num; } }ans[maxn]; //加边 void add_edge(int u,int v,int w){ edge *tmp=new edge; tmp->to=v; tmp->w=w; tmp->next=list[u]; list[u]=tmp; } //dijkstra算法实现 void dijk(int u){ edge *tmp=list[u]; for(int i=1;i<=num_view;i++)dist[i]=inf; memset(visit,0,sizeof(visit)); memset(path,-1,sizeof(path)); while(tmp!=NULL){ int v=tmp->to; if(dist[v] > tmp->w){ dist[v]=tmp->w; path[v]=u;} tmp=tmp->next; } visit[u]=1; dist[u]=0; for(int i=1;i<num_view;i++){ int min=inf,uu=u; for(int j=1;j<=num_view;j++){ if(!visit[j] && dist[j]<min){ uu=j; min=dist[j]; } } visit[uu]=1; tmp=list[uu]; while(tmp!=NULL){ int v=tmp->to; if(!visit[v] && dist[uu]+tmp->w<dist[v]){ dist[v]=dist[uu]+tmp->w; path[v]=uu; } tmp=tmp->next; } } } //递归输出 void dfs(int i){ //if(i==-1)return; if(path[i]==-1)return; dfs(path[i]); //printf("%d--->",s[path[i] - 1].name); cout<<s[path[i] - 1].name<<"--->"; }
就记这么多了= =、多的也不愿意写了,烦躁