⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simulatecache.cpp

📁 cache: you will be given a char at will and you should simulate the computer s memory and cache .
💻 CPP
字号:
#include<iostream>

using namespace std;

int main()
{
	struct node1
	{
		int valid;
		int flag;
		int data[100];
	}cache[1000];

	struct node2
	{
		int flag;
		int line;
		int word;
		int byte;
	}address;

	struct node3
	{   
		int data[100];
		int address;
	}memory[20];

	int cachenum = 5;
	int cachesize = 5;
	int r = -1;

	for( int u = 0; u < 20; u++ )
	{
		for( int t = 0; t < cachesize; t++ )
		{
			memory[u].data[t] = u + t;
		}
	}
	for( int i = 0; i < cachenum; i++ )
	{
		cache[i].valid = 1;
	}
	for( int j = 0; j < cachenum; j++ )
	{
		for( int k = 0; k < cachesize; k++ )
		{
			cache[j].data[k] = j + k;
		}
	}
	for( int h = 0; h < cachenum; h++ )
	{
		cache[h].flag = r + 1;
		r++;
	}
	cout<<" Please input the address: "<<endl;
	cin>>address.flag>>address.line>>address.word>>address.byte;
	if( cache[address.line].valid == 1 )
	{
		if( cache[address.line].flag == address.flag )
		{
			cout<<"The data you just input is in the cache and the data is :"
				<<cache[address.line].data[address.word]<<endl;
		}
		else
		{
			cout<<"The data you just input is not in the cache. "<<endl;
			cout<<"Please put the data from the memory into the cache."<<endl;
		
            int y = cache[address.line].flag;
			for( int z = 0; z < cachesize; z++ )
			{
				memory[y].data[z] = cache[address.line].data[z];
			}
			cache[address.line].flag = address.flag;
			for( int j = 0; j < cachesize; j++ )
			{
				cache[address.line].data[j] = memory[address.flag].data[j];
			}
		}
	}
	else{
	        cout<<"The data you just input is not in the cache. "<<endl;
			cout<<"Please put the data from the memory into the cache."<<endl;
		
			cache[address.line].valid = 1;
			for( int z = 0; z < cachesize; z++ )
			{
				memory[1].data[z] = cache[1].data[z];
			}
			cache[address.line].flag = address.flag;
			for( int j = 0; j < cachesize; j++ )
			{
				cache[address.line].data[j] = memory[address.flag].data[j];
			}

	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -