2

我正在做一些事情,我希望能够有一个像这个网站这样的转换器:辐射 - 剂量当量 - Sievert 转换因子不幸的是这种转换器没有来源,我需要这个。

有人可以指出我正确的方向或提供此辐射单位转换器的源代码。

一旦我找到了正确的答案(当几天过去了,我可以设置一个赏金),我愿意为一个正确和完美的来源提供 350 个赏金点。

谢谢你。

编辑:

从现在开始,赏金活动已经开始。我需要用 PHP 编写这个脚本,以便通过选择将输入的值转换为另一个辐射单位。

4

2 回答 2

4

像这样管理多种格式之间的转换的最简单方法是只维护一种类型的转换表,然后进行两部分转换以获得所需的格式。这样做会损失一些精度,但你真的应该有足够的精度来满足这样的需求(你总是可以通过向 roentgen 类型添加更多数字来提高精度)。

有很多方法可以做到这一点,但我创建了一个RadiationConverter类来保持一切整洁,我做了一些链接来保持它的整洁。不过,你明白了。

<?php

class RadiationConverter {

 // these are the keys we'll reference when sending a type to the methods, 
 // along with a value for what we'll display each type as:
 private $conversion_display = array ( 
     'sievert' => 'sievert',
     'millisievert' =>  'millisievert',
     'microsievert' => 'microsievert',
     'j/kg'=> 'joule/kilogram', 
     'm2/s2' => 'square meter/square second',   
     'rem' => 'Roentgen eq. man',   
     'millirem' => 'millirem',  
     'imillicurie' => 'intensity millicurie',   
     'electrons'=> 'gray (Wr=1, X-ray, gamma ray, electrons)',  
     'alpha' => 'gray (Wr=20, alpha particles)',
     'roentgen'=> 'Roentgen/hour (numerical equivalent)');

 // this is a conversion table for sieverts to anything else
 // (note the conversion factor of 1 for sievert)
 private $conversion_table = array (
    'sievert'   =>  1,
    'millisievert'  =>  0.001,
    'microsievert'  =>  0.000001,
    'j/kg'  =>  1,  
    'm2/s2' =>  1,  
    'rem'   =>  0.01,   
    'millirem'  =>  0.00001,    
    'imillicurie'   =>  1,  
    'electrons' =>  1,  
    'alpha' =>  20,
    'roentgen'  =>  0.119331742243,
);

 private $type;
 private $amount;
 private $base_type; // this is what our conversion table is based on

 public function __construct($from_type='sievert', $amount = 0) {

   $from_type = strtolower($from_type);
   if(!array_key_exists($from_type,$this->conversion_display)) {
      throw new Exception ("Radiation type does not exist.");
   }
   $this->type = $from_type;
   $this->amount = $amount;
   $this->base_type = 'sievert'; // for reference only, really

 }

 public function amount($amount=null) {
  if(empty($amount)) return $this->amount;
  $this->amount = $amount;
  return $this;
 }

 public function types() {
    return $this->conversion_display;
 }

 public function display_name($type=null) {
    if (empty($type)) $type=$this->type;
    return $this->conversion_display[$type];
 }

 public function display_as($type){
   return $this->convert_to($type) . ' '.$this->display_name($type);
 }

 public function convert_to($type) {
  $type = strtolower($type);
  if(!array_key_exists($type,$this->conversion_display)) {
      throw new Exception ("Radiation conversion type does not exist.");
  }
  // first we convert to our base type, then convert that to the new type:
  return ($this->amount*$this->conversion_table[$this->type])/$this->conversion_table[$type];
 }

 public function base_type(){
    return $this->base_type;
 }


}

现在您只需要几个<select>列表,其中选项值是要转换的各种类型,然后是<input>一个金额字段。你可以像这样使用它:

$converter = new RadiationConverter($_POST['convert_from']); 

// return just the number
echo $converter->amount($_POST['amount'])->convert_to($_POST['convert_to']);

// or return the number with the new type
echo $converter->amount($_POST['amount'])->display_as($_POST['convert_to']);

// you can include the amount in the construct if you want like this:
$converter = new RadiationConverter('rem', 100);
echo $converter->display_as('electrons');

如果您想在该站点上创建一个表格:

$display_amount = $_POST['amount'] . " " . $converter->display_name();

// just take the convert_from variable and cycle through all the types:
foreach($converter->types() as $type => $display) {
  echo  "$display_amount = " . $converter->display_as($type) . "<br />";
}
于 2011-05-10T14:32:20.593 回答
-2

It looks like it's mostly javascript. You can do a view source on the page and see the code that they used and interpret for your site.

于 2011-05-06T18:41:10.150 回答