24小时热门版块排行榜    

查看: 616  |  回复: 1

DemiAsh

新虫 (初入文坛)

[求助] [数据结构/C++]新人求助,表达式求值程序无法运行

#pragma once
//顺序栈类定义
template <class T>
class SqStack
{
private:
        T *base;//栈底指针
        int top;//栈顶
        int stacksize;//栈容量
public:
        SqStack(int m);//构建函数
        ~SqStack() { delete[] base; top = 0; stacksize = 0; }//析构函数
        void Push(T x);//入栈
        T Pop();//出栈
        T GetTop();//获取栈顶元素
        int StackEmpty();//测栈空
        void ClearStack();//清空栈
        void StackTop();//返回栈顶指针
        void StackTranverse();//显示栈中元素
};
//顺序栈类实现
template <class T>
SqStack<T>::SqStack(int m)
{

        base = new T[m];
        if (base == NULL)
        {
                cout << "栈创建失败,退出!" << endl;
                exit(1);
        }
        stacksize = m;
        top = -1;
}

template <class T>
void SqStack<T>:ush(T x)
{
        if (top == stacksize - 1) throw "栈满,无法入栈";
        top++;
        base[top] = x;
        //cout<<"top:"<<top<<endl;
}


template <class T>
T SqStack<T>:op()
{
        T x;
        if (top == -1) throw "栈空,不能出栈";
        x = base[top--];
        //cout<<"top:"<<top<<endl;
        return x;
}

template <class T>
T SqStack<T>::GetTop()
{
        if (top == -1) throw "栈空,栈顶无元素";
        //cout<<"top:"<<top<<endl;
        return base[top];
}

template <class T>
int SqStack<T>::StackEmpty()
{
        if (top == -1)
                return 1;
        else
                return 0;
}

template <class T>
void SqStack<T>::ClearStack()
{
        top = -1;
}

template <class T>
void SqStack<T>::StackTop()
{//返回栈顶指针
        cout << "栈顶top= " << top;
}

template <class T>
void SqStack<T>::StackTranverse()
{
        int i = top;
        while (i >= 0)
                cout << base[i--] << '\t';
        cout << endl;
}
//SqStack.h,堆栈的类

//下面是源程序代码

#include<iostream>
#include"process.h"
#include"stdio.h"
#include<string.h>
#include<stdlib.h>
#include"SqStack.h"
using namespace std;
char pause;

char Precede(char t1,char t2)  
{
   char f;
   switch(t2)
   {
     case '+':
     case '-':if(t1=='('||t1=='=')
                f='<';
              else
                f='>';
              break;
     case '*':
     case '/':if(t1=='*'||t1=='/'||t1==')')
                f='>';
              else
                f='<';
              break;
     case '(':if(t1==')')
              {
                cout<<"ERROR1"<<endl;
                exit(0);
              }
              else
                f='<';
              break;
     case ')':switch(t1)
              {
                case '(':f='=';
                         break;
                case '=':cout<<"ERROR2"<<endl;
                         exit(0);
                default: f='>';
              }
              break;
     case '=':switch(t1)
              {
                case '=':f='=';
                         break;
                case '(':cout<<"ERROR2"<<endl;
                         exit(0);
                default: f='>';
              }
   }
   return f;
}


bool IsOperator(char c)
{
   switch(c)
   {
     case'+':
     case'-':
     case'*':
     case'/':
     case'(':
     case')':
     case'=':return true;
     default:return false;
   }
}


double Operate(double a,char theta,double b)
{
   double c;
   switch(theta)
   {
     case'+':c=a+b;
             break;
     case'-':c=a-b;
             break;
     case'*':c=a*b;
             break;
     case'/':c=a/b;
   }
   return c;
}

