c++、php、java、python、javascript对比,同一功能用5种编程语言写一遍,谁才是最好的编程语言?
感觉编程语言里面,大家使用最多的就是这几种了吧,最火的是JavaScript,python好像说是无所不能,php没悬念还是世界上最好的编程语言,java用的人应该最多吧,c++吗,据说比较难,所以就是逼格最高的语言吧。
其实这几种语言,除了python,其他几个还都有共通之处,都是带花括号({})的C系语法,而python吧,不带花括号,感觉和之前的VBscript有点像,废话不多说,今天就用这5种语言写同一个功能,我一会分别把代码粘出来,大家对语法做一下比较,看看更喜欢那种?
昨天闲聊群里有人发了一串JavaScript代码,功能呢就是在控制台输出用字符拼出的字符画“V ME 50”,v me 50算是最近很火的一个梗了,运行效果如下图
代码如下:
const arr = [ [1, 9, 12, 20, 23, 24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 36, 39, 40, 41, 42, 43, 44], [2, 8, 12, 13, 19, 20, 23, 31, 39, 44], [3, 7, 12, 14, 18, 20, 23, 24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 36, 39, 44], [4, 6, 12, 15, 17, 20, 23, 36, 39, 44], [5, 5, 12, 16, 20, 23, 24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 36, 39, 40, 41, 42, 43, 44],];function demo(arr) { let str = ''; for (let i = 0; i < arr.length; i++) { for (j = 0; j < 50; j++) { if (arr[i].indexOf(j) > -1) { str += '█'; } else { str += ' '; } } str += '\n'; } return str;}console.log(demo(arr));
看着挺有意思吧,然后我把这段代码移植到了C++上,代码和效果如下:
#include #include #include using namespace std;class vMe50{private:template int arrLen(T& arr){return sizeof(arr)/sizeof(arr[0]);}public:vMe50(){string str=this->demo();cout << str;}string demo(){vector > arr;int a0[]={1, 9, 12, 20, 23, 24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 36, 39, 40, 41, 42, 43, 44};int a1[]={2, 8, 12, 13, 19, 20, 23, 31, 39, 44};int a2[]={3, 7, 12, 14, 18, 20, 23, 24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 36, 39, 44};int a3[]={4, 6, 12, 15, 17, 20, 23, 36, 39, 44};int a4[]={5, 5, 12, 16, 20, 23, 24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 36, 39, 40, 41, 42, 43, 44};vector b0(a0,a0+this->arrLen(a0));vector b1(a1,a1+this->arrLen(a1));vector b2(a2,a2+this->arrLen(a2));vector b3(a3,a3+this->arrLen(a3));vector b4(a4,a4+this->arrLen(a4));arr.push_back(b0);arr.push_back(b1);arr.push_back(b2);arr.push_back(b3);arr.push_back(b4);string str;for(short i=0;i
效果一样,但代码麻烦了不少,毕竟C++是强类型语言,处理不规则数组不像JavaScript那样轻松。
然后我又把代码移植到了世界上最好的编程语言php上,大家看下优不优雅,代码和效果如下
demo(); } private function demo(){ $arr=[ [1, 9, 12, 20, 23, 24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 36, 39, 40, 41, 42, 43, 44], [2, 8, 12, 13, 19, 20, 23, 31, 39, 44], [3, 7, 12, 14, 18, 20, 23, 24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 36, 39, 44], [4, 6, 12, 15, 17, 20, 23, 36, 39, 44], [5, 5, 12, 16, 20, 23, 24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 36, 39, 40, 41, 42, 43, 44],]; $str=""; foreach($arr as $key=>&$value){ foreach(range(0,50) as $k=>&$v){ if(in_array($v, $value)){ $str.="8"; }else{ $str.=" "; } } $str.="\n"; } return $str; }}new vMe50();
确实比C++版简单不少,毕竟php的array实现了各种类型的数组。
然后我又把代码用java重写一遍,代码和效果如下:
import java.util.Arrays;import java.util.List;import java.util.LinkedList;public class vMe50{ public static void main(String[] args){ System.out.print( demo() ); } private static String demo(){ List > arr=new LinkedList<>(); arr.add(new LinkedList<>(Arrays.asList(1, 9, 12, 20, 23, 24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 36, 39, 40, 41, 42, 43, 44))); arr.add(new LinkedList<>(Arrays.asList(2, 8, 12, 13, 19, 20, 23, 31, 39, 44))); arr.add(new LinkedList<>(Arrays.asList(3, 7, 12, 14, 18, 20, 23, 24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 36, 39, 44))); arr.add(new LinkedList<>(Arrays.asList(4, 6, 12, 15, 17, 20, 23, 36, 39, 44))); arr.add(new LinkedList<>(Arrays.asList(5, 5, 12, 16, 20, 23, 24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 36, 39, 40, 41, 42, 43, 44))); String str=""; for(int i=0;i
最后就再用无所不能的python语言重写一遍吧,代码和效果如下:
class vMe50: def __init__(self): print(self.demo()) def demo(self): arr=( (1, 9, 12, 20, 23, 24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 36, 39, 40, 41, 42, 43, 44), (2, 8, 12, 13, 19, 20, 23, 31, 39, 44), (3, 7, 12, 14, 18, 20, 23, 24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 36, 39, 44), (4, 6, 12, 15, 17, 20, 23, 36, 39, 44), (5, 5, 12, 16, 20, 23, 24, 25, 26, 27, 28, 31, 32, 33, 34, 35, 36, 39, 40, 41, 42, 43, 44) ) str="" for i in arr: for j in range(0,50): if j in i: str+="8" else: str+=" " str+="\n"; return strv=vMe50();
虽然我个人不喜欢python,因为他没有花括号,但感觉他确实diao,这几个版本里代码量最小,甚至比JavaScript还简单。
以上就是c++、php、java、python、javascript这五种编程语言语法的对比,到底谁才是最好的编程语言?
来源地址:https://blog.csdn.net/sdxjwkq01/article/details/127800299
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341