1样例
创建一个栈类,用来保存数据和删除
CMakeLists.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | cmake_minimum_required(VERSION 3.10) project(Stack_Test) #1手动设置指定文件 #set(Current_LIST main.cpp) #2自动搜所文件目录 aux_source_directory(. Current_LIST) # 搜索当前.目录下的所有.cpp文件 aux_source_directory(./src SRC_LIST) # 搜索当前.目录下的所有.cpp文件 include_directories(src)#关键 不是相对引用 需要单独制定包含 # 4指定生成目标 add_executable(Stack_demo ${Current_LIST} ${SRC_LIST}) |
main.cpp
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 | #ifndef MAIN_CPP #define MAIN_CPP //#include "API_stack.h" //关键地方 不是相对引用 src/API_Cout.h cmakelist.txt需要加包含文件include_directories #include "src/API_stack.h" //关键地方 相对引用 src/API_Cout.h cmakelist.txt不需要加包含文件include_directories #include <iostream> #include <cctype> using namespace std; int main() { Stack st; char ch; unsigned long po; cout<< "paleas enter A to add a data \n" << "P to pop a data \n" << "Q to quit" <<endl; while (cin >> ch && toupper(ch) != 'Q' ) //等待输入 且不为Q 退出 { while (cin. get () != '\n' ) //等待回车确认 换行符号 continue ; if (!isalpha(ch)) //检查c是否为字母。 { cout<< '\a' << "input is not char " <<endl; //凤鸣报警 continue ; } switch (ch) { case 'A' : cout<< "enter a number to add" ; cin>> po; if (st.isfull()) cout<< "stack already full\n" <<endl; else { st.push(po); } break ; case 'P' : if (st.isempty()) cout<< "stack alread empty" <<endl; else { st.pop(po); cout<< "Po " << po<< "popped\n" ; } break ; } cout<< "paleas enter A to add a data \n" << "P to pop a data \n" << "Q to quit" <<endl; } cout<< "Bye" <<endl; return 0; } #endif |
API_stack.h
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 | #ifndef STACK_H #define STACK_H #include <string> #include <iostream> typedef unsigned long Item; class Stack { private : enum {MAX=10}; Item items[MAX]; int top; public : Stack(); ~Stack(); bool isempty() const ; bool isfull() const ; bool push( const Item &item); bool pop(Item &item); }; #endif |
API_stack.cpp
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 | #ifndef STACK_CPP #define STACK_CPP #include "API_stack.h" Stack::Stack() { top=0; } Stack::~Stack() { using namespace std; cout<< "class is over" <<endl; } bool Stack::isempty() const { return top==0; } bool Stack::isfull() const { return top==MAX; } bool Stack::push( const Item &item) { if (top<MAX) { items[top++]=item; return true ; } else { return false ; } } bool Stack::pop(Item &item) { if (top >0) { item=items[--top]; return true ; } else return false ; } #endif |
编译
1 2 3 4 | cd build cmake .. make ./Stack_demo |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· 因为Apifox不支持离线,我果断选择了Apipost!
2020-01-09 50 python使用进程