double Val_Exp(char *exp)
{
   SqStack<char> OP(20);
   SqStack<double>  OD(20);
   char theta;
   double a,b,d;
   char c,x;
   char z[6];
   int i;
   OP.Push('=');
   c=*exp++;
   x=OP.GetTop();
   while(c!='='||x!='=')
   {
     if(IsOperator(c))
       switch(Precede(x,c))
       {
         case'<':OP.Push(c);
                 c=*exp++;
                 break;
         case'=':x=OP.Pop();
                 c=*exp++;
                 break;
         case'>':theta=OP.Pop();
                 b=OD.Pop();
                 a=OD.Pop();
                 OD.Push(Operate(a,theta,b));
       }
     else if(c>='0'&&c<='9'||c=='.')
     {
       i=0;
       do
       {
         z=c;
         i++;
         c=*exp++;
       }while(c>='0'&&c<='9'||c=='.');
       z='\0';
       d=atof(z);
       OD.Push(d);
     }
     else
     {
       cout<<"ERROR3"<<endl;;
       exit(0);
     }
     x=OP.GetTop();
   }
   d=OD.GetTop();
   return d;
}

void CreatePostExp(char * exp,char * &postexp)
{
        char c,x;
        int i=0;
        SqStack<char> OP(20);
        OP.Push('=');
        cout<<"exp:"<<exp<<endl;
        c=*exp++;
        while(c)
        {
                if((c>='0'&&c<='9')||c=='.')
                {
                        postexp[i++]=c;
                        c=*exp++;
                }
                if(IsOperator(c))
                {
                        postexp[i++]=' ';
                 x=OP.GetTop();
                        switch(Precede(x,c))
                        {
                                case'<':OP.Push(c);
                         c=*exp++;
                         break;
                 case'=':x=OP.Pop();
                         c=*exp++;
                         break;
                 case'>':postexp[i++]=OP.Pop();
                                             break;
                        }
                }
                postexp='\0';
        }
        cout<<"后缀表达式为:"<<postexp<<endl;
}

double Val_PostExp(char *postexp)
{
        int i;
        char z[6];
        double v=0,d=0,a,b;
        char c;
        SqStack<double> OD(20);
        c=*postexp++;
        while(c!='\0')
        {
                if((c>='0'&&c<='9')||c=='.')
                {
                        i=0;
                        do
                        {
                                z[i++]=c;
                                c=*postexp++;
                        }while(c>='0'&&c<='9'||c=='.');
                        z='\0';
                        d=atof(z);
                        OD.Push(d);
                }
                if(IsOperator(c))
                {
                        b=OD.Pop ();
                        a=OD.Pop ();
                        OD.Push (Operate(a,c,b));
                        c=*postexp++;
                }
                c=*postexp++;
        }
        v=OD.Pop ();
        return v;
}
void CreatePreFax(char *exp, char *&prefax)
{
        char q;
        char m[20];
        int i, j, k;
        i = j = k = 0;
        SqStack<char> OP(20);
        SqStack<char> OM(20);
        SqStack<char> ON(20);
        while (*exp != '=')
                OM.Push(*exp++);
        while (!OM.StackEmpty())
        {
                q = OM.Pop();
                if ((q >= '0'&&q <= '9') || q == '.')
                {
                        m[i++] = q;
                }
                if (q == ')')
                        OP.Push(q);
                while ((q == '+') || (q == '-') || (q == '*') || (q == '/'))
                {
                        m[i++] = ' ';
                        if (OP.StackEmpty() || OP.GetTop() == ')' || Precede(q, OP.GetTop()) == '>' || Precede(q, OP.GetTop() )== '=')
                        {
                                OP.Push(q);
                                break;
                        }
                        else
                                m[i++] = OP.Pop();
                }
                if (q == '(')
                {
                        while (OP.GetTop() != ')')
                                m[i++] = OP.Pop();
                        OP.Pop();
                }
        }
        while (!OP.StackEmpty())
        {
                m[i++] = OP.Pop();
        }
        m = '\0';
        while (m[j] != '\0')
        {
                ON.Push(m[j++]);
        }
        while (!ON.StackEmpty())
                prefax[k++] = ON.Pop();
        prefax[k] = '\0';
        cout << "前缀表达式为:" << prefax << endl;
}

