博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++数据结构——顺序栈(基本代码实现与案例)
阅读量:4227 次
发布时间:2019-05-26

本文共 4880 字,大约阅读时间需要 16 分钟。

一:enum关键字

  • 枚举 enum 是一个类型(class),可以保存一组由用户刻画的值。
  • enum 关键字的作用域是全局的,如果在enum A中声明了一个枚举类型 my_enum,则无法在enum B中声明同样的枚举类型,但是如果二者不在一个作用域里则可以声明。
    枚举变量的使用:
enum error_code{
success, overflow, underflow};error_code a;error_code b = success;

二:顺序栈基本代码

#include
using namespace std;enum error_code{
success,overflow,underflow};class stack{
public: stack(); bool empty()const; bool full()const; error_code getTop(int &x)const; error_code push(const int x); error_code pop(); private: int count; int data[10];};stack::stack(){
count = 0;}bool stack::empty()const{
return (count == 0);}bool stack::full()const{
return (count == 10);}error_code stack::getTop(int &x)const{
if(empty()) return underflow; x = data[count-1]; return success;}error_code stack::push(const int x){
if(full()) return overflow; data[count] = x; count++; return success;}error_code stack::pop(){
if(empty()) return underflow; count--; return success;}bool ReferenceError(error_code a){
if(a == overflow){
cout << " overflow! " << endl; return false; } if(a == underflow){
cout<< " underflow " << endl; return false; } return true;}int main(){
stack s; int top; //栈顶 //入栈 for(int i = 0;i < 7;i++){
ReferenceError(s.push(i)); } //出栈 for(int i = 0;i < 5;i++){
ReferenceError(s.pop()); } //取出栈顶 ReferenceError(s.getTop(top)); cout << top << endl; return 0;}

三:顺序栈的使用

  • 案例一:设计算法将十进制转换为八进制
  • 案例二:符号"{","}","[","]","(",")"应该是互相匹配的,设计算法对以字符串形式读取的表达式S,判断其中的各括号是否是匹配的
  • 案例三:使用栈结构写一个能计算带括号的四则表达式表达式的计算器
#include
#include
using namespace std;#include
enum error_code{
success, overflow, underflow};template
class stack{
public: stack(); bool empty()const; bool full()const; error_code getTop(T& x)const; error_code push(const T x); error_code pop();private: int count; int data[100];}; template
stack
::stack(){ count = 0;}template
bool stack
::empty()const { return count == 0;}template
bool stack
::full()const { return count == 10;}template
error_code stack
::getTop(T& x)const { if (empty()) return underflow; x = data[count - 1]; return success;}template
error_code stack
::push(const T x) { if (full()) return overflow; data[count] = x; count++; return success;}template
error_code stack
::pop() { if (empty()) return underflow; count--; return success;}template
bool ReferenceError(error_code a) { if (a == overflow) { cout << "overflow!" << endl; return false; } if (a == underflow) { cout << "underflow!" << endl; return false; } return true;}// 十进制转八进制 void transform(){ // 初始化题目条件 int num, result = 0; cout << "请输入要转化为八进制的十进制数:"; cin >> num; // 初始化栈结构 stack
s; int top; // 开始运算 while(num != 0) { s.push(num % 8); num /= 8; } while(!s.empty()) { s.getTop(top); s.pop(); result = result * 10 + top; } cout << "转成八进制为:" << result << endl;}// 检验符号匹配string symbol(){ // 初始化题目条件 string s; cout << "请输入表达式S: "; cin >> s; int length; length = s.length(); // 初始化栈结构 stack
a; char top; // 开始运算 for(int i = 0; i < length; i++) { if(s[i] == '(' || s[i] == '[' || s[i] == '{') a.push(s[i]); if(s[i] == ')') { a.getTop(top); if(top == '(') a.pop(); else return "不匹配!!"; } if(s[i] == ']') { a.getTop(top); if(top == '[') a.pop(); else return "不匹配!!"; } if(s[i] == '}') { a.getTop(top); if(top == '{') a.pop(); else return "不匹配!!"; } } return "匹配!!";}// 计算器 int getRes(int a, int b, char c) // 计算{ int result; switch(c){ case '+': result = a + b; break; case '-': result = a - b; break; case '*': result = a * b; break; case '/': result = a / b; break; } return result;}int getPri(char a) // 获得优先级 { if(a == ')') return 1; if(a == '+' || a == '-') return 2; if(a == '*' || a == '/') return 3; if(a == '(') return 4; else cout << "输入错误!!" << endl;}void calculator(){ // 初始化题目条件 string s; cout << "请输入计算式:"; cin >> s; int i = 0, k = 1, num1, num2; char sign; // 初始化栈结构 stack
num; int top_n; stack
oper; char top_o; // 开始计算 if(s[(s.length() - 1)] != '=') { cout << "算式请以 '=' 结尾"; return; } while(s[i] != '='){ if(s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/' || s[i] == '(' || s[i] == ')') { l: if(!oper.empty()) { oper.getTop(top_o); if(getPri(s[i]) > getPri(top_o) || top_o == '(') oper.push(s[i]); else { oper.getTop(top_o); if(top_o != '(') { num.getTop(top_n); num1 = top_n; num.pop(); num.getTop(top_n); num.pop(); num.push(getRes(top_n, num1, top_o)); oper.pop(); oper.getTop(top_o); if(top_o == '(') { oper.pop(); i++; continue; } goto l; } } } if(oper.empty()) oper.push(s[i]); k = 1; } else { if(k == 0) { num.getTop(top_n); num.pop(); num.push(top_n * 10 + s[i] - 48); } if(k == 1) { num.push(s[i] - 48); k = 0; } } i++; } num.getTop(top_n); num1 = top_n; num.pop(); num.getTop(top_n); oper.getTop(top_o); cout << "计算结果为:" << getRes(top_n, num1, top_o) << endl;}int main(){ // 十进制转八进制 transform(); // 检验符号匹配 cout << symbol() << endl; // 计算器 calculator(); return 0;}

转载地址:http://rvnqi.baihongyu.com/

你可能感兴趣的文章
Foundations of WPF: An Introduction to Windows Presentation Foundation
查看>>
Date on Database: Writings 2000-2006
查看>>
Using Microsoft Office 2007, Special Edition
查看>>
Telecommunications and Data Communications Handbook
查看>>
Networking with Microsoft Windows Vista: Your Guide to Easy and Secure Windows Vista Networking
查看>>
Web Application Vulnerabilities: Detect, Exploit, Prevent
查看>>
Silverlight 1.0 Unleashed
查看>>
Wireless Internet and Mobile Computing: Interoperability and Performance
查看>>
Wireless Mesh Networks: Architectures and Protocols
查看>>
Cross-Platform Development in C++: Building Mac OS X, Linux, and Windows Applications
查看>>
ZK: Ajax without the Javascript Framework
查看>>
Ruby Pocket Reference
查看>>
Cryptology Unlocked
查看>>
The ASP.NET 2.0 Anthology: 101 Essential Tips, Tricks & Hacks [ILLUSTRATED]
查看>>
Fedora Core 7 & Red Hat Enterprise Linux: The Complete Reference
查看>>
Windows Vista On Demand
查看>>
Wireless Networking
查看>>
Mike Meyers' A+ Guide: PC Technician
查看>>
Handbook of Network and System Administration
查看>>
Using OpenMP: Portable Shared Memory Parallel Programming
查看>>