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

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

问题描述

龙书包含了一个使用语法导向翻译方案将整数转换为罗马数字的练习。

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.

单位列:

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天全站免登陆