谁懂C++啊,帮忙看下程序,画个流程图出来!
我是学平面这块的,但要交一个与哈夫曼有关的论文,其中要画流程图,代码是在网上下的,但是会画!求大虾们赐教。下面是代码//#include "stdafx.h"//在Visual studio中运行,应取消该句的注释!
#include <iostream>
#include <queue>
#include <vector>
#include <iomanip>
#include <string>
#include<cctype>
using namespace std;
struct codeInformation{
double priority;
char codeName;
int lchild,rchild,parent;bool test;
bool operator < (const codeInformation& x) const {return !(priority<x.priority);}
};
bool check(vector<codeInformation> qa,const char c)
{
for (int i=0 ;i<(int)(qa.size());i++)
{
if(qa[i].codeName==c) return true;
} return false;
}
void aline(char c,int n){
for (int i=0;i<n;i++)
cout<<c;
}
int InputElement(vector<codeInformation>* Harffcode,priority_queue<codeInformation>* pq)
{
int i=1,j=1;codeInformation wk;
while(i){aline('-',80);
cout<<"请输入第"<<j<<"个元素的字符名称(Ascll码):"<<flush;
cin>>wk.codeName;
while(check(* Harffcode,wk.codeName)){
cout<<"字符已存在,请输入一个其他的字符:";cin>>wk.codeName;
}
cout<<"请输入第"<<j<<"个元素的概率(权值):"<<flush;
cin>>wk.priority;
wk.lchild=wk.rchild=wk.parent=-1;wk.test=false;
Harffcode->push_back(wk);pq->push(wk);j++;
cout<<"1…………继续输入下一个元素信息!"<<endl;
cout<<"2…………已完成输入,并开始构造哈弗曼树!"<<endl;
cin>>i;if (i==2) i=0;
}
int count=1;j=Harffcode->size();
int selectElement(vector<codeInformation>*,priority_queue<codeInformation>*);
for (int k=j;k<2*j-1;k++)
{aline('*',80);
cout<<"第"<<count<<"次合并:"<<endl;
int i1=selectElement(Harffcode,pq);
int i2=selectElement(Harffcode,pq);
(*Harffcode)[i1].parent=(*Harffcode)[i2].parent=k;
wk.lchild=wk.rchild=wk.parent=-1;wk.codeName='#';(*Harffcode).push_back(wk);
wk.priority=(*Harffcode)[k].priority=(*Harffcode)[i1].priority+(*Harffcode)[i2].priority;
(*Harffcode)[k].lchild=i1;(*Harffcode)[k].rchild=i2;wk.test=false;
pq->push(wk); count++;
cout<<"所合成的节点名称:#(虚节点)\t"<<"概率(权值):"<<(*Harffcode)[k].priority<<endl;
}aline('*',80);
return j;
}
void showChar(const char c)
{
if(isspace(c))
cout<<"#";
cout<<c;
}
int selectElement(vector<codeInformation>*Harffcode,priority_queue<codeInformation>*qurgh)
{
for (int i=0;i<(int)(*Harffcode).size();i++)
{
if (((*Harffcode)[i].priority==(*qurgh).top().priority)&&((*Harffcode)[i].test==false))
{
cout<<"所选择的节点的信息:"<<"频率(权值):"<<setw(5)<<(*qurgh).top().priority<<"\t名为:";showChar((*qurgh).top().codeName);cout<<endl;
(*qurgh).pop();(*Harffcode)[i].test=true;
return i;
}
}
}
void huffmanCode(vector<codeInformation> Harffcode,int n)
{
for (int i1=0;i1<(int)Harffcode.size();i1++)
{
cout<<"array["<<i1<<"]的概率(权值):"<<Harffcode[i1].priority<<"\t"<<"名为:";showChar(Harffcode[i1].codeName);cout<<"\t父节点的数组下标索引值:"<<Harffcode[i1].parent<<endl;
}aline('&',80);
for (int i=0;i<n;i++)
{
string s=" "; int j=i;
while(Harffcode[j].parent>=0){
if (Harffcode[Harffcode[j].parent].lchild==j) s=s+"0";
else s=s+"1";j=Harffcode[j].parent;
}
cout<<"\n概率(权值)为:"<<setw(8)<<Harffcode[i].priority<<" 名为:";showChar(Harffcode[i].codeName);cout<<"的符号的编码是:";
for (int i=s.length();i>0;i--)
cout<<s[i-1];
}
}
void choise(){cout<<endl;aline('+',80);
cout<<"\n1……………………继续使用该程序"<<endl;
cout<<"2……………………退出系统"<<endl;}
void welcome()
int main()
{welcome();system("color d1");
int i=1,n;vector<codeInformation>huffTree; priority_queue<codeInformation>qpTree;
while(i!=2){
n=InputElement(&huffTree,&qpTree);
huffmanCode(huffTree, n);choise();cin>>i;huffTree.clear();
while(qpTree.empty()) qpTree.pop();
}
return 0;
}