UVA 10033 - Interpreter

Description:
A certain computer has 10 registers and 1000 words of RAM. Each register or RAM location holds a
3-digit integer between 0 and 999. Instructions are encoded as 3-digit integers and stored in RAM. The
encodings are as follows:

  • 100 means halt
  • 2dn means set register d to n (between 0 and 9)
  • 3dn means add n to register d
  • 4dn means multiply register d by n
  • 5ds means set register d to the value of register s
  • 6ds means add the value of register s to register d
  • 7ds means multiply register d by the value of register s
  • 8da means set register d to the value in RAM whose address is in register a
  • 9sa means set the value in RAM whose address is in register a to the value of register s
  • 0ds means goto the location in register d unless register s contains 0

All registers initially contain 000. The initial content of the RAM is read from standard input. The
first instruction to be executed is at RAM address 0. All results are reduced modulo 1000.

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
The input to your program consists of up to 1000 3-digit unsigned integers, representing the contents of consecutive RAM locations starting at 0. Unspecified RAM locations are initialized to 000.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
The output from your program is a single integer: the number of instructions executed up to and including the halt instruction. You may assume that the program does halt.

Sample Input

1
299
492
495
399
492
495
399
283
279
689
078
100
000
000
000

Sample Output

16

题意:给出一系列的指令,求总共执行的指令数;

InstructionsMeaning
100停止执行
2dn设置寄存器d的值为n(0-9)
3dn寄存器d的值加上n
4dn寄存器d的值乘上n
5ds设置寄存器d的值为寄存器s的值
6ds寄存器d的值加上寄存器s的值
7ds寄存器d的值乘上寄存器s的值
8da设置寄存器d的值为RAM中地址为寄存器a的值
9sa设置RAM中地址为寄存器a的值为寄存器s的值
0ds跳转到RAM中地址为寄存器d的值处,除非此时寄存器s的值为0

思路:按照所给的指令进行模拟即可,要注意的是在输入第一个测试数的时候后面要跟一个空行,并且每两个测试用例之间要有一个空行;再有一个需要注意的是,虽然题目告诉我们RAM中的指令数只有1000条,但是不代表输入的指令数不超过1000条,所以数组RAM开的大一点,否则会出现Runtime Error;最后一点,在计算总共执行的指令数时,要加上停止指令的执行;

#include <cstdio>
#include <cstring>
#include <memory>

int RAM[10000];
int reg[10+5];

#define op1(num) (num/100)
#define op2(num) (num/10%10)
#define op3(num) (num%10)

int execute(){
    int cur = 0;   //the position of RAM
    int cnt = 0+1; //the number of instructions executed
    int op;        //current instruction

    while(1){
        op = RAM[cur++];
        switch(op1(op)){
        case 0:
            if(reg[op3(op)])
                cur = reg[op2(op)];
            break;
        case 1:
            if(!op2(op) && !op3(op))
                return cnt;
            break;
        case 2:
            reg[op2(op)] = op3(op);
            break;
        case 3:
            reg[op2(op)] = (reg[op2(op)] + op3(op))%1000;
            break;
        case 4:
            reg[op2(op)] = (reg[op2(op)] * op3(op))%1000;
            break;
        case 5:
            reg[op2(op)] = reg[op3(op)];
            break;
        case 6:
            reg[op2(op)] = (reg[op2(op)] + reg[op3(op)])%1000;
            break;
        case 7:
            reg[op2(op)] = (reg[op2(op)] * reg[op3(op)])%1000;
            break;
        case 8:
            reg[op2(op)] = RAM[reg[op3(op)]];
            break;
        case 9:
            RAM[reg[op3(op)]] = reg[op2(op)];
            break;
        default:
            break;
        }
        cnt++;
    }
}

int main(){
    int N;
    scanf("%d\n\n", &N);

    while(N--){
        char buff[10];
        int cnt = 0;
        memset(RAM, 0, sizeof(RAM));
        memset(reg, 0, sizeof(reg));
        while(gets(buff)){
            if(!strcmp(buff, ""))
                break;
            sscanf(buff, "%d", RAM+cnt);
            cnt++;
        }
        printf("%d\n", execute());
        if(N)
            printf("\n");
    }

    return 0;
}