double Val_PreFax(char *prefax)
{
        int i, j;
        char c;
        char z[6];
        char s[6];
        SqStack<char>OS(20);
        SqStack<double>OD(20);
        double v = 0, d = 0, a, b;
        while (*prefax != '\0')
        {
                OS.Push(*prefax++);
        }
        while (!OS.StackEmpty())
        {
                c = OS.Pop();
                if ((c>='0'&&c<='9')||c=='.')
                {
                        i = 0;
                        do
                        {
                                z[i++] = c;
                                c = OS.Pop();

                        } while ((c>='0'&&c<='9')||c=='.');
                                i--;
                                for (int j = 0; i >= 0; i--)
                                        s[j++] = z;
                                s = '\0';
                                d = atof(s);
                                OD.Push(d);
                }
                if (IsOperator(c))
                {
                        b = OD.Pop();
                        a = OD.Pop();
                        OD.Push(Operate(b,c,a));

                }
        }
        v = OD.Pop();
        return v;
}


int main(void)
{
        char exp[20]="(2.2+5)+4*(5-3.1)=";
        char *postexp;
        postexp=new char[20];
        *postexp='\0';
        char *prefax;
        prefax = new char[20];
        *prefax = '\0';
        double v1, v2;
        system("cls";
        int choice;
    do
        {
                cout<<"主菜单\n";
                cout<<"1-创建表达式\n";
                cout<<"2-表达式求值\n";
                cout<<"3-求后缀表达式\n";
                cout<<"4-后缀表达式求值\n";
                cout<<"5-求前缀表达式\n";
                cout << "6-前缀表达式求值\n";
                cout << "7-显示表达式\n";
                cout<<"8-退出\n";
                cout<<"Enter choice:";
                cin>>choice;
                switch(choice)
                        {
                        case 1:
                    cout<<"请输入表达式,以=结束"<<endl;
                                cin>>exp;
                                cin.get(pause);
                                system("pause";
                                break;
                        case 2:
                                v1=Val_Exp(exp) ;
                                cout<<exp;
                                cout<<v1<<endl;
                                cin.get(pause);
                            system("pause";
                                break;
                        case 3:
                                CreatePostExp(exp,postexp);
                                cin.get(pause);
                                system("pause";
                                break;
                        case 4:
                                v1=Val_PostExp(postexp);
                                cout<<postexp<<"="<<v1<<endl;
                                cin.get(pause);
                                system("pause";
                                break;
                        case 5:
                                CreatePreFax(exp, prefax);
                                cin.get(pause);
                                system("pause";
                                break;
                        case 6:
                                v2 = Val_PreFax(prefax);
                                        cout << prefax << "=" << v2 << endl;
                                cin.get(pause);
                                system("pause";
                                break;
                        case 7:
                                cout<<endl;
                                cout<<"已创建的表达式为:";
                                cout<<exp<<endl;
                          CreatePostExp(exp,postexp);
                          CreatePreFax(exp,prefax);
                                    cin.get(pause);
                                    system("pause"
                                break;
                        case 8:
                                cout<<"退出运算"<<endl;
                                break;
                        default:
                                cout<<"Invalid choice\n";
                                break;
                        }
        }while(choice!=8);
}

发自小木虫Android客户端
回复此楼

» 猜你喜欢

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

jiangs111

金虫 (小有名气)

【答案】应助回帖

★ ★
wolfghost: 金币+2, 积极应助奖励~ 2017-03-09 22:53:00
char c, x;
char z[6];
……
do
{
   z = c;       //这里前面定义的是数组,报错“表达式必须是可修改的左值”,后面也有类似的错误。
   i++;
普通
2楼2017-01-13 12:47:32
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 DemiAsh 的主题更新
信息提示
请填处理意见