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实现像JSP,ASP里Application那样的全局变量
Jan 12 PHP
破解图片防盗链的代码(asp/php)测试通过
Jul 02 PHP
php feof用来识别文件末尾字符的方法
Aug 01 PHP
在php中判断一个请求是ajax请求还是普通请求的方法
Jun 28 PHP
php.ini修改php上传文件大小限制的方法详解
Jun 17 PHP
PHP以指定字段为索引返回数据库所取的数据数组
Jun 30 PHP
php查找字符串出现次数的方法
Dec 01 PHP
php实现上传图片保存到数据库的方法
Feb 11 PHP
CodeIgniter配置之database.php用法实例分析
Jan 20 PHP
Nginx环境下PHP flush失效的解决方法
Oct 19 PHP
因str_replace导致的注入问题总结
Aug 08 PHP
分享几种好用的PHP自定义加密函数(可逆/不可逆)
Sep 15 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
ninety plus是什么?ninety plus咖啡好吗?
2021/03/04 新手入门
福利彩票幸运号码自动生成器
2006/10/09 PHP
php中$_REQUEST、$_POST、$_GET的区别和联系小结
2011/11/23 PHP
PHP时间和日期函数详解
2015/05/08 PHP
Dojo之路:如何利用Dojo实现Drag and Drop效果
2007/04/10 Javascript
List all the Databases on a SQL Server
2007/06/21 Javascript
JQuery 学习笔记01 JQuery初接触
2010/05/06 Javascript
jQuery EasyUI API 中文文档 - Calendar日历使用
2011/10/19 Javascript
js解析xml字符串和xml文档实现原理及代码(针对ie与火狐)
2013/02/02 Javascript
JS实现FLASH幻灯片图片切换效果的方法
2015/03/04 Javascript
Backbone.js的Hello World程序实例
2015/06/19 Javascript
Jquery UI实现一次拖拽多个选中的元素操作
2020/12/01 Javascript
浅谈pc端rem字体设置的问题
2017/08/03 Javascript
JS+canvas绘制的动态机械表动画效果
2017/09/12 Javascript
JavaScript实现写入文件到本地的方法【基于FileSaver.js插件】
2018/03/15 Javascript
通过vue提供的keep-alive减少对服务器的请求次数
2018/04/01 Javascript
maptalks+three.js+vue webpack实现二维地图上贴三维模型操作
2020/08/10 Javascript
Python 元类使用说明
2009/12/18 Python
Python Requests 基础入门
2016/04/07 Python
Python的Django中将文件上传至七牛云存储的代码分享
2016/06/03 Python
python数据类型判断type与isinstance的区别实例解析
2017/10/31 Python
python机器学习之神经网络(二)
2017/12/20 Python
python中数组和矩阵乘法及使用总结(推荐)
2019/05/18 Python
详解PANDAS 数据合并与重塑(join/merge篇)
2019/07/09 Python
Python字典的概念及常见应用实例详解
2019/10/30 Python
手把手教你从PyCharm安装到激活(最新激活码),亲测有效可激活至2089年
2020/11/25 Python
Python3 用matplotlib绘制sigmoid函数的案例
2020/12/11 Python
如何在Shell脚本中使用函数
2015/09/06 面试题
职业生涯规划怎么写
2013/12/29 职场文书
2014年党务公开方案
2014/05/08 职场文书
励志演讲稿500字
2014/08/21 职场文书
2014领导班子正风肃纪思想汇报
2014/09/18 职场文书
2014村党支部书记党建工作汇报材料
2014/11/02 职场文书
举起手来观后感
2015/06/09 职场文书
Nginx tp3.2.3 404问题解决方案
2021/03/31 Servers
深入理解CSS 中 transform matrix矩阵变换问题
2021/08/30 HTML / CSS