四则混合运算算法
c语言吧
全部回复
仅看楼主
level 6
功能:输入一行四则运算字符串,输出结果
eg:
输入:3+4*5-2/4
输出:22.5
算法概要:
建立两个动态栈A,B。A存放计算对象,B存放计算符号。
1.当遇到数字字符转化为数字压入A,遇到符号判断符号和B栈顶元素的优先级,若栈顶元素优先级不低于该符号,取出栈顶元素,从栈A中取出两个数字计算,将结果压入栈A。继续直到B栈顶元素比该符号优先级低或将栈B取空。
2.若B栈顶元素低于该运算符,直接将此符号压入栈B。
3.不断从栈B中取运算符,从栈A中取数字计算,结果压入栈A。直到栈B为空。栈A中的数字即为结果。

2009年11月27日 09点11分 1
level 6
刚学MFC 用这个算法做的计算器
2009年11月27日 09点11分 2
level 6
#include<iostream.h>
#include<malloc.h>
#include<stdlib.h>
#include<stdio.h>
typedef struct stacka //存放计算符的栈
{
     char *base,*top;
     int size;
     stacka();
     ~stacka();
     void push(char);//压入数据
     char pop();//弹出数据
     char gettop();//取栈顶元素
}OP;
typedef struct stackb //存放数字的栈
{
     double *base,*top;
     int size;
     stackb();
     ~stackb();
     void push(double);//压入数据
     double pop();//弹出数据
     double gettop();//取栈顶元素
}NU;
OP::stacka()//A栈功能实现
{
     base=top=new char[20];
     size=20;
}
void OP::push(char ch)
{
     if((top-base)==size)
     {
         base=(char *)realloc(base,size+20);
         size+=20;
     }
     *(top++)=ch;
}
char OP::pop()
{
     char ch;
     if(base==top)
     {
         cout<<"stacka is enpty!!"<<endl;
         exit(1);
     }
     else
     {
        ch=*(--top);
     }
     return ch;
}
OP::~stacka()
{
     delete []base;
}
char OP::gettop()
{
     char ch;
     if(base==top)
     {
         cout<<"stacka is already enpty!!"<<endl;
         exit(1);
     }
     else
     ch=*(top-1);
     return ch;
}
bool OP::empty()
{
     if(top==base)return true;
     else return false;
}
////////////////////////////////////////////////////////
NU::stackb()   //B栈功能实现
{
     base=top=new double[20];
     size=20;
}
NU::~stackb()
{
     delete []base;
}
void NU::push(double n)
{
     if((top-base)==size)
     {
         base=(double *)realloc(base,size+20);
         size+=20;
     }
     *(top++)=n;
}
double NU::pop()
{
     double n;
     if(base==top)
     {
         cout<<"stacka is enpty!!+++"<<endl;
         exit(1);
     }
     else
     {
        n=*(--top);
     }
     return n;
}
double NU::gettop()
{
     double n;
     if(base==top)
     {
         cout<<"stacka is enpty!"<<endl;
         exit(1);
     }
     else
     {
         n=*(top-1);
     }
     return n;
}
bool NU::empty()
{
     if(top==base)return true;
     else return false;
}
2009年11月27日 09点11分 3
level 6
void main()//主函数
{
     char ch,ch1,ch2;
     double n,n1,n2,num=0,result;
     OP A;
     NU B;
     cout<<"Please in put:"<<endl;
     while(ch=getchar())
     {
         if(ch=='\n')
         {
             B.push(num);
             break;
         }
         if(ch>=48 && ch<=57)
         {
             num=ch-48+num*10;
         }
         else
         {
             B.push(num);
             num=0;
             if(A.top==A.base)
             {
                 A.push(ch);
             }
             else
             {
                 if('+'==ch || '-'==ch)
                 {
                     while(A.base!=A.top)
                     {
                         n1=B.pop();
                         n2=B.pop();
                     switch(A.pop())
                     {
                     case '+':n=n1+n2;break;
                     case '-':n=n2-n1;break;
                     case '*':n=n2*n1;break;
                     case '/':n=n2/n1;break;
                     }
                     B.push(n);
                     }
                     A.push(ch);
                 }
                 else
                 {
                     switch(A.gettop())
                     {
                     case '+':A.push(ch);break;
                     case '-':A.push(ch);break;
2009年11月27日 09点11分 4
level 6
case '*':
                         {  
                             do
                             {
                             n1=B.pop();
                             n2=B.pop();
                             if(A.gettop()=='*')
                             {
                                 A.pop();
                                 n=n1*n2;
                             }
                             else if(A.gettop()=='/')
                             {
                                 A.pop();
                                 n=n2/n1;
                             }
                             B.push(n);
                             }while(A.base!=A.top && A.gettop()!='+' && A.gettop()!='-');A.push(ch);break;

2009年11月27日 09点11分 5
level 6
                            
                         }
                     case '/':
                         {
                             do
                             {
                             n1=B.pop();
                             n2=B.pop();
                             if(A.gettop()=='*')
                             {
                                 A.pop();
                                 n=n1*n2;
                             }
                             else if(A.gettop()=='/')
                             {
                                 A.pop();
                                 n=n2/n1;
                             }

2009年11月27日 09点11分 6
level 6
                             B.push(n);
                             }while(A.base!=A.top && A.gettop()!='+' && A.gettop()!='-');A.push(ch);break;
                            
                         }
                     }
                    
                 }
            
             }
         }
     }
    
     while(A.base!=A.top)
     {
         n1=B.pop();
         n2=B.pop();
         switch(A.pop())
         {
             case '+':n=n1+n2;break;
             case '-':n=n2-n1;break;
             case '*':n=n2*n1;break;
             case '/':n=n2/n1;break;
         }
             B.push(n);
     }
     result=*(B.base);
     cout<<result<<endl;//输出结果
}
2009年11月27日 09点11分 7
level 6
算法代码:
#include<iostream.h>
#include<malloc.h>
#include<stdlib.h>
#include<stdio.h>
typedef struct stacka //存放计算符的栈
{
     char *base,*top;
     int size;
     stacka();
     ~stacka();
     void push(char);//压入数据
     char pop();//弹出数据
     char gettop();//取栈顶元素
}OP;
typedef struct stackb //存放数字的栈
{
     double *base,*top;
     int size;
     stackb();
     ~stackb();
     void push(double);//压入数据
     double pop();//弹出数据
     double gettop();//取栈顶元素
}NU;
OP::stacka()//A栈功能实现
{
     base=top=new char[20];
     size=20;
}
void OP::push(char ch)
{
     if((top-base)==size)
     {
         base=(char *)realloc(base,size+20);
         size+=20;
     }
     *(top++)=ch;
}
char OP::pop()
{
     char ch;
     if(base==top)
     {
         cout<<"stacka is enpty!!"<<endl;
         exit(1);
     }
     else
     {
        ch=*(--top);
     }
     return ch;
}
OP::~stacka()
{
     delete []base;
}
char OP::gettop()
{
     char ch;
     if(base==top)
     {
         cout<<"stacka is already enpty!!"<<endl;
         exit(1);
     }
     else
     ch=*(top-1);
     return ch;
}
bool OP::empty()
{
     if(top==base)return true;
     else return false;
}
////////////////////////////////////////////////////////
NU::stackb()   //B栈功能实现
{
     base=top=new double[20];
     size=20;
}
NU::~stackb()
{
     delete []base;
}
void NU::push(double n)
{
     if((top-base)==size)
     {
         base=(double *)realloc(base,size+20);
         size+=20;
     }

2009年11月27日 10点11分 8
level 6
     *(top++)=n;
}
double NU::pop()
{
     double n;
     if(base==top)
     {
         cout<<"stacka is enpty!!+++"<<endl;
         exit(1);
     }
     else
     {
        n=*(--top);
     }
     return n;
}
double NU::gettop()
{
     double n;
     if(base==top)
     {
         cout<<"stacka is enpty!"<<endl;
         exit(1);
     }
     else
     {
         n=*(top-1);
     }
     return n;
}
bool NU::empty()
{
     if(top==base)return true;
     else return false;
}
void main()//主函数
{
     char ch,ch1,ch2;
     double n,n1,n2,num=0,result;
     OP A;
     NU B;
     cout<<"Please in put:"<<endl;
     while(ch=getchar())
     {
         if(ch=='\n')
         {
             B.push(num);
             break;
         }
         if(ch>=48 && ch<=57)
         {
             num=ch-48+num*10;
         }
         else
         {
             B.push(num);
             num=0;
             if(A.top==A.base)
             {
                 A.push(ch);
             }
             else
             {
                 if('+'==ch || '-'==ch)

2009年11月27日 10点11分 9
level 6
                 {
                     while(A.base!=A.top)
                     {
                         n1=B.pop();
                         n2=B.pop();
                     switch(A.pop())
                     {
                     case '+':n=n1+n2;break;
                     case '-':n=n2-n1;break;
                     case '*':n=n2*n1;break;
                     case '/':n=n2/n1;break;
                     }
                     B.push(n);
                     }
                     A.push(ch);
                 }
                 else
                 {
                     switch(A.gettop())
                     {
                     case '+':A.push(ch);break;
                     case '-':A.push(ch);break;
                     case '*':

2009年11月27日 10点11分 10
level 6
                         {  
                             do
                             {
                             n1=B.pop();
                             n2=B.pop();
                             if(A.gettop()=='*')
                             {
                                 A.pop();
                                 n=n1*n2;
                             }
                             else if(A.gettop()=='/')
                             {
                                 A.pop();
                                 n=n2/n1;
                             }
                             B.push(n);
                             }while(A.base!=A.top && A.gettop()!='+' && A.gettop()!='-');A.push(ch);break;

2009年11月27日 10点11分 11
level 6
                            
                         }
                     case '/':
                         {
                             do
                             {
                             n1=B.pop();
                             n2=B.pop();
                             if(A.gettop()=='*')
                             {
                                 A.pop();
                                 n=n1*n2;
                             }
                             else if(A.gettop()=='/')
                             {
                                 A.pop();
                                 n=n2/n1;
                             }

2009年11月27日 10点11分 12
level 6
                             B.push(n);
                             }while(A.base!=A.top && A.gettop()!='+' && A.gettop()!='-');A.push(ch);break;
                            
                         }
                     }
                    
                 }
            
             }
         }
     }
    
     while(A.base!=A.top)
     {
         n1=B.pop();
         n2=B.pop();
         switch(A.pop())
         {
             case '+':n=n1+n2;break;
             case '-':n=n2-n1;break;
             case '*':n=n2*n1;break;
             case '/':n=n2/n1;break;
         }
             B.push(n);
     }
     result=*(B.base);
     cout<<result<<endl;//输出结果
}

