如何将文本文件的行结尾从 DOS 转换为 Unix?
我将此 php 代码保存在文本文件中,我想将此文本文件转换为 Unix 行结尾。如何?
<?php
class City
{
private $lat=0.0;
private $lng=0.0;
private $name="";
private $visited=false;
private $order=1;
function City($name,$lat,$lng)
{
$this->name=$name;
$this->lat=$lat;
$this->lng=$lng;
}
function getName()
{
return $this->name;
}
function getLat()
{
return $this->lat;
}
function getLng()
{
return $this->lng;
}
function getOrder()
{
return $this->order;
}
function getVisited()
{
return $this->visited;
}
function setVisited($value)
{
$this->visited=$value;
}
function setOrder($value)
{
$this->order=$value;
}
}
class Main
{
public $allCities=array();
private $k=1;
private $totalDistance=0;
/*class main empty constructor*/
function Main()
{}
function sortArray()
{
for($i=count($this->allCities)-1;$i>=0;$i--)
{
for($j=$i-1;$j>=0;$j--)
{
if($this->allCities[$i]->getOrder()<$this->allCities[$j]->getOrder())
{
$temp=$this->allCities[$j];
$this->allCities[$j]=$this->allCities[$i];
$this->allCities[$i]=$temp;
}
}
}
}
/* method to read from cities.txt file */
function getCitiesFromTextFile()
{
$f="cities.txt";
$fo=fopen($f,'r');
while ( $line = fgets($fo, 1000) )
{
$delimiter=" ";
$temp = explode($delimiter, $line);
$c11=new City($temp[0],$temp[1],$temp[2]);
$this->allCities[]=$c11;
}
}
/* to print the data stored in an array ( without ordering the cities ) */
function displayAllCities()
{
for($i=0;$i<count($this->allCities);$i++)
{
print $this->allCities[$i]->getName()."<br>";
}
}
function rad($x)
{
return $x*pi()/180;
//return $x;
}
/* to calculate the distance between two cities */
function calculatedistance($city1,$city2)
{
$lat1=$city1->getLat();
$lng1=$city1->getLng();
$lat2=$city2->getLat();
$lng2=$city2->getLng();
// Spherical law of cosines: d = acos(sin(lat1).sin(lat2)+cos(lat1).cos(lat2).cos(long2-long1)).R
// R=3437.74677 (nautical miles)
// R=6378.7 (kilometers)
// R=3963.0 (statute miles)
$R = 6371; // earth's mean radius in km
$dLat = $this->rad($lat2 - $lat1);
$dLng = $this->rad($lng2 - $lng1);
$a = sin($dLat/2) * sin($dLat/2) +cos($this->rad($lat1)) * cos($this->rad($lat2)) * sin($dLng/2) * sin($dLng/2);
$c = 2 * atan2(sqrt($a), sqrt(1-$a));
$d = $R * $c;
return $d;
//$distance=acos(sin($lat1)*sin($lat2)+cos($lat1)*cos($lat2).cos($lng2-$lng1))*$R;
//return $distance;
}
/* to calculate the optimal path passing all cities once time for each city */
function findPath($start)
{
for($i=0;$i<count($this->allCities);$i++)
{
if($this->k>count($this->allCities))
break;
if($this->allCities[$i]->getName()==$start &&!$this->allCities[$i]->getVisited())
{
$this->allCities[$i]->setVisited(true);
$this->allCities[$i]->setOrder($this->k);
$lower_index=0;
$lower_dis=1000000;
for($j=0;$j<count($this->allCities);$j++)
{
if(!$this->allCities[$j]->getVisited())
{
$dis=$this->calculatedistance($this->allCities[$j],$this->allCities[$i]);
if($dis<$lower_dis)
{
$lower_index=$j;
$lower_dis=$dis;
}
}
}
$this->k++;
$this->findPath($this->allCities[$lower_index]->getName());
}// enf if
}// end for
}// end function
/* method to display the total distance of the route passign all cities */
function getTotalDistance()
{
return $this->totalDistance;
}
}// end class
//$test=new City("Gaza",1.2,1.50);
$m=new Main();
$m->getCitiesFromTextFile();
//$m->displayAllCities();
$m->findPath("Beijing");
$m->sortArray();
$m->displayAllCities();
$m->getCitiesFromTextFile();
?>
I have this php code saved in text file, I want to convert this text file to Unix line endings. How?
<?php
class City
{
private $lat=0.0;
private $lng=0.0;
private $name="";
private $visited=false;
private $order=1;
function City($name,$lat,$lng)
{
$this->name=$name;
$this->lat=$lat;
$this->lng=$lng;
}
function getName()
{
return $this->name;
}
function getLat()
{
return $this->lat;
}
function getLng()
{
return $this->lng;
}
function getOrder()
{
return $this->order;
}
function getVisited()
{
return $this->visited;
}
function setVisited($value)
{
$this->visited=$value;
}
function setOrder($value)
{
$this->order=$value;
}
}
class Main
{
public $allCities=array();
private $k=1;
private $totalDistance=0;
/*class main empty constructor*/
function Main()
{}
function sortArray()
{
for($i=count($this->allCities)-1;$i>=0;$i--)
{
for($j=$i-1;$j>=0;$j--)
{
if($this->allCities[$i]->getOrder()<$this->allCities[$j]->getOrder())
{
$temp=$this->allCities[$j];
$this->allCities[$j]=$this->allCities[$i];
$this->allCities[$i]=$temp;
}
}
}
}
/* method to read from cities.txt file */
function getCitiesFromTextFile()
{
$f="cities.txt";
$fo=fopen($f,'r');
while ( $line = fgets($fo, 1000) )
{
$delimiter=" ";
$temp = explode($delimiter, $line);
$c11=new City($temp[0],$temp[1],$temp[2]);
$this->allCities[]=$c11;
}
}
/* to print the data stored in an array ( without ordering the cities ) */
function displayAllCities()
{
for($i=0;$i<count($this->allCities);$i++)
{
print $this->allCities[$i]->getName()."<br>";
}
}
function rad($x)
{
return $x*pi()/180;
//return $x;
}
/* to calculate the distance between two cities */
function calculatedistance($city1,$city2)
{
$lat1=$city1->getLat();
$lng1=$city1->getLng();
$lat2=$city2->getLat();
$lng2=$city2->getLng();
// Spherical law of cosines: d = acos(sin(lat1).sin(lat2)+cos(lat1).cos(lat2).cos(long2-long1)).R
// R=3437.74677 (nautical miles)
// R=6378.7 (kilometers)
// R=3963.0 (statute miles)
$R = 6371; // earth's mean radius in km
$dLat = $this->rad($lat2 - $lat1);
$dLng = $this->rad($lng2 - $lng1);
$a = sin($dLat/2) * sin($dLat/2) +cos($this->rad($lat1)) * cos($this->rad($lat2)) * sin($dLng/2) * sin($dLng/2);
$c = 2 * atan2(sqrt($a), sqrt(1-$a));
$d = $R * $c;
return $d;
//$distance=acos(sin($lat1)*sin($lat2)+cos($lat1)*cos($lat2).cos($lng2-$lng1))*$R;
//return $distance;
}
/* to calculate the optimal path passing all cities once time for each city */
function findPath($start)
{
for($i=0;$i<count($this->allCities);$i++)
{
if($this->k>count($this->allCities))
break;
if($this->allCities[$i]->getName()==$start &&!$this->allCities[$i]->getVisited())
{
$this->allCities[$i]->setVisited(true);
$this->allCities[$i]->setOrder($this->k);
$lower_index=0;
$lower_dis=1000000;
for($j=0;$j<count($this->allCities);$j++)
{
if(!$this->allCities[$j]->getVisited())
{
$dis=$this->calculatedistance($this->allCities[$j],$this->allCities[$i]);
if($dis<$lower_dis)
{
$lower_index=$j;
$lower_dis=$dis;
}
}
}
$this->k++;
$this->findPath($this->allCities[$lower_index]->getName());
}// enf if
}// end for
}// end function
/* method to display the total distance of the route passign all cities */
function getTotalDistance()
{
return $this->totalDistance;
}
}// end class
//$test=new City("Gaza",1.2,1.50);
$m=new Main();
$m->getCitiesFromTextFile();
//$m->displayAllCities();
$m->findPath("Beijing");
$m->sortArray();
$m->displayAllCities();
$m->getCitiesFromTextFile();
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有一个实用程序可以实现此目的:
检查手册页以获取更多选项。
还有其他聪明的 sed hack,但最好使用经过尝试和测试的实用程序,而不是使用 sed 进行黑客攻击,人们可能不太了解 sed。
There is a utility for this:
Check the man pages for more options.
There are other clever sed hacks too, but better use a tried and tested utility than hacking with sed, which one may not know that well.
类似于:
=)
EDIT 1
如果您使用的是 Linux - 有一个名为
Kate
的优秀编辑器(转换编码和行结束符)Something like:
=)
EDIT 1
If you are on Linux - there is the good editor named
Kate
(converts encoding and line-end)如果 dos2unix 不可用,todos/fromdos 可能是:
If dos2unix is not available, todos/fromdos might be:
在 *nix 上,无需安装任何东西:
然后,您可以测试您的新文件,如果您愿意,可以替换旧文件
on *nix, without installing anything:
then, you can test your newFile, and replace the old one if you're happy