(基本上都在改别人的代码,时间太紧了。。。)
四次实验,搞完收工。。。
#include<bits/stdc++.h>
#include<conio.h>
using namespace std;
const int N = 1001;
int main()
{
int space ;//功能部件数
int inum ;//需要流水处理的指令数目
int length ;//存储不同时间段各个功能部件内指令值
char map[N][N];//时空图
cout << "-------Demo the pipline for float point add.------" << endl;
cout << "-------Here, we display all the content in the pipeline------" << endl;
cout << "请输入功能部件数目: " << endl;
cin >> space;
cout << "请输入需要流水处理的指令数目" << endl;
cin >> inum;
int cnt = 1;
char flag = 'n';
length = inum + space -1;
for(int i=0; i<space; i++)
{
int k;
for(k=0; k<(space-i-1)*3; k++)
map[i][k] = ' ';
--k;
for(int j=0; j<inum; j++)
{
map[i][++k+j] = (char)(65+i);
map[i][++k+j] = j+1+'0';
}
}
while(cnt <=length && flag == 'n')
{
//output the cnt-th slice
for(int i=0; i<space; i++)
{
for(int j=0; j<cnt*3; j++)
cout << map[i][j];
cout << endl;
}
cout << "Input next time slices"<<cnt<< "(y/n)" << endl; //如果想要看到完整的流水线过程请一直按’n‘
cnt++;
cin >> flag;
cout << endl;
}
cout << "The task has been finished" << endl;
cout << endl;
cout << "The Though Put of the pipeline is " << inum/(length*1.0) << "Δt" << endl;
cout << "The Speedup is of the pipeline is " << (inum*space)/(length*1.0) << endl;
cout << "The Efficiency of the pipeline is " << (inum*space)/(space*length*1.0) << endl;
getch();
return 0;
}
-------Demo the pipline for float point add.------
-------Here, we display all the content in the pipeline------
请输入功能部件数目:
4
请输入需要流水处理的指令数目
5
D1
Input next time slices1(y/n)
n
C1
D1 D2
Input next time slices2(y/n)
n
B1
C1 C2
D1 D2 D3
Input next time slices3(y/n)
n
A1
B1 B2
C1 C2 C3
D1 D2 D3 D4
Input next time slices4(y/n)
n
A1 A2
B1 B2 B3
C1 C2 C3 C4
D1 D2 D3 D4 D5
Input next time slices5(y/n)
n
A1 A2 A3
B1 B2 B3 B4
C1 C2 C3 C4 C5
D1 D2 D3 D4 D5
Input next time slices6(y/n)
n
A1 A2 A3 A4
B1 B2 B3 B4 B5
C1 C2 C3 C4 C5
D1 D2 D3 D4 D5
Input next time slices7(y/n)
n
A1 A2 A3 A4 A5
B1 B2 B3 B4 B5
C1 C2 C3 C4 C5
D1 D2 D3 D4 D5
Input next time slices8(y/n)
n
The task has been finished
The Though Put of the pipeline is 0.625Δt
The Speedup is of the pipeline is 2.5
The Efficiency of the pipeline is 0.625
#python 3.9运行成功
def fun():
Q = x # 通过队列进行处理
while len(Q) > 1:
Q.sort(key=lambda xx: xx.prob) # 根据使用频度排序
# for i in Q:
# print i.prob
l = Q.pop(0)
r = Q.pop(0)
if l.name is not None:
newQ.append(l)
if r.name is not None:
newQ.append(r)
# 左1右0左小右大
l.codeword = '0'
r.codeword = '1'
fa = hafnode(prob=l.prob + r.prob)
fa.l = l
fa.r = r
l.fa = fa
r.fa = fa
Q.append(fa)
Q[0].fa = None
return Q[0]
def hufdeal():
root = fun()
pro = []
codes = [''] * len(newQ)
symbol = []
for i in range(len(newQ)):
tmp = newQ[i]
while tmp.fa != None:
codes[i] = tmp.codeword + codes[i]
tmp = tmp.fa
newQ[i].codeword = codes[i]
class hafnode():
def __init__(self, name=None, prob=None, codeword='', procode=None):
self.name = name
self.prob = prob
self.codeword = codeword
self.procode = procode
self.fa = None
self.l = None
self.codeword = None
# ----------------------------------哈夫曼编码-----------------------------------------------------
class dengchang():
def __init__(self, name=None, prob=None, codeword=''):
self.name = name
self.prob = prob
self.codewode = codeword
def dcdeal():
n = 0
for i in range(1, 10):
if 2 ** i >= len(y):
n = i
break
for i in range(len(y)):
y[i].codeword = str(bin(i))[2:].zfill(n)
print (y[i].codeword)
# ------------------------------------等长编码-----------------------------------
def extend(n):
# x = input("请输入需要几种编码长度?")
# len = []
# for i in range(0,10):
# len[i] = (2**i)-1
m = int(input("输入编码的最短长度:"))
M = int(input("输入编码的最长长度:"))
print('扩展编码的结果是:\n')
z.sort(key=lambda xx: xx.prob, reverse=True)
cnt = 0
for i in range(len(z)):
if cnt == 7:
break
if i <= len(z) - 2:
tmp = float(z[i].prob) / float(z[i + 1].prob)
if int(tmp * 1000000 + M) >= int(m * 1000000):
break
cnt += 1
cnt += 1
for i in range(cnt):
z[i].codeword = str(bin(i))[2:].zfill(m)
print (z[i].codeword)
for i in range(cnt, len(z)):
z[i].codeword = str(bin(i - cnt))[2:].rjust(m, '0').rjust(M, '1')
print (z[i].codeword)
x = []
y = []
z = []
newQ = [] # 保存使用过的节点
def main():
n = int(input("请输入个数:"))
for i in range(n):
rate = input("请输入第%d的频率:" % i)
x.append(hafnode(i, rate, ''))
y.append(dengchang(i, rate))
z.append(dengchang(i, rate))
hufdeal()
print ('哈夫曼编码:\n')
for i in newQ:
print (i.name, i.codeword)
print ('等长编码:\n')
dcdeal()
print('扩展编码:\n')
extend(n)
len1 = 0
len2 = 0
len3 = 0
for i in range(len(z)):
len1 += len(newQ[i].codeword)
len2 += len(y[i].codeword)
len3 += len(z[i].codeword)
len1 = float(1.0 * len1 / n)
len2 = float(1.0 * len2 / n)
len3 = float(1.0 * len3 / n)
print ("哈夫曼编码平均码长为%.2f,等长编码平均码长为%.2f,扩展编码平均码长为%.2f" % (len1, len2, len3))
if __name__ == '__main__':
main();
# 10个值:0.30 0.20 0.16 0.09 0.08 0.07 0.04 0.03 0.02 0.01
运行结果:
请输入个数:10
请输入第0的频率:0.3
请输入第1的频率:0.2
请输入第2的频率:0.16
请输入第3的频率:0.09
请输入第4的频率:0.08
请输入第5的频率:0.07
请输入第6的频率:0.04
请输入第7的频率:0.03
请输入第8的频率:0.02
请输入第9的频率:0.01
哈夫曼编码:
9 000000000
8 000000001
7 00000001
6 0000001
5 000001
4 00001
3 0001
2 001
1 01
0 1
等长编码:
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
扩展编码:
输入编码的最短长度:2
输入编码的最长长度:4
扩展编码的结果是:
00
01
10
11
100
101
110
111
1100
1101
哈夫曼编码平均码长为5.40,等长编码平均码长为4.00,扩展编码平均码长为2.80
Process finished with exit code 0
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
public class LRU {
private static int N=5; //页面的最长长度
Object[] Seq = new Object[N]; //当前页面序列
private int size;
Vector<Object> Replace = new Vector<Object>(); //置换序列
public LRU() {}
public boolean isOverStack() //序列是否溢出
{
if(size>=N) return true;
else return false;
}
public int IndexofEle(Object o) //元素o的位置
{
for (int i=0; i<N; i++) {
if (o == Seq[i])
return i;
}
return -1;
}
public Object Operate(Object o) //元素o申请序列位置,并移除相应元素
{
int t=-1;
if(!isOverStack() && IndexofEle(o)==-1) //序列为满且元素o不在序列中
{
Seq[size]=o;
size++;
}
else if(isOverStack() && IndexofEle(o)==-1) //序列已满但元素o不在序列中
{
//此时需要进行置换
Replace.add(Seq[0]);
for(int i=0; i<size-1; i++) Seq[i]=Seq[i+1];
Seq[size-1]=o;
}
else
{
t=IndexofEle(o);
for(int i=t; i<size-1; i++) Seq[i]=Seq[i+1];
Seq[size-1]=o;
}
if(t==-1) return null;
else {
return Seq[t];
}
}
public void show()
{
System.out.println("页面序列:");
for (int i=0; i<size; i++)
{
System.out.print(Seq[i]+"\t");
}
}
public static void main(String[] args)
{
Integer iter[] ={4,7,0,7,1,0,1,2,1,2,6,5,8,7,4,3,1,9};
LRU lru=new LRU();
for(int i=0; i<iter.length; i++) {
lru.Operate(iter[i]);
lru.show();
System.out.println();
}
System.out.println("置换的页面序列");
for (int i=0; i<lru.Replace.size(); i++)
{
System.out.print(lru.Replace.get(i)+"\t");
}
System.out.println();
System.out.println("共置换了"+lru.Replace.size()+"次");
}
}
输出结果
页面序列:
4
页面序列:
4 7
页面序列:
4 7 0
页面序列:
4 0 7
页面序列:
4 0 7 1
页面序列:
4 7 1 0
页面序列:
4 7 0 1
页面序列:
4 7 0 1 2
页面序列:
4 7 0 2 1
页面序列:
4 7 0 1 2
页面序列:
7 0 1 2 6
页面序列:
0 1 2 6 5
页面序列:
1 2 6 5 8
页面序列:
2 6 5 8 7
页面序列:
6 5 8 7 4
页面序列:
5 8 7 4 3
页面序列:
8 7 4 3 1
页面序列:
7 4 3 1 9
置换的页面序列
4 7 0 1 2 6 5 8
共置换了8次
#include <bits/stdc++.h>
#include <vector>
#define MAX_CAP 30
using namespace std;
struct Device //设备定义
{
int actCap; //设备容量
string data; //内容
};
struct ChannelMannager //通道处理机
{
int interrupt;
};
enum state{NONE,INIT,FINISH};
vector <Device> vd(3); //设备数组
int MaxCap(vector<string> str) //寻找最大容量
{
int res=0;
for (int i=0; i<str.size(); i++)
if(str[i].size()>=res) res=str[i].size();
return res;
}
void delay()
{
int i;
int r=rand()%500+500;
for (i=0; i<r; i++);
}
void run(ChannelMannager cm) //CPU不同的中断算法
{
while(true)
{
if(cm.interrupt==NONE)
{
cout<<"The cpu is doing some thing..."<<endl;
cout<<"The cpu is doing some thing..."<<endl;
break;
}
if(cm.interrupt==INIT)
{
cout<<"CPU is interrupted"<<endl;
cout<<"This is a I/0 Init instruction,The channalManager is init the device..."<<endl;
break;
}
if(cm.interrupt==FINISH)
{
cout<<"CPU is interrupted"<<endl;
cout<<"This is a I/0 Finish instruction,The channalManager is close the device..."<<endl;
break;
}
}
}
void MemoryToDevice(vector<string> memo, int MaxLen, vector<Device> a, ChannelMannager cm)
{
cm.interrupt=INIT; //通道处理机初始化
run(cm);
for (int i=0; i<MaxLen; i++)
{
for (int j=0; j<a.size(); j++)
{
if(i<memo[j].size() && i<a[j].actCap) a[j].data+=memo[j][i]; //当前位置不大于设备容量且不大于内容长度时,传输给设备
for (int k=0; k<a.size(); k++) //打印过程
{
cout<<"Device"<<k<<" Content: "<<a[k].data<<endl;
}
cout<<endl;
}
}
cm.interrupt=FINISH; //通道处理机状态结束
run(cm);
}
int main()
{
cout<<"------------Demo the work flow of channel----------- "<<endl;
vector<string> memory(3);
memory[0]="Computer";
memory[1]="Structure";
memory[2]="Channel";
ChannelMannager cm;
cm.interrupt=NONE; //通道处理机状态为空
run(cm);
for (int i=0; i<vd.size(); i++)
{
vd[i].data=""; //设备内容初始化为空
vd[i].actCap=(rand()%10+50);
}
int MaxDevCap= MaxCap(memory);
delay();
MemoryToDevice(memory,MaxDevCap,vd,cm);
return 0;
}
------------Demo the work flow of channel-----------
The cpu is doing some thing...
The cpu is doing some thing...
CPU is interrupted
This is a I/0 Init instruction,The channalManager is init the device...
Device0 Content: C
Device1 Content:
Device2 Content:
Device0 Content: C
Device1 Content: S
Device2 Content:
Device0 Content: C
Device1 Content: S
Device2 Content: C
Device0 Content: Co
Device1 Content: S
Device2 Content: C
Device0 Content: Co
Device1 Content: St
Device2 Content: C
Device0 Content: Co
Device1 Content: St
Device2 Content: Ch
Device0 Content: Com
Device1 Content: St
Device2 Content: Ch
Device0 Content: Com
Device1 Content: Str
Device2 Content: Ch
Device0 Content: Com
Device1 Content: Str
Device2 Content: Cha
Device0 Content: Comp
Device1 Content: Str
Device2 Content: Cha
Device0 Content: Comp
Device1 Content: Stru
Device2 Content: Cha
Device0 Content: Comp
Device1 Content: Stru
Device2 Content: Chan
Device0 Content: Compu
Device1 Content: Stru
Device2 Content: Chan
Device0 Content: Compu
Device1 Content: Struc
Device2 Content: Chan
Device0 Content: Compu
Device1 Content: Struc
Device2 Content: Chann
Device0 Content: Comput
Device1 Content: Struc
Device2 Content: Chann
Device0 Content: Comput
Device1 Content: Struct
Device2 Content: Chann
Device0 Content: Comput
Device1 Content: Struct
Device2 Content: Channe
Device0 Content: Compute
Device1 Content: Struct
Device2 Content: Channe
Device0 Content: Compute
Device1 Content: Structu
Device2 Content: Channe
Device0 Content: Compute
Device1 Content: Structu
Device2 Content: Channel
Device0 Content: Computer
Device1 Content: Structu
Device2 Content: Channel
Device0 Content: Computer
Device1 Content: Structur
Device2 Content: Channel
Device0 Content: Computer
Device1 Content: Structur
Device2 Content: Channel
Device0 Content: Computer
Device1 Content: Structur
Device2 Content: Channel
Device0 Content: Computer
Device1 Content: Structure
Device2 Content: Channel
Device0 Content: Computer
Device1 Content: Structure
Device2 Content: Channel
CPU is interrupted
This is a I/0 Finish instruction,The channalManager is close the device...
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.