• 表达式计算





    #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]!='') { number1=0; while(numchar(str[i])) { number1=number1*10+(str[i]-'0'); i++; } //已经读取了一个整数 printf("number1_________uuuu=%d ",number1); 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=%f ",number2); } number=number1+number2; printf("number_float=%f ",number); push(opnd,number); } push(opnd,number1); printf("number_int=%d ",number1); if(!isoptr(str[i])) { printf("express error---1! "); exit(0); } 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); } push(optr,ch2); } else if(piror[chartoint(ch1)][chartoint(ch2)]=='<') push(optr,ch2); else if(piror[chartoint(ch1)][chartoint(ch2)]=='=') pop(optr,ch3); else { 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;
         }
    

      

  • 相关阅读:
    mysql 添加权限和撤销权限的实例(亲测可行)
    svn-经常遇到问题解答办法积累(一)
    一个命令查看mysql的所有配置(原创)
    IOC容器Unity的使用及独立配置文件Unity.Config
    JS如何封装一些列方法为一个对象的操作,然后集中管理这些操作,方便修改和调用
    VSS 的修复和扫描
    js正则获取图片的src属性及正则分割一个字符串
    Mongodb 服务(windows环境下)因被强制关闭,导致服务不能启动的处理办法
    dir结果重定向到剪切板
    property配置
  • 原文地址:https://www.cnblogs.com/wc1903036673/p/3395161.html
Copyright © 2020-2023  润新知