哈夫曼树编码构造.docx
《哈夫曼树编码构造.docx》由会员分享,可在线阅读,更多相关《哈夫曼树编码构造.docx(25页珍藏版)》请在冰豆网上搜索。
![哈夫曼树编码构造.docx](https://file1.bdocx.com/fileroot1/2023-1/2/9307cfd3-6e42-4260-8152-c0bdcb6ec42d/9307cfd3-6e42-4260-8152-c0bdcb6ec42d1.gif)
哈夫曼树编码构造
#include
usingnamespacestd;
#include
#include
#include
#include
#include
#include
#definecno_len10
#definename_len10
#defineaddr_len15
#definesdate_len10
#definenmno_len10
#definetitle_len15
#defineperiod_len10
#defineprice_len4
#definestart_len10
#defineterm_len10
#defineissue_len10
#defineqty_len4
classorder
{public:
intselect1();
intselect2();
intinsert1();
intupdate1();
intremove1();
intinsert2();
intupdate2();
intremove2();
intinsert3();
intupdate3();
intremove3();
};
intorder:
:
select1()
{
/*Step1定义句柄和变量*/
SQLHENVserverhenv;//环境句柄
SQLHDBCserverhdbc;//连接句柄
SQLHSTMTserverhstmt;//语句句柄
SQLRETURNret;
SQLCHARcname[name_len],caddr[addr_len],ccno[cno_len],nnmno[nmno_len],ostart[start_len],oterm[term_len],osdate[sdate_len];
SQLINTEGERoqty;
SQLINTEGERcbcno=SQL_NTS,cbname=SQL_NTS,cbaddr=SQL_NTS,cbnmno=SQL_NTS,cbstart=SQL_NTS,cbterm=SQL_NTS,cbsdate=SQL_NTS,cbqty=0;
/*Step2初始化环境*/
ret=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&serverhenv);
ret=SQLSetEnvAttr(serverhenv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0);
/*Step3:
建立连接*/
ret=SQLAllocHandle(SQL_HANDLE_DBC,serverhenv,&serverhdbc);
ret=SQLConnect(serverhdbc,(SQLCHAR*)"wt",SQL_NTS,(SQLCHAR*)"sa",SQL_NTS,(SQLCHAR*)"sa",SQL_NTS);
if(!
SQL_SUCCEEDED(ret))//连接失败时返回错误值
return-1;
/*Step4:
初始化语句句柄*/
ret=SQLAllocHandle(SQL_HANDLE_STMT,serverhdbc,&serverhstmt);
//ret=SQLSetStmtAttr(serverhstmt,SQL_ATTR_ROW_BIND_TYPE,(SQLPOINTER)SQL_BIND_BY_COLUMN,SQL_IS_INTEGER);
/*Step5:
执行语句*/
ret=SQLPrepare(serverhstmt,(SQLCHAR*)"selectname,o,addr,sdate,nmno,start,term,qty\
fromcustomer,sorder\
whereo=o\
ando=?
",SQL_NTS);
if(ret==SQL_SUCCESS||ret==SQL_SUCCESS_WITH_INFO)
{
ret=SQLBindCol(serverhstmt,1,SQL_C_CHAR,cname,10,&cbname);
ret=SQLBindCol(serverhstmt,2,SQL_C_CHAR,ccno,10,&cbcno);
ret=SQLBindCol(serverhstmt,3,SQL_C_CHAR,caddr,15,&cbaddr);
ret=SQLBindCol(serverhstmt,4,SQL_C_CHAR,osdate,10,&cbsdate);
ret=SQLBindCol(serverhstmt,5,SQL_C_CHAR,nnmno,10,&cbnmno);
ret=SQLBindCol(serverhstmt,6,SQL_C_CHAR,ostart,10,&cbstart);
ret=SQLBindCol(serverhstmt,7,SQL_C_CHAR,oterm,10,&cbterm);
ret=SQLBindCol(serverhstmt,8,SQL_C_LONG,&oqty,4,&cbqty);
ret=SQLBindParameter(serverhstmt,1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,10,0,ccno,0,&cbcno);
cout<<"输入订户编号:
cno=";
cin>>ccno;
}
ret=SQLExecute(serverhstmt);
if(ret==SQL_NO_DATA_FOUND)
cout<<"Fetcherror\n";
while((ret=SQLFetch(serverhstmt))!
=SQL_NO_DATA_FOUND)
{cout</*Step7中止处理*/
SQLFreeHandle(SQL_HANDLE_STMT,serverhstmt);
SQLDisconnect(serverhdbc);
SQLFreeHandle(SQL_HANDLE_DBC,serverhdbc);
SQLFreeHandle(SQL_HANDLE_ENV,serverhenv);
}
intorder:
:
select2()
{
SQLHENVserverhenv;//环境句柄
SQLHDBCserverhdbc;//连接句柄
SQLHSTMTserverhstmt;//语句句柄
SQLRETURNret;
SQLCHARnnmno[nmno_len];
SQLINTEGERsum1;
SQLDOUBLEsum2;
SQLINTEGERcbnmno=SQL_NTS,cbsum1=0,cbsum2=0;
ret=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&serverhenv);
ret=SQLSetEnvAttr(serverhenv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0);
ret=SQLAllocHandle(SQL_HANDLE_DBC,serverhenv,&serverhdbc);
ret=SQLConnect(serverhdbc,(SQLCHAR*)"wt",SQL_NTS,(SQLCHAR*)"sa",SQL_NTS,(SQLCHAR*)"sa",SQL_NTS);
if(!
SQL_SUCCEEDED(ret))//连接失败时返回错误值
return-1;
ret=SQLAllocHandle(SQL_HANDLE_STMT,serverhdbc,&serverhstmt);
ret=SQLSetStmtAttr(serverhstmt,SQL_ATTR_ROW_BIND_TYPE,(SQLPOINTER)SQL_BIND_BY_COLUMN,SQL_IS_INTEGER);
ret=SQLExecDirect(serverhstmt,(SQLCHAR*)"selectsending.nmno,sum(qty),sum(qty*price)\
fromsending,nmtable\
wherenmtable.nmno=sending.nmno\
GROUPBYsending.nmno;",SQL_NTS);
if(ret==SQL_SUCCESS||ret==SQL_SUCCESS_WITH_INFO)
{
ret=SQLBindCol(serverhstmt,1,SQL_C_CHAR,nnmno,10,&cbnmno);
ret=SQLBindCol(serverhstmt,2,SQL_C_LONG,&sum1,0,&cbsum1);
ret=SQLBindCol(serverhstmt,3,SQL_C_DOUBLE,&sum2,0,&cbsum2);
}
while((ret=SQLFetch(serverhstmt))!
=SQL_NO_DATA_FOUND)//从查询结果中取出数据
{
if(ret==SQL_ERROR)
cout<<"Fetcherror\n";
else
cout<}
SQLFreeHandle(SQL_HANDLE_STMT,serverhstmt);
SQLDisconnect(serverhdbc);
SQLFreeHandle(SQL_HANDLE_DBC,serverhdbc);
SQLFreeHandle(SQL_HANDLE_ENV,serverhenv);
}
intorder:
:
insert1()
{
SQLHENVserverhenv;//环境句柄
SQLHDBCserverhdbc;//连接句柄
SQLHSTMTserverhstmt;//语句句柄
SQLRETURNret;
SQLCHARccno[cno_len],cname[name_len],caddr[addr_len];
SQLINTEGERcbcno=SQL_NTS,cbname=SQL_NTS,cbaddr=SQL_NTS;
ret=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&serverhenv);
ret=SQLSetEnvAttr(serverhenv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0);
ret=SQLAllocHandle(SQL_HANDLE_DBC,serverhenv,&serverhdbc);
ret=SQLConnect(serverhdbc,(SQLCHAR*)"wt",SQL_NTS,(SQLCHAR*)"sa",SQL_NTS,(SQLCHAR*)"sa",SQL_NTS);
if(!
SQL_SUCCEEDED(ret))//连接失败时返回错误值
return-1;
ret=SQLAllocHandle(SQL_HANDLE_STMT,serverhdbc,&serverhstmt);
ret=SQLPrepare(serverhstmt,(SQLCHAR*)"insertintocustomer(cno,name,addr)values(?
?
?
)",SQL_NTS);
if(ret==SQL_SUCCESS||ret==SQL_SUCCESS_WITH_INFO)
{
ret=SQLBindParameter(serverhstmt,1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,10,0,ccno,0,&cbcno);
ret=SQLBindParameter(serverhstmt,2,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,10,0,cname,0,&cbname);
ret=SQLBindParameter(serverhstmt,3,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,15,0,caddr,0,&cbaddr);
cout<<"输入插入值:
";
cin>>ccno>>cname>>caddr;
}
ret=SQLExecute(serverhstmt);
SQLFreeHandle(SQL_HANDLE_STMT,serverhstmt);
ret=SQLAllocHandle(SQL_HANDLE_STMT,serverhdbc,&serverhstmt);
ret=SQLSetStmtAttr(serverhstmt,SQL_ATTR_ROW_BIND_TYPE,(SQLPOINTER)SQL_BIND_BY_COLUMN,SQL_IS_INTEGER);
ret=SQLExecDirect(serverhstmt,(SQLCHAR*)"select*\
fromcustomer",SQL_NTS);
if(ret==SQL_SUCCESS||ret==SQL_SUCCESS_WITH_INFO)
{
ret=SQLBindCol(serverhstmt,1,SQL_C_CHAR,ccno,10,&cbcno);
ret=SQLBindCol(serverhstmt,2,SQL_C_CHAR,cname,10,&cbname);
ret=SQLBindCol(serverhstmt,3,SQL_C_CHAR,caddr,15,&cbaddr);
}
while((ret=SQLFetch(serverhstmt))!
=SQL_NO_DATA_FOUND)
{
if(ret==SQL_ERROR)
cout<<"Fetcherror\n";
else
cout<}
SQLFreeHandle(SQL_HANDLE_STMT,serverhstmt);
SQLDisconnect(serverhdbc);
SQLFreeHandle(SQL_HANDLE_DBC,serverhdbc);
SQLFreeHandle(SQL_HANDLE_ENV,serverhenv);
}
intorder:
:
update1()
{
SQLHENVserverhenv;//环境句柄
SQLHDBCserverhdbc;//连接句柄
SQLHSTMTserverhstmt;//语句句柄
SQLRETURNret;
SQLCHARccno[cno_len],cname[name_len],caddr[addr_len];
SQLINTEGERcbcno=SQL_NTS,cbname=SQL_NTS,cbaddr=SQL_NTS;
ret=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&serverhenv);
ret=SQLSetEnvAttr(serverhenv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0);
ret=SQLAllocHandle(SQL_HANDLE_DBC,serverhenv,&serverhdbc);
ret=SQLConnect(serverhdbc,(SQLCHAR*)"wt",SQL_NTS,(SQLCHAR*)"sa",SQL_NTS,(SQLCHAR*)"sa",SQL_NTS);
if(!
SQL_SUCCEEDED(ret))//连接失败时返回错误值
return-1;
ret=SQLAllocHandle(SQL_HANDLE_STMT,serverhdbc,&serverhstmt);
ret=SQLPrepare(serverhstmt,(SQLCHAR*)"updatecustomer\
setname=?
addr=?
\
wherecno=?
",SQL_NTS);
if(ret==SQL_SUCCESS||ret==SQL_SUCCESS_WITH_INFO)
{
ret=SQLBindParameter(serverhstmt,1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,10,0,cname,0,&cbname);
ret=SQLBindParameter(serverhstmt,2,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,15,0,caddr,0,&cbaddr);
ret=SQLBindParameter(serverhstmt,3,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,10,0,ccno,0,&cbcno);
cout<<"输入修改值:
";
cin>>cname>>caddr;
cout<<"输入修改要求:
";
cin>>ccno;
}
ret=SQLExecute(serverhstmt);
SQLFreeHandle(SQL_HANDLE_STMT,serverhstmt);
ret=SQLAllocHandle(SQL_HANDLE_STMT,serverhdbc,&serverhstmt);
ret=SQLSetStmtAttr(serverhstmt,SQL_ATTR_ROW_BIND_TYPE,(SQLPOINTER)SQL_BIND_BY_COLUMN,SQL_IS_INTEGER);
ret=SQLExecDirect(serverhstmt,(SQLCHAR*)"select*fromcustomer",SQL_NTS);
if(ret==SQL_SUCCESS||ret==SQL_SUCCESS_WITH_INFO)
{
ret=SQLBindCol(serverhstmt,1,SQL_C_CHAR,ccno,10,&cbcno);
ret=SQLBindCol(serverhstmt,2,SQL_C_CHAR,cname,10,&cbname);
ret=SQLBindCol(serverhstmt,3,SQL_C_CHAR,caddr,15,&cbaddr);
}
while((ret=SQLFetch(serverhstmt))!
=SQL_NO_DATA_FOUND)
{
if(ret==SQL_ERROR)
cout<<"Fetcherror\n";
else
cout<}
SQLFreeHandle(SQL_HANDLE_STMT,serverhstmt);
SQLDisconnect(serverhdbc);
SQLFreeHandle(SQL_HANDLE_DBC,serverhdbc);
SQLFreeHandle(SQL_HANDLE_ENV,serverhenv);
}
intorder:
:
remove1()
{
SQLHENVserverhenv;//环境句柄
SQLHDBCserverhdbc;//连接句柄
SQLHSTMTserverhstmt;//语句句柄
SQLRETURNret;
SQLCHARccno[cno_len],cname[name_len],caddr[addr_len];
SQLINTEGERcbcno=SQL_NTS,cbname=SQL_NTS,cbaddr=SQL_NTS;
ret=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&serverhenv);
ret=SQLSetEnvAttr(serverhenv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0);
ret=SQLAllocHandle(SQL_HANDLE_DBC,serverhenv,&serverhdbc);
ret=SQLConnect(serverhdbc,(SQLCHAR*)"wt",SQL_NTS,(SQLCHAR*)"sa",SQL_NTS,(SQLCHAR*)"sa",SQL_NTS);
if(!
SQL_SUCCEEDED(ret))//连接失败时返回错误值
return-1;
ret=SQLAllocHandle(SQL_HANDLE_STMT,serverhdbc,&serverhstmt);
ret=SQLPrepare(serverhstmt,(SQLCHAR*)"deletefromcustomer\
wherecno=?
",SQL_NTS);
if(!
(ret==SQL_SUCCESS||ret==SQL_SUCCESS_WITH_INFO))
{
SQLFreeHandle(SQL_HANDLE_STMT,serverhstmt);
returnret;
}
ret=SQLBindParameter(serverhstmt,1,SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,10,0,ccno,0,&cbcno);
cout<<"输入删除要求:
cno=";
cin>>ccno;
ret=SQLExecute(serverhstmt);
SQLFreeHandle(SQL_HANDLE_STMT,serverhstmt);
ret=SQLAllocHandle(SQL_HANDLE_STMT,serverhdbc,&serverhstmt);
ret=SQLSetStmtAttr(serverhstmt,SQL_ATTR_ROW_BIND_TYPE,(SQLPOINTER)SQL_BIND_BY_COLUMN,SQL_IS_INTEGER);
ret=SQLExecDirect