有趣啊!各种编程语言实现 2 + 2 = 5

开发 后端 开发工具
今天在 stackexchange 上看到一篇非常有趣的帖子,是关于如何用各种编程语言实现 2 + 2 = 5 的,2 + 2 怎么会等于 5 呢?我一开始也很不解,不过看了下面各种编程语言实现的方法,我震惊了。

今天在 stackexchange 上看到一篇非常有趣的帖子,是关于如何用各种编程语言实现 2 + 2 = 5 的,2 + 2 怎么会等于 5 呢?我一开始也很不解,不过看了下面各种编程语言实现的方法,我震惊了,让我又一次相信人类真是一种不可思议的生物。

1、JAVA

  1. import java.lang.reflect.Field;  
  2.  
  3. public class Main {  
  4.     public static void main (String[] args) throws Exception {  
  5.         Class cache = Integer.class.getDeclaredClasses ()[0];  
  6.         Field c = cache.getDeclaredField ("cache");  
  7.         c.setAccessible (true);  
  8.         Integer[] array = (Integer[]) c.get (cache);  
  9.         array[132] = array[133];  
  10.  
  11.         System.out.printf ("%d",2 + 2);  
  12.     }  

输出:

5

2、C

 

  1. int main () {  
  2.     char __func_version__[] = “5″; // For source control char b[]=”2″, a=2;  
  3.     printf (“%d + %s = %s\n”, a, b, a+b);  
  4.     return 0;  

 

3、C (Linux, gcc 4.7.3)

 

  1. #include <stdio.h>  
  2. int main (void)  
  3. {  
  4.     int a=3, b=2;  
  5.     printf (“%d + %d = %d”, –a, b, a+b);  

 

4、Haskell

 

  1. λ> let 2+2=5 in 2+2 5 

 

5、BBC BASIC

 

  1. MODE 6  
  2. VDU 23,52,254,192,252,6,6,198,124,0  
  3. PRINT  
  4. PRINT “2+2=”;2+2  
  5. PRINT “2+3=”;2+3 

 

6、Python

 

  1. >>> patch = '\x312\x2D7' >>> import ctypes;ctypes.c_int8.from_address (id (len (patch)) +8) .value=eval (patch)  
  2. >>> 2 + 2 5 

 

7、JavaScript

 

  1. g = function () {  
  2.   H = 3 return H + H  
  3. }  
  4.  
  5. f = function () {  
  6.   Η = 2 return Η + H  
  7. }  
  8.  
  9. // 3 + 3 = 6 alert (g())  
  10. // 2 + 2 = 5 alert (f()) 

 

8、Bash

9、PHP

 

  1. echo ’2 + 2 = ‘ . (2 + 2 === 4 ? 4 : 2 + 2 === 5 ? 5 : ‘dunno’); 

 

10、Perl

 

  1. # Generic includes use strict;  
  2. use warnings;  
  3. use 5.010;  
  4. use Acme::NewMath;  
  5.  
  6. # Ok, time to begin the real program. if (2 + 2 == 5) {  
  7.     say 5;  
  8. }  
  9. else {  
  10.     say "Dunno...";  

 

11、C#

 

  1. static void Main (string[] args)  
  2. {  
  3.     var x = 2;  
  4.     var y = 2;  
  5.  
  6.     if (1 == 0) ;  
  7.     {  
  8.         ++x;  
  9.     }  
  10.  
  11.     Console.WriteLine (x + y);  

 

12、C++

 

  1. #include <iostream>  
  2.  
  3. class Int  
  4. {  
  5. public:  
  6.     Int (const int& a) : integ (a) {}  
  7.  
  8.     friend std::ostream& operator<<(std::ostream& oss, const Int& rhs)  
  9.     {  
  10.         return oss << rhs.integ;  
  11.     }  
  12.     int operator+(Int o)  
  13.     {  
  14.         if(integ == 2 && o.integ == 2)  
  15.             return integ+o.integ+1;  
  16.         return integ+o.integ;  
  17.     }  
  18.  
  19. private:  
  20.     int integ;  
  21. };  
  22.  
  23. int main ()  
  24. {  
  25.     Int two = 2;  
  26.     std::cout << two << " + " << two << " = " << two + two;  

 

各位有什么补充的,发挥你的想象吧,评论中告诉我们。

原文链接:http://news.html5tricks.com/2-2-5-with-all-language.html

本文作者:html5tricks – 蒋丽丽

责任编辑:林师授 来源: html5tricks
相关推荐

2014-03-31 15:26:45

Git代码托管

2021-04-12 18:44:47

编程语言合子

2020-07-24 09:40:04

C语言OOP代码

2021-02-19 11:55:36

C语言MD5加密

2015-07-20 11:32:07

编程语言

2011-08-05 17:54:33

Cocoa Touch 多语言

2020-08-12 08:56:30

代码凯撒密码函数

2013-02-21 17:02:00

C语言

2022-11-01 18:29:25

Go语言排序算法

2023-05-08 07:55:05

快速排序Go 语言

2013-02-25 09:31:35

Web编程语言

2020-02-14 09:19:12

编程语言JavaPython

2011-04-08 11:15:20

编程语言编程游戏开发

2012-02-07 08:48:00

编程语言排行榜

2009-02-18 09:11:22

编程语言排行榜Tiobe

2011-05-30 15:00:06

编程语言

2012-03-13 10:40:58

Google Go

2022-05-19 14:14:26

go语言限流算法

2010-03-29 17:56:20

Nginx反向代理

2020-10-15 10:38:35

C语言状态模型
点赞
收藏

51CTO技术栈公众号