添加Lesson2

This commit is contained in:
2025-10-11 11:05:51 +08:00
parent 5e8c571bb3
commit 921e096721
5 changed files with 165 additions and 0 deletions
+12
View File
@@ -9,6 +9,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson1_ArrayList", "Lesson
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson1_ArrayList练习题", "Lesson1_ArrayList练习题\Lesson1_ArrayList练习题.csproj", "{B9F9E174-21EA-40E7-A139-58FFD3448023}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson1_ArrayList练习题", "Lesson1_ArrayList练习题\Lesson1_ArrayList练习题.csproj", "{B9F9E174-21EA-40E7-A139-58FFD3448023}"
EndProject 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 Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU 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}.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.ActiveCfg = Release|Any CPU
{B9F9E174-21EA-40E7-A139-58FFD3448023}.Release|Any CPU.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
+109
View File
@@ -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
}
}
}
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
@@ -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());
}
}
}
}