diff --git a/DataStructureAndAlgorithm/02 C#语言基本语法结构.md b/DataStructureAndAlgorithm/02 C#语言基本语法结构.md new file mode 100644 index 0000000..2fe90a7 --- /dev/null +++ b/DataStructureAndAlgorithm/02 C#语言基本语法结构.md @@ -0,0 +1,512 @@ +# 02 C#Ի﷨ṹ + +**֪ʶṹ** + +![ͼ1 ֪ʶṹ](https://img-blog.csdnimg.cn/20200907113848271.png) + +--- +## 1 + +һַࣺ + +- ͣ`byte``short``int``long``float``double``char``bool` +- ͣ`struct``enum``class``interface` + + +![ͼ2 ](https://img-blog.csdnimg.cn/20200907114758708.png) + +ڶַࣺ + +- ֵͣΪʱݿ + - ͡struct͡enum +- ͣΪʱݵַ + - class͡ + +1 +```c +public struct Book +{ + public double Price; + public string Title; + public string Author; +} + +class Program +{ + static void ChangeBook(Book bk) + { + bk.Price = 1.01; + bk.Title = "Spss"; + bk.Author = "John"; + } + + static void PrintBook(Book bk) + { + Console.WriteLine("Book Infor:\n Price={0},Tile={1},Author={2}", + bk.Price, bk.Title, bk.Author); + } + static void Main(string[] args) + { + Book bk;// = new Book(); + bk.Price = 10.01; + bk.Title = "MatLab"; + bk.Author = "Tom"; + PrintBook(bk); + ChangeBook(bk); + PrintBook(bk); + + //Book Infor: + //Price=10.01,Tile=MatLab,Author=Tom + //Book Infor: + //Price=10.01,Tile=MatLab,Author=Tom + } +} +``` +˵ֵ͡ݿı䱾洢ֵ + +2 +```c +public class Book +{ + public double Price; + public string Title; + public string Author; +} + +class Program +{ + static void ChangeBook(Book bk) + { + bk.Price = 1.01; + bk.Title = "Spss"; + bk.Author = "John"; + } + + static void PrintBook(Book bk) + { + Console.WriteLine("Book Infor:\n Price={0}, Tile={1}, Author={2}", + bk.Price, bk.Title, bk.Author); + } + + static void Main(string[] args) + { + Book bk = new Book(); // Book bk; + bk.Price = 10.01; + bk.Title = "MatLab"; + bk.Author = "Tom"; + PrintBook(bk); + // Book Infor: + // Price = 10.01, Tile = MatLab, Author = Tom + + ChangeBook(bk); + PrintBook(bk); + // Book Infor: + // Price = 1.01, Tile = Spss, Author = John + } +} +``` + +3 +```c +class Program +{ + static void ChangeArrayItem(int[] array) + { + for (int i = array.Length - 1; i >= 0; i--) + { + array[i] = array.Length - 1 - i; + } + } + static void PrintArrayItem(int[] arry) + { + for (int i = 0; i < arry.Length; i++) + { + Console.Write("{0} ", arry[i]); + } + Console.WriteLine(); + } + + static void Main(string[] args) + { + int[] arr = new int[3]; + for (int i = 0; i < arr.Length; i++) + { + arr[i] = i; + } + PrintArrayItem(arr); // 0 1 2 + ChangeArrayItem(arr); + PrintArrayItem(arr); // 2 1 0 + } +} +``` + +23˵͡ݵַҪı䱾洢ֵӦʱҪע⡰ֵ͡͡͡ + + +--- +## 2볣 + +- 壺` ;` +- 壺 + - `readonly` 캯гʼ + - `const` ʱʼ + +4 +```c +public class SimpleClass +{ + public int X; + public readonly int Y = 2; + public readonly int Z; + public const double Pi = 3.1415926; + public const string Etc = "..."; + public SimpleClass() + { + Z = 3; + } + public SimpleClass(int p1, int p2, int p3) + { + X = p1; + Y = p2; + Z = p3; + } +} + +class Program +{ + static void Main(string[] args) + { + SimpleClass sp1 = new SimpleClass(); + sp1.X = 1; + Console.WriteLine("sp1:x={0}, y={1}, z={2}", sp1.X, sp1.Y, sp1.Z); + // sp1: x = 1, y = 2, z = 3 + + SimpleClass sp2 = new SimpleClass(-1, -2, -3); + Console.WriteLine("sp2:x={0} ,y={1}, z={2}", sp2.X, sp2.Y, sp2.Z); + // sp2: x = -1 ,y = -2, z = -3 + + Console.WriteLine("PI={0}{1}", SimpleClass.Pi, SimpleClass.Etc); + // PI = 3.1415926... + } +} +``` + +ע`readonly``const`峣Լʹøóʱ + +--- +## 3ʽ + + +- һԪ `x++`,`y++` +- Ԫ `x+y`,`x-y` +- Ԫ `max = (x>y)?x:y;` + + + +- `+-*/%` +- ϵ `>>===!=<=<` +- ߼ `!&&||` + +ʽͱɵʽӡ + + + +--- +## 4 + +**4.1 ֵ** + + = ʽ; + +**4.2 ** + +һ֣ +```c +if(ʽ) +{ + ; +} +``` + +ڶ֣ +```c +if(ʽ) +{ + ; +} +else +{ + ; +} +``` + +֣ +```c +if(ʽ1) +{ + 1; +} +else if(ʽ2) +{ + 2; +} +else if(ʽN) +{ + N; +} +else +{ + N+1; +} +``` + +**4.3 ** + +```c +swith(ʽ) +{ + case ֵ11; break; + case ֵ22; break; + case ֵNN; break; + defaultN+1; break; +} +``` + +5 +```c +class Program +{ + static void Main(string[] args) + { + Random rdm = new Random(); + int i = rdm.Next(1, 5); + Console.WriteLine(i); // 1 + switch (i) + { + case 1: + Console.WriteLine("Case 1."); + break; + case 2: + Console.WriteLine("Case 2."); + break; + case 3: + Console.WriteLine("Case 3."); + break; + default: + Console.WriteLine("Default Case."); + break; + } + // Case 1. + + i = rdm.Next(1, 5);// 1 + Console.WriteLine(i); + switch (i) + { + case 1: + case 2: + case 3: + Console.WriteLine("It's 1,2 or 3."); + break; + default: + Console.WriteLine("Not Sure What it is."); + break; + } + //It's 1,2 or 3. + } +} +``` + +ע`switch`﷨ṹرÿ`case`䶼Ҫƥ`break`䡣 + +**4.4 ѭ** + +һ֣ +```c +for(ʼѭʽ;жѭֹ;ݼѭʽ) +{ + ; +} +``` + +ڶ֣ + +```c +while(ʽ) +{ + ; +} +``` + +֣ +```c +do +{ + +}while(ʽ); +``` + +֣ +```c +foreach(Ԫ Ԫ in ) +{ + ;//ͨڱеÿԪ +} +``` + +6 + +```c +class Program +{ + static void Main(string[] args) + { + int i; + int sum = 0; + for (i = 1; i <= 10; i++) + { + sum += i; + } + Console.WriteLine(sum);// 55 + + sum = 0; + i = 1; + while (i <= 10) + { + sum += i; + i++; + } + Console.WriteLine(sum);// 55 + + sum = 0; + i = 1; + do + { + sum += i; + i++; + } while (i <= 10); + Console.WriteLine(sum);// 55 + } +} +``` + + + + +7 + +```c +class Program +{ + static void Main(string[] args) + { + int[] arry = new int[] { 1, 3, 5, 7 }; + foreach (int i in arry) + { + Console.WriteLine(i); + } + // 1 + // 3 + // 5 + // 7 + } +} +``` + +**4.5 trycatchfinally ** + +```c +try +{ + ; +} +catch(Exception ex) +{ + ; +} +finally +{ + ; +} +``` + +8ĻһĻʾš*š + +```c +class Program +{ + static void Main(string[] args) + { + Console.WriteLine("һ:"); + // һ: + // abc + + string sTemp = Console.ReadLine(); + try + { + int iCount = int.Parse(sTemp); + for (int i = 0; i < iCount; i++) + { + Console.Write("*"); + } + Console.WriteLine(); + } + catch (Exception ex) + { + Console.WriteLine("ԭΪ:" + ex.Message); + // ԭΪ:ַĸʽȷ + } + finally + { + Console.WriteLine("."); + // . + } + } +} +``` + +ע`trycatchfinally`﷨ṹͨ񲢴쳣 + + +**4.6 breakcontinue ** + +- `break`䣺ķѭ`switch` +- `continue`䣺Ȩݸڷѭһε + +9 +```c +class Program +{ + static void Main(string[] args) + { + for (int i = 1; i <= 100; i++) + { + if (i == 5) + break; + Console.WriteLine(i); + } + // 1 + // 2 + // 3 + // 4 + + for (int i = 1; i <= 100; i++) + { + if (i < 99) + continue; + Console.WriteLine(i); + } + // 99 + // 100 + } +} +``` + + +**4.7 ע** + +- עͣ`//` +- עͣ`/**/` + + + + diff --git a/DataStructureAndAlgorithm/02 数组.md b/DataStructureAndAlgorithm/03 数组.md similarity index 100% rename from DataStructureAndAlgorithm/02 数组.md rename to DataStructureAndAlgorithm/03 数组.md diff --git a/DataStructureAndAlgorithm/03 顺序表和链表.md b/DataStructureAndAlgorithm/04 顺序表和链表.md similarity index 100% rename from DataStructureAndAlgorithm/03 顺序表和链表.md rename to DataStructureAndAlgorithm/04 顺序表和链表.md diff --git a/DataStructureAndAlgorithm/04 栈与递归.md b/DataStructureAndAlgorithm/06 栈与递归.md similarity index 100% rename from DataStructureAndAlgorithm/04 栈与递归.md rename to DataStructureAndAlgorithm/06 栈与递归.md diff --git a/DataStructureAndAlgorithm/06 字符串.md b/DataStructureAndAlgorithm/07 字符串.md similarity index 100% rename from DataStructureAndAlgorithm/06 字符串.md rename to DataStructureAndAlgorithm/07 字符串.md