【问题标题】:Array implementation of stack栈的数组实现
【发布时间】:2013-04-28 01:23:04
【问题描述】:

我编写了这个程序,它应该测试三个分组符号 "(",")";"[","]"; 的正确使用。和 ”{”,”}”。它正在使用堆栈的数组实现,并且应该评估它是好字符串还是坏字符串。例如:(a+b), [(ab)+c] 会很好,而 )a+b( 等会是坏字符串。当我运行程序时,我只得到一个错误。我以为我错过了一个半-冒号什么的,但是看了几遍代码都找不到。也许我有隧道视觉。你能看看这是什么问题吗?这是错误:project1.cpp:41:错误: 'while' 之前的预期初始化程序。

#include <string>
#include <iostream>
#include <stdio.h>

using namespace std;

const int DefaultListSize = 100;
typedef char Elem;

class Astack {
private:
    int size;
    int top;
    Elem *listArray;
public:
    Astack (int sz = DefaultListSize)
    {size = sz; top= 0; listArray = new Elem[sz];}
    ~Astack() {delete [] listArray;}
    void clear() {top=0;}
    bool push(const Elem& item) {
            if (top == size) return false;
    else {listArray[top++] = item; return true;}}
    bool pop(Elem& it) {
    if (top==0) return false;
    else {it = listArray[--top]; return true;}}
    bool topValue(Elem& it) const {
    if (top==0) return false;
    else {it = listArray[top-1]; return true;}}
    bool isEmpty() const {if (top==0) return true;
     else return false;}
     int length() const{return top;}
}; //end of class Astack

Astack s;

const string LEFTGROUP="([{";
const string RIGHTGROUP=")]}";

int main()

while (!EOF) {
  while (!EOL) {
   ch = getc();
   if (ch == LEFTGROUP[0]) {
      s.push(ch);
      }
   if (ch == LEFTGROUP[1] {
      s.push(ch);
      }
   if (ch == LEFTGROUP[2] {
      s.push(ch);
      }
    } //checking for openers

   while (!EOL) {
    ch = getc();
    if (s.top() == LEFTGROUP[0]) {
       if (ch == RIGHTGROUP[0]) {
          s.pop();
          }
         }
    if (s.top() == LEFTGROUP[1]) {
       if (ch == RIGHTGROUP[1]) {
          s.pop();
          }
         }
    if (s.top() == LEFTGROUP[2]) {
       if (ch == RIGHTGROUP[2]) {
          s.pop();
          }
         }
    if (!s.empty()) {
      cout<<"Bad String."<<endl;
    else {
      cout<<"Good String."endl;
     }
    }
   }

 return 0;

【问题讨论】:

  • 包含标题内部有什么?
  • 等一下。 这段代码中没有while。
  • 哎呀让我编辑代码
  • @UmNyobe 我猜while 可能会被其中一些不起眼的宏使用。
  • 这真是讽刺。用于检查分号正确性的程序无法编译,因为缺少分号...

标签: c++ arrays stack


【解决方案1】:

您忘记了 int main() 开头的 {。您还应该以 }

结尾
int main(){
    //your while code
    return 0;
}

【讨论】:

    猜你喜欢
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 2011-07-29
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    相关资源
    最近更新 更多