zoukankan      html  css  js  c++  java
  • C#学习笔记(七):结构体、数组、冒泡排序和调试

    结构体

    结构体不能重写默认无参构造函数

    一位数组

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace m1w2d3_struct_array
    {
        //用结构体描述一个学生的信息
        struct Student
        {
            public Point postion;
            public Rect body;
            int id;
            public string name;
            public float cSharp;
            float unity;
            public ConsoleColor color;
            //Student desker;不能包含自身,会死循环           
        }
        //用结构体描述二维坐标
        struct Point //Postion太具体了
        {
            public int x;
            public int y;
        }
        //用结构体描述三维坐标
        struct Point3d //Postion太具体了
        {
            public int x;
            public int y;
            public int z;
        }
        //用结构体描述颜色
        struct Color
        {
            public int r;
            public int g;
            public int b;
        }
        //用结构体描述长宽高
        struct Shape
        {
            public int length;
            public int wide;
            public int high;
        }
        //用结构体描述一个矩形
        struct Rect
        {
            public Point postion;
            public Point size;
        }
        class Program
        {
            static void Main(string[] args)
            {
                #region 结构体
                //结构体
                //用途:一般情况下我们使用结构体来描述复杂事物,如桌子,如位置
                //这个复杂事物不能包含自己,会发生递归调用
                //结构体用于:属性不多,频繁计算的复杂事物
                //结构体储存在栈里,运行速度快,但栈里的空间宝贵,所以用来描述的事物不宜过多(<=5)(也可以不放在栈里,运算不用跳地址)
                //一般用于定义常用的数学概念及其需要计算的结果
                //
                //位置{行,列}
                //桌子{长方形,颜色(RGB),容积,音乐}
                //
                //用结构体描述一个学生的信息
                //{
                //    学号,csharp成绩,unity成绩
                //}
                //将一个现实中的事物抽象成程序的类型时
                //我们不必将所有的属性写上
                //我们只抽象需要用到的属性
                //如果我们做的是一个校园恋爱的游戏
                //{身高,体重,颜值,血型,星座,性别}
                //如果我们做的是一个校园战斗的游戏
                //{攻击,防御,移动速度}
                //如果我们做的是一个校园教育养成
                //{csharp成绩,unity成绩}
                #endregion
                #region 结构体的定义与使用
                //定义结构体的类型
                //struct 自定义类型名{成员;成员;}
                //成员之间用分号隔开
                //成员是其它数据类型
                //成员需要外部访问要用public
                //定义结构体的变量
                //数据类型 变量名 = 初始值;       
                //Student xiaoMing = new Student();//一次性给结构体里所有变量附初值
                //使用变量
                //使用结构体我们可以.获取他的可访问(作用域允许)成员
                //使用结构就是使用其成员,但是这个成员需要有访问权限
                //我们可以在成员前加public关键字,来修改访问权限为公开
                //xiaoMing.name = "小明";
                //Console.WriteLine(xiaoMing.name);
                #endregion
                #region 结构体的练习
                //用结构体描述二维坐标的位置
                Point a = new Point();
                a.x = 10;
                a.y = 5;
                Console.WriteLine(a.x);
                Console.WriteLine(a.y);
                Point3d b = new Point3d();
                b.x = 10;
                b.y = 5;
                b.z = 8;
                Console.WriteLine(b.x);
                Console.WriteLine(b.y);
                Console.WriteLine(b.z);
                Color c = new Color();
                c.r = 100;
                c.g = 50;
                c.b = 80;
                Console.WriteLine(c.r);
                Console.WriteLine(c.g);
                Console.WriteLine(c.b);
                Shape d = new Shape();
                d.length = 60;
                d.wide = 70;
                d.high = 80;
                Console.WriteLine(d.length);
                Console.WriteLine(d.wide);
                Console.WriteLine(d.high);
                Square e = new Square();
                e.mySquare.length = int.Parse(Console.ReadLine());
                e.myColor.r = int.Parse(Console.ReadLine());
                #endregion
                #region 复杂类型综合练习
                {
                    //实例化一个学生
                    Student xiaoMing = new Student();
                    Student xiaoHua = new Student();
                    //给学生一个位置,一个大小,颜色
                    xiaoMing.name = "小明";
                    xiaoMing.postion.x = 5;
                    xiaoMing.postion.y = 5;
                    xiaoMing.body.size.x = 2;
                    xiaoMing.body.size.y = 3;
                    xiaoMing.color = ConsoleColor.Magenta;
                    xiaoHua.name = "小花";
                    xiaoHua.postion.x = 10;
                    xiaoHua.postion.y = 5;
                    xiaoHua.body.size.x = 1;
                    xiaoHua.body.size.y = 2;
                    xiaoHua.color = ConsoleColor.DarkBlue;
                    //把学生绘制在位置
                    #region 把学生绘制在位置
                    for (int y = 0; y < xiaoMing.body.size.y + 1; y++)
                    {
                        Console.SetCursorPosition(xiaoMing.postion.x * 2, xiaoMing.postion.y+y);
                        for (int x = 0; x < xiaoMing.body.size.x; x++)
                        {
                            if (y == xiaoMing.body.size.y && x == 0)
                            {
                                Console.WriteLine(xiaoMing.name);
                                break;
                            }
                            else
                            {
                                //头希望绿色的
                                if (y == 0)
                                {
                                    Console.BackgroundColor = ConsoleColor.DarkGreen;
                                }
                                else
                                {
                                    //其它部分是学生本身的颜色
                                    Console.BackgroundColor = xiaoMing.color;
                                }
                                Console.Write("  ");
                                Console.BackgroundColor = ConsoleColor.Black;
                            }
                        }
                    }
                    for (int y = 0; y < xiaoHua.body.size.y + 1; y++)
                    {
                        Console.SetCursorPosition(xiaoHua.postion.x * 2, xiaoHua.postion.y + y);
                        for (int x = 0; x < xiaoHua.body.size.x; x++)
                        {
                            if (y == xiaoHua.body.size.y && x == 0)
                            {
                                Console.WriteLine(xiaoHua.name);
                                break;
                            }
                            else
                            {
                                //头希望绿色的
                                if (y == 0)
                                {
                                    Console.BackgroundColor = ConsoleColor.DarkGreen;
                                }
                                else
                                {
                                    //其它部分是学生本身的颜色
                                    Console.BackgroundColor = xiaoHua.color;
                                }
                                Console.Write("  ");
                                Console.BackgroundColor = ConsoleColor.Black;
                            }
                        }
                    }
                    #endregion
                    //等待按键,通过不同按键修改学生位置,重新绘制
                    while (true)
                    {
                        char input = Console.ReadKey(true).KeyChar;
                        Console.Clear();
                        switch (input)
                        {
                            case 'w':
                                xiaoMing.postion.y--;
                                xiaoMing.postion.y = xiaoMing.postion.y < 0 ? 0 : xiaoMing.postion.y;
                                break;
                            case 's':
                                xiaoMing.postion.y++;
                                break;
                            case 'a':
                                xiaoMing.postion.x--;
                                xiaoMing.postion.x = xiaoMing.postion.x < 0 ? 0 : xiaoMing.postion.x;
                                ; break;
                            case 'd':
                                xiaoMing.postion.x++;
                                break;
                            case 'i':
                                xiaoHua.postion.y--;
                                xiaoHua.postion.y = xiaoHua.postion.y < 0 ? 0 : xiaoHua.postion.y;
                                break;
                            case 'k':
                                xiaoHua.postion.y++;
                                break;
                            case 'j':
                                xiaoHua.postion.x--;
                                xiaoHua.postion.x = xiaoHua.postion.x < 0 ? 0 : xiaoHua.postion.x;
                                ; break;
                            case 'l':
                                xiaoHua.postion.x++;
                                break;
                        }
                        //重新绘制
                        for (int y = 0; y < xiaoHua.body.size.y + 1; y++)
                        {
                            Console.SetCursorPosition(xiaoHua.postion.x * 2, xiaoHua.postion.y + y);
                            for (int x = 0; x < xiaoHua.body.size.x; x++)
                            {
                                if (y == xiaoHua.body.size.y && x == 0)
                                {
                                    Console.WriteLine(xiaoHua.name);
                                    break;
                                }
                                else
                                {
                                    //头希望绿色的
                                    if (y == 0)
                                    {
                                        Console.BackgroundColor = ConsoleColor.DarkGreen;
                                    }
                                    else
                                    {
                                        //其它部分是学生本身的颜色
                                        Console.BackgroundColor = xiaoHua.color;
                                    }
                                    Console.Write("  ");
                                    Console.BackgroundColor = ConsoleColor.Black;
                                }
                            }
                        }
                        for (int y = 0; y < xiaoMing.body.size.y + 1; y++)
                        {
                            Console.SetCursorPosition(xiaoMing.postion.x * 2, xiaoMing.postion.y + y);
                            for (int x = 0; x < xiaoMing.body.size.x; x++)
                            {
                                if (y == xiaoMing.body.size.y && x == 0)
                                {
                                    Console.WriteLine(xiaoMing.name);
                                    break;
                                }
                                else
                                {
                                    //头希望绿色的
                                    if (y == 0)
                                    {
                                        Console.BackgroundColor = ConsoleColor.DarkGreen;
                                    }
                                    else
                                    {
                                        //其它部分是学生本身的颜色
                                        Console.BackgroundColor = xiaoMing.color;
                                    }
                                    Console.Write("  ");
                                    Console.BackgroundColor = ConsoleColor.Black;
                                }
                            }
                        }
                    }
                }
                #endregion
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace m1w2d3_array
    {
        class Program
        {
            static void Main(string[] args)
            {
                //数组是一种最基本的 数据结构 线性并列且连续,固定大小
                //所有的数据都有它的自有结构          
                //数组是其它数据类型集合,这个集合要求所有成员(元素)是同一类型
                //1、定义一个数组变量
                //数据类型[] 变量名;
                int[] array;
                //数据类型[] 变量名 = {成员,成员};
                int[] array1 = { 1, 2, 3, 4, 5 };//不指定元素个数,只指定元素
                int[] array2 = new int[8] { 1, 2, 3, 4, 5, 6, 7, 8 };//先限制元素个数,然后再指定元素
                //数据类型[] 变量名 = 指定的长度空数组。
                int[] array3 = new int[8];//最常用的形式,不关心值,只关心处理逻辑
                //元素的值是默认值(微软MSDN默认值)
                //数值类型是对应0
                //如果字符串是" "
                //2、使用数组
                //访问成员,通过下标 变量名[下标号]访问对应下标的成员
                array1[2] = 100;
                Console.WriteLine(array1[2]);
                //如果我使用的下标是负数,或者超出了元素个数,指定了数组中一个不存在的元素
                //会出现 越界异常
                //下标的有效范围是(0 - 数组名.Length-1)
                Console.WriteLine(array1.Length);
                Console.WriteLine(array1.[array1.Length - 1 ]);
                //通过数组名.Length我们能知道这个数组中有多少个元素
                //3、遍历数组
                //遍历就是从一个 数据结构 第一个元素 到 最后一个元素
                //遍历就是把 数据结构中 所有元素访问完
                //创建一个长度为n的数组A,值与下标一样。
                int n = 1000;
                int[] a = new int[n];
                for (int i = 0; i < a.Length; i++)
                {
                    a[i] = i;
                    Console.WriteLine("下标:{0},值:{1}", i, a[i]);
                }
                //创建另一个数组b,将数组a中每个元素的值乘以2存入数组b。
                //编程理念:一个过程只做一个事情,结构比算法重要,可读性大于算法精妙
                //数据处理和显示要分开
                int[] b = new int[a.Length];
                for (int i = 0; i < b.Length; i++)
                {
                    b[i] = a[i] * 2;//处理过程
                }
                for (int i = 0; i < b.Length; i++)
                {
                    Console.WriteLine(b[i]);//显示过程
                }
                #region 数组综合练习
                //从一个整数数组中找出最大值、最小值、总和、平均值。(可以使用随机数1~100)
                Random roll = new Random();
                int[] array1 = new int[10];           
                float sum = 0;
                float average = 0;
                int exchange = 0;
                //赋随机值
                for (int i = 0; i < array1.Length; i++)
                {
                    array1[i] = roll.Next(1, 101);
                    Console.WriteLine(array1[i]);
                }
                Console.WriteLine();
                //找最大值
                int max = array1[0];//max和min的值要在数组区间之内
                for (int i = 0; i < array1.Length; i++)
                {
                    if (max < array1[i])
                    {
                        max = array1[i];
                    }
                }
                Console.WriteLine(max);
                //找最小值
                int min = array1[0];
                for (int i = 0; i < array1.Length; i++)
                {
                    if (min > array1[i])
                    {
                        min = array1[i];
                    }
                }
                Console.WriteLine(min);
                //总和
                for (int i = 1; i < array1.Length; i++)
                {
                    sum += array1[i];
                }
                Console.WriteLine(sum);
                //平均值
                average = sum / 10;
                Console.WriteLine(average);
                Console.WriteLine();
                //交换这个数组中的第一个和最后一个、第二个和倒数第二个、依次类推,把数组进行反转。
                for (int i = 0; i < (array1.Length / 2); i++)
                {
                    exchange = array1[i];
                    array1[i] = array1[array1.Length - 1 - i];
                    array1[array1.Length - 1 - i] = exchange;
                }
                for (int i = 0; i < array1.Length; i++)
                {
                    Console.WriteLine(array1[i]);//显示过程
                }
                Console.WriteLine();
                //随机(0~100)生成1个长度为10的整数数组
                int[] array2 = new int[10];
                for (int i = 0; i < array2.Length; i++)
                {
                    array2[i] = roll.Next(1, 101);
                    Console.WriteLine(array2[i]);
                }
                Console.WriteLine();
                //将数组用 | 分割输出, 如 10 | 108 | 99 | 5
                for (int i = 0; i < array2.Length; i++)
                {
                    if (i < array2.Length - 1)
                    {
                        Console.Write("{0}|", array2[i]);
                    }
                    else
                    {
                        Console.Write("{0}", array2[i]);
                    }
                }
                #endregion
            }
        }
    }

    复习

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace m1w2d3_review
    {
        class Program
        {
            //匈牙利命名法
            int m_nAge = 20;
            string m_szName = "111";
            //变量的作用范围
            static int num1;//全局变量,自动添加初始值
            int num2;//成员变量,自动添加初始值
            struct Person
            {
                public int age;
                public string name;
            }
            static Person[] persons = new Person[4];
            static string XX;
            static void Main(string[] args)//入口函数
            {
                int num3 = 100;//局部变量,没有初始值
                string name = "aa";
                //隐式转换
                byte numA = 100;
                int numB = numA;
                float numC = numB;
                //显式转换,精度丢失
                numC = 100.0f;
                numB = (int)numC;//强转符
                numB = Convert.ToInt32(numC);//Convert
                numB = int.Parse(numC + "");//Parse
                Console.WriteLine(numB);
                Console.ReadKey();
                //显式转换的兼容类型
                //1、基本数据类型:整数之间
                //2、复杂数据类型:枚举
                //3、类和对象:??
                int a = 2;
                //if (1 < a < 3)//布尔表达式表示逻辑关系,结果只能是一个bool值(1<a&&a<3)
                //{
                //}
                //逻辑运算符的优化特点:以&&为例,从左至右执行,当左边的为false,那么右边的不会执行
                int num1 = 10;
                int num2 = 0;
                //不报错
                if (num1 > 10 && num1 / num2 > 0)
                {
                    Console.WriteLine("123");
                }
                Console.WriteLine("over");
                Console.ReadKey();
                //报错
                if (num1 < 10 && num1 / num2 > 0)
                {
                    Console.WriteLine("123");
                }
                Console.WriteLine("over");
                Console.ReadKey();
                //int a = 0;
                //int num1 = (a++)+(++a)://1 + 3
                //位运算符
                //二进制
                //11 = 3
                //101 = 5
                //1011 = 11
                //十进制转二进制:除2取余法
                //二进制转十进制:数按权展开、相加
                float a1 = 8 / 3;
                Console.WriteLine(a1);
                //输出2
                float a2 = 8.0f / 3;
                Console.WriteLine(a2);
                //输出2.666667
                //int a = 7;
                //int b = 11;
                //int c = a & b;//按位与
                #region foreach 迭代
                // foreach 用来遍历集合(数组及其他...)
                //数组名是引用类型
                int[] nums = { 2, 3, 4, 7 };
                // in 后面填集合
                //第一次访问集合中的第一个元素的时候,会把第一个元素的值赋值给item
                //
                foreach (var item in nums)//var 是推断类型
                {
                    //item *= 2;//会报错。item 是只读的(针对于值类型的数据)
                    Console.WriteLine(item);
                }
                //var 是推断类型 根据赋值的数据类型 推断出变量的类型
                //用 var 创建的变量必须赋初始值
                var name1 = "12345";
                int[] nums1 = new int[] { 2, 3, 4, 7 };
                var nums2 = new[] { 2, 3, 4, 7 };//可以推断出数组的类型和数组的长度
                var persons = new Person[4];
                for (int i = 0; i < persons.Length; i++)
                {
                    persons[i] = new Person();
                }
                foreach (var item in persons)
                {
                    if (item.name == "")
                    {
                        Console.Write("''	");
                    }
                    if (item.name == null)
                    {
                        Console.Write("null	");
                    }
                    if (string.IsNullOrEmpty(item.name))
                    {
                        Console.Write("*	");
                    }
                    else
                    {
                        Console.WriteLine(item.name + "	");
                    }
                    Console.WriteLine(item.age);
                }
                #endregion
            }
        }
    }

    代码调试

    双击侧边栏,打断点

    逐条进行

    &取地址符,存在栈里的地址

    num: 内存地址:0x064beb48,值:100
    name:内存地址:0x064beb44,值:35771180(值里面存的是堆的地址)
  • 相关阅读:
    内存队列使用Channels
    笔记20210101mongodb
    管道式的开发模式
    企业级应用架构设计
    再入历史旧坑
    路径问题 再次记录
    mongdb驱动的问题
    使用Bumblebee记录
    我和小兔子不得不说的消息v2
    流程设计器jQuery + svg/vml(Demo7
  • 原文地址:https://www.cnblogs.com/vuciao/p/10362346.html
Copyright © 2011-2022 走看看