此问题已解决!代码如下:
#include "Stdio.h"
#include "Conio.h"
typedef struct battery
{
char *data;
int front,rear;
int size;
}Battery;
void Initqueue(Battery *Q,int size)
{ /*初始化链表队列*/
Q->data=(char *)malloc(sizeof(char)*size);
if (!Q->data) printf("Application failed!");
Q->front=Q->rear=0;
Q->size=size;
}
void Enqueue(Battery *Q,char elem)
{ /*入队操作*/
if ((Q->rear+1)%(Q->size)==Q->front) return;
Q->data[Q->rear]=elem;
Q->rear=(Q->rear+1)%(Q->size);
}
char Dequeue(Battery *Q)
{ /*出队操作*/
char ret;
if (Q->front==Q->rear) return;
ret=Q->data[Q->front];
Q->front=(Q->front+1)%(Q->size);
return ret;
}
char Get(Battery *Q)
{ /*得到队头元素*/
return Q->data[Q->front];
}
char Index(Battery *Q,int i)
{ /*定位第i个元素*/
return Q->data[Q->front+i-1];
}
int Isfull(Battery *Q)
{ /*队满返回1*/
if ((Q->rear+1)%(Q->size)==Q->front)
return 1;
}
int Length(Battery *Q)
{ /*返回队列长度*/
return (Q->rear-Q->front+Q->size)%(Q->size);
}
void Cache(int Page_size/*页面大小*/,int Mainmen_size/*主存大小*/,char *string/*输入的数据*/,int num/*需输入的元素个数*/)
{
int size=Mainmen_size/Page_size; /*segment是需要分的段数。如Page_size=200,则0、1属于一段*/
Battery *p,*q;
int i=0,j=0,count1=0,count2=0,count3=0,count=0,n;
Initqueue(p,size+1);
for (i=0;i<num;i++) /*简化地址流*/
{
switch(Page_size)
{
case 100:
if ('0'==string[i])
string[i]='0';
if ('1'==string[i])
string[i]='1';
if ('2'==string[i])
string[i]='2';
if ('3'==string[i])
string[i]='3';
if ('4'==string[i])
string[i]='4';
if ('5'==string[i])
string[i]='5';
if ('6'==string[i])
string[i]='6';
if ('7'==string[i])
string[i]='7';
if ('8'==string[i])
string[i]='8';
if ('9'==string[i])
string[i]='9';
break;
case 200:
if ('0'==string[i] || '1'==string[i])
string[i]='0';
if ('2'==string[i] || '3'==string[i])
string[i]='2';
if ('4'==string[i] || '5'==string[i])
string[i]='4';
if ('6'==string[i] || '7'==string[i])
string[i]='6';
if ('8'==string[i] || '9'==string[i])
string[i]='8';
break;
case 300:
if ('0'==string[i] || '1'==string[i] || '2'==string[i])
string[i]='0';
if ('3'==string[i] || '4'==string[i] || '5'==string[i])
string[i]='3';
if ('6'==string[i] || '7'==string[i] || '8'==string[i])
string[i]='6';
if ('9'==string[i])
string[i]='9';
break;
case 400:
if ('0'==string[i] || '1'==string[i] || '2'==string[i] || '3'==string[i])
string[i]='0';
if ('4'==string[i] || '5'==string[i] || '6'==string[i] || '7'==string[i])
string[i]='4';
if ('8'==string[i] || '9'==string[i])
string[i]='8';
break;
}
}
if (num<=size) /*地址流数小于页数*/
{
Enqueue(p,string[0]);
for (i=1;i<num;i++) /*边入队边与队列中所有数比较*/
{
for (j=1;j<=i;j++)
if (string[i]==Index(p,j))
{
count1++;
break;
}
Enqueue(p,string[i]);
}
printf("Target:%d ",count1);
printf("Rate:%d%%\n",100*count1/num);
return;
}
else
{
Enqueue(p,string[0]);
for (i=1;i<num;i++)
{
if (Isfull(p)) /*队列已满*/
{
for (j=1;j<=size;j++) /*与队列中数比较*/
{
if (Index(p,j)==string[i]) /*命中,但不入队*/
{
count2++;
break;
}
else if (j==size && string[i]!=Index(p,j)) /*最后一个数不命中,则出队,并把新数入队*/
{
Dequeue(p);
Enqueue(p,string[i]);
}
}
}
else /*队列不满*/
{
for (n=1;n<=Length(p);n++) /*与队列所有数比较*/
{
if (string[i]==Index(p,n)) /*命中,但不入队*/
{
count3++;
break;
}
}
if (string[i]==Index(p,n)) /*确定无重复数入队*/
continue;
Enqueue(p,string[i]);
}
}
count=count2+count3;
printf("Target:%d ",count);
printf("Rate:%d%%\n",100*count/num);
}
}
int main(void)
{
int pagesize,mainmensize,num=0,i=0;
char s[50];
while (getch!='*')
{
num=0;i=0;pagesize=0;mainmensize=0;
gotoxy(5,3);
printf("Welcome to cache test! Now you can input the Address flow(End with ' '):\n");
gotoxy(5,5);
for (i=0;i<50;i++)
{
scanf("%c",&s[i]);
if (s[i]!=' ')
num++;
else
break;
}
gotoxy(5,8);
printf("OK! You can input Pagesize:");
scanf("%d",&pagesize);
printf("\n");
gotoxy(5,10);
printf("Good! You can input Mainmensize:");
scanf("%d",&mainmensize);
printf("\n");
gotoxy(5,12);
printf("The result is:\n");
gotoxy(5,14);
Cache(pagesize,mainmensize,s,num);
gotoxy(5,16);
getch();
clrscr();
}
return 0;
}