查看完整版本: [-- 请教这段代码 --]

武当休闲山庄 -> 电脑求助 -> 请教这段代码 [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

scode 2009-01-07 11:37
请问这段代码的意思:
#include<iostream.h>
#define  M  40
int N;
struct Pro
{
    int num,time;
};
int Input(int m,Pro p[M])
{  
    cout<<"请输入实际页数:";
    do
    {
        cin>>m;
        if(m>M)cout<<"数目太多,请重试"<<endl;
        else break;
    }while(1);
    cout<<endl<<"请输入各页面号"<<endl;
       for(int i=0;i<m;i++)
    {
        cin>>p.num;
        p.time=0;
    }
    return m;
}

void print(Pro *page1)//打印当前的页面
{
    Pro *page=new Pro[N];
    page=page1;
    for(int i=0;i<N;i++)cout<<page.num<<"  ";
    cout<<endl;
}


int Search(int e,Pro *page1)
{   Pro *page=new Pro[N];
    page=page1;
    for(int i=0;i<N;i++)if(e==page.num)return i;
    return -1;
}

int Max(Pro *page1)                                            
{
    Pro *page=new Pro[N];
    page=page1;
    int e=page[0].time,i=0;
    while(i<N)               //找出离现在时间最长的页面
    {
        if(e<page.time)e=page.time;
        i++;
    }
    for( i=0;i<N;i++)if(e==page.time)return i;
    
    return -1;
}

int Compfu(Pro *page1,int i,int t,Pro p[M])
{
    Pro *page=new Pro[N];
    page=page1;
    
    int count=0;
    for(int j=i;j<M;j++)
    {
        if(page[t].num==p[j].num )break;
        else count++;
    }
    return count;
    
}

int main()
{
    
    cout<<"可用内存页面数"<<endl;
    cin>>N;    
    Pro p[M];
    Pro *page=new Pro[N];
    char c;
    int m=0,t=0;
    float n=0;
    m=Input(m,p);    
    
    do{
        
        for(int i=0;i<N;i++)//初试化页面基本情况
        {
            page.num=0;
            page.time=2-i;
        }
        int k=1,j=0,count=1;
        page[0].num=p[0].num;
           i=1;
        while(i<N)
        {
        int flag=1;
        for(j=0;j<i;j++)
        if(p[k].num==page.num)
        {n++;flag=0;break;}
        if(flag==1){page.num=p[k].num;i++;}
        count++;k++;
    }
        i=0;        
        cout<<"f:FIFO页面置换"<<endl;
        cout<<"l:LRU页面置换"<<endl;
        cout<<"o:OPT页面置换"<<endl;
        cout<<"按其它键结束"<<endl;
        cin>>c;
        
        if(c=='f')//FIFO页面置换
        {
            n=0;
            cout<<"页面置换情况:   "<<endl;
            while(i<m)
            {
                if(Search(p.num,page)>=0)i++;//找到相同的页面
                else
                {  
                    if(t==N)t=0;
                    else
                    {
                        n++;//
                        page[t].num=p.num;
                        print(page);
                        t++;
                    }
                }
            }
            cout<<"缺页次数:"<<n<<"    缺页率:"<<n/m<<endl;      
            
        }
        if(c=='l')//LRU页面置换
        {    n=0;
        cout<<"页面置换情况:   "<<endl;
        while(i<m)
        {    
            int k;
            k=t=Search(p.num,page);
            if(t>=0)
                
                page[t].time=0;
            
            
            else
            {  
                n++;
                t=Max(page);
                
                page[t].num=p.num;
                page[t].time=0;
            }
            if(t==0){page[t+1].time++;page[t+2].time++;}
            if(t==1){page[2].time++;page[0].time++;}
            if(t==2){page[1].time++;page[0].time++;}
            if(k==-1)   print(page);                
            i++;
        }
        cout<<"缺页次数:"<<n<<"    缺页率:"<<n/m<<endl;
        }
        if(c=='o')//OPT页面置换
        {
            n=0;
            while(i<m)
            {
                if(Search(p.num,page)>=0)i++;
                else
                {
                    int temp=0,cn;
                    for(t=0;t<N;t++)
                    {
                        if(temp<Compfu(page,i,t,p))
                        {
                            temp=Compfu(page,i,t,p);
                            cn=t;
                        }
                    }
                    page[cn]=p;
                    n++;
                    print(page);
                    i++;
                }
            }
            cout<<"缺页次数:"<<n<<"    缺页率:"<<n/m<<endl;
        }
        
    }while(c=='f'||c=='l'||c=='o');
    
    
    
    return 0;
}
谢谢!

gonghugen 2009-01-07 23:37
引用楼主scode于2009-01-07 11:37发表的 请教这段代码 :
请问这段代码的意思:
#include<iostream.h>
#define  M  40
int N;
struct Pro
.......

楼主说的不对,这个不是代码的问题;这是一个关于页面选择操作的程序。

平顶山 2009-01-08 08:56
太高深了,确实搞不清楚啊

jiangxilg 2009-01-09 09:52
用户被禁言,该主题自动屏蔽!


查看完整版本: [-- 请教这段代码 --] [-- top --]


Powered by www.wdsz.net v8.7.1 Code ©2005-2018www.wdsz.net
Gzip enabled


沪ICP备:05041533号