求一个用C语言开发一个多功能简易计算器,基本信息包括:四则运算、常见单位转换。使之能提供以下两大部分功能
c语言开发一个多功能简易计算器,基本信息包括:四则运算、常见单位转换。使之能提供以下两大部分功能
程序代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #define DATA 0 #define PARENTHESIS_LEFT 1 #define ADD_SUB 2 #define MUL_DIV 3 #define PARENTHESIS_RIGHT 4 #define MAX_LEN 256 union Value { double value_f; char value_c; }; struct SIGN { union Value value; unsigned int flag; //0.数据。1.2.3.符号 }; int main() { double value[20]; char buf[MAX_LEN],buf_temp[20]; int i=0,mm=0,count=0,sign_len=-1,lose_len=0;; struct SIGN m_sign[20],m_infix[50],m_posfix[50]; printf("请输入表达式,以#结束:"); if(fgets(buf,MAX_LEN,stdin)!=NULL) { while(i<MAX_LEN&&buf[i]!='#') { while((buf[i]>='0'&&buf[i]<='9')||buf[i]=='.') //读取数据 { buf_temp[mm++]=buf[i]; if(mm>=10) { printf("出错!\n"); return -1; } i++; } if(mm>0) //数据入栈 { buf_temp[mm]='\0'; m_infix[count].flag=DATA; m_infix[count].value.value_f=atof(buf_temp); count++; mm=0; continue; } switch(buf[i]) { case '+': m_infix[count].flag=ADD_SUB; m_infix[count].value.value_c='+'; count++; break; case '-': m_infix[count].flag=ADD_SUB; m_infix[count].value.value_c='-'; count++; break; case '*': m_infix[count].flag=MUL_DIV; m_infix[count].value.value_c='*'; count++; break; case '/': m_infix[count].flag=MUL_DIV; m_infix[count].value.value_c='/'; count++; break; case '(': m_infix[count].flag=PARENTHESIS_LEFT; m_infix[count].value.value_c='('; count++; break; case ')': m_infix[count].flag=PARENTHESIS_RIGHT; m_infix[count].value.value_c=')'; count++; break; default:break; } i++; } mm=0; for(i=0;i<count;i++) { if(m_infix[i].flag==DATA) { m_posfix[mm].flag=m_infix[i].flag; m_posfix[mm].value.value_f=m_infix[i].value.value_f; mm++; } else { if(sign_len<0||m_infix[i].flag==PARENTHESIS_LEFT) { lose_len++; sign_len++; m_sign[sign_len].flag=m_infix[i].flag; m_sign[sign_len].value.value_c=m_infix[i].value.value_c; } else if(m_infix[i].flag==PARENTHESIS_RIGHT) { lose_len++; while(m_sign[sign_len].flag != PARENTHESIS_LEFT) { m_posfix[mm].flag=m_sign[sign_len].flag; m_posfix[mm].value.value_c=m_sign[sign_len].value.value_c; sign_len--; mm++; if(sign_len<0) { printf("出错!\n"); break; } } sign_len--; } else if(m_infix[i].flag<m_sign[sign_len].flag) { while(m_infix[i].flag<=m_sign[sign_len].flag&&m_sign[sign_len].flag!=PARENTHESIS_LEFT) { m_posfix[mm].flag=m_sign[sign_len].flag; m_posfix[mm].value.value_c=m_sign[sign_len].value.value_c; sign_len--; mm++; if(sign_len<0) break; } sign_len++; m_sign[sign_len].flag=m_infix[i].flag; m_sign[sign_len].value.value_c=m_infix[i].value.value_c; } else { sign_len++; m_sign[sign_len].flag=m_infix[i].flag; m_sign[sign_len].value.value_c=m_infix[i].value.value_c; printf("in2--%d,%d,%c\n",i,m_sign[sign_len].flag,m_sign[sign_len].value.value_c); } } } while(sign_len>=0) { m_posfix[mm].flag=m_sign[sign_len].flag; m_posfix[mm].value.value_c=m_sign[sign_len].value.value_c; sign_len--; mm++; } for(i=0;i<mm;i++) { if(m_posfix[i].flag==DATA) printf("%g,",m_posfix[i].value.value_f); else printf("%c,",m_posfix[i].value.value_c); } printf("\n"); count=0; for(i=0;i<mm;i++) { if(m_posfix[i].flag==DATA) value[++count]=m_posfix[i].value.value_f; else { switch(m_posfix[i].value.value_c) { case '+':value[count-1]=value[count-1]+value[count];break; case '-':value[count-1]=value[count-1]-value[count];break; case '*':value[count-1]=value[count-1]*value[count];break; case '/':value[count-1]=value[count-1]/value[count];break; } count--; } } printf("result=%g\n",value[1]); } else { printf("输入的表达式有误,请重新输入!\n"); } return 0; }