本文介绍了这是由fifo,lru和最佳算法替换页面的程序。在lru函数中,它在第198行199声明错误中给出了3个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 #include < stdio.h > #include < conio.h > void fifo(); void lru(); void optimal(); int nof,nos; char input [] = 70120304230321201701 ; char slot [ 10 ] [ 2 ]; void main() { int ch,i,cont = 0 ; clrscr(); printf( 输入帧数); scanf( %d,& nof); printf( 输入插槽数); scanf( %d,& nos); printf( \ n \ nn输入值); / * for(i = 0; i< nof; i ++)> { scanf(%c,& input [i]); } * / printf( 输入您的选择); printf( \ n1)FIFIO \\\2)Optimal\\\3)LRU \\\4)退出); scanf( %d,& ch); 执行 { 开关(ch) { case 1 :fifo(); break ; / * case 2:optimal(); 休息; * / case 3 :lru(); break ; case 4 :exit( 0 ); } printf( \ n你想继续吗?大于1 / NO大于0:); scanf( %d,& cont); } while (cont == 1 ); getch(); } void fifo() { int i,j,pf = 0 ,loc = 0 ,空= 0 ,found = 0 ; int min; for (j = 0 ; j< nos; j ++)> { slot [j] [ 0 ] = ' \0' ; slot [j] [ 1 ] = 0 + 48; } pf = 0 ; loc = 0 ; for (i = 0 ; i< nof; i ++)> { found = 0 ; empty = 0 ; for (j = 0 ; j< nos; j ++)> { if (input [i] == slot [j] [ 0 ]) { found = 1 ; break ; } } 如果(找到== 0 ) // 页面替换 { for (j = 0 ; j< nos; j ++)> { if (slot [j] [ 0 ] == ' \ 0') { loc = j; empty = 1 ; break ; } } 如果(空== 1 ) { pf ++; slot [loc] [ 0 ] = input [i]; slot [loc] [ 1 ] = i + 48; } else { pf ++; min = slot [ 0 ] [ 1 ] - 48 ; loc = 0 ; for (j = 0 ; j< nos; j ++)> { if (min>(slot [j] [ 1 ] - 48 )) { min = slot [j] [ 1 ] - 48 ; loc = j; } } slot [loc] [ 0 ] = input [i]; slot [loc] [ 1 ] = i + 48; } } printf( \ n); for (j = 0 ; j< nos; j ++)> printf( %c,slot [j] [ 0 ]); } printf( \ n页面错误数%d ,PF); } void lru() { int i,j,pf = 0 ,loc = 0 ,empty = 0 ,found = 0 ; int min; for (j = 0 ; j< nos; j ++)> { slot [j] [ 0 ] = ' \0' ; slot [j] [ 1 ] = 0 + 48; } pf = 0 ; loc = 0 ; for (i = 0 ; i< nof; i ++)> { found = 0 ; empty = 0 ; for (j = 0 ; j< nos; j ++)> { if (input [i] == slot [j] [ 0 ]) { found = 1 ; slot [j] [ 1 ] = i + 48; break ; } } 如果(找到== 0 ) // 页面替换 { for (j = 0 ; j< nos; j ++)> { if (slot [j] [ 0 ] == ' \ 0') { loc = j; empty = 1 ; break ; } } 如果(空== 1 ) { pf ++; slot [loc] [ 0 ] = input [i]; slot [loc] [ 1 ] = i + 48; } else { pf ++; min = slot [ 0 ] [ 1 ] - 48 ; loc = 0 ; for (j = 0 ; j< nos; j ++)> { if (min>(slot [j] [ 1 ] - 48 )) { min = slot [j] [ 1 ] - 48 ; loc = j; } for (i = 0 ; i< nof ;我++)> { found = 0 ; empty = 0 ; for (j = 0 ; j< nos; j ++)> { if (input [i] == slot [j] [ 0 ]) { found = 1 ; slot [j] [ 1 ] = i + 48; break ; } }} slot [loc] [ 0 ] = input [i]; slot [loc] [ 1 ] = i + 48; } } printf( \ n) ; for (j = 0 ; j< nos; j ++)> printf( %c,slot [j] [ 0 ]); } printf( \ n页面错误数%d ,PF); } void 最佳() { int val,f_flag = 0 ,o_index,f_index; for (j = 0 ; j< nos; j ++)> { slot [j] [ 0 ] = ' \0' ; slot [j] [ 1 ] = 0 + 48; // f_flag slot [j] [ 2 ] = 0 + 48; // 旧 slot [j] [ 3 ] = 0 + 48; // future } for (i = 0 ; i< nof; i ++)> { found = 0 ; empty = 0 ; for (j = 0 ; j< nos; j ++)> { if (input [i] == slot [j] [ 0 ]) { found = 1 ; slot [j] [ 1 ] = i + 48; break ; } } 如果(slot [j] [ 1 ] == 1 ) { pf ++; max = slot [ 0 ] [ 1 ] - 48 ; loc = 0 ; for (j = 0 ; j< nos; j ++)> { if (max<(slot [j] [ 1 ] - 48 )) { max = slot [j] [ 1 ] - 48 ; loc = j; } } slot [loc] [ 0 ] = input [i]; slot [loc] [ 1 ] = i + 48; } else pf ++; min = slot [ 0 ] [ 2 ] - 48 ; loc = 0 ; for (j = 0 ; j< nos; j ++)> { if (min>(slot [j] [ 2 ] - 48 )) { min = slot [j] [ 2 ] - 48 ; loc = j; } } slot [loc] [ 0 ] = input [i]; slot [loc] [ 2 ] = i + 48; } } } [由Jochen Arndt编辑:添加代码块] 解决方案 optimal()函数在 lru()函数内定义。这是不允许的。因此,将结束括号从代码末尾移动到 lru()函数的末尾,并将必要的局部变量添加到 optimal() #include<stdio.h>#include<conio.h>void fifo();void lru();void optimal();int nof,nos;char input[]="70120304230321201701";char slot[10][2];void main(){ int ch,i,cont=0 ; clrscr();printf("Enter number of frames"); scanf("%d",&nof); printf("Enter number of slots"); scanf("%d",&nos); printf("\n\nEnter values");/* for(i=0;i<nof;i++)> {scanf("%c",&input[i]); }*/ printf("Enter your choice"); printf("\n1)FIFIO\n2)Optimal\n3)LRU\n4)Exit"); scanf("%d",&ch); do {switch(ch){case 1: fifo();break; /*case 2: optimal();break; */case 3:lru(); break;case 4: exit(0);} printf("\n Do you want to continue Yes->1/No->0:"); scanf("%d",&cont); }while(cont==1); getch();}void fifo(){ int i,j,pf=0,loc=0,empty=0,found=0; int min; for(j=0;j<nos;j++)>{ slot[j][0]='\0'; slot[j][1]=0+48;}pf=0;loc=0;for(i=0;i<nof;i++)>{ found=0; empty=0; for(j=0;j<nos;j++)> {if(input[i]==slot[j][0]){ found=1; break;} } if(found==0)//for page replacement {for(j=0;j<nos;j++)>{ if(slot[j][0]=='\0') {loc=j;empty=1;break; }}if(empty==1){ pf++; slot[loc][0]=input[i]; slot[loc][1]=i+48;}else{ pf++; min=slot[0][1]-48; loc=0; for(j=0;j<nos;j++)> {if(min>(slot[j][1]-48)){ min=slot[j][1]-48; loc=j;} } slot[loc][0]=input[i]; slot[loc][1]=i+48;} } printf("\n"); for(j=0;j<nos;j++)> printf("%c ",slot[j][0]);}printf("\n Number of page faults %d",pf);}void lru(){ int i,j,pf=0,loc=0,empty=0,found=0; int min; for(j=0;j<nos;j++)>{ slot[j][0]='\0'; slot[j][1]=0+48;}pf=0;loc=0;for(i=0;i<nof;i++)>{ found=0; empty=0; for(j=0;j<nos;j++)> {if(input[i]==slot[j][0]){ found=1; slot[j][1]=i+48; break;} } if(found==0)//for page replacement {for(j=0;j<nos;j++)>{ if(slot[j][0]=='\0') {loc=j;empty=1;break; }}if(empty==1){ pf++; slot[loc][0]=input[i]; slot[loc][1]=i+48;}else{ pf++; min=slot[0][1]-48; loc=0; for(j=0;j<nos;j++)> {if(min>(slot[j][1]-48)){ min=slot[j][1]-48; loc=j;} for(i=0;i<nof;i++)>{ found=0; empty=0; for(j=0;j<nos;j++)> {if(input[i]==slot[j][0]){ found=1; slot[j][1]=i+48; break;} }} slot[loc][0]=input[i]; slot[loc][1]=i+48;} } printf("\n"); for(j=0;j<nos;j++)> printf("%c ",slot[j][0]);}printf("\n Number of page faults %d",pf);}void optimal(){ int val,f_flag=0,o_index,f_index; for(j=0;j<nos;j++)> {slot[j][0]='\0';slot[j][1]=0+48; //f_flagslot[j][2]=0+48;//oldslot[j][3]=0+48; //future } for(i=0;i<nof;i++)>{ found=0; empty=0; for(j=0;j<nos;j++)> {if(input[i]==slot[j][0]){ found=1; slot[j][1]=i+48; break;} } if(slot[j][1]==1) { pf++; max=slot[0][1]-48; loc=0; for(j=0;j<nos;j++)> {if(max<(slot[j][1]-48)){ max=slot[j][1]-48; loc=j;} } slot[loc][0]=input[i]; slot[loc][1]=i+48;}elsepf++; min=slot[0][2]-48; loc=0; for(j=0;j<nos;j++)> {if(min>(slot[j][2]-48)){ min=slot[j][2]-48; loc=j;} } slot[loc][0]=input[i]; slot[loc][2]=i+48;} }}[EDIT by Jochen Arndt: Added code block] 解决方案 Your optimal() function is defined inside the lru() function. That is not allowed. So move the closing brace from the end of the code to the end of your lru() function and add the necessary local variables to optimal(). 这篇关于这是由fifo,lru和最佳算法替换页面的程序。在lru函数中,它在第198行199声明错误中给出了3个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 20:46