php实现阿拉伯数字和罗马数字相互转换的方法


Posted in PHP onApril 17, 2015

本文实例讲述了php实现阿拉伯数字和罗马数字相互转换的方法。分享给大家供大家参考。具体如下:

<?php
// Function that calculates the roman string to the given number:
function dec2roman($f)
{
 // Return false if either $f is not a real number, 
 //$f is bigger than 3999 or $f is lower or equal to 0:  
  if(!is_numeric($f) || $f > 3999 || $f <= 0) return false;
 // Define the roman figures:
  $roman = array(
  'M' => 1000,
  'D' => 500,
  'C' => 100,
  'L' => 50,
  'X' => 10,
  'V' => 5,
  'I' => 1
  );
 // Calculate the needed roman figures:
  foreach($roman as $k => $v)
  if(($amount[$k] = floor($f / $v)) > 0)
  $f -= $amount[$k] * $v;
 // Build the string:
  $return = '';
  foreach($amount as $k => $v)
  {
   $return .= $v <= 3 ? str_repeat($k, $v) : $k . $old_k;
   $old_k = $k;  
  }
 // Replace some spacial cases and return the string:
  return str_replace(array('VIV','LXL','DCD'),array('IX','XC','CM'),$return);
}
// echo dec2romen(1981);
// Function to get the decimal value of a roman string:
function roman2dec($str = '')
{
 // Return false if not at least one letter is in the string:
  if(is_numeric($str)) return false;
 // Define the roman figures:
  $roman = array(
  'M' => 1000,
  'D' => 500,
  'C' => 100,
  'L' => 50,
  'X' => 10,
  'V' => 5,
  'I' => 1
  );
 // Convert the string to an array of roman values:
  for($i = 0; $i < strlen($str); $i++) 
  if(isset($roman[strtoupper($str[$i])]))
  $values[] = $roman[strtoupper($str[$i])];
 // Calculate the sum of that array:
  $sum = 0;
  while($current = current($values))
  {
   $next = next($values);
   $next > $current ? $sum += $next - $current + 0 * next($values) : $sum += $current;
  }
 // Return the value:
  return $sum;
}
// echo roman2dec(IX);  
?>

希望本文所述对大家的php程序设计有所帮助。

PHP 相关文章推荐
如何使用动态共享对象的模式来安装PHP
Oct 09 PHP
使用PHP和XSL stylesheets转换XML文档
Oct 09 PHP
介绍php设计模式中的工厂模式
Jun 12 PHP
php include,include_once,require,require_once
Sep 05 PHP
php的webservice的wsdl的XML无法显示问题的解决方法
Mar 11 PHP
安装ImageMagick出现error while loading shared libraries的解决方法
Sep 23 PHP
CI框架常用方法小结
May 17 PHP
PHP简单计算两个时间差的方法示例
Jun 20 PHP
使用SMB共享来绕过php远程文件包含的限制执行RFI的利用
May 31 PHP
tp5框架基于ajax实现异步删除图片的方法示例
Feb 10 PHP
浅谈php常用的7大框架的优缺点
Jul 20 PHP
PHP安装扩展mcrypt以及相关依赖项深入讲解
Mar 04 PHP
php实现根据词频生成tag云的方法
Apr 17 #PHP
php计算两个坐标(经度,纬度)之间距离的方法
Apr 17 #PHP
php使用GD创建保持宽高比缩略图的方法
Apr 17 #PHP
PHP中preg_match正则匹配中的/u、/i、/s含义
Apr 17 #PHP
php和editplus正则表达式去除空白行
Apr 17 #PHP
PHP生成唯一订单号的方法汇总
Apr 16 #PHP
微信access_token的获取开发示例
Apr 16 #PHP
You might like
php安全之直接用$获取值而不$_GET 字符转义
2012/06/03 PHP
使用Discuz关键词服务器实现PHP中文分词
2014/03/11 PHP
对于ThinkPHP框架早期版本的一个SQL注入漏洞详细分析
2014/07/04 PHP
PHP如何开启Opcache功能提升程序处理效率
2020/04/27 PHP
仿猪八戒网左下角的文字滚动效果
2011/10/28 Javascript
javascript陷阱 一不小心你就中招了(字符运算)
2013/11/10 Javascript
javascript 循环调用示例介绍
2013/11/20 Javascript
Google (Local) Search API的简单使用介绍
2013/11/28 Javascript
jquery自定义下拉列表示例
2014/04/25 Javascript
点击A元素触发B元素的事件在IE8下会识别成A元素
2014/09/04 Javascript
js单独获取一个checkbox看其是否被选中
2014/09/22 Javascript
jquery插件推荐浏览器嗅探userAgent
2014/11/09 Javascript
javascript中Array数组的迭代方法实例分析
2015/02/04 Javascript
js实现点击链接后窗口缩小并居中的方法
2015/03/02 Javascript
javascript实现手机震动API代码
2015/08/05 Javascript
荐书|您有一份JavaScript书单待签收
2017/07/21 Javascript
ECMAscript 变量作用域总结概括
2017/08/18 Javascript
使用JQuery实现图片轮播效果的实例(推荐)
2017/10/24 jQuery
微信小程序利用canvas 绘制幸运大转盘功能
2018/07/06 Javascript
python利用matplotlib库绘制饼图的方法示例
2016/12/18 Python
python gensim使用word2vec词向量处理中文语料的方法
2019/07/05 Python
Python可以实现栈的结构吗
2020/05/27 Python
Python 按比例获取样本数据或执行任务的实现代码
2020/12/03 Python
详解BeautifulSoup获取特定标签下内容的方法
2020/12/07 Python
印度尼西亚综合购物网站:Lazada印尼
2016/09/07 全球购物
Traffic People官网:女式花裙、上衣和连身裤
2020/10/12 全球购物
大学生咖啡店创业计划书
2014/01/21 职场文书
赔偿协议书范本
2014/04/15 职场文书
怎样写离婚协议书
2014/09/10 职场文书
2014年领导班子专项整治整改方案
2014/09/28 职场文书
个人作风建设总结
2014/10/23 职场文书
门面房租房协议书
2014/12/01 职场文书
暂住证明怎么写
2015/06/19 职场文书
参观监狱警示教育心得体会
2016/01/15 职场文书
mybatis3中@SelectProvider传递参数方式
2021/08/04 Java/Android
python 中的jieba分词库
2021/11/23 Python