我編寫了下面的代碼來判斷用戶輸入的表達式是否有正確的括號序列,例如,如果用戶輸入[a *(b + c)],則表示確定。但如果他進入[a *(b + c)[它不正確。堆棧執行判斷表達式
Stacklist.cpp是一個文件,它包含堆棧的鏈表實現和push和pop函數的定義。顯示是僅顯示頂部條目的功能。
#include<iostream>
#include<exception>
using namespace std;
#include"stacklist.cpp"
int main()
{
string s;
cin>>s;//user inputs the string
stacklist<int> stack1;//the class in stacklist.cpp...int because all bracket's ascii values are ints
char c;
while((c=cin.get())!=EOF)
{
switch('c')
{
case '(': case '{': case '[':
stack1.push('c');
break;
case ')':
{char s=stack1.display();
try
{
if(s=='(')
{ stack1.pop();
continue;}
else
throw 5;
}//try block
catch(5) //.......(a)
{
cout<<"unmatched bracket error";
exit(-1);
}//catch over
}//')' case
break;
case '}': //.......(b)
{char s=stack1.display();
try
{
if(s=='{')
{ stack1.pop();
continue;}
else
throw 6;
}//try block
catch(6) //......(a)
{
cout<<"unmatched bracket error";
exit(-1);
}//catch over
}//'}' case
break;
case ']': ........(c)
{char s=stack1.display();
try
{
if(s==']')
{ stack1.pop();
continue;}
else
throw 7;
}//try block
catch(7) //.............(a)
{
cout<<"unmatched bracket error";
exit(-1);
}//catch over
}//']' case
break;//..........(d)
default:
break;
} //switch
} //while
if(stack1.display==0)//0 is displayed if stack is empty
cout<<"string is correct"<<endl;
else
cout<<"unequal number of brackets"<<endl;
system("pause"); //........(e)
return 0;
} //main
現在的問題是,當我編譯的代碼有各種錯誤:
syntax error before numeric constant.........in all (a)
case label '}' not within switch statement........(b)
case label ']' not within switch statement........(c)
syntax error before break.................(d)
ISO forbids declaration of 'system' with no type...........(e)
請告訴我如何堵塞這些錯誤?
可能希望與您的支架風格更加一致。 – Marlon