gpt4 book ai didi

c - C 中的 FIFO 页面替换,输出错误

转载 作者:行者123 更新时间:2023-11-30 19:39:10 24 4
gpt4 key购买 nike

我正在尝试从具有页面框架和引用字符串的文本文件输入中读取,例如:在我的输入文件中,我有 3,7,0,1,2,0,3,0,4,2,3,0, 3,2,1,2,其中从 3 开始是页框数,从 7 开始是引用字符串。 expected output

#include <stdio.h>
#include <string.h>
void FIFO(char [],char [],int,int);

//FIFO
void FIFO(char s[],char Fi[],int n,int f)
{
int i,j=0,k,flag=0,cnt=0;
printf("\n\tPAGE\t FRAMES\t FAULTS");
for(i=0;i<n;i++)
{
for(k=0;k<f;k++)
{
if(Fi[k]==s[i])
flag=1;
}

if(flag==0)
{
printf("\n\t%c\t",s[i]);
Fi[j]=s[i];
j++;

for(k=0;k<f;k++)
{
printf(" %c",Fi[k]);
}
printf("\tPage-fault%d =",cnt);
cnt++;
// sum = cnt++;
printf(" Total Faults",cnt);

}

else
{
flag=0;
printf("\n\t%c\t",s[i]);
for(k=0;k<f;k++)
{
printf(" %c",Fi[k]);
}

printf("\tNo page-fault");
}
if(j==f)
j=0;
}

}

int main()
{

int n1, n2,n3,n4;
int ch,YN=1,i,l,f,k,j;
char Fi[30],s[25];
char g[0];

/*printf("\nPlease Enter Number of Frames:" );
scanf("%d",&f);
printf("\nPlease Enter the length of the string: ");
scanf("%d",&l);
printf("\nPlease Enter the string: ");
scanf("%s",s);*/

FILE *fp1; // File Pointer Declaration

fp1 = fopen("Inr.txt","r"); // File Opening by File Name and Read Mode r
printf("The number of Frames");
fscanf(fp1,"%d",&f);



for(i=0;i<f;i++)
//fscanf(fp1,"%d,",&f);
Fi[i]=-1;
fclose(fp1);
do
{

printf("\n_________ Please Select One Option __________\n");
printf("\n\t1:FIFO\t2:EXIT");
printf("\n\n\tEnter your choice: ");
scanf("%d",&ch);

switch(ch)
{
case 1:
for(i=0;i<f;i++)
{
Fi[i]=-1;
}

FIFO(s,Fi,l,f);
break;
case 2:
exit(0);
}
printf("\n\n\tDo u want to continue IF YES PRESS 1\n\n\tIF NO PRESS 0 : ");
scanf("%d",&YN);
}while(YN==1);return(0);

}

最佳答案

也许这段代码可以帮助您入门......

#include <stdio.h>
#include <string.h>

//FIFO
void FIFO(
int *I__pages,
char *I__frames,
int I__maxPages,
int I__maxFrames
)
{
int pageIndex;
int j=0;
int pageFaults=0;
int frameIndex;
int noFault=0;

for(frameIndex=0; frameIndex<I__maxFrames; frameIndex++)
I__frames[frameIndex]=-1;

printf("\tPAGE\t FRAMES\tFAULTS\n");
for(pageIndex=0; pageIndex < I__maxPages; ++pageIndex)
{
printf("\t%d\t", I__pages[pageIndex]);

for(frameIndex=0; frameIndex<I__maxFrames; frameIndex++)
{
if(I__frames[frameIndex]==I__pages[pageIndex])
noFault=1;
}

if(noFault)
{
noFault=0;
for(frameIndex=0; frameIndex<I__maxFrames; frameIndex++)
printf(" %2d",I__frames[frameIndex]);

printf("\tNo page-fault\n");
continue;
}

I__frames[j++] = I__pages[pageIndex];
if(j==I__maxFrames)
j=0;

for(frameIndex=0; frameIndex<I__maxFrames; frameIndex++)
printf(" %2d",I__frames[frameIndex]);

printf("\tPage-fault%d =", pageFaults++);
printf(" Total Faults\n");
}

return;
}

int main()
{
int ch,YN=1;
int maxPages = 0;
int maxFrames;
char frames[30];
int pages[25];
FILE *fp1 = NULL; // File Pointer Declaration

fp1 = fopen("Inr.txt","r"); // File Opening by File Name and Read Mode r
fscanf(fp1,"%d,", &maxFrames); // Read the maxFrames value first.

while(EOF != fscanf(fp1,"%d,",&pages[maxPages]))
++maxPages;

fclose(fp1);

do {
printf("\n_________ Please Select One Option __________\n");
printf("\n\t1:FIFO\t2:EXIT");
printf("\n\n\tEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
FIFO(pages, frames, maxPages, maxFrames);
break;

case 2:
return(0);
}

printf("\n\n\tDo u want to continue IF YES PRESS 1\n\n\tIF NO PRESS 0 : ");
scanf("%d",&YN);
} while(YN==1);

return(0);
}

关于c - C 中的 FIFO 页面替换,输出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36878345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com