commit 0c3a2a3d7a92c546dc102930f2020378f824a33c Author: XiaoLFeng Date: Sun May 21 11:25:12 2023 +0800 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e30bef3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# 项目排除路径 +/cmake-build-debug/ \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..0128a67 --- /dev/null +++ b/main.cpp @@ -0,0 +1,100 @@ +#include + +#define MaxSize 100 + +typedef int datatype; +typedef struct { + datatype data[MaxSize]; + int top; +} SqStack; + +//ÓÃÀ´³õʼ»¯Ò»¸ö˳ÐòÕ»: +void InitStack(SqStack &stack) { + stack.top = 0; +} + +// ÓÃÀ´ÊµÏÖÔªËصÄÈëÕ»²Ù×÷: +int Push(SqStack &stack, int data) { + if (stack.top + 1 > MaxSize) return 0; + stack.data[stack.top] = data; + stack.top++; + return 1; +} + +//ÓÃÀ´ÊµÏÖÔªËصijöÕ»²Ù×÷: +int Pop(SqStack &stack) { + if (stack.top != 0) { + stack.top--; + return stack.data[stack.top]; + } else { + return -99999; + } +} + +//ÓÃÀ´ÊµÏÖÈ¡Õ»ÏîÔªËصIJÙ×÷¡£ +int GetTop(SqStack stack) { + return stack.data[stack.top-1]; +} + +void GetAll(SqStack stack) { + if (stack.top != 0) { + printf("[SELECT] Êä³öÈ«²¿Êý¾Ý£º[ "); + int size = stack.top-1; + while (size) { + printf("%d, ",stack.data[size]); + size = size - 1; + } + printf("%d ]\n",stack.data[0]); + printf("\n"); + } else { + printf("[WARNING] Õ»±íΪΪ¿Õ\n"); + printf("\n"); + } +} + +int main() { + SqStack stack; + InitStack(stack); + int ch,data; + Start: + printf("[INFO] ˳ÐòÕ»²Ù×÷\n"); + printf("\t======================\n"); + printf("\tÇëÑ¡Ôñ\n"); + printf("\t1£ºÈëÕ»\n"); + printf("\t2£º³öÕ»\n"); + printf("\t3£ºÈ¡Õ»ÏîÔªËØ\n"); + printf("\t4£º²é¿´È«²¿ÔªËØ\n"); + printf("\t0£ºÍ˳ö\n"); + printf("\t======================\n"); + printf("\tÑ¡Ôñ(0, 1, 2, 3, 4): "); + scanf_s("%d",&ch); + printf("\n"); + + switch (ch) { + case 1: + printf("[INSERT] ÇëÊäÈëÒª²åÈëÔªËØ£º"); + scanf_s("%d",&data); + if (Push(stack,data)) { + printf("[SUCCESS] ÈëÕ»Íê³É\n"); + printf("\n"); + } else { + printf("[FAIL] Èëջʧ°Ü\n"); + printf("\n"); + } + goto Start; + case 2: + printf("[OUTPUT] ³öÕ»Êý¾Ý£º%d\n", Pop(stack)); + printf("\n"); + goto Start; + case 3: + printf("[SELECT] Õ»¶¥Êý¾Ý£º%d\n",GetTop(stack)); + printf("\n"); + goto Start; + case 4: + GetAll(stack); + goto Start; + default: + break; + } + return 0; +} \ No newline at end of file