eval函數(shù)c語言,C語言eval函數(shù)

C語言怎么動態(tài)創(chuàng)建函數(shù)?

C語言可以通過宏,在你需要的位置宏展開出一個新的函數(shù)。

成都創(chuàng)新互聯(lián)公司是專業(yè)的班瑪網(wǎng)站建設(shè)公司,班瑪接單;提供網(wǎng)站設(shè)計、網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行班瑪網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!

例如:

#define XXX(funcname) \

int funcname (int arg1, int arg2) \

{ return arg1 + arg2; }

但這也是編譯期就決定好了的,也不能實現(xiàn)運行期動態(tài)創(chuàng)建。

C++里面有類似MATLAB eval函數(shù)嗎

matlab的代碼是解釋運行的

所以可以在命令行用交互式地一句一句輸入命令和運行命令

本身你在命令行輸入的命令就是一串字符串

matlab 負責解釋和執(zhí)行命令

而eval('str')就是執(zhí)行str字符串內(nèi)容的指令

實際上跟你在命令行輸入str內(nèi)容后按回車執(zhí)行命令是一樣的

而c語言運行之前是需要先將代碼整體編譯再運行的

不存在像matlab一樣解釋運行的機制,所以沒有類似的eval函數(shù)

用C語言實現(xiàn)類似的功能就比較麻煩了

c++ eval

C和C++是沒有eval的,不過可以自己實現(xiàn)一個。一般的算式計算還是比較好實現(xiàn)的。C++的優(yōu)勢在別的地方,僅僅是沒有類似eval的函數(shù)并不代表C語言就是一門差語言。

編寫一段c程序,實現(xiàn)多項式的計算,誰能幫我呀,

我可以寫個簡單的只有+ - * / 冪和括號的多項式的計算

/*

主要是堆棧的應(yīng)運,把多項式轉(zhuǎn)換為后綴表達式,再計算后綴表達式的值

*/

//中綴表達式轉(zhuǎn)化為后綴表達式的程序

#include stdio.h

#include ctype.h

#include stdlib.h

typedef struct node

{

char data; int code; int pri;

struct node *link;

}NODE;

struct Tb1

{

char data; int code; int pri;

}opchTb1[]={{'*',1,4},{'/',2,4},{'+',3,2},{'-',4,2},{'(',5,5},{')',6,1},{'\0',7,0},{'#',-1,0}};

NODE *optop;

char num[200], *numtop;

char expStr[200];

void push(char x,int c,int p,NODE **toppt)

{

NODE *q=(NODE *)malloc(sizeof(NODE));

q-data=x;

q-code=c;

q-pri=p;

q-link=*toppt;

*toppt=q;

}

int pop(char *op,int *cp, NODE **toppt)

{

NODE *q=*toppt;

if(*toppt==NULL) return 1;

*op=q-data;

*cp=q-code;

*toppt=q-link;

free(q);

return 0;

}

int expr(char *pos)

