2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > C语言科学计算器思路 大神教你如何用C语言实现科学计算器

C语言科学计算器思路 大神教你如何用C语言实现科学计算器

时间:2022-03-15 18:50:06

相关推荐

C语言科学计算器思路 大神教你如何用C语言实现科学计算器

i++;

*len=i;

return temp;

}

/*功能:翻译操作数

* 如果运算符非法,则返回0,合法则返回非零标志

*/

int translateopt(char *p,int *len)

{

char fu[NUM_OPT][LEN_OPT]={"+","-","*","/","^","(",")","sin","cos","tan","asin","acos","atan","ln","lg","="};

int fu_int[NUM_OPT]={'+','-','*','/','^','(',')','s','c','t','s'+'a','c'+'a','t'+'a','n','g','='};

int i=0;

for(i=0;itp==Opt&&n->opt=='-')

OPRD[db]=0.0,db++; //Push(&OPRD,0.0);

while(1)

{

//判断前面有没有负号

if(n->tp==Opd)

{

OPRD[db]=n->opd,db++,n++; //Push(&OPRD,n->opd),n++;

}

else

{

//双目运算符

if(prid(n->opt))

{

//if(!GetTop(OPRT,&top))

if(tb)

top=OPRT[tb-1];

if(!tb)

{

if(n->opt=='='||n->opt==')')

{

if(n->opt==')')

{

bb--; //if(!Pop(&brackets,&temp))

if(bb<0)

{

printf("Bracket does not match!");

return 0;

}

}

break;

}

else

OPRT[tb]=n->opt,tb++,n++; //Push(&OPRT,n->opt),n++;

continue;

}

if(prid(top)==0)

{

//Pop(&OPRD,&a);

//Pop(&OPRT,&o);

db--;

a=OPRD[db];

tb--;

o=OPRT[tb];

if(sclt(o,&a))

{

OPRD[db]=a,db++; //Push(&OPRD,a);

continue;

}

else

return 0;

}

if(prid(top)>=prid(n->opt))

{

//Pop(&OPRD,&b);

//Pop(&OPRD,&a);

//Pop(&OPRT,&o);

db--;

b=OPRD[db];

db--;

a=OPRD[db];

tb--;

o=OPRT[tb];

if(dclt(o,&a,b))

{

OPRD[db]=a,db++; //Push(&OPRD,a);

}

else

return 0;

}

else

{

OPRT[tb]=n->opt,tb++,n++; //Push(&OPRT,n->opt),n++;

}

}

else

{

//单目运算符和括号

double x=0.0;

if(n->opt=='(')

{

brackets[bb]='(',bb++; //Push(&brackets,'(');

if(len=calculate(n+1,&x))

{

OPRD[db]=x,db++; //Push(&OPRD,x);

n+=len,n++;

if(n->tp==Opt&&n->opt==-1)

{

printf("Bracket does not match!");

return 0;

}

}

else

return 0;

}

else

{

OPRT[tb]=n->opt,tb++; //Push(&OPRT,n->opt);

n++;

}

}

}

}

*r=OPRD[db-1];

return n-nb+1;

}

/*功能:单目运算

*/

int sclt(int opr,double *opd)

{

switch(opr)

{

case 's':

*opd=sin(*opd);

break;

case 'c':

*opd=cos(*opd);

break;

case 't':

*opd=tan(*opd);

break;

case 'a'+'s':

if(*opd1)

{

printf("Beyond asin()!");

return 0;

}

else

*opd=asin(*opd);

break;

case 'a'+'c':

if(*opd1)

{

printf("Beyond acos()!");

return 0;

}

else

*opd=acos(*opd);

break;

case 'a'+'t':

if(*opd>-3.141592654/2&&*opd<3.141592654/2)

*opd=atan(*opd);

else

{

printf("Beyond atan()!");

return 0;

}

break;

case 'n':

if(*opd>0)

*opd=log(*opd);

else

{

printf("Beyond ln()!");

return 0;

}

break;

case 'g':

if(*opd>0)

*opd=log10(*opd);

else

{

printf("Beyond lg()!");

return 0;

}

break;

}

return 1;

}

/*功能:双目运算

*/

int dclt(int opr,double *opd1,double opd2)

{

switch(opr)

{

case '+':

*opd1 = *opd1+opd2;break;

case '-':

*opd1 = *opd1-opd2;break;

case '*':

*opd1 = (*opd1)*opd2;break;

case '/':

if(opd2>pow(0.1,8)||opd2<0-pow(0.1,8))

*opd1 = (*opd1)/opd2;

else

{

printf("Error 1/0 !");

return 0;

}

break;

case '^':

*opd1 = pow(*opd1,opd2);break;

}

return 1;

}

/*功能:判断优先级

*/

int prid(int c)

{

switch(c)

{

case '+':

case '-':

case '=':

case ')':

return 1;break;

case '*':

case '/':

return 2;break;

case '^':

return 3;break;

default :return 0;break;

}

}

int main()

{

int c=0;

while(1)

{

Node nodes[NODE_MAX]={0};

double r=0.0;

//清空括号栈

bb=0; //InitStack(&brackets);

printf("Enter: ");

//输入

if(!input(nodes))

{

printf("Input Error!");

continue;

}

//计算

if(calculate(nodes,&r))

{

if(bb)

{

printf("Bracket does not match!");

continue;

}

if(floor(r)==r)

printf("%d",(int)r);

else

printf("%.4f",r);

}

c=getchar();

if(c=='q') //退出

break;

else if(c=='c') //清屏

system("clear");

if(c!='\n')

getchar();

}

printf("Quit...\n");

return 0;

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。