数据结构实训
数据结构实训结束了,准确地说是这个学期结束了,现在正坐在回家的高铁上写这篇博客,主要内容是山东工商学院数据结构实训的代码。数据结构实训一共有6个任务,我做了其中的1、2、3、5、6,第4个任务交给队友处理了(要画平面图实在太麻烦就交给队友的屑)。先说明数据结构的实训任务全是玩具,代码并没有什么实际价值,全当敲着玩了。另外由于时间紧任务重,代码质量不高,有些功能上的缺失以及逻辑上的漏洞,山商的学弟学妹切勿直接复制、粘贴 。
任务一
成绩统计系统
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#define MAX_N 10005
#define MAX_M 15
string les[MAX_M];
double val[MAX_M];
struct Stu
{
string id;
string name;
int score[MAX_M];
double sum;
int rank;
}stu[MAX_N];
void input_1(int n,int m) //权值相同的输入
{
cout << "请输入每门考试的名称:" << endl << endl;
for(int i=1;i<=m;i++)
cin >> les[i];
cout << endl;
cout << "请输入每名学生的学号、姓名以及各科成绩:" << endl << endl;
for(int i=1;i<=n;i++)
{
cin >> stu[i].id;
cin >> stu[i].name;
for(int j=1;j<=m;j++)
{
cin >> stu[i].score[j];
stu[i].sum+=stu[i].score[j];
}
}
cout << endl;
}
void input_2(int n,int m) //权值不同的输入
{
cout << "请输入每门考试的名称、权值:" << endl << endl;
for(int i=1;i<=m;i++)
cin >> les[i] >> val[i];
cout << endl;
cout << "请输入每名学生的学号、姓名以及各科成绩:" << endl << endl;
for(int i=1;i<=n;i++)
{
cin >> stu[i].id;
cin >> stu[i].name;
for(int j=1;j<=m;j++)
{
cin >> stu[i].score[j];
stu[i].sum+=stu[i].score[j]*val[j];
}
}
cout << endl;
}
void output_1(int n,int m) //权值相同的输出
{
cout << "名次表为:" << endl << endl;
cout << setw(10) << left << "排名" << "姓名" << endl;
for(int i=1;i<=n;i++)
cout << setw(10) << left << stu[i].rank << stu[i].name << endl;
cout << endl;
cout << "成绩表为:" << endl << endl;
cout << setw(10) << left << "学号";
cout << setw(10) << left << "姓名";
cout << setw(10) << left << "总分";
for(int i=1;i<=m;i++)
{
if(i!=m)
cout << setw(10) << left << les[i];
else
cout << les[i] << endl;
}
for(int i=1;i<=n;i++)
{
cout << setw(10) << left << stu[i].id;
cout << setw(10) << left << stu[i].name;
cout << setw(10) << left << stu[i].sum;
for(int j=1;j<=m;j++)
{
if(j!=m)
cout << setw(10) << left << stu[i].score[j];
else
cout << stu[i].score[j] << endl;
}
}
}
void output_2(int n,int m) //权值不同的输出
{
cout << "名次表为:" << endl << endl;
cout << setw(10) << left << "排名" << "姓名" << endl;
for(int i=1;i<=n;i++)
cout << setw(10) << left << stu[i].rank << stu[i].name << endl;
cout << endl;
cout << "成绩表为:" << endl << endl;
cout << setw(10) << left << "学号";
cout << setw(10) << left << "姓名";
cout << setw(20) << left << "总分(带权计算)";
for(int i=1;i<=m;i++)
{
if(i!=m)
cout << setw(10) << left << les[i];
else
cout << les[i] << endl;
}
for(int i=1;i<=n;i++)
{
cout << setw(10) << left << stu[i].id;
cout << setw(10) << left << stu[i].name;
cout << setw(20) << left << stu[i].sum;
for(int j=1;j<=m;j++)
{
if(j!=m)
cout << setw(10) << left << stu[i].score[j];
else
cout << stu[i].score[j] << endl;
}
}
}
void insert_sort(int n) //插入排序(稳定)
{
for(int i=2;i<=n;i++)
{
int j=i;
while(j>1&&stu[j].sum>stu[j-1].sum)
{
swap(stu[j],stu[j-1]);
j--;
}
}
}
void rank_sort(int n) //计算排名
{
for(int i=1;i<=n;i++)
{
if(stu[i].sum!=stu[i-1].sum)
stu[i].rank=i;
else
stu[i].rank=stu[i-1].rank;
}
}
int main()
{
cout << "********************成绩统计系统********************" << endl << endl;
cout << "请输入学生人数以及考试门数:" << endl << endl;
int n,m;
cin >> n >> m;
cout << endl;
cout << "每门考试的权值是否相同?" << endl << endl;
cout << "1.是" << endl << endl;
cout << "2.否" << endl << endl;
int opt;
cin >> opt;
cout << endl;
switch(opt)
{
case 1: input_1(n,m); break;
case 2: input_2(n,m); break;
}
insert_sort(n);
rank_sort(n);
switch(opt)
{
case 1: output_1(n,m); break;
case 2: output_2(n,m); break;
}
return 0;
}
任务二
员工管理系统
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;
struct Stf
{
string id;
string name;
string gender;
string bir;
string edu;
string title;
string tel;
string place;
Stf *next;
};
typedef Stf * PStf;
vector <PStf> v;
PStf creat() //创建头节点
{
PStf head=new Stf;
head->next=NULL;
return head;
}
void print(PStf head) //打印信息
{
PStf p=head->next;
if(!p)
cout << "系统为空!!!" << endl << endl;
else
{
cout << "系统信息如下:" << endl << endl;
cout << setw(10) << left << "编号";
cout << setw(10) << left << "姓名";
cout << setw(10) << left << "性别";
cout << setw(10) << left << "出生年月";
cout << setw(10) << left << "学历";
cout << setw(10) << left << "职务";
cout << setw(10) << left << "电话";
cout << "住址" << endl;
while(p)
{
cout << setw(10) << left << p->id;
cout << setw(10) << left << p->name;
cout << setw(10) << left << p->gender;
cout << setw(10) << left << p->bir;
cout << setw(10) << left << p->edu;
cout << setw(10) << left << p->title;
cout << setw(10) << left << p->tel;
cout << p->place << endl;
p=p->next;
}
cout << endl;
}
}
void ins(PStf head) //增加信息
{
cout << "请输入待增人员数量:" << endl << endl;
int n;
cin >> n;
cout << endl;
cout << "请输入待增人员信息(编号、姓名、性别、出生年月、学历、职务、电话和住址):" << endl << endl;
for(int i=1;i<=n;i++)
{
PStf p=new Stf;
cin >> p->id >> p->name >> p->gender >> p->bir >> p->edu >> p->title >> p->tel >> p->place;
cout << endl;
p->next=head->next;
head->next=p;
}
cout << "信息增加成功!!!" << endl << endl;
}
void del(PStf head) //删除信息
{
cout << "请输入待删人员编号:" << endl << endl;
string id;
cin >> id;
cout << endl;
int flag=0;
PStf pre=head,p=head->next;
while(p)
{
if(p->id==id)
{
flag=1;
pre->next=p->next;
delete p;
break;
}
else
{
pre=pre->next;
p=p->next;
}
}
if(!flag)
cout << "系统中无此人员!!!" << endl << endl;
else
cout << "信息删除成功!!!" << endl << endl;
}
void upd(PStf head) //修改信息
{
cout << "请输入待改人员编号:" << endl << endl;
string id;
cin >> id;
cout << endl;
int flag=0;
PStf p=head->next;
while(p)
{
if(p->id==id)
{
flag=1;
break;
}
else
p=p->next;
}
if(!flag)
cout << "系统中无此人员!!!" << endl << endl;
else
{
cout << "请输入要修改的关键字的个数:" << endl << endl;
int n;
cin >> n;
cout << endl;
cout << "请输入要修改的关键字以及要修改的值:" << endl << endl;
for(int i=1;i<=n;i++)
{
string key,val;
cin >> key >> val;
cout << endl;
if(key=="编号") p->id=val;
if(key=="姓名") p->name=val;
if(key=="性别") p->gender=val;
if(key=="出生年月") p->bir=val;
if(key=="学历") p->edu=val;
if(key=="职务") p->title=val;
if(key=="电话") p->tel=val;
if(key=="住址") p->place=val;
}
cout << "修改后的信息如下:" << endl << endl;
cout << setw(10) << left << p->id;
cout << setw(10) << left << p->name;
cout << setw(10) << left << p->gender;
cout << setw(10) << left << p->bir;
cout << setw(10) << left << p->edu;
cout << setw(10) << left << p->title;
cout << setw(10) << left << p->tel;
cout << p->place << endl << endl;
}
}
void que(PStf head) //查询信息
{
v.clear();
cout << "请输入要查询的关键字以及要查询的值:" << endl << endl;
string key,val;
cin >> key >> val;
cout << endl;
int flag=0;
PStf p=head->next;
if(key=="编号")
{
while(p)
{
if(p->id==val) {flag=1; v.push_back(p); p=p->next;}
else p=p->next;
}
}
if(key=="姓名")
{
while(p)
{
if(p->name==val) {flag=1; v.push_back(p); p=p->next;}
else p=p->next;
}
}
if(key=="性别")
{
while(p)
{
if(p->gender==val) {flag=1; v.push_back(p); p=p->next;}
else p=p->next;
}
}
if(key=="出生年月")
{
while(p)
{
if(p->bir==val) {flag=1; v.push_back(p); p=p->next;}
else p=p->next;
}
}
if(key=="学历")
{
while(p)
{
if(p->edu==val) {flag=1; v.push_back(p); p=p->next;}
else p=p->next;
}
}
if(key=="职务")
{
while(p)
{
if(p->title==val) {flag=1; v.push_back(p); p=p->next;}
else p=p->next;
}
}
if(key=="电话")
{
while(p)
{
if(p->tel==val) {flag=1; v.push_back(p); p=p->next;}
else p=p->next;
}
}
if(key=="住址")
{
while(p)
{
if(p->place==val) {flag=1; v.push_back(p); p=p->next;}
else p=p->next;
}
}
if(!flag)
cout << "系统中无此人员!!!" << endl << endl;
else
{
cout << "相关人员信息如下:" << endl << endl;
cout << setw(10) << left << "编号";
cout << setw(10) << left << "姓名";
cout << setw(10) << left << "性别";
cout << setw(10) << left << "出生年月";
cout << setw(10) << left << "学历";
cout << setw(10) << left << "职务";
cout << setw(10) << left << "电话";
cout << "住址" << endl;
for(int i=0;i<v.size();i++)
{
cout << setw(10) << left << v[i]->id;
cout << setw(10) << left << v[i]->name;
cout << setw(10) << left << v[i]->gender;
cout << setw(10) << left << v[i]->bir;
cout << setw(10) << left << v[i]->edu;
cout << setw(10) << left << v[i]->title;
cout << setw(10) << left << v[i]->tel;
cout << v[i]->place << endl;
}
cout << endl;
}
}
void my_sort(PStf head) //信息排序,操作指针实现插入排序
{
cout << "请输入排序的关键字(支持编号、姓名、性别和出生年月):" << endl << endl;
string key;
cin >> key;
cout << endl;
if(key=="编号")
{
cout << "请输入操作:" << endl << endl;
cout << "1.升序排序" << endl << endl;
cout << "2.降序排序" << endl << endl;
int opt;
cin >> opt;
cout << endl;
if(opt==1)
{
if(head->next->next)
{
PStf pre=head->next,p=head->next->next;
while(p)
{
PStf t=head;
while(p->id>t->next->id)
t=t->next;
if(t->next==p)
{
pre=pre->next;
p=p->next;
}
else
{
PStf suf=p->next;
p->next=t->next;
t->next=p;
pre->next=suf;
p=suf;
}
}
}
}
if(opt==2)
{
if(head->next->next)
{
PStf pre=head->next,p=head->next->next;
while(p)
{
PStf t=head;
while(p->id<t->next->id)
t=t->next;
if(t->next==p)
{
pre=pre->next;
p=p->next;
}
else
{
PStf suf=p->next;
p->next=t->next;
t->next=p;
pre->next=suf;
p=suf;
}
}
}
}
}
if(key=="姓名")
{
cout << "请输入操作:" << endl << endl;
cout << "1.升序排序" << endl << endl;
cout << "2.降序排序" << endl << endl;
int opt;
cin >> opt;
cout << endl;
if(opt==1)
{
if(head->next->next)
{
PStf pre=head->next,p=head->next->next;
while(p)
{
PStf t=head;
while(p->name>t->next->name)
t=t->next;
if(t->next==p)
{
pre=pre->next;
p=p->next;
}
else
{
PStf suf=p->next;
p->next=t->next;
t->next=p;
pre->next=suf;
p=suf;
}
}
}
}
if(opt==2)
{
if(head->next->next)
{
PStf pre=head->next,p=head->next->next;
while(p)
{
PStf t=head;
while(p->name<t->next->name)
t=t->next;
if(t->next==p)
{
pre=pre->next;
p=p->next;
}
else
{
PStf suf=p->next;
p->next=t->next;
t->next=p;
pre->next=suf;
p=suf;
}
}
}
}
}
if(key=="性别")
{
cout << "请输入操作:" << endl << endl;
cout << "1.先男后女" << endl << endl;
cout << "2.先女后男" << endl << endl;
int opt;
cin >> opt;
cout << endl;
if(opt==1)
{
if(head->next->next)
{
PStf pre=head->next,p=head->next->next;
while(p)
{
PStf t=head;
while(p->gender>t->next->gender)
t=t->next;
if(t->next==p)
{
pre=pre->next;
p=p->next;
}
else
{
PStf suf=p->next;
p->next=t->next;
t->next=p;
pre->next=suf;
p=suf;
}
}
}
}
if(opt==2)
{
if(head->next->next)
{
PStf pre=head->next,p=head->next->next;
while(p)
{
PStf t=head;
while(p->gender<t->next->gender)
t=t->next;
if(t->next==p)
{
pre=pre->next;
p=p->next;
}
else
{
PStf suf=p->next;
p->next=t->next;
t->next=p;
pre->next=suf;
p=suf;
}
}
}
}
}
if(key=="出生年月")
{
cout << "请输入操作:" << endl << endl;
cout << "1.升序排序" << endl << endl;
cout << "2.降序排序" << endl << endl;
int opt;
cin >> opt;
cout << endl;
if(opt==1)
{
if(head->next->next)
{
PStf pre=head->next,p=head->next->next;
while(p)
{
PStf t=head;
while(p->bir>t->next->bir)
t=t->next;
if(t->next==p)
{
pre=pre->next;
p=p->next;
}
else
{
PStf suf=p->next;
p->next=t->next;
t->next=p;
pre->next=suf;
p=suf;
}
}
}
}
if(opt==2)
{
if(head->next->next)
{
PStf pre=head->next,p=head->next->next;
while(p)
{
PStf t=head;
while(p->bir<t->next->bir)
t=t->next;
if(t->next==p)
{
pre=pre->next;
p=p->next;
}
else
{
PStf suf=p->next;
p->next=t->next;
t->next=p;
pre->next=suf;
p=suf;
}
}
}
}
}
print(head);
}
void my_free(PStf head) //释放内存空间
{
PStf p=head->next;
while(p)
{
PStf t=p;
p=p->next;
head->next=p;
delete t;
}
delete head;
}
int main()
{
PStf head=creat();
int flag=1;
while(flag)
{
cout << "********************员工管理系统********************" << endl;
cout << "** **" << endl;
cout << "** 1.增加信息 **" << endl;
cout << "** **" << endl;
cout << "** 2.删除信息 **" << endl;
cout << "** **" << endl;
cout << "** 3.修改信息 **" << endl;
cout << "** **" << endl;
cout << "** 4.查询信息 **" << endl;
cout << "** **" << endl;
cout << "** 5.信息排序 **" << endl;
cout << "** **" << endl;
cout << "** 6.打印信息 **" << endl;
cout << "** **" << endl;
cout << "** 7.退出系统 **" << endl;
cout << "** **" << endl;
cout << "********************祝您使用愉快********************" << endl << endl;
cout << "请输入您想进行的操作:" << endl << endl;
int opt;
cin >> opt;
cout << endl;
switch(opt)
{
case 1: ins(head); break;
case 2: del(head); break;
case 3: upd(head); break;
case 4: que(head); break;
case 5: my_sort(head); break;
case 6: print(head); break;
case 7: flag=0; break;
}
if(opt!=7)
{
getchar();
cout << "按回车键继续..." << endl;
getchar();
}
}
my_free(head);
return 0;
}
任务三
哈希表
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;
#define N 100005
#define P 31
#define M 32
#define PRIME 389
#define MOD 98317
string name[N];
string hash_table_1[M];
vector <string> v[MOD];
struct Node
{
string s;
Node *next;
};
typedef Node * PNode;
struct Hash_Table
{
PNode *head;
};
typedef Hash_Table * PHash_Table;
void hash_1(int n) //取余法(顺序表)
{
for(int i=1;i<=n;i++)
{
int key=0;
for(int j=0;j<name[i].length();j++)
key+=name[i][j]-'a'+1;
int pos=key%P;
for(int j=0;j<M;j++)
{
if(hash_table_1[pos+j]=="")
{
hash_table_1[pos+j]=name[i];
break;
}
}
}
}
double query_1(int n) //线性探测法
{
int sum=0;
for(int i=1;i<=n;i++)
{
int key=0;
for(int j=0;j<name[i].length();j++)
key+=name[i][j]-'a'+1;
int pos=key%P;
int cnt=1;
for(int j=0;j<M;j++)
{
if(hash_table_1[pos+j]!=name[i])
cnt++;
else
break;
}
sum+=cnt;
}
return (double)sum/n;
}
PHash_Table creat_hash_table() //创建链表头指针
{
PHash_Table hash_table=(PHash_Table)malloc(sizeof(Hash_Table));
hash_table->head=(PNode *)malloc(M*sizeof(PNode));
for(int i=0;i<M;i++)
{
hash_table->head[i]=new Node;
hash_table->head[i]->next=NULL;
}
return hash_table;
}
void hash_2(PHash_Table hash_table_2,int n) //取余法(链表)
{
for(int i=1;i<=n;i++)
{
int key=0;
for(int j=0;j<name[i].length();j++)
key+=name[i][j]-'a'+1;
int pos=key%P;
PNode p=new Node;
p->s=name[i];
p->next=hash_table_2->head[pos]->next;
hash_table_2->head[pos]->next=p;
}
}
double query_2(PHash_Table hash_table_2,int n) //链地址法
{
int sum=0;
for(int i=1;i<=n;i++)
{
int key=0;
for(int j=0;j<name[i].length();j++)
key+=name[i][j]-'a'+1;
int pos=key%P;
int cnt=1;
PNode p=hash_table_2->head[pos]->next;
while(p->s!=name[i])
{
cnt++;
p=p->next;
}
sum+=cnt;
}
return (double)sum/n;
}
void my_free(PHash_Table hash_table_2) //释放内存空间
{
for(int i=0;i<M;i++)
{
PNode p=hash_table_2->head[i]->next;
while(p)
{
PNode t=p;
p=p->next;
hash_table_2->head[i]->next=p;
delete t;
}
}
delete hash_table_2->head;
delete hash_table_2;
}
void hash_3(int n) //定址法(素数优化)
{
for(int i=1;i<=n;i++)
{
int key=0;
for(int j=0;j<name[i].length();j++)
key=((key*P)+(name[i][j]-'a'+1))%MOD;
v[key].push_back(name[i]);
}
}
double query_3(int n) //vector数组模拟链地址法
{
int sum=0;
for(int i=1;i<=n;i++)
{
int key=0;
for(int j=0;j<name[i].length();j++)
key=((key*P)+(name[i][j]-'a'+1))%MOD;
int cnt=1;
for(int j=0;j<v[key].size();j++)
{
if(v[key][j]!=name[i])
cnt++;
else
break;
}
sum+=cnt;
}
return (double)sum/n;
}
int main()
{
cout << "请输入待增人数:" << endl << endl;
int n;
cin >> n;
cout << endl;
cout << "请输入待增人员姓名:" << endl << endl;
for(int i=1;i<=n;i++)
cin >> name[i];
cout << endl;
hash_1(n);
double asl_1=query_1(n);
PHash_Table hash_table_2=creat_hash_table();
hash_2(hash_table_2,n);
double asl_2=query_2(hash_table_2,n);
hash_3(n);
double asl_3=query_3(n);
cout << "根据不同的构造方式以及不同的冲突处理方式得到的平均查找长度如下:" << endl << endl;
cout << setw(20) << left << "构造方式" << setw(20) << left << "冲突处理方式" << "平均查找长度" << endl << endl;
cout << setw(20) << left << "取余法" << setw(20) << left << "线性探测法" << asl_1 << endl << endl;
cout << setw(20) << left << "取余法" << setw(20) << left << "链地址法" << asl_2 << endl << endl;
cout << setw(20) << left << "定址法(素数优化)" << setw(20) << left << "链地址法" << asl_3 << endl;
my_free(hash_table_2);
return 0;
}
任务五
哈夫曼编码、译码系统
#include <iostream>
#include <string>
#include <vector>
#include <queue>
using namespace std;
#define N 200
#define INF 0x3f3f3f3f
struct Node //哈夫曼树节点信息
{
int id;
char c;
int sum_val;
int depth;
string s;
Node *lch;
Node *rch;
};
typedef Node * PNode;
struct Infor //字符信息
{
char c;
int val;
bool flag;
PNode p;
};
vector <Infor> infor; //字符集信息
string code[N]; //字符集编码
vector <PNode> pre; //先序遍历哈夫曼树序列
bool judge() //判断是否所有的字符都已上树
{
int cnt=0;
for(int i=0;i<infor.size();i++)
if(!infor[i].flag)
cnt++;
if(cnt==1)
return false;
else
return true;
}
PNode build_hfmtree() //构建哈夫曼树
{
for(int i=0;i<infor.size();i++)
{
PNode p=new Node;
p->id=0;
p->c=infor[i].c;
p->sum_val=infor[i].val;
p->depth=0;
p->s="";
p->lch=NULL;
p->rch=NULL;
infor[i].p=p;
}
while(judge())
{
int min=INF,min_id=-1;
int sub_min=INF,sub_min_id=-1;
for(int i=0;i<infor.size();i++)
{
if(!infor[i].flag)
{
if(infor[i].val<min)
{
sub_min=min;
sub_min_id=min_id;
min=infor[i].val;
min_id=i;
}
else if(infor[i].val<sub_min)
{
sub_min=infor[i].val;
sub_min_id=i;
}
}
}
infor[min_id].flag=true;
infor[sub_min_id].flag=true;
PNode p=new Node;
p->id=0;
p->c=0;
p->sum_val=infor[min_id].p->sum_val+infor[sub_min_id].p->sum_val;
p->depth=0;
p->s="";
p->lch=infor[min_id].p;
p->rch=infor[sub_min_id].p;
infor.push_back((Infor){'\0',p->sum_val,false,p});
}
int id=-1;
for(int i=0;i<infor.size();i++)
{
if(!infor[i].flag)
{
id=i;
break;
}
}
return infor[id].p;
}
void coding(PNode fa,PNode now,char c) //为字符集编码
{
if(now!=fa) now->s=fa->s+c;
if(now->c) code[now->c]=now->s;
if(now->lch) coding(now,now->lch,'0');
if(now->rch) coding(now,now->rch,'1');
}
void print_code(string s) //打印编码、译码结果
{
int cnt=0;
for(int i=0;i<s.length();i++)
{
cout << s[i];
cnt++;
if(cnt==50)
{
cout << endl;
cnt=0;
}
}
if(cnt) cout << endl;
cout << endl;
}
void encoding() //字符串编码操作
{
cout << "请输入待编码字符串:" << endl << endl;
string s;
getchar();
getline(cin,s);
cout << endl;
for(int i=0;i<s.length();i++)
{
if(code[s[i]]=="")
{
cout << "待编码字符串含非法字符!!!" << endl << endl;
return ;
}
}
string res="";
for(int i=0;i<s.length();i++)
res+=code[s[i]];
cout << "字符串编码如下:" << endl << endl;
print_code(res);
}
void decoding(PNode root) //字符串译码操作
{
cout << "请输入待译码字符串" << endl << endl;
string s;
getchar();
getline(cin,s);
cout << endl;
string res="";
PNode p=root;
for(int i=0;i<s.length();i++)
{
if(s[i]=='0')
{
if(!p->lch)
{
cout << "带译码字符串含非法字符!!!" << endl << endl;
return ;
}
p=p->lch;
if(p->c)
{
res+=p->c;
p=root;
}
}
else
{
if(!p->rch)
{
cout << "带译码字符串含非法字符!!!" << endl << endl;
return ;
}
p=p->rch;
if(p->c)
{
res+=p->c;
p=root;
}
}
}
cout << "字符串译码如下:" << endl << endl;
print_code(res);
}
void depth_order(PNode root) //层次遍历哈夫曼树(用于打印凹入表)
{
int cnt=0;
queue <PNode> q;
root->id=++cnt;
root->depth=1;
q.push(root);
while(!q.empty())
{
PNode now=q.front();
q.pop();
if(now->lch)
{
now->lch->id=++cnt;
now->lch->depth=now->depth+1;
q.push(now->lch);
}
if(now->rch)
{
now->rch->id=++cnt;
now->rch->depth=now->depth+1;
q.push(now->rch);
}
}
}
void pre_order(PNode root) //先序遍历哈夫曼树(用于打印凹入表)
{
pre.push_back(root);
if(root->lch) pre_order(root->lch);
if(root->rch) pre_order(root->rch);
}
void print_tree(PNode root) //打印哈夫曼树的凹入表
{
depth_order(root);
pre_order(root);
cout << "根据输入的字符集建立的哈夫曼树如下:" << endl << endl;
for(int i=0;i<pre.size();i++)
{
int len=6*(pre[i]->depth-1);
for(int j=1;j<=len;j++) cout << " ";
if(pre[i]->id<10) cout << " ";
cout << pre[i]->id << ": ";
if(pre[i]->c) cout << pre[i]->c;
else cout << "#";
if(pre[i]==root) cout << "(r)";
else cout << "(" << pre[i]->s[pre[i]->s.length()-1] << ")";
for(int j=1;j<=90-len-8;j++) cout << "-";
cout << endl;
}
cout << endl;
}
void my_free(PNode root) //释放内存空间
{
if(root->lch) my_free(root->lch);
if(root->rch) my_free(root->rch);
delete root;
}
int main()
{
cout << "请输入字符集大小:" << endl << endl;
int n;
cin >> n;
cout << endl;
cout << "请输入每个字符及其对应的权值:" << endl << endl;
for(int i=1;i<=n;i++)
{
char c;
int val;
getchar();
scanf("%c%d",&c,&val);
infor.push_back((Infor){c,val,false,NULL});
}
cout << endl;
PNode root=build_hfmtree();
coding(root,root,'\0');
int flag=1;
while(flag)
{
cout << "********************哈夫曼编码、译码系统********************" << endl;
cout << "** **" << endl;
cout << "** 1.编码 **" << endl;
cout << "** **" << endl;
cout << "** 2.译码 **" << endl;
cout << "** **" << endl;
cout << "** 3.打印哈夫曼树 **" << endl;
cout << "** **" << endl;
cout << "** 4.退出系统 **" << endl;
cout << "** **" << endl;
cout << "************************祝您使用愉快************************" << endl << endl;
cout << "请输入您想进行的操作:" << endl << endl;
int opt;
cin >> opt;
cout << endl;
switch(opt)
{
case 1: encoding(); break;
case 2: decoding(root); break;
case 3: print_tree(root); break;
case 4: flag=0; break;
}
}
my_free(root);
return 0;
}
任务六
银行排队管理系统
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
struct Vip //vip客户信息
{
string name;
string card;
int dep;
int rank;
};
struct Cus //今日顾客信息
{
string name;
string card;
int arr;
int dur;
int rank;
};
struct Node //队列顾客信息
{
string name;
string card;
int arr;
int dur;
int rank;
int id;
int st;
Node *next;
};
typedef Node * PNode;
struct Q //队列总体信息
{
int num;
PNode next;
};
typedef Q * PQ;
int sum_time; //今日顾客逗留总时长
double ave_time; //今日顾客逗留平均时长
vector <Vip> vip; //vip客户档案
vector <Cus> cus; //今日顾客档案
vector <Vip> v; //今日vip顾客档案
void my_sort() //插入排序,第一关键字为顾客的到达时间(由早到晚),第二关键字为顾客的服务优先级(由高到低)
{
for(int i=1;i<cus.size();i++)
{
Cus c=cus[i];
int pos;
for(pos=i-1;pos>=0;pos--)
{
if(c.arr<cus[pos].arr)
cus[pos+1]=cus[pos];
else if(c.arr==cus[pos].arr)
{
if(c.rank<cus[pos].rank)
cus[pos+1]=cus[pos];
else
break;
}
else
break;
}
cus[pos+1]=c;
}
}
PQ creat_queue() //创建队列以及头节点
{
PQ q=new Q;
PNode p=new Node;
q->num=0;
q->next=p;
p->next=NULL;
return q;
}
int query(PQ q,int rank) //查询q号队列中有多少顾客的服务优先级高于被查者(认为正在接受服务的人的服务优先级也高于被查者)
{
int num=0;
PNode p=q->next->next;
while(p)
{
if(p->st!=INF||p->rank<=rank)
num++;
p=p->next;
}
return num;
}
void ins(PQ q,Cus c,int id) //在q号队列的合适位置插入顾客c(按服务优先级不同分为两种操作)
{
PNode p=new Node;
p->name=c.name;
p->card=c.card;
p->arr=c.arr;
p->dur=c.dur;
p->rank=c.rank;
p->id=id;
p->st=INF;
PNode t=q->next;
if(c.rank==4)
{
while(t->next)
t=t->next;
p->next=t->next;
t->next=p;
}
else
{
if(t->next&&t->next->st!=INF)
t=t->next;
while(t->next&&t->next->rank<=c.rank)
t=t->next;
p->next=t->next;
t->next=p;
}
q->num++;
}
void upd_1(PQ q,int time) //检查在time时间q号队列是否有完成业务的顾客,如有则更新
{
PNode p=q->next->next;
if(p&&p->st+p->dur==time)
{
int arr_h=time/60,arr_m=time%60;
printf("当前时间为:%02d:%02d ",arr_h,arr_m);
cout << "尊敬的" << p->name << "顾客,您本次办理的业务已完成,欢迎下次光临" << endl << endl;
sum_time+=time-p->arr;
q->num--;
q->next->next=p->next;
delete p;
}
}
void upd_2(PQ q,int time,int id) //检查在time时间q号队列是否有在等待服务的顾客,如有则更新
{
PNode p=q->next->next;
if(p&&p->st==INF)
{
int arr_h=time/60,arr_m=time%60;
printf("当前时间为:%02d:%02d ",arr_h,arr_m);
cout << "请顺序号为" << p->id << "的" << p->name << "顾客到" << id << "号窗口办理业务" << endl << endl;
p->st=time;
}
}
void upd_3(PQ q) //检查q号队列在营业时间结束后是否还有未完成业务的顾客,如有则计算其逗留时间
{
if(q->next->next)
{
PNode pre=q->next->next;
PNode p=pre->next;
while(p)
{
p->st=pre->st+pre->dur;
pre=pre->next;
p=p->next;
}
p=q->next->next;
while(p)
{
sum_time+=p->st+p->dur-p->arr;
p=p->next;
}
}
}
void my_free(PQ q) //释放内存空间
{
PNode p=q->next;
while(p)
{
PNode t=p;
p=p->next;
q->next=p;
delete t;
}
delete q;
}
int main()
{
cout << "请输入本银行vip客户人数:" << endl << endl;
int n;
cin >> n;
cout << endl;
cout << "请输入每个vip客户的姓名、银行卡号和存款金额:" << endl << endl;
for(int i=1;i<=n;i++)
{
string name,card;
int dep,rank;
cin >> name >> card >> dep;
cout << endl;
if(dep<=500000) rank=3;
else if(dep<=1000000) rank=2;
else rank=1;
vip.push_back((Vip){name,card,dep,rank});
}
cout << "请输入今天来银行办理业务的客户的姓名、银行卡号、到达时间(例如8:00)和预计办理业务时长(以-1结束):" << endl << endl;
string name,card;
int arr_h,arr_m,arr,dur;
while(cin >> name)
{
if(name=="-1")
{
cout << endl;
break;
}
cin >> card;
scanf("%d:%d %d",&arr_h,&arr_m,&dur);
cout << endl;
arr=arr_h*60+arr_m;
if(arr>=480&&arr<=960)
{
int flag=0,id;
for(id=0;id<vip.size();id++)
{
if(card==vip[id].card)
{
flag=1;
break;
}
}
if(flag)
cus.push_back((Cus){name,card,arr,dur,vip[id].rank});
else
cus.push_back((Cus){name,card,arr,dur,4});
}
}
my_sort();
cout << "骋哥家的银行开始营业啦!营业时间为8:00-16:00,欢迎光临!" << endl << endl;
PQ q[3];
for(int i=0;i<3;i++)
q[i]=creat_queue();
int id=0,vip_num=0;
for(int i=480;i<=960;i++)
{
for(int j=0;j<3;j++)
upd_1(q[j],i);
for(;id<cus.size();id++)
{
if(cus[id].arr==i)
{
int flag=0;
for(int j=0;j<vip.size();j++)
{
if(cus[id].card==vip[j].card)
{
flag=1;
vip_num++;
v.push_back(vip[j]);
break;
}
}
if(flag) //vip客户走的流程
{
int min=INF,id=-1;
for(int j=0;j<3;j++)
{
int num=query(q[j],cus[id].rank);
if(num<min)
{
min=num;
id=j;
}
}
int arr_h=i/60,arr_m=i%60;
printf("当前时间为:%02d:%02d ",arr_h,arr_m);
cout << "尊敬的" << cus[id].name << "顾客,您的顺序号为" << id+1 << ",由于您是vip客户,享有vip特权,请您前往" << id+1 << "号窗口的第" << min+1 << "位等待" << endl << endl;
ins(q[id],cus[id],id+1);
}
else //普通客户走的流程
{
int min=INF,id=-1;
for(int j=0;j<3;j++)
if(q[j]->num<min)
min=q[j]->num,id=j;
int arr_h=i/60,arr_m=i%60;
printf("当前时间为:%02d:%02d ",arr_h,arr_m);
cout << "尊敬的" << cus[id].name << "顾客,您的顺序号为" << id+1 << ",请您前往" << id+1 << "号窗口等待" << endl << endl;
ins(q[id],cus[id],id+1);
}
}
else
break;
}
for(int j=0;j<3;j++)
upd_2(q[j],i,j+1);
}
for(int i=0;i<3;i++)
upd_3(q[i]);
cout << "今天共接待了" << cus.size() << "位顾客,其中有" << vip_num << "位顾客为vip客户,其信息如下:" << endl << endl;
cout << setw(20) << left << "客户姓名";
cout << setw(20) << left << "银行卡号";
cout << setw(20) << left << "存款金额";
cout << "服务优先级" << endl << endl;
for(int i=0;i<v.size();i++)
{
cout << setw(20) << left << v[i].name;
cout << setw(20) << left << v[i].card;
cout << setw(20) << left << v[i].dep;
cout << v[i].rank << endl << endl;
}
ave_time=(double)sum_time/cus.size();
cout << "今天顾客的平均逗留时间(包括等待时间和办理业务时间)为:" << ave_time << "分钟" << endl << endl;
cout << "骋哥家的银行,服务态度包您满意!" << endl << endl;
for(int i=0;i<3;i++)
my_free(q[i]);
return 0;
}