From 921e096721685e3e2c2299b47df7572aca534f85 Mon Sep 17 00:00:00 2001
From: Kister Hakusan <2753888203@qq.com>
Date: Sat, 11 Oct 2025 11:05:51 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Lesson2?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
C#进阶/C#进阶.sln | 12 ++
C#进阶/Lesson2_Stack/Lesson2_Stack.csproj | 10 ++
C#进阶/Lesson2_Stack/Program.cs | 109 ++++++++++++++++++
.../Lesson2_Stack练习题.csproj | 10 ++
C#进阶/Lesson2_Stack练习题/Program.cs | 24 ++++
5 files changed, 165 insertions(+)
create mode 100644 C#进阶/Lesson2_Stack/Lesson2_Stack.csproj
create mode 100644 C#进阶/Lesson2_Stack/Program.cs
create mode 100644 C#进阶/Lesson2_Stack练习题/Lesson2_Stack练习题.csproj
create mode 100644 C#进阶/Lesson2_Stack练习题/Program.cs
diff --git a/C#进阶/C#进阶.sln b/C#进阶/C#进阶.sln
index 87dd804..4530a4d 100644
--- a/C#进阶/C#进阶.sln
+++ b/C#进阶/C#进阶.sln
@@ -9,6 +9,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson1_ArrayList", "Lesson
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson1_ArrayList练习题", "Lesson1_ArrayList练习题\Lesson1_ArrayList练习题.csproj", "{B9F9E174-21EA-40E7-A139-58FFD3448023}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson2_Stack", "Lesson2_Stack\Lesson2_Stack.csproj", "{8353E167-0804-4902-A15A-3FA209EB6C6E}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson2_Stack练习题", "Lesson2_Stack练习题\Lesson2_Stack练习题.csproj", "{BE917425-E4A8-4C51-89BE-8F6DD86A1C00}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -27,6 +31,14 @@ Global
{B9F9E174-21EA-40E7-A139-58FFD3448023}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B9F9E174-21EA-40E7-A139-58FFD3448023}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B9F9E174-21EA-40E7-A139-58FFD3448023}.Release|Any CPU.Build.0 = Release|Any CPU
+ {8353E167-0804-4902-A15A-3FA209EB6C6E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {8353E167-0804-4902-A15A-3FA209EB6C6E}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {8353E167-0804-4902-A15A-3FA209EB6C6E}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {8353E167-0804-4902-A15A-3FA209EB6C6E}.Release|Any CPU.Build.0 = Release|Any CPU
+ {BE917425-E4A8-4C51-89BE-8F6DD86A1C00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {BE917425-E4A8-4C51-89BE-8F6DD86A1C00}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {BE917425-E4A8-4C51-89BE-8F6DD86A1C00}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {BE917425-E4A8-4C51-89BE-8F6DD86A1C00}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
diff --git a/C#进阶/Lesson2_Stack/Lesson2_Stack.csproj b/C#进阶/Lesson2_Stack/Lesson2_Stack.csproj
new file mode 100644
index 0000000..2150e37
--- /dev/null
+++ b/C#进阶/Lesson2_Stack/Lesson2_Stack.csproj
@@ -0,0 +1,10 @@
+
+
+
+ Exe
+ net8.0
+ enable
+ enable
+
+
+
diff --git a/C#进阶/Lesson2_Stack/Program.cs b/C#进阶/Lesson2_Stack/Program.cs
new file mode 100644
index 0000000..400ea17
--- /dev/null
+++ b/C#进阶/Lesson2_Stack/Program.cs
@@ -0,0 +1,109 @@
+using System.Collections;
+namespace Lesson2_Stack
+{
+ class Test()
+ {
+
+ }
+ class Program
+ {
+ static void Main(string[] args)
+ {
+ #region 知识点一 Stack本质
+ //stack是C#是为我们封装好的类
+ //本质是一个Object数组,只是封装了特殊的存储规则
+
+ //stack是栈存储容器,栈是一种先进后出的数据结构
+ //先存入的数据后取出,后存入的数据先取出
+ //栈的存储规则:后进先出
+ #endregion
+
+ #region 知识点二 声明
+ //需要引用命名空间system.collections
+ Stack stack = new Stack();
+ #endregion
+
+ #region 知识点三 增取查改
+
+ #region 增
+ //压栈
+ stack.Push(1);
+ stack.Push("123");
+ stack.Push(true);
+ stack.Push(3.14f);
+ stack.Push(new Test());
+ #endregion
+
+ #region 取
+ //栈中不存在删除的概念
+ //只能取出栈顶的数据
+ //弹栈
+ object v = stack.Pop();//弹出栈顶元素,下次弹出的是下一个元素
+ Console.WriteLine(v);
+ v = stack.Pop();
+ Console.WriteLine(v);
+ #endregion
+
+ #region 查
+ //栈无法查看指定位置的元素,栈没有提供索引器
+ //只能查看栈顶的元素
+ v = stack.Peek();//只看不弹出
+ Console.WriteLine(v);
+ v = stack.Peek();
+ Console.WriteLine(v);
+
+ //查看元素是否存在于栈中
+ if (stack.Contains(3.14f))
+ {
+ Console.WriteLine("存在3.14");
+ }
+ if (stack.Contains("123"))
+ {
+ Console.WriteLine("存在123");
+ }
+ #endregion
+
+ #region 改
+ //栈无法修改指定位置的元素
+ //只能修改栈顶的元素或清空
+ stack.Clear();//清空栈
+ stack.Push("1");
+ stack.Push(2);
+ stack.Push("gfjd");
+ #endregion
+ #endregion
+
+ #region 知识点四 遍历
+ //1.长度
+ Console.WriteLine(stack.Count);
+
+ //2.遍历
+ foreach(Object o in stack)
+ {
+ Console.WriteLine(o);
+ }
+
+ //3.还有一种遍历方式
+ //先将栈转换为数组
+ //注意转换后的数组是倒序的
+ object[] arr = stack.ToArray();
+ for(int i = 0;i < arr.Length; i++)
+ {
+ Console.WriteLine(arr[i]);
+ }
+
+ //4.循环弹栈
+ while(stack.Count > 0)
+ {
+ Console.WriteLine(stack.Pop());
+ }
+ #endregion
+
+ #region 知识点五
+ //由于用object来存储数据,所以存储值类型时会装箱,取出时需要拆箱
+ //Push值类型时装箱
+ //弹出时拆箱
+ #endregion
+ }
+ }
+}
diff --git a/C#进阶/Lesson2_Stack练习题/Lesson2_Stack练习题.csproj b/C#进阶/Lesson2_Stack练习题/Lesson2_Stack练习题.csproj
new file mode 100644
index 0000000..2150e37
--- /dev/null
+++ b/C#进阶/Lesson2_Stack练习题/Lesson2_Stack练习题.csproj
@@ -0,0 +1,10 @@
+
+
+
+ Exe
+ net8.0
+ enable
+ enable
+
+
+
diff --git a/C#进阶/Lesson2_Stack练习题/Program.cs b/C#进阶/Lesson2_Stack练习题/Program.cs
new file mode 100644
index 0000000..69c1a76
--- /dev/null
+++ b/C#进阶/Lesson2_Stack练习题/Program.cs
@@ -0,0 +1,24 @@
+using System.Collections;
+namespace Lesson2_Stack练习题
+{
+ internal class Program
+ {
+ static void Main(string[] args)
+ {
+ Console.Write("输入一个数字:");
+ int num = int.Parse(Console.ReadLine());
+ Stack bio = new Stack();
+ while (num != 0)
+ {
+ int r = num % 2;
+ bio.Push(r);
+ num /= 2;
+ }
+ Console.Write("你输入的数字二进制为:");
+ while (bio.Count > 0)
+ {
+ Console.Write(bio.Pop());
+ }
+ }
+ }
+}