(挑战编程_1_6)Interpreter

题目链接:http://www.programming-challenges.com/pg.php?page=downloadproblem&probid=110106&format=html

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>

using namespace std;

// register
int reg[11];
// memory
int memory[1010];

/*
	输入memory,开始解析计算,
	返回一共执行的次数
*/
int Execute();

/*
	输入一个三位数,得到第二位,第三位的值
*/
void ParseNum(int num, int &n2, int &n3);

void Test();


//int main()
//{
//	//cout << "Test" << endl;
//	int count = 0;	
//	cin >> count;
//
//	char ch;
//	// 读入第一个空行
//	ch = cin.get();
//
//	while (count--)
//	{
//		memset(memory, 0, sizeof(memory));
//		memset(reg, 0, sizeof(reg));
//		// 内存的index
//		int index = 0;
//		// 从控制台读入内存的数
//		int num = 0;
//
//		while (true)
//		{
//			ch = cin.get();
//			ch = cin.peek();
//			if (ch == '\n')
//			{
//				break;
//			}
//			else
//			{
//				cin >> num;
//				memory[index++] = num;
//			}
//		}
//		cout << Execute() << endl;
//		if (count)
//		{
//			cout << endl;
//		}
//	}
//	return 0;
//}


int main()
{
	int count = 0;	
	cin >> count;

	getchar();
	getchar();

	char buf[11];

	while (count--)
	{
		memset(memory, 0, sizeof(memory));
		memset(reg, 0, sizeof(reg));
		// 内存的index
		int index = 0;
		// 从控制台读入内存的数
		int num = 0;

		while (gets(buf))
		{			
			if (buf[0] == '\0')
			{
				break;
			}			
			sscanf(buf, "%d", &memory[index]);
			++index;			
		}

		cout << Execute() << endl;
		if (count)
		{
			cout << endl;
		}
	}

	return 0;
}

int Execute()
{
	// 指令执行的总数
	int ans = 0;
	// 从0开始
	// 内存中的index
	int curIndex = 0;
	// 当前执行的指令
	int instr = memory[curIndex];

	while (instr != 100)
	{
		// 下一条
		++curIndex;
		++ans;
		// 当前指令的类型
		int kind = instr / 100;
		int d = 0;
		int n = 0;
		int s = 0;
		int a = 0;
		switch(kind)
		{
		case 2:
			ParseNum(instr, d, n);
			// 将寄存器的值设为n
			reg[d] = n;
			break;
		case 3:			
			ParseNum(instr, d, n);
			// 将寄存器的值增加N
			reg[d] = (reg[d] + n) % 1000;
			break;
		case 4:
			ParseNum(instr, d, n);
			// 将寄存器的值乘以N
			reg[d] = (reg[d] * n) % 1000;
			break;
		case 5:
			ParseNum(instr, d, s);
			// 将s的值赋给d
			reg[d] = reg[s];
			break;
		case 6:
			ParseNum(instr, d, s);
			// 将s的值加到d
			reg[d] = (reg[d] + reg[s]) % 1000;
			break;
		case 7:
			ParseNum(instr, d, s);
			// 将s的值乘给d
			reg[d] = (reg[d] * reg[s]) % 1000;
			break;
		case 8:
			ParseNum(instr, d, a);
			reg[d] = memory[reg[a]];
			break;
		case 9:
			ParseNum(instr, s, a);
			memory[reg[a]] = reg[s];
			break;		
		case 0:
			ParseNum(instr, d, s);
			if (reg[s])
			{
				curIndex = reg[d];
			}
			break;
		}
		instr = memory[curIndex];
	}
	++ans;
	return ans;
}


void ParseNum(int num, int &n2, int &n3)
{
	n3 = num % 10;
	n2 = (num / 10) % 10;
}

void Test()
{
	// 测试078
	//int num;
	//cin >> num;
	//memory[0] = num;

	memset(memory, 0, sizeof(memory));
	memset(reg, 0, sizeof(reg));

	memory[0] = 299;
	memory[1] = 492;
	memory[2] = 495;
	memory[3] = 399;
	memory[4] = 492;
	memory[5] = 495;
	memory[6] = 399;
	memory[7] = 283;
	memory[8] = 279;
	memory[9] = 689;
	memory[10] = 78;
	memory[11] = 100;
	memory[12] = 0;
	memory[13] = 0;
	memory[14] = 0;
	Execute();
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自制一门编程语言是一项非常有挑战性的任务,但使用Python可以相对容易地实现一个简单的解释型编程语言。下面是一个简单的示例,展示了如何使用Python来实现一个自制的编程语言: ```python # 定义一个解释器类 class Interpreter: def __init__(self): self.variables = {} # 存储变量的字典 # 解释执行代码 def interpret(self, code): lines = code.split('\n') # 按行分割代码 for line in lines: if line.startswith('print'): # 处理print语句 self.print_statement(line) elif '=' in line: # 处理赋值语句 self.assignment_statement(line) # 处理print语句 def print_statement(self, line): _, value = line.split('print') value = value.strip() if value.startswith('"') and value.endswith('"'): # 处理字符串 print(value[1:-1]) else: # 处理变量 print(self.variables.get(value)) # 处理赋值语句 def assignment_statement(self, line): variable, value = line.split('=') variable = variable.strip() value = value.strip() if value.isdigit(): # 处理整数 self.variables[variable] = int(value) elif value.startswith('"') and value.endswith('"'): # 处理字符串 self.variables[variable] = value[1:-1] else: # 处理变量赋值 self.variables[variable] = self.variables.get(value) # 创建一个解释器实例 interpreter = Interpreter() # 编写自制语言的代码 code = ''' x = 10 y = "Hello, world!" print(x) print(y) ''' # 解释执行代码 interpreter.interpret(code) ``` 这个示例展示了一个简单的解释型编程语言,它支持变量赋值和打印语句。你可以根据自己的需求扩展这个语言,添加更多的功能和语法规则。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值