1.判断一个字符串中左右括号是否匹配
思路:使用一个栈。遇到左括号时,执行入栈操作,将其压入栈中。当遇到第一个右括号时,判断栈顶元素是否与其匹配,若匹配则出栈,继续搜索下一个右括号;若不匹配则说明整个字符串左右括号不匹配。以此类推,直到搜索完全部字符,若搜索完成后栈中还有元素则说明不匹配。
程序:
typedef char Stack_entry; //栈中数据类型为char字符型
#include "stack.cpp" //调用栈
int main()
{
Stack openings; //定义一个对象
char symbol; //存放符号
bool is_matched = true;
while (is_matched && (symbol = cin.get()) != '\n') { //搜索一个字符
if (symbol == '{' || symbol == '(' || symbol == '[') //如果属于左括号则入栈
openings.push(symbol);
if (symbol == '}' || symbol == ')' || symbol == ']') { //如果属于右括号则检测是否与栈顶元素匹配
if (openings.empty()) { //判断栈是否为空
cout << "Unmatched closing bracket " << symbol
<< " detected." << endl; //栈为空时说明不匹配
is_matched = false;
}
else { //栈不为空时检测是否与栈顶元素匹配
char match;
openings.top(match);
openings.pop();
is_matched = (symbol == '}' && match == '{')
|| (symbol == ')' && match == '(')
|| (symbol == ']' && match == '[');
if (!is_matched) //如果不匹配则整个字符串不匹配
{
cout << "Bad match " << match << symbol << endl;
is_matched = false;
}
}
}
}
if(!openings.empty()) //如果栈为空则返回不匹配信息
{
cout << "Unmatched opening bracket(s) detected." << endl;
is_matched = false;
}
if(is_matched == true) //如果匹配则返回“matched”
cout << "matched!" << endl;
}
2.简单的计算器
思路:用一个栈,解决简单的+、-、*、/问题。将表达式写成后缀表达式,如:3+5= 用后缀表达式表示为:?3?5+=,在程序中’?’表示入栈,‘q’表示结束程序。
程序:
commands.cpp
typedef double Stack_entry;
#include "stack.cpp"
void introduction() //说明程序作用
{
cout << "Reverse Polish Calculator Program." << "\n----------------------------------" << endl << endl;
}
void instructions() //说明输入规则
{
cout << "User commands are entered to read in and operate on integers." << endl;
cout << "The valid commands are as follows:" << endl << "[Q]uit." << endl;
cout << "[?] to enter an integer onto a stack." << endl << "[=] to print the top integer in the stack." << endl;
cout << "[+] [-] [*] [/] are arithmetic operations." << endl << "These operations apply to the top pair of stacked integers." << endl;
}
char get_command() //检测输入的字符是否有效
{
char command;
bool waiting = true;
cout << "Select command and press <Enter>:";
while (waiting) {
cin >> command;
command = tolower(command);
if (command == '?' || command == '=' || command == '+' ||
command == '-' || command == '*' || command == '/' ||
command == 'q' ) waiting = false; //当输入合法时给waiting赋false,跳出循环,执行操作
else {
cout << "Please enter a valid command:" << endl
<< "[?]push to stack [=]print top" << endl
<< "[+] [-] [*] [/] are arithmetic operations" << endl
<< "[Q]uit." << endl; //当输入不合法时,提示重新输入
}
}
return command;
}
bool do_command(char command, Stack &numbers) //根据输入字符执行相应操作
{
double p, q;
switch (command) {
case '?': //'?'表示一个元素将入栈
cout << "Enter a real number: " << flush; //提示输入一个数据
cin >> p;
if (numbers.push(p) == overflow) //检测栈是否已满
cout << "Warning: Stack full, lost number" << endl;
break;
case '=': //'='表示将栈顶元素读出
if (numbers.top(p) == underflow)
cout << "Stack empty" << endl;
else
cout << p << endl;
break;
case '+': //'+'表示取出栈顶两个数据相加
if (numbers.top(p) == underflow)
cout << "Stack empty" << endl;
else {
numbers.pop();
if (numbers.top(q) == underflow) {
cout << "Stack has just one entry" << endl;
numbers.push(p);
} //若栈中只有一个元素则提示只有一个元素,并将读出的返回
else {
numbers.pop();
if (numbers.push(q + p) == overflow)
cout << "Warning: Stack full, lost result" << endl;
} //检测是否栈已满
}
break;
case '-': //'-'表示取出栈顶两个数据相减
if (numbers.top(p) == underflow)
cout << "Stack empty" << endl;
else {
numbers.pop();
if (numbers.top(q) == underflow) {
cout << "Stack has just one entry" << endl;
numbers.push(p);
}
else {
numbers.pop();
if (numbers.push(q - p) == overflow)
cout << "Warning: Stack full, lost result" << endl;
}
}
break;
case '*': //'*'表示取出栈顶两个数据相乘
if (numbers.top(p) == underflow)
cout << "Stack empty" << endl;
else {
numbers.pop();
if (numbers.top(q) == underflow) {
cout << "Stack has just one entry" << endl;
numbers.push(p);
}
else {
numbers.pop();
if (numbers.push(q * p) == overflow)
cout << "Warning: Stack full, lost result" << endl;
}
}
break;
case '/': //'/'表示取出栈顶两个数据相除
if (numbers.top(p) == underflow)
cout << "Stack empty" << endl;
else {
numbers.pop();
if (numbers.top(q) == underflow) {
cout << "Stack has just one entry" << endl;
numbers.push(p);
}
else {
numbers.pop();
if (numbers.push(q / p) == overflow)
cout << "Warning: Stack full, lost result" << endl;
}
}
break;
case 'q': //'q'表示结束程序
cout << "Calculation finished.\n";
return false;
}
return true;
}
主函数main.cpp
#include "commands.cpp"
int main()
{
Stack stored_numbers; //定义一个栈存数据
introduction(); //说明程序作用
instructions(); //说明输入规则
while (do_command(get_command(), stored_numbers)); //当do_command(get_command(), stored_numbers)值为真时一直执行
}