定制网站感觉编程语言里面,定制网站大家使用最多的就是这几种了吧,最火的是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 += '';
- }
- return str;
- }
- console.log(demo(arr));
看着挺有意思吧,然后我把这段代码移植到了C++上,代码和效果如下:
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- class vMe50{
- private:
- template <typename T>
- int arrLen(T& arr){
- return sizeof(arr)/sizeof(arr[0]);
- }
- public:
- vMe50(){
- string str=this->demo();
- cout << str;
- }
- string demo(){
- vector<vector<int> > 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<int> b0(a0,a0+this->arrLen(a0));
- vector<int> b1(a1,a1+this->arrLen(a1));
- vector<int> b2(a2,a2+this->arrLen(a2));
- vector<int> b3(a3,a3+this->arrLen(a3));
- vector<int> 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<arr.size();i++){
- for(short j=0;j<50;j++){
- if(count(arr[i].begin(), arr[i].end(), j)){
- str+="0";
- }else{
- str+=" ";
- }
- }
- str+="\";
- }
- return str;
- }
- };
- int main(){
- vMe50* v=new vMe50();
- delete v;
- return true;
- }
效果一样,但代码麻烦了不少,毕竟C++是强类型语言,处理不规则数组不像JavaScript那样轻松。
然后我又把代码移植到了世界上最好的编程语言php上,大家看下优不优雅,代码和效果如下
- <?php
- namespace vMe50;
- class vMe50{
- public function __construct(){
- echo $this->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.="\";
- }
- 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<List<Integer> > 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<arr.size();i++){
- for(int j=0;j<50;j++){
- if(arr.get(i).contains(j)){
- str+="8";
- }else{
- str+=" ";
- }
- }
- str+="\";
- }
- return str;
- }
- }
最后就再用无所不能的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+="\";
- return str
- v=vMe50();
虽然我个人不喜欢python,因为他没有花括号,但感觉他确实diao,这几个版本里代码量最小,甚至比JavaScript还简单。
以上就是c++、php、java、python、javascript这五种编程语言语法的对比,到底谁才是最好的编程语言?