LRU c ++程序

时间:2015-12-03 02:20:00

标签: c++ lru

我在我的一个大学课程中一直在做一个项目。我一直在执行我的LRU代码时遇到问题,因为它没有显示任何错误或任何错误,而是编译。有两个部分。我们输入值的主要内容,然后我们指定要用于查找页面错误的算法。我知道主要的工作,以及FIFO算法,但我没有得到任何东西与我的LRU代码(它编译和"运行"但没有显示,好像我没有点击使用算法)。任何人都可以帮我弄清楚出了什么问题吗?

的main.cpp

    #include <iostream>
#include <string>

//#include "fifo.cpp"
#include "lru.cpp"
//#include "optimal.cpp"

using namespace std;

int main() {

    // List of different variables
    string pagestring;
    int fs,pn[50], n;

    // Prompt for page references
    cout<<"Virtual Memory Simulation\nBy blah\n----------\nEnter the number of pages : " << endl;
        cin >> n;
    cout<<"\n-------------\nPlease enter a list of page numbers separated by commas.\n"<< endl;
        cin>>pagestring;



    // algorithm to use
    char algo;

    while (true) {
        // Prompt algorithm to use
        cout<<"----------\nPlease select an algorithm to use.\n\n1: First-In-First-Out (FIFO)\n2: Least-Recently-Used (LRU)\n3: Optimal\n0: Quit\n"<<endl;
        cin>>algo;

        if (algo == '1') {
            //fifo(pagestring);
        }
        else if (algo == '2'){
            LRU_Execute(pagestring, n);
        }
        else if (algo == '3'){
            cout<<"Optimal Not yet coded"<<endl;
        }
        else if (algo == '0'){
            break;
        }
        else {
            cout<<"Invalid choice. Please try again."<<endl;
        }

    }

    cout<<"Goodbye!!"<<endl;


};

LRU.cpp

#include <iostream>
#include <string>

using namespace std;

class pra
{
int fs,z;
 int  frame[50], frame1[50][2], pn[50], n, cnt, p, x;
public:
 pra();
 void init(string pagestring);
 void getdata(string pagestring, int n);
 void lru(int* pn, int n, string pagestring);

};


pra::pra()
{
 int i;

 for (i = 0; i < fs; i++)
 {
  frame[i] = -1;
 }
 for (i = 0; i < fs; i++)
 {
  frame1[i][0] = -1;
  frame1[i][1] = 0;
 }
 p = 0;
 cnt = 0;
}

void pra::init(string pagestring)
{
 int i;
 for (i = 0; i < fs; i++)
 {
  frame[i] = -1;
 }
 for (i = 0; i < fs; i++)
 {
  frame1[i][0] = -1;
  frame1[i][1] = 0;
 }
 p = 0;
 cnt = 0;
}

void pra::getdata(string pagestring, int n)
{
fs=3;


// index to loop through input string
    int i = 0;

    // current input string character
    char z = pagestring[i];
    int x = 0;

//cout << "\nEnter the page numbers : ";
while (z != '\0'){
        // skip over commas and spaces
        if (!(z == ',')) {
              pn[x] = z;
        x++;

//  cout<<pn[x]<<"-This is pn[x]\n";    
}
    z = pagestring[++i];


}
//cout<<pn[x]<<"-This is pn[x] AGAIN\n";
this->lru(pn, n, pagestring);
}

void pra::lru(int* pn, int n, string pagestring)
{
 init(pagestring);
 int ind = 0, fault = 0, pi = 0, j, fn;
 char i, z;
 p = 0;
 cnt = 0;
 int min;
cout<<n<<"---"<<i<<" - "<<j<<" - "<<" - "<<fn<<" - "<<z;
 for (i = 0; i < fs; i++)
 {
  frame1[i][0] = -1;
  frame1[i][1] = 0;
 }
pi = 0;
 for (i = 0; i < n; i++)

 {
  j = 0;
  if (ind > fs - 1)
   ind = 0;
  fault = 1;
  min = 999;

  while (j < fs)
  {
   if (frame1[j][0] = pn[pi])
   {
    fault = 0;
    p++;
    frame1[j][1] = p;

    goto l2;
   }
   if (frame1[j][1] < min)
   {
    min = frame1[j][1];
    fn = j;
   }
   j++;
  }
  j = 0;
  while (j < fs)
  {
   if (frame1[j][0] = -1)
   {
    fault = 1;
    fn = j;
    goto l2;
   }
   j++;
  }
  ind++;
l2:
  if (fault == 1)
  {
   p++;
   frame1[fn][0] = pn[pi];
   frame1[fn][1] = p;
   cnt++;
  }
  cout << "\nElement: " << pn[pi];
  pi++;
  for (z = 0; z < fs; z++)
  {
   cout << "\t" << frame1[z][0];
  }
  if (fault == 1)
   cout << "\t**Page Fault**";
  else
   cout << "\t--No Page Fault--";
 }
 cout << "\nTotal number of page faults: " << cnt;
 cout << "\n";
}


void LRU_Execute(string pagestring, int n)
{
 pra p;
 int  j, fault = 0, i, pi, z, fn, ind = 0, ans, ch;
 p.getdata(pagestring, n);
 //p.lru();
 while (ans == 1);
 //return 1;
}

0 个答案:

没有答案