表达式求值(浮点型).docx_第1页
表达式求值(浮点型).docx_第2页
表达式求值(浮点型).docx_第3页
表达式求值(浮点型).docx_第4页
表达式求值(浮点型).docx_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

#include #include #include #include #define max 100#define Elemtype floattypedef struct char *data;int top;int stacksize;seqstack;typedef struct Elemtype *data1;int top;int stacksize;seqstack1;int prior(char str1,char str2);/字符栈int initstack(seqstack &L)L.data=(char *)malloc(max * sizeof(char);if(L.data=NULL)printf(内存分配失败!n); exit(0);L.top=-1;L.stacksize=max;return 1;int pushstack(seqstack &L,char ch)if(L.top=L.stacksize-1)printf(栈满!n); return 0;L.top+;L.dataL.top=ch;return 1;char popstack(seqstack &L)if(L.top=-1)printf(空栈!n);elsereturn L.dataL.top-;char gettop(seqstack L)if(L.top=-1)printf( 空栈!n);elsereturn L.dataL.top;/求值栈int initstack1(seqstack1 &L1)L1.data1=(Elemtype *)malloc(max * sizeof(Elemtype);if(L1.data1=NULL)printf(内存分配失败!n); exit(0);L1.top=-1;L1.stacksize=max;return 1;int pushstack1(seqstack1 &L1,Elemtype x)if(L1.top=L1.stacksize-1)printf(栈满!n); return 0;L1.top+;L1.data1L1.top=x;return 1;float popstack1(seqstack1 &L1) float n;if(L1.top=-1) printf(空栈!n); elsen=L1.data1L1.top-;return n;/优先级比较int prior(char str1,char str2)switch(str1)case (:case #: return 0;case +:case -:switch(str2) case #:case +:case -: return 1; case *:case /: return 0; break;case *:case /:return 1;/计算float calculate(float a,float b,char ch)if(ch=*)return (a*b);else if(ch=/)return (a/b);else if(ch=+)return (a+b);else return (a-b);/字符转换为实数float transform(char *ch)int i=0,k=0,j;float n,n2=0,n1=0;char str10;while(chi=0& chi=0& ch=9| ch=.)return 1;elsereturn 0;/转为后缀表达式 void Tosuffix(char *p,char *suffix)int i=0,j=0;seqstack L;char ch;initstack(L);pushstack(L,#);while(pi!=#)if(indigt(pi)suffixj+=pi;else switch(pi) case (:pushstack(L,pi);break; case ): ch=popstack(L); while(ch!=() suffixj+=ch; ch=popstack(L); break; default:suffixj+= ; /遇运算符读一个空格,使运算符前后数字字符隔开ch=gettop(L);while(prior(ch,pi)suffixj+=ch; ch=popstack(L); ch=gettop(L);pushstack(L,pi);break; i+;while(L.top!=0) /扫描完输入表达式后,栈中还有运算符 ch=popstack(L);suffixj+=ch; suffixj=0;/有后缀表达式求值 float result_(char *suffix) float result1,result2,result,n;int i=0,j=0,k,N;N=strlen(suffix);char str10;seqstack1 L1;initstack1(L1);while(suffixi) if(indigt(suffixi)while(indigt(suffixi) strj+=suffixi; i+; strj=0;j=0;n=transform(str);pushstack1(L1,n);i-; /防止跳过字符 else if(suffixi= ) for(k=i;kN;k+) suffixk=suffixk+1; N=N-1; i-; /防止跳过字符 else result1=popstack1(L1); result2=popstack1(L1); result=calculate(result2,result1,suffixi); pushstack1(L1,result); i+; return result; int main() char *p,*suffix; int i; p=(char *)malloc(max * sizeof(char); suffix=(char *)malloc(max * sizeof(char); printf(输入表达式(以#号结束):); scanf(%s,p); i=strlen(p); i-; if(pi!=#)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论