使用语法导向的翻译方案将整数转换为罗马数字? [英] Convert integers to roman numerals using a syntax-directed translation scheme?

查看:17
本文介绍了使用语法导向的翻译方案将整数转换为罗马数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dragon Book 包含一个使用语法指导的翻译方案将整数转换为罗马数字的练习.

The Dragon Book includes an exercise on converting integers to roman numerals using a syntax-directed translation scheme.

如何完成?

推荐答案

我会考虑从右到左解析.

I would consider parsing from right-to-left.

首先,我将映射单位列:

First, I would map the units column:

0 -> ''
1 -> 'I'
2 -> 'II'
3 -> 'III'
4 -> 'IV'
...
9 -> 'IX'

然后,如果有第二列(例如右数第二列 = 十列),我将使用它映射到

Then, if there was a second column (e.g. second from the right = tens column), I would use that to map to

0 -> ''
1 -> 'X'
2 -> 'XX'
...
9 -> 'XC'

这需要预先添加到初始输出中.

That would need to be prepended to the initial output.

重复下一列(数百、数千),直到用完字母.

Repeat for next columns (hundreds, thousands) until you run out of letters.

仔细检查数字不是0"或负数.

Double-check the number isn't '0' or negative.

这篇关于使用语法导向的翻译方案将整数转换为罗马数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