计算机体系结构实验报告Word文档下载推荐.docx

上传人:b****3 文档编号:18092517 上传时间:2022-12-13 格式:DOCX 页数:14 大小:65.38KB
下载 相关 举报
计算机体系结构实验报告Word文档下载推荐.docx_第1页
第1页 / 共14页
计算机体系结构实验报告Word文档下载推荐.docx_第2页
第2页 / 共14页
计算机体系结构实验报告Word文档下载推荐.docx_第3页
第3页 / 共14页
计算机体系结构实验报告Word文档下载推荐.docx_第4页
第4页 / 共14页
计算机体系结构实验报告Word文档下载推荐.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

计算机体系结构实验报告Word文档下载推荐.docx

《计算机体系结构实验报告Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《计算机体系结构实验报告Word文档下载推荐.docx(14页珍藏版)》请在冰豆网上搜索。

计算机体系结构实验报告Word文档下载推荐.docx

果然,学习任何一门课程,只要学得用心,都可以从中体会到学习的快乐。

今后我的进步,想必都是从这一点一点敲入编译器的代码中获得的。

5.代码附录

实验1

#include<

iostream>

math.h>

usingnamespacestd;

#defineN8

classhuff_p{

public:

huff_p*r_child;

//大概率的节点;

huff_p*l_child;

//小概率的节点;

charop_mask[3];

//指令标号;

floatp;

//指令使用概率;

};

