欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

C語言中有eval函數(shù)嗎 c++eval

幫我寫一個c語言計(jì)算器

#include stdio.h

成都創(chuàng)新互聯(lián)從2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元二七做網(wǎng)站,已為上家服務(wù),為二七各地企業(yè)和個人服務(wù),聯(lián)系電話:028-86922220

#include stdlib.h

#include string.h

int errorflag;

typedef struct

{

double array[100];

int top;

}stack;

int isNumeric(char s) // 判斷是不是數(shù)字

{

if(s='0's='告叢9' || s=='.')

return 1;

return 0;

}

int empty(stack* s) // 判斷stack是不是為空

{

return s-top == 0;

}

void init(stack *s) // 初始化 stack

{

s-top = 0;

}

void push(stack* s, double a) // 入stack

{

s-array[s-top++] = a;

}

double pop(stack* s) // 出stack

{

if(!empty(s))

return s-array[--s-top];

}

double eval(char* s, char* e) // 解析算式

{

stack t;

double temp,temp2;

char *p, *q, *r;

int flag;

char op = '+';

init(t);

push(t,0);

while(se *s==' ') s++; // 清空前導(dǎo) 0

while(se *(e-1)==' ') e--; // 清空后導(dǎo) 0

if(s==e) return 0; // 空串 返回 0

p = s;

q = e;

for(;!errorflagpq;p++) // 歷遍整個串

{

if(*p == ' ') continue; // 無視空格

else if(*p=='+' || *p == '-' || *p == '*' || *p == '/')

{

op = *p; // 更換操作納早符

continue;

}

else

{

if(*p == '(') // 遇到左括號

{

r = p+1;

flag = 1;

while(flag)

{

p++;

if(p=e)

{

printf("\nError: ( and ) not matched.\n");

errorflag = 1;

q = s;

while(qe)

{

putchar(*q);

q++;

}

putchar(10);

q = s;

while(q p)

{

putchar(' ');

q++;

}

printf("^\n");

break;

}

if(*p=='(')

flag++;

if(*p==')') // 找到匹配的右括號

flag--;

}

temp = eval(r,p); // 括號內(nèi)調(diào)用eval函數(shù)得到值

}

else if( *p == ')' )

{

printf("\nError: ( and ) not matched.\n");

errorflag = 1;

q = s;

while(qe)

{

putchar(*q);

q++;

}

putchar(10);

q = s;

while(q p)

{

putchar(' ');

q++;

}

printf("^\n");

break;

}

else if(isNumeric(*p)) // 如果是簡單數(shù)字

{

sscanf(p,"%lf",temp); // 利用sscanf掃入數(shù)字

while(isNumeric(*p)) p++; // 指針偏移

p--;

}

else

{

printf("襪茄櫻\nError: character not recognized.\n");

errorflag = 1;

q = s;

while(qe)

{

putchar(*q);

q++;

}

putchar(10);

q = s;

while(q p)

{

putchar(' ');

q++;

}

printf("^\n");

break;

}

if(op=='+') // 根據(jù)操作符入stack

push(t,temp);

else if(op=='-')

push(t,-temp);

else if(op=='*')

{

temp2 = pop(t);

temp2 *= temp;

push(t,temp2);

}

else if(op=='/')

{

if(temp == 0)

{

printf("\nError: divided by zero.\n");

errorflag = 1;

q = s;

while(qe)

{

putchar(*q);

q++;

}

putchar(10);

q = s;

while(q p)

{

putchar(' ');

q++;

}

printf("^\n");

break;

}

temp2 = pop(t);

temp2 /= temp;

push(t,temp2);

}

}

}

if(errorflag)

{

return -1;

}

temp = 0;

while(!empty(t)) // 統(tǒng)計(jì)stack內(nèi)所有數(shù)之和

{

temp2 = pop(t);

temp += temp2;

}

return temp; // 返回總和

}

int main()

{

char s[1000];

int len;

double result;

while(gets(s))

{

errorflag = 0;

len = strlen(s);

result = eval(s,s+len);

if(!errorflag)

printf("%lf\n",result);

}

}

新聞標(biāo)題:C語言中有eval函數(shù)嗎 c++eval
網(wǎng)站地址:http://chinadenli.net/article8/dspgiop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化網(wǎng)站收錄定制開發(fā)品牌網(wǎng)站建設(shè)網(wǎng)站導(dǎo)航網(wǎng)站排名

廣告

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

成都定制網(wǎng)站網(wǎng)頁設(shè)計(jì)