欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

用C語言模仿Python函數的實例

系統 1860 0

首先得說明一點,C 語言不是函數式編程語言,要想進行完全的函數式編程,還得先寫個虛擬機,然后再寫個解釋器才行(相當于 CPython )。

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

我們有如下的 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函數:

            
/* 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)


                  
                
              
            
          

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

以上這篇用C語言模仿Python函數的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产网址在线观看 | 国产综合视频在线 | 国内成人啪啪网站 | 激情久久av一区av二区av三区 | 一级一级 a爱片免费视频 | 欧美一级做一级做片性十三 | 日本高清免费网站 | 爱爱无遮挡| 亚洲精品国偷拍自产在线观看蜜桃 | 亚洲一区中文字幕 | 日韩欧美日本 | 国产成人综合在线观看 | 夜本色 | 啪啪免费网站 | 另类国产ts人妖高潮系列视频 | 欧美男女网站 | 男女爽爽无遮挡午夜动态图 | 欧美在线观看a | 91视频观看 | 中国一级毛片在线视频 | 日韩 欧美 自拍 | 男女性刺激爽爽免费视频 | 91精品成人免费国产 | 精品国产一区二区三区性色av | 欧美精品国产第一区二区 | 99精品久久秒播无毒不卡 | 一区二区三区欧美在线观看 | 激情五月色婷婷 | 精品欧美一区二区精品久久久 | 国产欧美在线视频 | 排球少年第五季樱花动漫免费观看 | 国产精品久久嫩一区二区免费 | 电家庭影院午夜 | 国产精品一区二区久久久久 | 日韩视频不卡 | 猛h辣h高h文湿快穿np | 99精品国产一区二区青青牛奶 | 色婷婷综合久久久久中文一区二区 | 毛片基地免费视频a | 日韩在线观看你懂的 | 狠狠干2020 |