ZOJ 1072 Microprocessor Simulation
2011-03-13 17:39
344 查看
也是条简单题,不过题目让我们模拟CPU挺有意思。
出现过的问题:要注意地址都是十六进制表示的,即命令(010),表示的地址(10)是十六进制,也就是指十进制的16。刚开始就是这个地方搞错了,看不懂意思。。。。
#include "iostream"
using namespace std;
int nMemory[256];
int A, B;
int ExtractCommand(int nCmdIndex)
{
int cmd = nMemory[nCmdIndex];
int nNewCmdIndex = nCmdIndex + 1;
int pos = nMemory[nCmdIndex + 1] * 10 + nMemory[nCmdIndex + 2];
switch ( cmd )
{
case 0 :
A = nMemory[pos];
nNewCmdIndex += 2;
break;
case 1 :
nMemory[pos] = A;
nNewCmdIndex += 2;
break;
case 2 :
A = A + B, B = A - B, A = A - B;
break;
case 3 :
A = A + B;
B = A / 16;
A = A % 16;
break;
case 4 :
A = (A + 1) % 16;
break;
case 5 :
A = (A - 1) % 16;
break;
case 6 :
if (A == 0)
{
nNewCmdIndex = pos;
}
break;
case 7 :
nNewCmdIndex = pos;
break;
case 8 :
nNewCmdIndex = 256;
default :
break;
}
return nNewCmdIndex;
}
int main(int argc, char *argv[])
{
int i;
while( 1 )
{
for(i = 0; i < 256 && cin >> hex >> nMemory[i] ; i++ );
if (i < 256)
{
break;
}
for( i = 0; i < 256 ; )
{
i = ExtractCommand(i);
}
}
return 0;
}
#include "iostream"
using namespace std;
int nMemory[256];
int A, B;
int ExtractCommand(int nCmdIndex)
{
int cmd = nMemory[nCmdIndex];
int nNewCmdIndex = nCmdIndex + 1;
int pos = nMemory[nCmdIndex + 1] * 10 + nMemory[nCmdIndex + 2];
switch ( cmd )
{
case 0 :
A = nMemory[pos];
nNewCmdIndex += 2;
break;
case 1 :
nMemory[pos] = A;
nNewCmdIndex += 2;
break;
case 2 :
A = A + B, B = A - B, A = A - B;
break;
case 3 :
A = A + B;
B = A / 16;
A = A % 16;
break;
case 4 :
A = (A + 1) % 16;
break;
case 5 :
A = (A - 1) % 16;
break;
case 6 :
if (A == 0)
{
nNewCmdIndex = pos;
}
break;
case 7 :
nNewCmdIndex = pos;
break;
case 8 :
nNewCmdIndex = 256;
default :
break;
}
return nNewCmdIndex;
}
int main(int argc, char *argv[])
{
int i;
while( 1 )
{
for(i = 0; i < 256 && cin >> hex >> nMemory[i] ; i++ );
if (i < 256)
{
break;
}
for( i = 0; i < 256 ; )
{
i = ExtractCommand(i);
}
}
return 0;
}
出现过的问题:要注意地址都是十六进制表示的,即命令(010),表示的地址(10)是十六进制,也就是指十进制的16。刚开始就是这个地方搞错了,看不懂意思。。。。
#include "iostream"
using namespace std;
int nMemory[256];
int A, B;
int ExtractCommand(int nCmdIndex)
{
int cmd = nMemory[nCmdIndex];
int nNewCmdIndex = nCmdIndex + 1;
int pos = nMemory[nCmdIndex + 1] * 10 + nMemory[nCmdIndex + 2];
switch ( cmd )
{
case 0 :
A = nMemory[pos];
nNewCmdIndex += 2;
break;
case 1 :
nMemory[pos] = A;
nNewCmdIndex += 2;
break;
case 2 :
A = A + B, B = A - B, A = A - B;
break;
case 3 :
A = A + B;
B = A / 16;
A = A % 16;
break;
case 4 :
A = (A + 1) % 16;
break;
case 5 :
A = (A - 1) % 16;
break;
case 6 :
if (A == 0)
{
nNewCmdIndex = pos;
}
break;
case 7 :
nNewCmdIndex = pos;
break;
case 8 :
nNewCmdIndex = 256;
default :
break;
}
return nNewCmdIndex;
}
int main(int argc, char *argv[])
{
int i;
while( 1 )
{
for(i = 0; i < 256 && cin >> hex >> nMemory[i] ; i++ );
if (i < 256)
{
break;
}
for( i = 0; i < 256 ; )
{
i = ExtractCommand(i);
}
}
return 0;
}
#include "iostream"
using namespace std;
int nMemory[256];
int A, B;
int ExtractCommand(int nCmdIndex)
{
int cmd = nMemory[nCmdIndex];
int nNewCmdIndex = nCmdIndex + 1;
int pos = nMemory[nCmdIndex + 1] * 10 + nMemory[nCmdIndex + 2];
switch ( cmd )
{
case 0 :
A = nMemory[pos];
nNewCmdIndex += 2;
break;
case 1 :
nMemory[pos] = A;
nNewCmdIndex += 2;
break;
case 2 :
A = A + B, B = A - B, A = A - B;
break;
case 3 :
A = A + B;
B = A / 16;
A = A % 16;
break;
case 4 :
A = (A + 1) % 16;
break;
case 5 :
A = (A - 1) % 16;
break;
case 6 :
if (A == 0)
{
nNewCmdIndex = pos;
}
break;
case 7 :
nNewCmdIndex = pos;
break;
case 8 :
nNewCmdIndex = 256;
default :
break;
}
return nNewCmdIndex;
}
int main(int argc, char *argv[])
{
int i;
while( 1 )
{
for(i = 0; i < 256 && cin >> hex >> nMemory[i] ; i++ );
if (i < 256)
{
break;
}
for( i = 0; i < 256 ; )
{
i = ExtractCommand(i);
}
}
return 0;
}
相关文章推荐
- ZOJ-1072 MicroprocessorSimulation
- POJ 1049 Microprocessor Simulation 已被翻译
- POJ1049 Microprocessor Simulation
- During Barry's "Intel Microprocessor"
- Processor ARM920T raised an exception.Cause:The pr
- UVa 10714 & POJ 1852 & ZOJ 2376 - Ants
- poj 1436 && zoj 1391 Horizontally Visible Segments (Segment Tree)
- POJ 3652 & ZOJ 2934 & HDU 2721 Persistent Bits(数学 元)
- zoj 3232 It's not Floyd Algorithm
- atitit.eclipse有多少api  扩展点,以及扩展点的设计
- ZZULIOJ【1072】青蛙爬井【循环】&&【模拟】
- POJ 1469 && ZOJ 1140 --COURSES【二分图 && 最大匹配】
- 自适应高度 —  按照文字 设置显示控件自适应 高度
- 多级数据级联选择,附省市县级联选择功能 &#160; ----- &#160; JQUERY特效模板
- HDOJ_1425  sort
- 蓝桥杯——报时助手 ,龟兔赛跑预测  ,芯片测试
- poj1122&&zoj1053 FDNY to the Rescue! ——最短路入门题_Dijkstra算法
- Atitit 《摩奴法典》overivew 读后感 不是由国王 颁布的,而是 僧侣编制
- !POJ 2251 & ZOJ 1940--BFS(第一道BFS)
- Android深入探究笔记之二十 -- 广播接收者,BroadcastReceiver