?? 括號配對.cpp
字號:
#include<iostream>
#include<cstring>
#include "assert.h"
#define NULL 0
using namespace std;
class stack
{
public:
char *elmlist;
char top;
char msize;
stack(int sz=20)
{
msize=sz;
elmlist=new char[msize];
assert(elmlist!=NULL);
top=-1;
}
~stack()
{delete []elmlist;}
void push(char i);
char pop();
char gettop();
void makeempty();
int isfull(){return top==msize;}
int isempty(){return top==-1;}
};
void stack::push(char i)
{
assert(!isfull());
top++;
elmlist[top]=i;
}
char stack::pop()
{
assert(!isempty());
return elmlist[top--];
}
char stack::gettop()
{
assert(!isempty());
return elmlist[top];
}
void stack::makeempty()
{
top=-1;
}
void match(char *s)
{
stack a(10);
int i=0;
char k=s[i];
while(k!='\0')
{ k=s[i];
if(k=='(')
{
a.push(k);i++;
}
else if(k== ')')
{
if(!a.isempty())
{
a.pop();
i++;
}
else
{
cout<<"括號配對錯誤!"<<endl;
exit(0);
}
}
else i++;
}
if(a.isempty())
cout<<"括號配對成功!"<<endl;
else
cout<<"括號配對錯誤!"<<endl;
}
void main()
{
char s[20];//={"5+8*(1-2)/3\0"};
cout<<"請輸入一個式子:";
cin.getline(s,20);
match(s);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -