2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 数据结构课程设计——宿舍管理查询软件

数据结构课程设计——宿舍管理查询软件

时间:2022-12-11 17:22:17

相关推荐

数据结构课程设计——宿舍管理查询软件

数据结构课程设计——宿舍管理查询软件

问题描述

1)编写一个宿舍管理查询软件,程序设计要求

A采用交互工作方式

B建立数据文件,数据文件按照关键字(姓名,学号,房号)进行排序

2)查询菜单

A按照姓名查询

B按照学号查询

C按照房号查询

3)打印任意查询结果(可以连续操作)

程序设计

1,读取数据文件(xxx.txt),如不存在该文件,则创建同名文件(添加 增加和删除 两项操作)。

2,对数据文件可以进行关键字排序

3,可以按照关键字进行查询

4,可以对查询结果进行修改(多次查询结果累加,可以进行连续的删除)

5,使用循环实现交互式工作

算法设计和数据结构

算法设计:

对数据文件进行初始化操作采用及时终止的冒泡排序,复杂度为O(n^2),如果两次采用相同的排序方式,复杂度为O(1)

进行查询,插入,删除操作复杂度都是O(n)

数据结构:

采用自动扩容的数组存储,使用结构组织数据

你可能会需要注意的地方

1,在txt文件中存储的格式如下:

Amy 10000010 101

Ben 10000001 101

注意:格式为名字 学号 宿舍号。名字与学号,学号与宿舍号之间为一个空格,每条数据占一行,文件最后留一空行。

2,你可能必须要在文件位置输入时加上.txt

3,在文件直接输入中文读取时可能产生乱码

4,可以直接输入文件名称,默认为当前目录下,如果没有则会自动生成同名文件

代码展示

