c++ - 中缀方程求解器C++ While循环堆栈

标签 c++ while-loop stack infix-notation

我创建了一个中缀问题求解器,它在最后的while循环中崩溃,以完成方程的最后一部分。

我在main中调用最后一个while循环来解决堆栈上的剩余内容,并将其卡在那里,如果我从堆栈中弹出最后一个元素,它将离开循环并返回错误的答案。

//
//
//
//
//
#include <iostream>
#include<stack>
#include<string>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <sstream>
using namespace std;
#define size 30
int count=0;
int count2=0;
int total=0;
stack< string > prob;
char equ[size];
char temp[10];
string oper;
string k;
char t[10];
int j=0;
char y;


   int solve(int f,int s, char o)
   {
  cout<<"f="<<f<<endl;
  cout<<"s="<<s<<endl;
  cout<<"o="<<o<<endl;
  int a;
  if (o== '*')//checks the operand stack for operator
  {
    cout << f << "*" << s << endl;
    a= f*s;
  }
  if (o == '/')//checks the operand stack for operator
  {
    cout << f << "/" << s << endl;
    if(s==0)
    {
      cout<<"Cant divide by 0"<<endl;
    }
    else
      a= f/s;
  }
  if (o == '+')//checks the operand stack for operator
  {
    cout << f << "+" << s << endl;
    a= f+s;
  }
  if (o == '-')//checks the operand stack for operator
  {
    cout << f << "-" << s << endl;
    a= f-s;
  }
  return a;
}



int covnum()
{
  int l,c;
  k=prob.top();
  for(int i=0;k[i]!='\n';i++)t[i]=k[i];
  return l=atoi(t);
}


char covchar()
{
  k=prob.top();
  for(int i=0;k[i]!='\n';i++)t[i]=k[i];
  return t[0];
}


void tostring(int a)
{
  stringstream out;
  out << a;
  oper = out.str();
}


void charstack(char op)
{
  oper=op;
  prob.push(oper);
}


void numstack(char n[])
{
  oper=n;
  prob.push(oper);
}

void setprob()
{
  int f,s;
  char o;
  char t;
  int a;
  int i;
  t=covchar();
  if(ispunct(t))
  {
    if(t=='(')
    {
      prob.pop();
    }
    if(t==')')
    {
      prob.pop();
    }
    else if(t=='+'||'-')
    {
      y=t;
      prob.pop();
    }
    else if(t=='/'||'*')
    {
      y=t;
      prob.pop();
    }
  }
  cout<<"y="<<y<<endl;
  i=covnum();
  cout<<"i="<<i<<endl;
  s=i;
  prob.pop();
  t=covchar();
  cout<<"t="<<t<<endl;
  if(ispunct(t))
  {
    o=t;
    prob.pop();
  }
  i=covnum();
  cout<<"i="<<i<<endl;
  f=i;
  prob.pop();
  t=covchar();
  if (t=='('||')')
  {
    prob.pop();
  }
  a=solve(f,s, o);
  tostring(a);
  prob.push(oper);
  cout<<"A="<<prob.top()<<endl;
}


void postfix()
{
  int a=0;
  char k;
  for(int i=0;equ[i]!='\0';i++)
  {
    if(isdigit(equ[i]))//checks array for number
    {
      temp[count]=equ[i];
      count++;
    }
    if(ispunct(equ[i]))//checks array for operator
    {
      if(count>0)//if the int input is done convert it to a string and push to stack
      {
        numstack(temp);
        count=0;//resets the counter
      }
      if(equ[i]==')')//if char equals the ')' then set up and solve that bracket
      {
        setprob();
        i++;//pushes i to the next thing in the array
        total++;
      }
      while(equ[i]==')')//if char equals the ')' then set up and solve that bracket
      {
        i++;
      }
      if(isdigit(equ[i]))//checks array for number
      {
        temp[count]=equ[i];
        count++;
      }
      if(ispunct(equ[i]))
      {
        if(equ[i]==')')//if char equals the ')' then set up and solve that bracket
        {
          i++;
        }
        charstack(equ[i]);
      }
      if(isdigit(equ[i]))//checks array for number
      {
        temp[count]=equ[i];
        count++;
      }
    }
  }
}



int main()
{
  int a=0;
  char o;
  int c=0;

  cout<<"Enter Equation: ";
  cin>>equ;
  postfix();
  while(!prob.empty())
  {
    setprob();
    a=covnum();
    cout<<a<<" <=="<<endl;
    prob.pop();
    cout<<prob.top()<<"<top before c"<<endl;
    c=covnum();
    a=solve(c,a,y);
  }
  cout<<"Final Awnser"<<a<<endl;
  system ("PAUSE");
  return 0;
}

最佳答案

我看到许多可能导致其无法正常工作的问题:

  • 没有错误或界限检查。我意识到这是家庭作业,因此可能具有特定的要求/规格,从而无需进行某些检查,但是您仍然需要一些以确保正确解析输入。如果超过了equ / tmp / t的数组大小怎么办?如果您尝试弹出/顶起堆栈时该堆栈为空,该怎么办?
  • 有一些if语句看起来像else if (t == '+' || '-'),很可能不会执行您希望它们执行的操作。该表达式实际上始终为true,因为'-'为非零值,并转换为true值。您可能需要else if (t == '+' || t == '-')
  • 据我所知,您似乎跳过了解析或将'('添加到堆栈中,这将使您无法实际正确地评估表达式。
  • 您在postfix()的中间有一个while循环,该循环跳过多个')',但不执行任何操作。
  • 您的代码很难遵循。适当地命名变量和函数并消除大多数全局变量(实际上并不需要它们中的大多数)将非常有帮助,因为适当地缩进并在表达式中添加一些空格。
  • 还有其他一些特别值得一提的小问题。例如,covchar()和covnum()函数比所需的复杂得多。

  • 这些年来,我已经写了一些postfix解析器,但我实在无法完全遵循您的尝试,这并不是说您尝试的方式是不可能的,但我建议您对基本逻辑进行重新检查需要解析表达式,尤其是嵌套的括号级别。

    关于c++ - 中缀方程求解器C++ While循环堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9457262/

    相关文章:

    c++ - Vim 将长字符串分成几个

    Java - 访问在不同的 'if/else if' 语句中创建的对象?

    php - 使用 PHP while 循环显示多个表中的 MySQL 数据

    python - 为什么我的 while 循环输出越界值?

    java - 错误: illegal start of type

    c++ - 函数错误的多重定义,即使在使用#if 保护子句时也是如此

    C++默认参数类成员

    c++ - 在 C/C++ 中分配和访问矩阵

    c - 推送功能期间堆栈结构更新不正确

    javascript - P5 洪水填充尝试