【实验】基于webservice的嵌入式计算器的设计
目的: 实验内容与分析设计: 步骤: 1、安装交叉编译工具;(不再详述) 疑难小结: 本次试验需要在linxu操作系统下独立完成,还需要用到开发板,需要学会最基本的操作命令,还要会挂载。 #include<stdio.h> #include<string.h> #include<math.h> #define SIZE1 20 #define SIZE2 100 int check(char ch[]); double convert(int *place); int OPSWR(char c); int OVSWR(double s); int OPSRE(char *c); int OVSRE(double *s); int OPSDEL(); int OVSDEL(); int ERRORINF(int flag); int CALCULATE(); int COMP(); struct OPSSTA { char stack[SIZE1]; int top; } OPS; struct OVSSTA { double stack[SIZE1]; int top; } OVS; double RESULT; char str[SIZE2],str1[SIZE1]; int main () { int flag=0,sign=0; OPS.top=-1; OVS.top=-1; printf("请输入表达式:"); gets(str); strcpy(str1,str); flag=check(str); sign=ERRORINF(flag); if(sign!=1) { getch(); return -1; } flag=CALCULATE(); sign=ERRORINF(flag); if(sign!=1) { getch(); return -1; } else printf("n%s = %.10f",str1,RESULT); getch(); return 1; } int check(char ch[]) { int i=0,j=0,left=0,right=0; for(i=0;ch[i]!=' ';i++) { if(ch[i]>='('&&ch[i]<='9') { if(ch[i]=='(') left++; if(ch[i]==')') right++; if(ch[i]==44) return -1; } } ch[i]=';'; ch[i+1]=' '; if(left!=right) return -1; for(i=0;ch[i]!=' ';i++) { if(ch[i]>='0'&&ch[i]<='9') continue; if(ch[i]=='.') if(!((ch[i+1]>='0'&&ch[i+1]<='9')&&(ch[i-1]>='0'&&ch[i-1]<='9'))) return -1; if(ch[i]=='+'||ch[i]=='-'||ch[i]=='*'||ch[i]=='/') { if(!((ch[i+1]>='0'&&ch[i+1]<='9'||ch[i+1]=='(')&&(ch[i-1]>='0'&&ch[i-1]<='9'||ch[i-1]=='('))) return -1; continue; } if(ch[i]=='(') if(ch[i-1]>='0'&&ch[i-1]<='9') return -1; if(ch[i]==')') if(ch[i+1]>='0'&&ch[i+1]<='9') return -1; } return 1; } int ERRORINF(int flag) { switch(flag) { case 1: return 1; case -1: printf("表达式格式错误!"); return 0; case -2: printf("栈OPS溢出!"); return 0; case -3: printf("除0!"); return 0; case -4: printf("栈OVS溢出!"); return 0; case -5: printf("栈OVS访问越界!"); return 0; case -6: printf("栈OPS访问越界!"); return 0; default: printf("程序运行错误!"); return 0; } } double convert(int *place) { char num[SIZE1]; int i=0,j=*place; for(;str[j]>='0'&&str[j]<='9'||str[j]=='.';j++,i++) num[i]=str[j]; num[i]=' '; *place=j; return atof(num); } int OPSWR(char c) { OPS.top++; if(OPS.top>=SIZE1) return -2; OPS.stack[OPS.top]=c; return 1; } int OVSWR(double s) { OVS.top++; if(OVS.top>=SIZE1) return -4; OVS.stack[OVS.top]=s; return 1; } int OPSRE(char *c) { if(OPS.top<0) return -5; else { *c=OPS.stack[OPS.top]; OPSDEL(); } return 1; } int OVSRE(double *s) { if(OVS.top<0) return -6; else { *s=OVS.stack[OVS.top]; OVSDEL(); } return 1; } int OPSDEL() { if(OPS.top<0) return -5; else { OPS.stack[OPS.top]=' '; OPS.top--; } return 1; } int OVSDEL() { if(OVS.top<0) return -6; else { OVS.stack[OVS.top]=0; OVS.top--; } return 1; } int CALCULATE() { int place,flag=0; double RES; flag=OPSWR(';'); if(flag!=1) return flag; for(place=0;str[place]!=' ';place++) { flag=0; if(str[place]>='0'&&str[place]<='9') { RES=convert(&place); place--; flag=OVSWR(RES); if(flag!=1) return flag; continue; } if(str[place]=='(') { flag=OPSWR('('); if(flag!=1) return flag; continue; } if(str[place]==')') { if(OPS.stack[OPS.top]!='(') { flag=COMP(); if(flag!=1) return flag; place--; continue; } else { flag=OPSDEL(); if(flag!=1) { return flag; } } continue; } if(str[place]=='+'||str[place]=='-') { if(OPS.stack[OPS.top]=='('||OPS.stack[OPS.top]==';') { flag=OPSWR(str[place]); if(flag!=1) { return flag; } continue; } else { flag=COMP(); if(flag!=1) return flag; place--; continue; } } if(str[place]=='*'||str[place]=='/') { if(OPS.stack[OPS.top]=='*'||OPS.stack[OPS.top]=='/') { flag=COMP(); if(flag!=1) return flag; place--; continue; } else { flag=OPSWR(str[place]); if(flag!=1) return flag; continue; } } if(str[place]==';') { if(OPS.stack[OPS.top]==';') { RESULT=OVS.stack[OVS.top]; return 1; } else { flag=COMP(); if(flag!=1) { return flag; } place--; continue; } } return -1; } return 1; } int COMP() { int flag; double A,B,RES; char ops; flag=OPSRE(&ops); if(flag!=1) { return flag; } flag=OVSRE(&B); if(flag!=1) { return flag; } flag=OVSRE(&A); if(flag!=1) { return flag; } switch(ops) { case '+': RES=A+B; break; case '-': RES=A-B; break; case '*': RES=A*B; break; case '/': if(B==0.0) return -3; RES=A/B; break; default: return -1; } flag=OVSWR(RES); if(flag!=1) { return flag; } return 1; } ?"综合实验——基于webservice的嵌入式计算器的设计.doc"?点击打开链接 源代码:点击打开链接 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |