2013-05-03 149 views
0

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

#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; 
+4

有哪些*包括*头*中*类? – Nawaz 2013-05-03 15:20:28

+2

等一下。***这里没有什么代码。*** – UmNyobe 2013-05-03 15:21:40

+0

oops lemme编辑代码 – user2340034 2013-05-03 15:23:17

回答

3

你忘了{在int main()开始。你也应该结束}

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

谢谢。我通过了那部分 – user2340034 2013-05-03 15:39:39