2009年11月27日 10点11分 13
level 6
void main()//主函数
{
     char ch,ch1,ch2;
     double n,n1,n2,num=0,result;
     OP A;
     NU B;
     cout<<"Please in put:"<<endl;
     while(ch=getchar())
     {
         if(ch=='\n')
         {
             B.push(num);
             break;
         }
         if(ch>=48 && ch<=57)
         {
             num=ch-48+num*10;
         }
         else
         {
             B.push(num);
             num=0;
             if(A.top==A.base)
             {
                 A.push(ch);
             }
             else
             {
                 if('+'==ch || '-'==ch)
                 {
                     while(A.base!=A.top)
                     {
                         n1=B.pop();
                         n2=B.pop();
                     switch(A.pop())
                     {
                     case '+':n=n1+n2;break;
                     case '-':n=n2-n1;break;

2009年11月27日 10点11分 14
level 6
                     case '*':n=n2*n1;break;
                     case '/':n=n2/n1;break;
                     }
                     B.push(n);
                     }
                     A.push(ch);
                 }
                 else
                 {
                     switch(A.gettop())
                     {
                     case '+':A.push(ch);break;
                     case '-':A.push(ch);break;
                     case '*':
                         {  
                             do
                             {
                             n1=B.pop();
                             n2=B.pop();
                             if(A.gettop()=='*')
                             {

2009年11月27日 10点11分 15
level 6
                                 A.pop();
                                 n=n1*n2;
                             }
                             else if(A.gettop()=='/')
                             {
                                 A.pop();
                                 n=n2/n1;
                             }
                             B.push(n);
                             }while(A.base!=A.top && A.gettop()!='+' && A.gettop()!='-');A.push(ch);break;
                            
                         }
                     case '/':
                         {
                             do
                             {
                             n1=B.pop();

2009年11月27日 10点11分 16
level 6
                             n2=B.pop();
                             if(A.gettop()=='*')
                             {
                                 A.pop();
                                 n=n1*n2;
                             }
                             else if(A.gettop()=='/')
                             {
                                 A.pop();
                                 n=n2/n1;
                             }
                             B.push(n);
                             }while(A.base!=A.top && A.gettop()!='+' && A.gettop()!='-');A.push(ch);break;
                            
                         }
                     }
                    
                 }
            
             }
         }
     }
    
     while(A.base!=A.top)
     {
         n1=B.pop();
         n2=B.pop();
         switch(A.pop())
         {
             case '+':n=n1+n2;break;
             case '-':n=n2-n1;break;
             case '*':n=n2*n1;break;
             case '/':n=n2/n1;break;
         }
             B.push(n);
     }
     result=*(B.base);
     cout<<result<<endl;//输出结果
}

2009年11月27日 10点11分 17
1 2 尾页