poj2410

本文介绍了一个模拟运算程序,通过解析特定指令集实现简单的累加、递增、递减等功能。该程序使用C++编写,能够根据预设的指令进行内存单元的操作,并最终返回计算结果。

模拟

View Code
#include <iostream>
using namespace std;

const    int maxn=32;

int        byte[maxn];

void init()
{
    int        i;

    for (i=1;i<=31;i++)
        cin>>byte[i];
}

int num(int add)
{
    int        x=0,temp=1;
    
    while (add)
    {
        x+=add%10*temp;
        add/=10;
        temp*=2;
    }
    return x;
}

int    binary(int h)
{
    int        bin[9],x=0,i,ans=0;

    while (h)
    {
        bin[++x]=h%2;
        h/=2;
    }
    for (i=x;i>=1;i--)
        ans=ans*10+bin[i];
    return ans;
}

void inc(int &accu)
{
    int        i=1,temp;
    
    temp=num(accu)+1;
    if (temp>255)
        temp=0;
    accu=binary(temp);
}

void dec(int &accu)
{
    int        i=1,temp;
    
    temp=num(accu)-1;
    if (temp<0)
        temp=255;
    accu=binary(temp);
}

int work()
{
    int        accu=0,pc=0,x,ord;
    
    while (true)
    {
        x=num(byte[pc]%100000);
        ord=byte[pc]/100000;
        pc++;
        if (pc>31)
            pc=0;
        switch(ord)
        {
            case 0    :
                byte[x]=accu;
                break;
            case 1    :
                accu=byte[x];
                break;
            case 10    :
                pc=accu==0?x:pc;
                break;
            case 11    :
                break;
            case 100:
                dec(accu);
                break;
            case 101:
                inc(accu);
                break;
            case 110:
                pc=x;
                break;
            case 111:
                return accu;
                break;
        }
    }
}

void make(int x)
{
    int        temp=10000000;

    if (x==0)
    {
        cout<<"00000000\n";
        return;
    }
    while (x/temp==0)
    {
        cout<<"0";
        temp/=10;
    }
    cout<<x<<endl;
}

int main()
{
//    freopen("t.txt","r",stdin);
    while (cin>>byte[0])
    {
        init();
        make(work());
    }
    return 0;
}

 

 

转载于:https://www.cnblogs.com/rainydays/archive/2013/01/15/2861385.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值