classf_min_p{

f_min_p*next;

huff_p*huf_p;

classhuff_code{

huff_code*next;

charcode[N];

//huffman编码;

f_min_p*input_instruct_set();

//输入指令集子模块;

huff_p*creat_huffman_tree(f_min_p*head);

//构造huffman树;

f_min_p*fin_min(f_min_p*h);

f_min_p*del_min(f_min_p*h,f_min_p*p);

voidinsert_n(f_min_p*h,f_min_p*p);

huff_p*creat_huffp(f_min_p*p);

voidcreat_huffman_code(huff_p*h1,huff_code*h);

//生成huffman编码;

voidr_find(huff_p*p1,charcode[],inti,huff_code*h);

voidoutput_huffman(huff_code*head);

//输出huffman编码;

voidcal_sort_length(huff_code*head);

//计算指令用huffman编码的平均编码字长

intmain(){

f_min_p*h,*h1;

huff_p*root;

huff_code*head,*pl;

inti=0;

h=input_instruct_set();

h1=h;

root=creat_huffman_tree(h1);

head=newhuff_code;

head->

next=NULL;

creat_huffman_code(root,head);

output_huffman(head);

cal_sort_length(head);

pl=head->

next;

while(pl){

deletehead;

head=pl;

pl=pl->

}}

f_min_p*input_instruct_set(){

f_min_p*head;

f_min_p*h;

h=newf_min_p;

h->

huf_p=NULL;

head=h;

intn;

cout<

<

"

请输入指令数:

;

cin>

>

n;

请输入指令标号:

op_mask;

请输入指令的使用概率:

p;

f_min_p*point;

f_min_p*p1=head;

for(;

i<

n-1;

i++){

point=newf_min_p;

point->

op_mask[2]='

\0'

next=p1->

p1->

next=point;

p1=point;

}

returnhead;

huff_p*creat_huffman_tree(f_min_p*h){

f_min_p*h1,*min1,*min2,*comb;

huff_p*head,*rd,*ld,*parent;

min1=fin_min(h1);

ld=creat_huffp(min1);

h1=del_min(h1,min1);

if(h1->

next)

min2=fin_min(h1);

else

min2=h1;

rd=creat_huffp(min2);

comb=newf_min_p;

comb->

p=rd->

p+ld->

op_mask[0]='

op_mask[1]='

parnt=creat_huffp(comb);

insert_n(h1,comb);

next!

=NULL)

h1=del_min(h1,min2);

parent->

l_child=ld;

r_child=rd;

huf_p=parent;

head=parent;

while(h1->

=NULL){

if(min1->

huf_p==NULL){

else{

ld=min1->

huf_p;

if(min2->

rd=min2->

parent=creat_huffp(comb);

if(h1!

next==NULL)break;

deletecomb;

f_min_p*fin_min(f_min_p*h){

f_min_p*h1,*p1;

p1=h1;

floatmin=h1->

h1=h1->

while(h1){

if(min>

(h1->

p)){

min=h1->

returnp1;

f_min_p*del_min(f_min_p*h,f_min_p*p){

f_min_p*p1,*p2;

p1=h;

p2=h;

if(h==p){

h=h->

deletep;

while(p1->

p1=p1->

if(p1==p){

p2->

break;

p2=p1;

returnh;

voidinsert_n(f_min_p*h,f_min_p*p1){

next=h->

next=p1;

huff_p*creat_huffp(f_min_p*d){

huff_p*p1;

p1=newhuff_p;

l_child=NULL;

r_child=NULL;

p=d->

op_mask[0]=d->

op_mask[0];

op_mask[1]=d->

op_mask[1];

voidr_find(huff_p*p1,charcode[],inti,huff_code*h){

if(p1->

l_child){

code[i]='

1'

r_find(p1->

l_child,code,i+1,h);

op_mask[0]!

='

){

huff_code*p2=newhuff_code;

op_mask[0]=p1->

op_mask[1]=p1->

p=p1->

intj;

for(j=0;

j<

i;

j++){

code[j]=code[j];

code[j]='

next=p2;

r_child){

0'

r_child,code,i+1,h);

deletep1;

voidcreat_huffman_code(huff_p*h1,huff_code*h){

r_find(h1,code,i,h);

voidoutput_huffman(huff_code*head){

huff_code*h=head->

endl;

标号:

--概率--"

'

'

--编码--"

---------------------------------"

while(h){

op_mask<

:

"

p<

code<

voidcal_sort_length(huff_code*head){

huff_code*h=head->

doublej=0;

floatone_length=0;

floatper_length=0;

floatext_length=0;

//按1-2-3-5扩展编码的最小长度为。

floatlength=0;

while(h->

code[i]!

length++;

i++;

one_length=h->

p*length;

per_length=per_length+one_length;

//12

h=h->

j++;

inti1=int(j);

huff_code*p2=head->

float*p_a=newfloat[i1];

inti0=0;

while(p2){

p_a[i0++]=p2->

p2=p2->

floatmax,temp;

intl;

for(ints=0;

s<

i1;

s++){

max=p_a[s];

l=s;

for(intk=s+1;

k<

k++){

if(max<

p_a[k]){

max=p_a[k];

l=k;

temp=p_a[s];

p_a[s]=max;

p_a[l]=temp;

float*code_len=newfloat[i1];

code_len[0]=1;

code_len[1]=2;

code_len[2]=3;

code_len[3]=5;

for(inti=4;

j;

i++)code_len[i]=5;

while(l<

i1){

ext_length=ext_length+code_len[l]*p_a[l];

doubleq_length=log10(j)/log10

(2);

此指令集操作码huffman编码的平均长度为"

per_length<

等长编码的平均长度为"

q_length<

按1-2-3-5的扩展编码的最短平均编码长度为"

ext_length;

实验2

stdio.h>

conio.h>

#defineM4

#defineN12

#defineMyprintfprintf("

|---+---+---+---+---+---+---+---+---+---+---+---|\n"

typedefstructpage

{intnum;

/*记录页面号*/

inttime;

/*记录调入内存时间*/

}Page;

/*页面逻辑结构,结构为方便算法实现设计*/

Pageb[M];

/*内存单元数*/

intc[M][N];

/*暂保存内存当前的状态:

缓冲区*/

intqueue[100];

/*记录调入队列*/

intK;

/*调入队列计数变量*/

voidInit(Page*b,intc[M][N])

{inti,j;

for(i=0;

N;

b[i].num=-1;

b[i].time=N-i-1;

M;

i++)

for(j=0;

j++)

c[i][j]=-1;

intGetMax(Page*b)

{inti;

intmax=-1;

inttag=0;

{if(b[i].time>

max){

max=b[i].time;

tag=i;

}}

returntag;

intEquation(intfold,Page*b){inti;

{if(fold==b[i].num)

returni;

return-1;

voidLru(intfold,Page*b)

intval;

val=Equation(fold,b);

if(val>

=0)

{b[val].time=0;

if(i!

=val)

b[i].time++;

else

{queue[++K]=fold;

/*记录调入页面*/

val=GetMax(b);

b[val].num=fold;

b[val].time=0;

inta[N]={1,1,2,4,3,5,2,1,6,7,1,3};

inti,j;

start:

K=-1;

Init(b,c);

Lru(a[i],b);

c[0][i]=a[i];

c[j][i]=b[j].num;

printf("

内存状态为:

\n"

);

%2d"

a[j]);

{for(j=0;

if(c[i][j]==-1)printf("

%2c"

32);

elseprintf("

c[i][j]);

printf("

\n调入队列为:

K+1;

%3d"

queue[i]);

\n缺页次数为:

%6d\n缺页率:

%16.6f\n"

K+1,(float)(K+1)/N);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育 > 其它课程

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1