国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

用C語言模仿Python函數的一種簡單實現方法

2020-01-26 14:09:10
字體:
來源:轉載
供稿:網友

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

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

我們有如下的 Python 程序:

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

我們在C程序中適度地模擬其中的line_conf函數:

/* MIT LicenseCopyright (c) 2017 Yuandong-ChenPermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, 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 THESOFTWARE. *////////////////////////////////////////////////////////////////////////////////// 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 <stdio.h> #include <stdlib.h> #include <unistd.h> #include <stdarg.h>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函數的一種簡單實現方法,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 平山县| 稷山县| 岳西县| 永胜县| 盖州市| 成都市| 天长市| 子长县| 东明县| 琼结县| 湘西| 瑞金市| 湘乡市| 景洪市| 和政县| 奇台县| 格尔木市| 都安| 兴山县| 玉环县| 三门县| 丘北县| 永安市| 沁水县| 凯里市| 巴林左旗| 定安县| 灵石县| 诏安县| 曲靖市| 舞阳县| 定结县| 嘉黎县| 桃园市| 芜湖县| 甘德县| 衡东县| 扎囊县| 曲靖市| 贞丰县| 韶关市|