eval函数c语言 eval函数用法

编写一段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*/

vc中有类似matlab中eval语句的函数吗

matlab的代码是解释运行的

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

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

matlab 负责解释和执行命令

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

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

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

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

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

如何用C语言实现MATLAB中eval函数的功能

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

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

c++ eval

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


网页名称:eval函数c语言 eval函数用法
标题链接:http://pwwzsj.com/article/dossihd.html