堆栈的数组实现

时间:2013-05-03 15:18:01

标签: c++ arrays stack

我写了这个程序,它应该测试正确使用三个分组符号“(”,“)”;“[”,“]”;和“{”,“}”。它正在使用堆栈的数组实现,并且应该评估它是好字符串还是坏字符串。例如:(a + b),[(ab)+ c]将是好的,而a + b(等等将是坏字符串。当我运行程序时,我只得到一个错误。我以为我错过了一个半-colon或者其他东西,但是经过几次查看代码后,我找不到它。也许我有隧道视觉。你能看看这里的问题是什么吗?这是错误: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;

1 个答案:

答案 0 :(得分:3)

你在int main()的开头忘了{你也应该以}

结束
int main(){
    //your while code
    return 0;
}
相关问题