亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

用C語言模仿Python函數(shù)的一種簡(jiǎn)單實(shí)現(xiàn)方法

系統(tǒng) 1520 0

首先得說明一點(diǎn),C 語言不是函數(shù)式編程語言,要想進(jìn)行完全的函數(shù)式編程,還得先寫個(gè)虛擬機(jī),然后再寫個(gè)解釋器才行(相當(dāng)于 CPython )。

下面我們提供一個(gè)例子,說明 C 語言函數(shù)可以“適度地模仿” Python 函數(shù)。

我們有如下的 Python 程序:

            
def line_conf(a, b):
  def line(x):
    return a*x + b
  return line
line1 = line_conf(1, 1)
line2 = line_conf(4, 5)
print(line1(5), line2(5))
          

我們?cè)贑程序中適度地模擬其中的line_conf函數(shù):

            
/* MIT License
Copyright (c) 2017 Yuandong-Chen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. */
///////////////////////////////////////////////////////////////////////////////
// Note: The C program is almost equivalent to the Python program as follows:
// def line_conf(a, b):
//   def line(x):
//     return a*x + b
//   return line
//
// line1 = line_conf(1, 1)
// line2 = line_conf(4, 5)
// print(line1(5), line2(5))
#include 
            
               
#include 
              
                 
#include 
                
                   
#include 
                  
                    
typedef int Func();
Func *line_conf(int x, int y,...)
{ 
  va_list ap; 
  va_start(ap, y);
  asm volatile(
    "push %%eax\n\t"
    "subl $40, %%esp\n\t"
    "movl 8(%%ebp), %%eax\n\t"
    "movl %%eax, -36(%%ebp)\n\t"
    "movl 12(%%ebp), %%eax\n\t"
    "movl %%eax, -40(%%ebp)\n\t"
    "addl $40, %%esp\n\t"
    "pop %%eax\n\t"
    :::"memory"
    );
if(va_arg(ap,int) == 1){
LINE:
  asm volatile(
    "push %%ebp\n\t"
    "movl %%esp, %%ebp\n\t"
    "movl 8(%%ebp), %%eax\n\t"
    "imul -36(%%ebp), %%eax\n\t"
    "addl -40(%%ebp), %%eax\n\t"
    "movl %%ebp, %%esp\n\t"
    "pop %%ebp\n\t"
    "ret\n\t"
    :::"memory","%eax"
    );
}  
__END: 
  va_end(ap);
  return (Func *)(&&LINE);
}
int main(int argc, const char *argv[]){ 
  printf("====TEST START====\n");
  printf("34*234+6 ?= %d\n",line_conf(34,6)(234));
  printf("1*3+2 ?= %d; 324*65+3 ?= %d; 13*66+2 ?= %d\n",line_conf(1,2)(3),line_conf(324,3)(65),line_conf(13,2)(66));
  int fd = line_conf(1,6)(4);
  Func *fun = line_conf(3,3);
  int a = 1; // Limited point
  printf("3*3+3 ?= %d; 1*4+6 ?= %d\n",fun(3),fd);
  printf("====TEST END====\n");
  return 0; 
}
// Compile it by the following command:
// gcc -m32 -O0 -fno-stack-protector CFunctional.c; ./a.out
// The terminal output should looks like:
// ====TEST START====
// 34*234+6 ?= 7962
// 1*3+2 ?= 5; 324*65+3 ?= 21063; 13*66+2 ?= 860
// 3*3+3 ?= 12; 1*4+6 ?= 10
// ====TEST END====
//Note: The limitation happens between line 86 and line 88, we cannot insert any function here
// whose stack is larger than 40 bytes.(Why is 40? check the inline assembler language)
                  
                
              
            
          

結(jié)果在MacOSX和Ubuntu上(i386)都能通過簡(jiǎn)單的測(cè)試。但是可以看到,僅僅是簡(jiǎn)單的模擬,我們也得用到大量(按比例)的匯編,可讀性很差,而且模擬程度非常有限,代碼長(zhǎng)度也更長(zhǎng)。相反,對(duì)于這類一般功能的函數(shù),Python可以很容易地模擬C語言的函數(shù),而且模擬程度很高。

以上所述是小編給大家介紹的用C語言模仿Python函數(shù)的一種簡(jiǎn)單實(shí)現(xiàn)方法,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 中文字幕综合网 | 成人免费视频在线 | 在线国产区 | 国产va| 欧美激情伦妇在线观看 | 五十路一区二区三区视频 | 国产成人亚洲综合在线 | 热99re久久精品这里都是免费 | 日本欧美高清 | 国内精品不卡一区二区三区 | 久久频这里精品香蕉久久 | 四虎地址8848 | 国产成人免费a在线视频色戒 | 妞干网中文字幕 | 国产麻豆精品aⅴ免费观看 国产麻豆精品hdvideoss | 天天色天天射天天操 | 搜毛片 | 国产亚洲综合精品一区二区三区 | 欧美精品亚洲精品日韩经典 | 欧美成人国产 | 日本又黄又爽又色的视频免费 | 日韩国产成人精品视频 | 日韩美女强理论片 | 精品一久久香蕉国产线看观看下 | 精品免费视在线观看 | 日韩五月天| 99爱视频精品免视看 | 99精品视频在线观看 | 精品视频在线免费播放 | 波多野结衣一区免费作品 | 天海翼精品久久中文字幕 | 嫩小xxxxx性bbbbb孕妇 | 亚洲精品欧美精品国产精品 | 欧美一级刺激毛片 | 国产乱仑 | aaaaa级毛片 aaaa级毛片欧美的 | 国产一级免费在线观看 | m3u8久久国产精品影院 | 人人爱人人做 | 亚洲欧美久久精品一区 | 欧美综合亚洲图片综合区 |