0

我编写了这个程序,它应该测试三个分组符号 "(",")";"[","]"; 的正确使用。和 ”{”,”}”。它使用堆栈的数组实现,并应该评估它是好字符串还是坏字符串。例如:(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;
4

1 回答 1

3

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

int main(){
    //your while code
    return 0;
}
于 2013-05-03T15:26:17.457 回答