#include <iostream>#include <cstring>#include <fstream>#include <sstream>#include <string>using namespace std;struct Student//储存数据的结构{string name;long id;int room;};string file_name ;//输入的文件名称,只支持txt文件void manage(Student* s,int i,int& length,Student* store,int& store_length,int arr)//s存储全部数据,i为输入的指令,length为s的数据个数,store为查询得到的数据,store_length为store的数据个数,arr为最开始所选择的排序方式{if(i==0)//主菜单{cout<<"Welcome"<<endl;cout<<"0 for The Start"<<endl;cout<<"1 for Insert"<<endl;cout<<"2 for Delete"<<endl;cout<<"3 for QueryByName"<<endl;cout<<"4 for QueryById"<<endl;cout<<"5 for QueryByRoom"<<endl;cout<<"6 for Print What You Query"<<endl;cout<<"7 for Print All"<<endl;cout<<"8 for Delete What You Query"<<endl; cout<<"9 for The End"<<endl;}else if(i==1)//对文件进行插入操作{Student temp;cout<<"Name:";cin>>temp.name;cout<<"Id:";cin>>temp.id;cout<<"Room:";cin>>temp.room;for(int p=0;p<length;p++){bool bo;switch (arr){case 1:bo=(temp.name<s[p].name);break;case 2:bo=(temp.id<s[p].id);break;case 3:bo=(temp.room<s[p].room);break;case 4:bo=(temp.name>s[p].name);break;case 5:bo=(temp.id>s[p].id);break;case 6:bo=(temp.room>s[p].room);break; default:break;}if(bo){for(int c=length;c>p;c--){s[c]=s[c-1];}s[p]=temp;length++;break;}if(p==length-1){s[length]=temp;length++;break;}}if(length==0){s[0]=temp;length++;}ofstream of;of.open(file_name,ios::out|ios::trunc);if(of.is_open()){for(int k=0;k<length;k++){of<<s[k].name<<" "<<s[k].id<<" "<<s[k].room<<"\n";}}of.close();cout<<"Insert successfully"<<endl;}else if(i==2)//对文件进行删除操作{cout<<"Which one ?";int de;cin>>de;if(de<=length&&de>0){for(int p=de;p<length;p++){s[p-1]=s[p];}length--;ofstream of;of.open(file_name,ios::out|ios::trunc);if(of.is_open()){for(int k=0;k<length;k++){of<<s[k].name<<" "<<s[k].id<<" "<<s[k].room<<"\n";}}of.close();cout<<"Deleted successfully"<<endl;}else cout<<"Wrong !"<<endl;}else if(i==3)//对文件进行姓名查询{cout<<"Name ?";string na;int count=0;cin>>na;for(int i=0;i<length;i++){if(s->name==na){cout<<"Name: "<<s->name<<" Id: "<<s->id<<" Room: "<<s->room<<endl;store[store_length]=*s;store_length++;count++;}s++;}if(count==0)cout<<"NULL"<<endl; }else if(i==4)//对文件进行学号查询{cout<<"Id ?";long na;int count=0;cin>>na;for(int i=0;i<length;i++){if(s->id==na){cout<<"Name: "<<s->name<<" Id: "<<s->id<<" Room: "<<s->room<<endl;store[store_length]=*s;store_length++;count++;}s++;}if(count==0)cout<<"NULL"<<endl; }else if(i==5)//对文件进行房间号查询{cout<<"Room ?";int na;int count=0;cin>>na;for(int i=0;i<length;i++){if(s->room==na){cout<<"Name: "<<s->name<<" Id: "<<s->id<<" Room: "<<s->room<<endl;store[store_length]=*s;store_length++;count++;}s++;}if(count==0)cout<<"NULL"<<endl; }else if(i==6)//输出查询结果{for(int k=0;k<store_length;k++){cout<<"Name: "<<store[k].name<<" Id: "<<store[k].id<<" Room: "<<store[k].room<<endl;} }else if(i==7)//输出全部数据{for(int q=0;q<length;q++){cout<<"Name: "<<s[q].name<<" Id: "<<s[q].id<<" Room: "<<s[q].room<<endl;}}else if(i==8)//对查询结果进行删除操作{int start,end;cout<<"The start:";cin>>start;cout<<"The end:";cin>>end;if(end>store_length||start<1||end<start){cout<<"wrong"<<endl;}else{int b=store_length;for(int e=b;e<b+(end-start+1);e++){store[e].name="a";store[e].id=0;store[e].room=0;}int d=end;for(int o=start;o<b+(end-start+1);o++){store[o-1]=store[d];d++;}for(int p=0;p<b;p++){if(store[p].id==0)store_length--;}}}}void sort(int a,int length,Student* so)//对文件数据进行排序,采用及时终止的冒泡排序{if(a==1){for(int i=0;i<length;i++){int count=0;for(int j=0;j<length-1;j++){if(so[j].name>so[j+1].name){Student s=so[j];so[j]=so[j+1];so[j+1]=s;count++;}}if(count==0) break;}}else if(a==2){for(int i=0;i<length;i++){int count=0;for(int j=0;j<length-1;j++){if(so[j].id>so[j+1].id){Student s=so[j];so[j]=so[j+1];so[j+1]=s;count++;}}if(count==0) break;}}else if(a==3){for(int i=0;i<length;i++){int count=0;for(int j=0;j<length-1;j++){if(so[j].room>so[j+1].room){Student s=so[j];so[j]=so[j+1];so[j+1]=s;count++;}}if(count==0) break;}}else if(a==4){for(int i=0;i<length;i++){int count=0;for(int j=0;j<length-1;j++){if(so[j].name<so[j+1].name){Student s=so[j];so[j]=so[j+1];so[j+1]=s;count++;}}if(count==0) break;}}else if(a==5){for(int i=0;i<length;i++){int count=0;for(int j=0;j<length-1;j++){if(so[j].id<so[j+1].id){Student s=so[j];so[j]=so[j+1];so[j+1]=s;count++;}}if(count==0) break;}}else if(a==6){for(int i=0;i<length;i++){int count=0;for(int j=0;j<length-1;j++){if(so[j].room<so[j+1].room){Student s=so[j];so[j]=so[j+1];so[j+1]=s;count++;}}if(count==0) break;}}ofstream of;of.open(file_name,ios::out|ios::trunc);if(of.is_open()){for(int k=0;k<length;k++){of<<so[k].name<<" "<<so[k].id<<" "<<so[k].room<<"\n";}}of.close();}int main(){cout<<"Your File :";cin>>file_name;//输入在同一目录的下的txt文件名称,如果没有将会创建一个新的同名文件cout<<"1 name ascend"<<endl;cout<<"2 id ascend"<<endl;cout<<"3 room ascend"<<endl;cout<<"4 name descend"<<endl;cout<<"5 id descend"<<endl;cout<<"6 room descend"<<endl;int arr;cin>>arr;//选择排序方式while(arr!=1&&arr!=2&&arr!=3&&arr!=4&&arr!=5&&arr!=6){cout<<"Wrong Number !"<<endl;cout<<"Again:";cin>>arr;}ifstream infile;string str;infile.open(file_name);if(!infile.is_open()){ofstream fout(file_name);if(fout){fout.close();}}int Number=0;while(getline(infile,str)){Number++;}Number+=2000;//创建的数组为原文件加2000,确保在使用该程序输入过程中不会发生数组越界Student stu[Number],store[Number];infile.close();infile.open(file_name);int length=0;int store_length=0;while(getline(infile,str))//将文件中的数据读入并存入数组{char* co=new char[str.length()+1];strcpy(co,str.c_str());char *p=strtok(co," ");while(p!=0){stu[length].name=p;p=strtok(NULL," "); string o1(p);stringstream s1;s1<<o1;s1>>stu[length].id;p=strtok(NULL," ");string o2(p);stringstream s2;s2<<o2;s2>>stu[length].room;p=strtok(NULL," ");}length++;delete[] co;} infile.close();sort(arr,length,stu);//对文件进行排序,并重写文件内容int order=0; while(order!=9)//实现交互式操作{manage(stu,order,length,store,store_length,arr);cin>>order;} return 0;//粗制滥造,感谢观看}

测试文本

结果展示

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。