189 8069 5689

eval函数c语言,C语言eval函数

C语言怎么动态创建函数?

C语言可以通过宏,在你需要的位置宏展开出一个新的函数。

成都创新互联公司是专业的班玛网站建设公司,班玛接单;提供网站设计、网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行班玛网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

例如:

#define XXX(funcname) \

int funcname (int arg1, int arg2) \

{ return arg1 + arg2; }

但这也是编译期就决定好了的,也不能实现运行期动态创建。

C++里面有类似MATLAB eval函数吗

matlab的代码是解释运行的

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

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

matlab 负责解释和执行命令

而eval('str')就是执行str字符串内容的指令

实际上跟你在命令行输入str内容后按回车执行命令是一样的

而c语言运行之前是需要先将代码整体编译再运行的

不存在像matlab一样解释运行的机制,所以没有类似的eval函数

用C语言实现类似的功能就比较麻烦了

c++ eval

C和C++是没有eval的,不过可以自己实现一个。一般的算式计算还是比较好实现的。C++的优势在别的地方,仅仅是没有类似eval的函数并不代表C语言就是一门差语言。

编写一段c程序,实现多项式的计算,谁能帮我呀,

我可以写个简单的只有+ - * / 幂和括号的多项式的计算

/*

主要是堆栈的应运,把多项式转换为后缀表达式,再计算后缀表达式的值

*/

//中缀表达式转化为后缀表达式的程序

#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函数,这个函数只是计算算法的C语言实现,同时考虑了特定的环境和数据的输入*/

/*输出格式。eval调用了一个isdigit函数,它用来判断其参数是不是一个操作数。在函数eval及其调用的*/

/*pop和push例程中都使用了下面的堆栈说明。eval函数在声明后给出*/

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

/*下面的函数在许多C系统中都被预定义为一个宏*/

int isdigit(char symb)

{

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

}

/*函数oper首先检查它的第一个参数是不是一个合法的运算符,如果是,则用另外两个参数来决定运算结果*/

/*对于求幂运算,使用了math.h中定义的函数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语言实现MATLAB中eval函数的功能

eval_r()函数的功能就是将括号内的字符串视为语句并运行

例如: eval_r('y1=sin(2)')就是相当于在matlab命令窗口输入了y1=sin(2)这条命令。

matlab中eval函数的详细用法是什么?

eval()函数的用法就是将括号内的字符串视为语句并运行,具体如下:

1、假如我要对a1,a2,a3,a4,……,a100分别赋予1,2,3,……,100,这时eval就发挥作用了。

for i=1:100

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

end

2、再比如批量存数据或图片文件等等。

那么开始提到的例子也就好解释了。

注意:eval中的中括号在两个以上字符串出现时一定要有,起连接作用。

如:

input:[‘hello’ ‘world’]

output:helloworld

扩展资料:

eval命令在Linux下的应用非常广泛

1、eval会把赋值语句中双引号之间的内容直接赋给‘=’前的变量,而不是当作字符串赋给变量

例如:

version="(2 4)" 是将字符串赋值给 version;

eval version="(2 4)" 执行时变成了 version=(2 4), 是把数组 (2 4) 赋值给 version

2、当在一个赋值语句的前面加上 eval 时,它就会将 后面表达式中以 $ 开头的所有变量进行整体替换

例如:

var1="1234 4556"

tmp=var

eval test="$"$tmp"1"

echo   $test

那么就会在屏幕上显示出 :

1234 4556


网站栏目:eval函数c语言,C语言eval函数
当前地址:http://cdxtjz.cn/article/phgiip.html

其他资讯