zoukankan      html  css  js  c++  java
  • expresscalculate



    //本程序接受任意含有+、-、*、/和()的数值表达式,数值允许带小数,输入以#结束。如:((2.4+3.5)*2-(2+3.5)/2)*(2+3)# #include <stdio.h> #include "G:expressmystack.h" char piror[7][7]={'>','>','<','<','<','>','>', '>','>','<','<','<','>','>', '>','>','>','>','<','>','>', '>','>','>','>','<','>','>', '<','<','<','<','<','=','f', '>','>','>','>','f','>','>', '<','<','<','<','<','f','='}; //保存算符优先矩阵 int chartoint(char ch)//将运算符变成数组(算符优先矩阵)下标 { switch(ch) { case '+':return 0; case '-':return 1; case '*':return 2; case '/':return 3; case '(':return 4; case ')':return 5; case '#':return 6; } } int numchar(char ch)//判断是否为数字符号 { if (ch>='0' && ch<='9') return 1; else return 0; } int isoptr(char ch)//判断是否为合法的运算符号 { switch(ch) { case '+': case '-': case '*': case '/': case '(': case ')': case '#':return 1; default :return 0; } } float operate(float n1,char ch,float n2)//对操作数按运算符进行运算 { switch(ch) { case '+':return n1+n2; case '-':return n1-n2; case '*':return n1*n2; case '/':return n1/n2; } } main() { char str[80]; gets(str); int i=0; int number1,exp10; char ch1,ch2,curroptr1; float number,number2,n1,n2,n3,curroptr,ch3; sqstack optr,opnd; initstack(optr);push(optr,'#'); initstack(opnd); while(str[i]!='') { if(numchar(str[i])) { number1=0; while(numchar(str[i])) { number1=number1*10+(str[i]-'0'); i++; } //已经读取了一个整数 if (str[i]=='.') { i++; number2=0.0;exp10=10; while(numchar(str[i])) { number2=number2+((str[i]-'0')*1.0)/exp10; exp10=exp10*10; i++; printf("number2_____decimal part=%f ",number2); } number=number1+number2; printf("number_float=%f ",number);// 将实数压入堆栈 push(opnd,number); } else { push(opnd,number1);// 将整数压入堆栈 printf("number_____int=%d ",number1); } } else if(!isoptr(str[i])) { printf("express error---1! "); exit(0); } else { ch1=(char)gettop(optr); ch2=str[i]; printf("ch1=%c,ch2=%c ",ch1,ch2); if(piror[chartoint(ch1)][chartoint(ch2)]=='>') { while(piror[chartoint(ch1)][chartoint(ch2)]=='>') { pop(opnd,n2); pop(opnd,n1); pop(optr,curroptr); curroptr1=(char)curroptr; printf(" ---%f %c %f=====%f ",n1,curroptr1,n2,operate(n1,curroptr1,n2)); push(opnd,operate(n1,curroptr1,n2)); ch1=(char)gettop(optr); } } if(piror[chartoint(ch1)][chartoint(ch2)]=='<') push(optr,ch2); if(piror[chartoint(ch1)][chartoint(ch2)]=='=') pop(optr,ch3); if(piror[chartoint(ch1)][chartoint(ch2)]=='f') { printf("express error---2! "); exit(0); } i++; } } pop(opnd,n3); printf(" result=%f ",n3); }

      

    #include <stdlib.h>
    #include <stdio.h>
    
    #define stackinitsize 50
    #define stackincrement 8
    
    typedef float elemtype;
    
    typedef struct{
      elemtype *base;
      elemtype *top;
      int stacksize;
    }sqstack;
    
    
    int  initstack(sqstack &s)
      {s.base=(elemtype * ) malloc(stackinitsize*sizeof(int));
       s.top=s.base;
       s.stacksize=stackinitsize;
       return 1;
       }
    
    int push(sqstack &s,elemtype e)
     {
       *(s.top)=e;
       s.top++;
       return 1;
     }
    
    elemtype gettop(sqstack s)
    {
      return *(s.top-1);
     }
    
    int emptystack(sqstack s)
      {if (s.top==s.base)  return 1;
       else return 0;
       }
    
    int pop(sqstack &s,elemtype &e)
       { if (emptystack(s)) return 0;
         --s.top;
         e=*(s.top);
        return 1;
         }
    

      

  • 相关阅读:
    POJ 3253 Fence Repair STL 优先队列
    P1196 [NOI2002]银河英雄传说 题解
    UVA1316 Supermarket 题解
    P1955 [NOI2015]程序自动分析 题解
    P3807 【模板】卢卡斯定理 题解
    P2480 [SDOI2010]古代猪文 题解
    题解 P4778 【Counting swaps】
    P1313 计算系数 题解
    P3810 【模板】三维偏序(陌上花开)题解
    P1072 Hankson 的趣味题 题解
  • 原文地址:https://www.cnblogs.com/wc1903036673/p/3395199.html
Copyright © 2011-2022 走看看