{

struct Tb1 *op;

char sop;

int type,code,n,m,i,c;

optop=NULL;

numtop=num;

n=m=0;

c=' ';

push('#',0,0,*optop);

while(1){

while(c==' '||c=='\t') c=*pos++;

if(isalpha(c)){

*numtop++=' ';

while(isalpha(c)||isdigit(c)) {*numtop++=c;c=*pos++;}

if(m) return 1;

m=1;

continue;

}

else {

for(i=0;opchTb1[i].code!=-1opchTb1[i].data!=c;i++)

if(opchTb1[i].code==-1) return 3;

op=opchTb1.[i];

type=opchTb1.[i].code;

c=*pos++;

}

if(type5){

if(m!=1) return 1;

m=0;

}

if(type==5) n++;

if(type==6){

if(n--==0) return 2;

if(op-prioptop-pri)

if(op-data=='(') push(op-code,1,*optop);

else push(op-data,op-code,op-pri,*optop);

else{

while(optop!=NULLop-pri=optop-pri) {

pop(sop,code,optop);

if(code5code0) {

*numtop++=' ';

*numtop++=sop;

}

}

if(op-data=='\0') return(n!=0||(m!=1numtopnum))?4:(*numtop='\0');

else if(op-data!=')') push(op-data,op-code,op-pri,optop);

}

}

}

void main()

{

int d;

printf("please input the string!\n");

gets(expStr);

if((d=expr(expStr))==0) printf("the postfix string is:%s\n",num);

else printf("The string error! the error is:%d\n",d);

getch();

}

//后綴表達式的計算

#include stdio.h

#include stdlib.h

#include math.h

#define MAXCOLS 80

#define TRUE 1

#define FLASE 0

double eval(char[]);

double pop(struct stack *ps);

void push(struct stack *ps,double x);

int empty(struct stack *ps);

int isdigit(char);

double oper(int,double,double);

void main()

{

char expr[MAXCOLS];

int position=0;

printf("\nPlease input the string:");

while((expr[position++]=getchar())!='\n');

expr[--position]='\0';

printf("%s%s","the original postfix expression is",expr);

printf("\n%f",eval(expr));

getch();

} /*end main*/

/*程序的主要部分eval函數(shù),這個函數(shù)只是計算算法的C語言實現(xiàn),同時考慮了特定的環(huán)境和數(shù)據(jù)的輸入*/

/*輸出格式。eval調(diào)用了一個isdigit函數(shù),它用來判斷其參數(shù)是不是一個操作數(shù)。在函數(shù)eval及其調(diào)用的*/

/*pop和push例程中都使用了下面的堆棧說明。eval函數(shù)在聲明后給出*/

struct stack{

int top;

double items[MAXCOLS];

};

double eval(char expr[])

{

int c,position;

double opnd1,opnd2,value;

struct stack opndstk;

opndstk.top=-1;

for(position=0;(c=expr[position])!='\0';position++)

if(isdigit(c)) /*operand--convert the character representation of the digit into double and*/

/*push it onto the stack*/

push(opndstk,(double)(c-'0'));

else{ /*operator*/

opnd2=pop(opndstk);

opnd1=pop(opndstk);

value=oper(c,opnd1,opnd2);

push(opndstk,value);

} /*end else*/

return(pop(opndstk));

}/*end eval*/

/*下面的函數(shù)在許多C系統(tǒng)中都被預(yù)定義為一個宏*/

int isdigit(char symb)

{

return(symb='0'symb='9');

}

/*函數(shù)oper首先檢查它的第一個參數(shù)是不是一個合法的運算符,如果是,則用另外兩個參數(shù)來決定運算結(jié)果*/

/*對于求冪運算,使用了math.h中定義的函數(shù)pow(op1,op2)。*/

double oper(int symb,double op1,double op2)

{

switch(symb){

case '+' : return(op1+op2);

case '-' : return(op1-op2);

case '*' : return(op1*op2);

case '/' : return(op1/op2);

case '$' : return(pow(op1,op2));

default:printf("%s","illegal operation");

exit(1);

}/*end switch*/

}/*end oper*/

double pop(struct stack *ps)

{

if(empty(ps)){

printf("%s","stack underflow");

exit(1);

} /*end if*/

return(ps-items[ps-top--]);

}/*end pop*/

void push(struct stack *ps,double x)

{

ps-items[++(ps-top)]=x;

return;

} /*end push*/

int empty(struct stack *ps)

{

return(ps-top==-1);

}/*end empty*/

如何用C語言實現(xiàn)MATLAB中eval函數(shù)的功能

eval_r()函數(shù)的功能就是將括號內(nèi)的字符串視為語句并運行

例如: eval_r('y1=sin(2)')就是相當于在matlab命令窗口輸入了y1=sin(2)這條命令。

matlab中eval函數(shù)的詳細用法是什么?

eval()函數(shù)的用法就是將括號內(nèi)的字符串視為語句并運行,具體如下:

1、假如我要對a1,a2,a3,a4,……,a100分別賦予1,2,3,……,100,這時eval就發(fā)揮作用了。

for i=1:100

eval(['a' num2str(i) '=' num2str(i)]);

end

2、再比如批量存數(shù)據(jù)或圖片文件等等。

那么開始提到的例子也就好解釋了。

注意:eval中的中括號在兩個以上字符串出現(xiàn)時一定要有,起連接作用。

如:

input:[‘hello’ ‘world’]

output:helloworld

擴展資料:

eval命令在Linux下的應(yīng)用非常廣泛

1、eval會把賦值語句中雙引號之間的內(nèi)容直接賦給‘=’前的變量,而不是當作字符串賦給變量

例如:

version="(2 4)" 是將字符串賦值給 version;

eval version="(2 4)" 執(zhí)行時變成了 version=(2 4), 是把數(shù)組 (2 4) 賦值給 version

2、當在一個賦值語句的前面加上 eval 時,它就會將 后面表達式中以 $ 開頭的所有變量進行整體替換

例如:

var1="1234 4556"

tmp=var

eval test="$"$tmp"1"

echo ? $test

那么就會在屏幕上顯示出 :

1234 4556

分享標題:eval函數(shù)c語言,C語言eval函數(shù)
文章鏈接:http://muchs.cn/article8/phgiip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司、做網(wǎng)站微信公眾號、虛擬主機靜態(tài)網(wǎng)站、搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營