课后作业,用栈实现括号匹配检查,代码如下:
[C++] 纯文本查看 复制代码 // C++ Program to check for balanced parentheses in an expression using stack.
// 2023-02-20_190142.png
#include <iostream>
#include <string.h>
#include <stack>
using namespace std;
#define parentheses_left '('
#define parentheses_right ')'
#define brackets_left '['
#define brackets_right ']'
#define braces_left '{'
#define braces_right '}'
int Check_Parentheses(char *Brackets_list)
{
stack<char> S;
int i = 0;
if (Brackets_list[i] == '{' || Brackets_list[i] == '[' || Brackets_list[i] == '(')
{
S.push(Brackets_list[i]);
}
else if (Brackets_list[i] == '}' || Brackets_list[i] == ']' || Brackets_list[i] == ')')
{
if (S.empty())
{
cout << "False!" << endl;
return 0;
}
if (S.top() == parentheses_left && Brackets_list[i] == parentheses_right)
S.pop();
else if (S.top() == brackets_left && Brackets_list[i] == brackets_right)
S.pop();
else if (S.top() == braces_left && Brackets_list[i] == braces_right)
S.pop();
else
{
cout << "The Brackets isn't match" << endl;
return 0;
}
}
return S.empty() ? 1 : 0;
}
int main()
{
char test1[] = {"{{[()]}}"};
char test2[] = {"{[[}"};
char test3[] = {']','['};
char test4[] = {"{}}"};
Check_Parentheses(test1);
Check_Parentheses(test2);
Check_Parentheses(test3);
Check_Parentheses(test4);
return 0;
}
然后就报了错误,具体如下:
正在执行任务: C/C++: gcc 生成活动文件
正在启动生成...
/usr/bin/gcc -fdiagnostics-color=always -g /mnt/z/Users/17572/Documents/VS_Code_Workspace/cwork/Harsha_Suryanarayana/Introduction_to_Data_Structures/BalancedParentheses.cpp -o /mnt/z/Users/17572/Documents/VS_Code_Workspace/cwork/Harsha_Suryanarayana/Introduction_to_Data_Structures/BalancedParentheses
/usr/bin/ld: /tmp/ccQjdfFd.o: warning: relocation against _ZSt4cout' in read-only section .text'
/usr/bin/ld: /tmp/ccQjdfFd.o: in function Check_Parentheses(char*)': /mnt/z/Users/17572/Documents/VS_Code_Workspace/cwork/Harsha_Suryanarayana/Introduction_to_Data_Structures/BalancedParentheses.cpp:27: undefined reference to std::cout'
/usr/bin/ld: /mnt/z/Users/17572/Documents/VS_Code_Workspace/cwork/Harsha_Suryanarayana/Introduction_to_Data_Structures/BalancedParentheses.cpp:27: undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' /usr/bin/ld: /mnt/z/Users/17572/Documents/VS_Code_Workspace/cwork/Harsha_Suryanarayana/Introduction_to_Data_Structures/BalancedParentheses.cpp:27: undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
/usr/bin/ld: /mnt/z/Users/17572/Documents/VS_Code_Workspace/cwork/Harsha_Suryanarayana/Introduction_to_Data_Structures/BalancedParentheses.cpp:27: undefined reference to std::ostream::operator<<(std::ostream& (*)(std::ostream&))' /usr/bin/ld: /mnt/z/Users/17572/Documents/VS_Code_Workspace/cwork/Harsha_Suryanarayana/Introduction_to_Data_Structures/BalancedParentheses.cpp:38: undefined reference to std::cout'
/usr/bin/ld: /mnt/z/Users/17572/Documents/VS_Code_Workspace/cwork/Harsha_Suryanarayana/Introduction_to_Data_Structures/BalancedParentheses.cpp:38: undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' /usr/bin/ld: /mnt/z/Users/17572/Documents/VS_Code_Workspace/cwork/Harsha_Suryanarayana/Introduction_to_Data_Structures/BalancedParentheses.cpp:38: undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
/usr/bin/ld: /mnt/z/Users/17572/Documents/VS_Code_Workspace/cwork/Harsha_Suryanarayana/Introduction_to_Data_Structures/BalancedParentheses.cpp:38: undefined reference to std::ostream::operator<<(std::ostream& (*)(std::ostream&))' /usr/bin/ld: /tmp/ccQjdfFd.o: in function static_initialization_and_destruction_0(int, int)':
/usr/include/c++/11/iostream:74: undefined reference to std::ios_base::Init::Init()' /usr/bin/ld: /usr/include/c++/11/iostream:74: undefined reference to std::ios_base::Init::~Init()'
/usr/bin/ld: /tmp/ccQjdfFd.o: in function std::_Deque_base<char, std::allocator<char> >::_Deque_impl::~_Deque_impl()': /usr/include/c++/11/bits/stl_deque.h:520: undefined reference to std::allocator<char>::~allocator()'
/usr/bin/ld: /tmp/ccQjdfFd.o: in function std::_Deque_base<char, std::allocator<char> >::_Deque_impl::_Deque_impl()': /usr/include/c++/11/bits/stl_deque.h:525: undefined reference to std::allocator<char>::allocator()'
/usr/bin/ld: /tmp/ccQjdfFd.o: in function std::_Deque_base<char, std::allocator<char> >::_M_initialize_map(unsigned long)': /usr/include/c++/11/bits/stl_deque.h:637: undefined reference to cxa_begin_catch'
/usr/bin/ld: /usr/include/c++/11/bits/stl_deque.h:642: undefined reference to __cxa_rethrow' /usr/bin/ld: /usr/include/c++/11/bits/stl_deque.h:637: undefined reference to cxa_end_catch'
/usr/bin/ld: /tmp/ccQjdfFd.o: in function void std::deque<char, std::allocator<char> >::_M_push_back_aux<char const&>(char const&)': /usr/include/c++/11/bits/deque.tcc:493: undefined reference to std::throw_length_error(char const)'
/usr/bin/ld: /tmp/ccQjdfFd.o: in function std::_Deque_base<char, std::allocator<char> >::_M_create_nodes(char**, char**)': /usr/include/c++/11/bits/stl_deque.h:664: undefined reference to __cxa_begin_catch'
/usr/bin/ld: /usr/include/c++/11/bits/stl_deque.h:667: undefined reference to __cxa_rethrow' /usr/bin/ld: /usr/include/c++/11/bits/stl_deque.h:664: undefined reference to cxa_end_catch'
/usr/bin/ld: /tmp/ccQjdfFd.o: in function `gnu_cxx::new_allocator<char>::deallocate(char, unsigned long)':
/usr/include/c++/11/ext/new_allocator.h:145: undefined reference to operator delete(void*, unsigned long)' /usr/bin/ld: /tmp/ccQjdfFd.o: in function gnu_cxx::new_allocator<char*>::deallocate(char*, unsigned long)':
/usr/include/c++/11/ext/new_allocator.h:145: undefined reference to `operator delete(void, unsigned long)'
/usr/bin/ld: /tmp/ccQjdfFd.o: in function __gnu_cxx::new_allocator<char*>::allocate(unsigned long, void const*)': /usr/include/c++/11/ext/new_allocator.h:116: undefined reference to std::throw_bad_array_new_length()'
/usr/bin/ld: /usr/include/c++/11/ext/new_allocator.h:117: undefined reference to std::__throw_bad_alloc()' /usr/bin/ld: /usr/include/c++/11/ext/new_allocator.h:127: undefined reference to operator new(unsigned long)'
/usr/bin/ld: /tmp/ccQjdfFd.o: in function __gnu_cxx::new_allocator<char>::allocate(unsigned long, void const*)': /usr/include/c++/11/ext/new_allocator.h:117: undefined reference to std::__throw_bad_alloc()'
/usr/bin/ld: /usr/include/c++/11/ext/new_allocator.h:127: undefined reference to operator new(unsigned long)' /usr/bin/ld: /tmp/ccQjdfFd.o:(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to __gxx_personality_v0'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
生成已完成,但出现错误。
- 终端进程已终止,退出代码: -1。
- 终端将被任务重用,按任意键关闭。
|