mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
document paranthesis matching
This commit is contained in:
parent
0c85156573
commit
4206ac2a29
@ -1,76 +1,75 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief Perform paranthesis matching. \note Do not know the application of
|
||||
* this, however.
|
||||
* @note Implementation is C-type and does not utilize the C++ constructs
|
||||
* @todo implement as a C++ class
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
#ifdef _MSC_VER
|
||||
#include <string> // Visual Studio C requires this include
|
||||
#else
|
||||
#include <cstring>
|
||||
#endif
|
||||
|
||||
/** check number */
|
||||
#define MAX 100
|
||||
|
||||
// -------------- stack --------------
|
||||
|
||||
//! @{-------------- stack --------------
|
||||
//! global stack
|
||||
char stack[MAX];
|
||||
|
||||
//! pointer to track stack index
|
||||
int top = -1;
|
||||
|
||||
void push(char ch)
|
||||
{
|
||||
stack[++top] = ch;
|
||||
//! push byte to stack variable
|
||||
void push(char ch) { stack[++top] = ch; }
|
||||
|
||||
//! pop a byte out of stack variable
|
||||
char pop() { return stack[top--]; }
|
||||
|
||||
//! @}-------------- end stack -----------
|
||||
|
||||
/** return opening paranthesis corresponding to the close paranthesis
|
||||
* @param[in] ch closed paranthesis character
|
||||
*/
|
||||
char opening(char ch) {
|
||||
switch (ch) {
|
||||
case '}':
|
||||
return '{';
|
||||
case ']':
|
||||
return '[';
|
||||
case ')':
|
||||
return '(';
|
||||
case '>':
|
||||
return '<';
|
||||
}
|
||||
return '\0';
|
||||
}
|
||||
|
||||
char pop()
|
||||
{
|
||||
return stack[top--];
|
||||
}
|
||||
|
||||
// -------------- end stack -----------
|
||||
|
||||
char opening(char ch)
|
||||
{
|
||||
switch (ch)
|
||||
{
|
||||
case '}':
|
||||
return '{';
|
||||
case ']':
|
||||
return '[';
|
||||
case ')':
|
||||
return '(';
|
||||
case '>':
|
||||
return '<';
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
string exp;
|
||||
int valid = 1, i = 0;
|
||||
cout << "Enter The Expression : ";
|
||||
cin >> exp;
|
||||
|
||||
while (valid == 1 && i < exp.length())
|
||||
{
|
||||
if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[' || exp[i] == '<')
|
||||
{
|
||||
push(exp[i]);
|
||||
}
|
||||
else if (top >= 0 && stack[top] == opening(exp[i]))
|
||||
{
|
||||
pop();
|
||||
}
|
||||
else
|
||||
{
|
||||
valid = 0;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
// makes sure the stack is empty after processsing (above)
|
||||
if (valid == 1 && top == -1)
|
||||
{
|
||||
cout << "\nCorrect Expression";
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "\nWrong Expression";
|
||||
}
|
||||
|
||||
return 0;
|
||||
int main() {
|
||||
std::string exp;
|
||||
int valid = 1, i = 0;
|
||||
std::cout << "Enter The Expression : ";
|
||||
std::cin >> exp;
|
||||
|
||||
while (valid == 1 && i < exp.length()) {
|
||||
if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[' || exp[i] == '<') {
|
||||
push(exp[i]);
|
||||
} else if (top >= 0 && stack[top] == opening(exp[i])) {
|
||||
pop();
|
||||
} else {
|
||||
valid = 0;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
// makes sure the stack is empty after processsing (above)
|
||||
if (valid == 1 && top == -1) {
|
||||
std::cout << "\nCorrect Expression";
|
||||
} else {
|
||||
std::cout << "\nWrong Expression";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user