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;
有哪些*包括*頭*中*類? – Nawaz 2013-05-03 15:20:28
等一下。***這裏沒有什麼代碼。*** – UmNyobe 2013-05-03 15:21:40
oops lemme編輯代碼 – user2340034 2013-05-03 15:23:17