### 回答1: PHP公式解释器(PHP-Formula-Interpreter)是一个用于解释和计算数学公式的工具。它可以通过解析和评估用户提供的数学表达式来计算结果。 PHP-Formula-Interpreter可以处理包括基本算术运算符(例如加法、减法、乘法和除法)、指数计算(例如乘方)、三角函数、对数函数和常量(如π和e)等各种数学运算。它还可以处理复杂的公式,包括括号和多个运算符的组合。 使用PHP-Formula-Interpreter,开发者可以将数学公式嵌入到他们的PHP应用程序中,并通过调用相应的函数来计算结果。这使得开发者能够在应用程序中实现复杂的数学计算功能,而无需编写繁琐的算法或手动计算。 PHP-Formula-Interpreter的工作原理是通过将数学表达式解析成语法树,然后按照树的结构逐步计算出结果。它使用递归下降解析器的概念来解析表达式,并通过遍历语法树来执行计算。 使用PHP-Formula-Interpreter需要一定的数学和编程知识,因为开发者需要了解数学表达式的语法和运算规则,并使用合适的函数和参数进行调用。然而,一旦掌握了使用方法,在PHP应用程序中实现数学计算功能将变得更加简单和高效。 总而言之,PHP-Formula-Interpreter是一个强大的工具,可以帮助开发者在PHP应用程序中轻松解析和计算数学公式。它不仅简化了数学计算的实现过程,还提供了灵活性和可扩展性,使开发者能够在应用程序中使用各种数学运算。 ### 回答2: PHP公式解释器是一种用于解析和执行数学和逻辑表达式的工具。它可以解析包含变量、常数、操作符和函数的表达式,并返回计算结果。这种解释器通常被用于开发动态网页,特别是在处理表单和用户输入时。 使用PHP公式解释器,我们可以创建各种复杂的计算和逻辑表达式。它允许我们使用算术运算符(如加法、减法、乘法、除法)和逻辑运算符(如与、或、非)来操作和比较数值。 此外,PHP公式解释器还提供了许多内置函数,如求平方根、取整、绝对值等。我们还可以自定义函数,以满足特定的要求和计算需求。 使用PHP公式解释器可以有效地处理用户输入。例如,在一个表单中,我们可以使用公式解释器来验证用户输入的数值是否符合特定的规则。如果输入不符合要求,我们可以返回错误信息,提示用户重新输入。 总而言之,PHP公式解释器是一个重要的工具,可以在动态网页开发中处理各种数学和逻辑运算。通过将变量、常数和运算符结合起来,我们可以创建有趣和功能丰富的表达式,并获得相应的计算结果。它为我们提供了更大的灵活性和控制力,以满足各种计算需求。 ### 回答3: php-formula-interpreter 是一个用于解析和计算数学公式的 PHP 库。它可以帮助开发人员在他们的 PHP 项目中处理复杂的数学计算。 使用 php-formula-interpreter,我们可以轻松地解释和运行包含数学函数、变量和运算符的公式。这个库提供了一种灵活和强大的方式来处理数学表达式,无论是简单的加法和乘法,还是复杂的三角函数和对数等。 该库具有用户友好的界面和简单的使用方法。首先,我们需要加载库的包。然后,我们可以将数学表达式作为字符串传递给库的解释器。解释器将分析表达式并返回计算结果。 php-formula-interpreter 还提供了一些额外的功能,使开发人员能够控制解释和计算过程。例如,我们可以定义自己的数学函数和变量,并使用它们来编写更复杂的公式。我们还可以指定数值的精度和规则,以满足特定需求。 总之,php-formula-interpreter 是一个方便和实用的 PHP 库,用于解析和计算数学公式。它提供了简单易用的接口,并支持各种数学函数和运算符。无论是简单的计算还是复杂的数学公式,这个库都可以帮助我们实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值