Stacks using Array
Sep 9, 2022 at 4:10am UTC
Any ideas on how to convert this program into Stacks that use Arrays? This one right here used an STL.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#include<iostream>
#include<string>
#include<stack>
//function prototypes
bool arePair();
bool areBalanced();
bool checking();
int main(){
int choice=1;
while (choice==1){
std::cout << "What will you do?\n1. ENTER EXPRESSION, 2. EXIT\n" ;
std::cin >> choice;
switch (choice){
case 1: checking();
break ;
case 2: std::cout << "\nThank you for using this service.\n" ;
return 0;
break ;
default : std::cout << "Wrong input. Try again.\n" ;
}
}
}
//functions
bool arePair(char openingD,char closingD){
if (openingD == '(' && closingD == ')' ){
return true ;
}
else if (openingD == '{' && closingD == '}' ){
return true ;
}
else if (openingD == '[' && closingD == ']' ){
return true ;
}
return false ;
};
bool areBalanced(std::string expression){
std::stack<char > delimiter;
int i = 0;
while (i<expression.length()){
if (expression[i] == '(' || expression[i] == '{' || expression[i] == '[' )
delimiter.push(expression[i]);
else if (expression[i] == ')' || expression[i] == '}' || expression[i] == ']' ){
if (delimiter.empty() || !arePair(delimiter.top(),expression[i])){
return false ;
}
else {
delimiter.pop();
}
}
i++;
}
return delimiter.empty();
};
bool checking(){
std::string expr;
std::cout<<"Enter an expression: " ;
std::cin>>expr;
if (areBalanced(expr)){
std::cout << "The symbols are balanced." << std::endl;
return true ;
}
else {
std::cout << "Error. It's unbalanced.\n" ;
return false ;
}
};
Sep 9, 2022 at 4:59am UTC
Could be:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include <cassert>
#include <iterator>
#include <iostream>
#include <string>
struct stack { char data[1000]; int n = 0; };
[[nodiscard]] bool is_empty(stack const & s) { return s.n == 0; }
[[nodiscard]] bool is_full(stack const & s) { return s.n == std::size(s.data); }
void push(stack& s, char c) { if (is_full(s)) throw "overflow" ; s.data[s.n++] = c; }
[[nodiscard]] char top(stack const & s) { if (is_empty(s)) throw "underflow" ; return s.data[s.n - 1]; }
void pop(stack& s) { if (is_empty(s)) throw "underflow" ; --s.n; }
[[nodiscard]] bool is_balanced(std::string const & expr)
{
stack my_stack;
for (char c: expr) switch (c)
{
case '(' : case '{' : case '[' : push(my_stack, c); break ;
case ')' : if (top(my_stack) != '(' ) return false ; else pop(my_stack); break ;
case '}' : if (top(my_stack) != '{' ) return false ; else pop(my_stack); break ;
case ']' : if (top(my_stack) != '[' ) return false ; else pop(my_stack); break ;
case '\"' : if (top(my_stack) != '\"' ) push(my_stack, '\"' ); else pop(my_stack); break ;
}
return is_empty(my_stack);
}
int main()
{
for (std::string line; std::getline(std::cin, line); )
std::cout << (is_balanced(line)? "balanced\n" : "not balanced\n" );
}
Last edited on Sep 9, 2022 at 5:03am UTC
Sep 9, 2022 at 7:58am UTC
You have to use a reasonably modern standard library in order to get access to
size :
https://en.cppreference.com/w/cpp/iterator/size
No matter, just replace it with a constant
1 2 3 4
int constexpr stack_size = 1000;
struct stack { char data[stack_size ]; int n = 0; };
// ...
[[nodiscard]] bool is_full(stack const & s) { return s.n == stack_size; }
Last edited on Sep 9, 2022 at 7:58am UTC
Sep 9, 2022 at 7:59am UTC
thank you man it works :)
Sep 9, 2022 at 12:04pm UTC
Any ideas on how to convert this program into Stacks that use Arrays?
But why? Using an STL container is far 'better' than using a fixed sized array.
In any case, the code as posted in post #1 is incorrect as the function declarations don't match the definitions (and only checking() is needed). Using std::stack, then consider:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#include <iostream>
#include <string>
#include <stack>
bool checking();
int main() {
int choice { 1 };
while (choice != 2) {
std::cout << "\n1. ENTER EXPRESSION\n2. EXIT\nWhat will you do: " ;
std::cin >> choice;
switch (choice) {
case 1:
checking();
break ;
case 2:
break ;
default :
std::cout << "Wrong input. Try again.\n" ;
break ;
}
}
std::cout << "\nThank you for using this service.\n" ;
}
bool arePair(char openingD, char closingD) {
if (openingD == '(' && closingD == ')' )
return true ;
if (openingD == '{' && closingD == '}' )
return true ;
if (openingD == '[' && closingD == ']' )
return true ;
return false ;
}
bool areBalanced(const std::string& expression) {
std::stack<char > delimiter;
for (const auto & ch : expression)
if (ch == '(' || ch == '{' || ch == '[' )
delimiter.push(ch);
else if (ch == ')' || ch == '}' || ch == ']' ) {
if (delimiter.empty() || !arePair(delimiter.top(), ch))
return false ;
delimiter.pop();
}
return delimiter.empty();
}
bool checking() {
std::string expr;
std::cout << "Enter an expression: " ;
std::getline(std::cin >> std::ws, expr);
if (areBalanced(expr)) {
std::cout << "The symbols are balanced.\n" ;
return true ;
}
std::cout << "Error. It's unbalanced.\n" ;
return false ;
}
Last edited on Sep 9, 2022 at 1:11pm UTC
Sep 9, 2022 at 3:23pm UTC
... and as more generalised, then consider:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#include <iostream>
#include <string>
#include <stack>
const std::string opnBrk { "({[" }, clsBrk { ")}]" };
bool checking();
int main() {
int choice { 1 };
while (choice != 2) {
std::cout << "\n1. ENTER EXPRESSION\n2. EXIT\nWhat will you do: " ;
std::cin >> choice;
switch (choice) {
case 1:
checking();
break ;
case 2:
break ;
default :
std::cout << "Wrong input. Try again.\n" ;
break ;
}
}
std::cout << "\nThank you for using this service.\n" ;
}
bool arePair(char openingD, char closingD) {
const auto fnd { opnBrk.find(openingD) };
return fnd != std::string::npos && clsBrk[fnd] == closingD;
}
bool areBalanced(const std::string& expression) {
std::stack<char > delimiter;
for (const auto & ch : expression)
if (opnBrk.find(ch) != std::string::npos)
delimiter.push(ch);
else if (clsBrk.find(ch) != std::string::npos) {
if (delimiter.empty() || !arePair(delimiter.top(), ch))
return false ;
delimiter.pop();
}
return delimiter.empty();
}
bool checking() {
std::string expr;
std::cout << "Enter an expression: " ;
std::getline(std::cin >> std::ws, expr);
if (areBalanced(expr)) {
std::cout << "The symbols are balanced.\n" ;
return true ;
}
std::cout << "Error. It's unbalanced.\n" ;
return false ;
}
Topic archived. No new replies allowed.