【问题标题】:How to fix incorrect file output after reading characters from a stack从堆栈中读取字符后如何修复不正确的文件输出
【发布时间】:2019-05-06 04:54:28
【问题描述】:

我的程序正在向我的输出文件写入不正确的数据,我不确定为什么会发生这种情况。我实际上是在尝试通过确定是否缺少括号或括号来检查方程式是否平衡。这些方程是从输入文件中读取的。为了检查它们是否平衡,我使用了一个堆栈。无论出于何种原因,我的程序要么过度计算了缺失字符的数量,要么甚至没有识别出缺失的字符。

从我尝试做的测试来看,我的程序似乎在某些地方循环了更多次。但是,我并不完全确定。我还尝试使用较小的输入大小来测试我的程序,但没有奏效。

这里是相关代码。应该注意的是,这是针对我被挑战不使用 STL 模板的作业。我的堆栈函数基本上以相同的方式工作。我的 pop 函数删除堆栈顶部的任何内容,并将其复制到字符变量中

//create stack
DynStack mystack;

//pushes elements to stack
for (int i=0; i<size; i++) {
    if (array[i] == '(' || array[i] == '[') {

        mystack.push( array[i] );
        continue;
    }

    // Stack will remain empty until an
    // an open character is reached.
    // While empty, opening characters are missing
    if ( mystack.isEmpty() ) {

        if (array[i] == ')') {

            closedP++;
        }

        else if ( array[i] == ']' )
      ;
        ClosedB++;
    }



    //compares stack to elements in array.
    // if the do not match, then an element is missing.
    if (array[i] == ')') {
        mystack.pop(c);

        if ( c == '[' ) {
            openP++;
        }
    }

    if ( array[i] == ']' ) {
        mystack.pop(c);

        if (c == '(') {
            OpenB++;
        }
    }
}


//if there is still an element left in the stack
//after checking the entire expression, it is either
//missing a ')' or a ']'
if (!mystack.isEmpty()) {
    mystack.pop(c);
    if (c == '(') {
        closedP++;
    }
    else if (c == '[')
        ClosedB++;
}

// cout<<"OP: "<<openP<<endl<<"CP: "<<closedP<<endl<<
// "OpenB "<<OpenB<<endl<<"CB: "<<ClosedB<<endl;
//  cout<<"total: "<<totalmissing<<endl;

totalmissing=openP+closedP+OpenB+ClosedB;

//prints valid expression
if (totalmissing == 0) {
    outfile<<math<<" "<< "=== "
    <<"valid expression"<<endl;
}

//prints the amount of each element missing to output file
//if the number of missing elements is less than 6
if ( totalmissing >= 1 && totalmissing < 6 ) {
    outfile<<math<<" "<< "=== "<<"missing "
    <<"("<<openP<<") "<<"('s"<<" and "<<"("<<closedP<<") "<<")'s"
    <<" and "<<"("<<OpenB<<") "<<"['s"<<" and "<<"("<<ClosedB
    <<") "<<"]'s"<<endl;
}

// expression is missing 6 or more elements
if (totalmissing >= 6 || OpenB >=6
    || ClosedB>=6 ||openP >=6 || closedP >=6 ) {
    outfile<<math<<" "<<"6 or more elements are missing"<<endl;
}

鉴于我的输入是这样的:

a+(b/c)*abcd(efgh
(60+[efg]+[efm)
70+1]
)aphids(
]dkdjsfg-4tw[abds()

我应该告诉用户表达式是否有效,如果不是,告诉用户缺少什么。如果失踪超过 6 个,我应该告诉他们。

根据我给你的内容你会看到,我提供的表达式有任何问题的输出都是不正确的。

a+(b/c)*abcd(efgh 6 or more elements are missing
(60+[efg]+[efm) === missing (1) ('s and (1) )'s and (0) ['s and (0) ]'s
70+1] 6 or more elements are missing
)aphids( 6 or more elements are missing
]dkdjsfg-4tw[abds() 6 or more elements are missing

任何帮助将不胜感激!

【问题讨论】:

  • 只是为了测试,用std::stack&lt;char&gt;替换你的自制堆栈。那你的代码能用吗?如果是这样,那么问题在于您自制的堆栈类,而不是您发布的代码。所有那些你不能使用 STL 的废话——至少用它来检查你的基本代码是否真的有效。
  • 您只需将一行:DynStack mystack; 替换为std::stack&lt;char&gt; mystack;。发代码的时候要发minimal reproducible example,除了你和你的同学,没人知道DynStack是什么。
  • Paul 的重点不是丢弃堆栈,而是使用您可以信任的堆栈来测试您的程序是否正常工作。如果代码在std::stack 下按预期执行,您现在知道错误在哪里,您的堆栈,您可以修复它。如果您没有得到预期的行为,您可以修复使用堆栈的代码,而不会出现堆栈中可能存在的任何错误。关键是要隔离问题并一次尽可能少地攻击。完成后,已知堆栈和使用堆栈的代码都在工作,然后将堆栈放回原处。
  • @Schmeep 好吧,std::stack 工作正常——我的方法没有问题——是你的代码有问题。也许您在调用pop() 时没有检查空堆栈?这就是为什么使用std::stack 是个好主意的原因——如果您尝试pop() 一个空堆栈,我们不知道您的 DynStack 的行为。
  • @Schmeep if (array[i] == ')') { mystack.pop(c); -- 如果mystack 为空并且您尝试使用popstd::stack,则行为未定义(可能是崩溃)。使用您的DynStack,如果编写pop 只是为了忽略一个空堆栈,那么您在代码中至少发现了一个错误,感谢std::stack,而不感谢DynStack

标签: c++ loops stack fstream


【解决方案1】:

正确的缩进对于阅读代码很重要。这部分看起来很可疑:

if ( mystack.isEmpty() ) {

    if (array[i] == ')') {

        closedP++;
    }

    else if ( array[i] == ']' )
  ;
    ClosedB++;
}

其实是这样的:

if ( mystack.isEmpty() ) {

    if (array[i] == ')') {

        closedP++;
    }

    else if ( array[i] == ']' )
        ;        // noop here
    ClosedB++;   // always executed (if stack is empty)because outside of inner if block
}

为避免此类错误,我强烈建议您对 if 块保持一致:始终使用花括号 ({}),除非唯一的语句与 if(或 else if 或 @ 987654326@) 是。

这足以解释您的错误结果,但由于您没有提供最小、完整和可验证的示例,我无法测试修复它是否足以提供预期的输出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 2012-11-30
    • 2020-12-04
    • 1970-01-01
    • 2022-12-18
    相关资源
    最近更新 更多