鱼C论坛

 找回密码
 立即注册
查看: 2985|回复: 6

[学习笔记] 开始学 C# 记录贴!

[复制链接]
发表于 2021-2-24 18:26:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Cool_Breeze 于 2021-2-25 21:02 编辑

汽车加油站! switch 使用
  1. using System;
  2. using System.Threading;

  3. namespace Switch
  4. {
  5.     class He
  6.     {
  7.         static void Main()
  8.         {
  9.             double a = 6.8;
  10.             double b = 6.42;
  11.             double c = 7.02;
  12.             double d = 5.75;
  13.             Thread.Sleep(1000);
  14.             Console.WriteLine("----------欢迎光临------");
  15.             Console.WriteLine("请选择燃油种类");
  16.             Console.WriteLine("1:90号\t 2:93号汽油\t3:97号汽油\t4:0号汽油");
  17.             string n;
  18.             He method = new He();
  19.             n = Console.ReadLine();
  20.             switch (n)
  21.             {
  22.                 case "1":
  23.                     Console.WriteLine("您选择的是90号汽油,价格为:{0}元/升", a);
  24.                     method.Shen(a);
  25.                     break;
  26.                 case "2":
  27.                     Console.WriteLine("您选择的是93号汽油,价格为:{0}元/升", b);
  28.                     method.Shen(b);
  29.                     break;
  30.                 case "3":
  31.                     Console.WriteLine("您选择的是97号汽油,价格为:{0}元/升", c);
  32.                     method.Shen(c);
  33.                     break;
  34.                 case "4":
  35.                     Console.WriteLine("您选择的是90号柴油,价格为:{0}元/升", d);
  36.                     method.Shen(d);
  37.                     break;
  38.                 default:
  39.                     Console.WriteLine("输入有误!");
  40.                     break;
  41.             }
  42.             Console.ReadLine();
  43.         }
  44.         
  45.         public double Shen(double mo)
  46.         {
  47.             Console.WriteLine("请输入您要购买多少升油,例如 5.5 代表您想要购买5.5升油!");
  48.             string n1 = "";
  49.             double n = 0;
  50.             n1 = Console.ReadLine();
  51.             Console.WriteLine("您所要购买的燃油量是:{0} 升", n1);
  52.             n = Convert.ToDouble(n1) * mo;
  53.             Console.WriteLine("请选择服务: A:自助加油    B:协助加油");
  54.             n1 = Console.ReadLine();
  55.             He zhe = new He();
  56.             switch (n1)
  57.             {
  58.                 case "A":
  59.                     Console.WriteLine("您选择的是“自助加油”,优惠10%");
  60.                     zhe.zhekou(ref n);
  61.                     Console.WriteLine("您本次消费金额为:{0}", n*0.9);
  62.                     break;
  63.                 default:
  64.                     Console.WriteLine("您选择的是“协助加油”,优惠5%");
  65.                     zhe.zhekou(ref n);
  66.                     Console.WriteLine("您本次消费金额为:{0}", n*0.95);
  67.                     break;
  68.             }
  69.             return n;
  70.         }
  71.         
  72.         public void zhekou(ref double mo)
  73.         {
  74.             // double res = mo;
  75.             if (mo < 600 && mo >200)
  76.             {
  77.                 // res*=0.85;
  78.                 mo*=0.85;
  79.             }
  80.             else if (mo < 1000 && mo >600)
  81.             {
  82.                 // res*=0.7;
  83.                 mo*=0.7;
  84.             }
  85.             else if (mo > 1000 )
  86.             {
  87.                 // res*=0.6;
  88.                 mo*=0.6;
  89.             }
  90.             // return res;
  91.         }
  92.     }
  93. }
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
hrp + 5 + 5

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-2-25 21:03:14 | 显示全部楼层
  1. using System;
  2. using System.Collections;


  3. namespace Girl
  4. {
  5.     // 抽象类
  6.     public abstract class Animal
  7.     {
  8.         // 虚拟方法 子类中使用 override 重写父类方法
  9.         public virtual void Cry(){
  10.             Console.WriteLine("你听,动物在叫。。。");
  11.         }
  12.         // 未实现的虚拟方法 子类中使用 override 重写父类方法
  13.         // 未实现的虚拟方法(抽象方法)必须写在抽象类里面
  14.         public abstract void Eat();
  15.     }
  16.      
  17.     // 继承自 Animal
  18.     class Cat: Animal
  19.     {
  20.         // 使用 override 重写父类方法
  21.         public override void Cry(){
  22.             Console.WriteLine("你听,小猫在叫。。。");
  23.         }
  24.         // 使用 override 重写父类方法
  25.         public override void Eat()
  26.         {
  27.             Console.WriteLine("你看,小猫在吃小鱼干。。。");
  28.         }
  29.     }
  30.      
  31.     // 继承自 Animal
  32.     class Dog: Animal
  33.     {
  34.         // 使用 override 重写父类方法
  35.         public override void Cry(){
  36.             Console.WriteLine("你听,小狗在叫。。。");
  37.         }
  38.         // 使用 override 重写父类方法
  39.         public override void Eat(){
  40.             Console.WriteLine("你看,哦!小狗正在使劲舔,是吃不到吗!");
  41.         }
  42.     }
  43.      
  44.     class Pragma
  45.     {
  46.         static void Main(){
  47.             // 注意 抽象类不能被实体化,new 子类就好了!
  48.             // Animal 是Cat的基类,都可以申明变量
  49.             Cat cat = new Cat();
  50.             Animal dog = new Dog();
  51.             cat.Cry();
  52.             dog.Cry();
  53.             cat.Eat();
  54.             dog.Eat();
  55.             Mans hua = new Mans("小明", "华子");
  56.             hua.Man();
  57.             hua.Women();
  58.             Console.ReadLine();
  59.             // 你听,小猫在叫。。。
  60.             // 你听,小狗在叫。。。
  61.             // 你看,小猫在吃小鱼干。。。
  62.             // 你看,哦!小狗正在使劲舔,是吃不到吗!
  63.             // 小明 和 华子 去了男厕所
  64.             // 小明 和 华子 去了女厕所
  65.         }
  66.     }
  67.      
  68.     // 接口 声明
  69.     public interface IWc
  70.     {
  71.         // 去厕所干嘛咱也不知道啊!
  72.         // 女厕所
  73.         void Women();
  74.         // 男厕所
  75.         void Man();
  76.     }
  77.      
  78.     public class Mans: IWc
  79.     {
  80.         // 只读变量
  81.         public readonly string sexm = "男厕所";
  82.         public readonly string sexw = "女厕所";
  83.          
  84.         // 私有变量
  85.         private string namea = string.Empty;
  86.         private string nameb = string.Empty;
  87.          
  88.         // 构造函数
  89.         public Mans(string a, string b){
  90.             this.namea = a;
  91.             this.nameb = b;
  92.         }
  93.          
  94.         // 直接重写方法就行
  95.         // 基接口的2个接口必须重写,不然无法编译
  96.         public void Man(){
  97.             Console.WriteLine("{0} 和 {1} 去了{2}", namea, nameb, sexm);
  98.         }
  99.         public void Women(){
  100.             Console.WriteLine("{0} 和 {1} 去了{2}", namea, nameb, sexw);
  101.         }
  102.     }
  103. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-2-25 21:04:18 | 显示全部楼层
语句 示例
  1. // 局部变量声明
  2. static void Main() {
  3.     int a;
  4.     int b = 2, c = 3;
  5.     a = 1;
  6.     Console.WriteLine(a + b + c);
  7. }
  8. // 局部常量声明
  9. static void Main() {
  10.     const float pi = 3.1415927f;
  11.     const int r = 25;
  12.     Console.WriteLine(pi * r * r);
  13. }
  14. // 表达式语句
  15. static void Main() {
  16.     int i;
  17.     i = 123; // Expression statement
  18.     Console.WriteLine(i);  // Expression statement
  19.     i++; // Expression statement
  20.     Console.WriteLine(i);  // Expression statement
  21. }
  22. // if 语句
  23. static void Main(string[] args) {
  24.     if (args.Length == 0) {
  25.         Console.WriteLine("No arguments");
  26.     }
  27.     else {
  28.         Console.WriteLine("One or more arguments");
  29.     }
  30. }
  31. // switch 语句
  32. static void Main(string[] args) {
  33.     int n = args.Length;
  34.     switch (n) {
  35.         case 0:
  36.             Console.WriteLine("No arguments");
  37.             break;
  38.         case 1:
  39.             Console.WriteLine("One argument");
  40.             break;
  41.         default:
  42.             Console.WriteLine("{0} arguments", n);
  43.             break;
  44.     }
  45. }
  46. // while 语句
  47. static void Main(string[] args) {
  48.     int i = 0;
  49.     while (i < args.Length) {
  50.         Console.WriteLine(args[i]);
  51.         i++;
  52.     }
  53. }
  54. // do 语句
  55. static void Main() {
  56.     string s;
  57.     do {
  58.         s = Console.ReadLine();
  59.         if (s != null) Console.WriteLine(s);
  60.     } while (s != null);
  61. }
  62. // for 语句
  63. static void Main(string[] args) {
  64.     for (int i = 0; i < args.Length; i++) {
  65.         Console.WriteLine(args[i]);
  66.     }
  67. }
  68. // foreach 语句
  69. static void Main(string[] args) {
  70.     foreach (string s in args) {
  71.         Console.WriteLine(s);
  72.     }
  73. }
  74. // break 语句
  75. static void Main() {
  76.     while (true) {
  77.         string s = Console.ReadLine();
  78.         if (s == null) break;
  79.         Console.WriteLine(s);
  80.     }
  81. }
  82. // continue 语句
  83. static void Main(string[] args) {
  84.     for (int i = 0; i < args.Length; i++) {
  85.         if (args[i].StartsWith("/")) continue;
  86.         Console.WriteLine(args[i]);
  87.     }
  88. }
  89. // goto 语句
  90. static void Main(string[] args) {
  91.     int i = 0;
  92.     goto check;
  93.     loop:
  94.         Console.WriteLine(args[i++]);
  95.     check:
  96.         if (i < args.Length) goto loop;
  97. }
  98. // return 语句
  99. static int Add(int a, int b) {
  100.     return a + b;
  101. }
  102. static void Main() {
  103.     Console.WriteLine(Add(1, 2));
  104.     return;
  105. }
  106. // yield 语句
  107. static IEnumerable<int> Range(int from, int to) {
  108.     for (int i = from; i < to; i++) {
  109.         yield return i;
  110.     }
  111.     yield break;
  112. }
  113. static void Main() {
  114.     foreach (int x in Range(-10,10)) {
  115.         Console.WriteLine(x);
  116.     }
  117. }
  118. // throw 和 try 语句
  119. static double Divide(double x, double y) {
  120.     if (y == 0) throw new DivideByZeroException();
  121.     return x / y;
  122. }
  123. static void Main(string[] args) {
  124.     try {
  125.         if (args.Length != 2) {
  126.             throw new Exception("Two numbers required");
  127.         }
  128.         double x = double.Parse(args[0]);
  129.         double y = double.Parse(args[1]);
  130.         Console.WriteLine(Divide(x, y));
  131.     }
  132.     catch (Exception e) {
  133.         Console.WriteLine(e.Message);
  134.     }
  135.     finally {
  136.         Console.WriteLine(“Good bye!”);
  137.     }
  138. }
  139. // checked 和 unchecked 语句
  140. static void Main() {
  141.     int i = int.MaxValue;
  142.     checked {
  143.         Console.WriteLine(i + 1); // Exception
  144.     }
  145.     unchecked {
  146.         Console.WriteLine(i + 1); // Overflow
  147.     }
  148. }
  149. // lock 语句
  150. class Account
  151. {
  152.     decimal balance;
  153.     public void Withdraw(decimal amount) {
  154.         lock (this) {
  155.             if (amount > balance) {
  156.                 throw new Exception("Insufficient funds");
  157.             }
  158.             balance -= amount;
  159.         }
  160.     }
  161. }
  162. // using 语句
  163. static void Main() {
  164.     using (TextWriter w = File.CreateText("test.txt")) {
  165.         w.WriteLine("Line one");
  166.         w.WriteLine("Line two");
  167.         w.WriteLine("Line three");
  168.     }
  169. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-11 15:30:51 | 显示全部楼层
<委托>,遍历目录文件大小 过滤保存日志

  1. using System;
  2. using System.Windows.Forms;
  3. using System.IO;
  4. using System.Collections.Generic;

  5. class DelegateExtension
  6. {
  7.     // 委托类型声明 返回值类型 string 类型名 MyDelegate 参数 digits 类型为 int
  8.     public delegate string MyDelegate(int digits);
  9.      
  10.     static void Main()
  11.     {
  12.         // Test test = new Test();
  13.          
  14.         // 自定义委托
  15.         // MyDelegate fu1 = new MyDelegate(test.fun1);
  16.         // MyDelegate fu2 = new MyDelegate(Test.fun2);
  17.         // MyDelegate fu1 = test.fun1;
  18.         // MyDelegate fu2 = Test.fun2;
  19.          
  20.         // 系统封装好的不带返回值委托类
  21.         // Action<string> fu3;
  22.         // fu3 = test.fun3;
  23.          
  24.         // 系统封装好的带返回值的委托类
  25.         // 方法返回值类型放在Func最后面
  26.         // Func<int, string> fu1 = test.fun1;
  27.         // Func<int, string> fu2 = Test.fun2;
  28.          
  29.         // Console.WriteLine(fu1(2));
  30.         // Console.WriteLine(fu2(20));
  31.         // fu3("GIN");
  32.          
  33.         string logPath = @"D:\GIN\c#\Exercise\log.log";
  34.         // 实例默认输出至logPath
  35.         FileLogger fileLogger = new FileLogger(logPath);
  36.          
  37.          
  38.         // Console.WriteLine("日志级别:{0}", Logger.LogLevel);
  39.         // Logger.LogMessage(Severity.Error, "Logger", "ERROR");
  40.         // Logger.LogMessage(Severity.Warning, "Logger", "Warning");
  41.         // 等级低于设置级别,不打印
  42.         // Logger.LogMessage(Severity.Information, "Logger", "Information");
  43.          
  44.         // 卸载LogPath.LogMessage, 将不会输出之文本
  45.         // fileLogger.DetachLog();
  46.         // Logger.LogLevel = Severity.Information;
  47.         // Console.WriteLine("日志级别:{0}", Logger.LogLevel);
  48.         // Logger.LogMessage(Severity.Information, "Logger", "Information");
  49.          
  50.         FileInformation target = new FileInformation(@"D:");
  51.          
  52.         // 修改日志级别
  53.         Logger.LogLevel = Severity.Information;
  54.         Console.WriteLine("日志级别:{0}", Logger.LogLevel);
  55.          
  56.         // 不在控制台输出
  57.         // 从多播委托中卸载控制台输出
  58.         Logger.WriteMessage -= Console.WriteLine;
  59.          
  60.         // 扫描之前设置一下过滤文件的大小
  61.         // 文件小于 1M 的将被记录
  62.         target.filterFileSizeKB = 1024;
  63.         // 开始扫描
  64.         target.StartScan();
  65.          
  66.     }
  67.      
  68.     // logger 输出级别
  69.     public enum Severity
  70.     {
  71.         Verbose,
  72.         Trace,
  73.         Information,
  74.         Warning,
  75.         Error,
  76.         Critical
  77.     }
  78.      
  79.     // 日志输出
  80.     public static class Logger
  81.     {
  82.         // 静态类中也不能出现实例的字段成员
  83.         // 静态类的字段需要在编译前赋值, 不能在运行时赋值, 除了它的属性
  84.         // 消息处理处理委托,参数为 string
  85.          
  86.         // 默认输出至控制台
  87.         public static Action<string> writeMessage = Console.WriteLine;
  88.         public static Action<string> WriteMessage{get{return writeMessage;} set {writeMessage = value;}}
  89.          
  90.         // 日志输出等级设置
  91.         private static Severity logLevel = Severity.Warning;
  92.          
  93.         // 设置字段属性
  94.         public static Severity LogLevel{get{return logLevel;} set {logLevel = value;}}
  95.          
  96.          
  97.         // 判断输入日志是否满足日志输出等级 (需要三个参数)
  98.         public static void LogMessage(Severity s, string component, string msg)
  99.         {
  100.             // 设置日志级别小于 logLevel 级别跳过
  101.             if (s < logLevel)
  102.             {
  103.                 return;
  104.             }
  105.             
  106.             // 设置日志级别大于等于 Warning 将在控制台输出
  107.             // 这里有个 BUG 没有检查多播链中是否已经存在这个方法
  108.             if (s >= Severity.Warning)
  109.             {
  110.                 writeMessage += Console.WriteLine;
  111.                 writeMessage(String.Format("{0}\t{1}\t{2}", DateTime.Now, component, msg));
  112.                 writeMessage -= Console.WriteLine;
  113.                 return;
  114.             }
  115.             
  116.             // 调用日志输出方法
  117.             // String.Format 将返回一个字符串,传入 writeMessage 委托的方法
  118.             writeMessage(String.Format("{0}\t{1}\t{2}", DateTime.Now, component, msg));
  119.         }
  120.          
  121.     }
  122.      
  123.     // 输出日志到本地文件
  124.     public class FileLogger
  125.     {
  126.         private readonly string logPath;
  127.         public FileLogger(string path)
  128.         {
  129.             logPath = path;
  130.             // 加入 追加模式写入文本方法(多播委托)
  131.             Logger.WriteMessage += LogMessage;
  132.         }
  133.          
  134.         // 追加模式写入文本 utf-8
  135.         public void LogMessage(string msg)
  136.         {
  137.             try
  138.             {
  139.                 using (var log = File.AppendText(logPath))
  140.                 {
  141.                     log.WriteLine(String.Format("{0}", msg));
  142.                     log.Flush();
  143.                 }
  144.             }
  145.             catch
  146.             {
  147.                     Console.WriteLine("记录日志到 {0} 文件中失败!", logPath);
  148.             }
  149.         }
  150.          
  151.         // 卸载 LogMessage
  152.         public void DetachLog()
  153.         {
  154.             Logger.writeMessage -= LogMessage;
  155.         }
  156.     }
  157.      

  158.     // 文件信息扫描
  159.     public class FileInformation
  160.     {
  161.         // 扫描文件夹路径
  162.         public string TargetDirectory{get;set;}
  163.          
  164.         public FileInformation(string dir)
  165.         {
  166.             this.TargetDirectory = dir;
  167.         }
  168.          
  169.         // 这个可以设置成其他容量,需要转换
  170.         // 设置过滤文件大小
  171.         public float filterFileSizeKB = 0;
  172.         public float FilterFileSizeKB
  173.         {
  174.             get
  175.             {
  176.                 return filterFileSizeKB;
  177.             }
  178.             set
  179.             {
  180.                 this.filterFileSizeKB = value;
  181.             }
  182.         }
  183.          
  184.         // 指定不可访问的目录
  185.         public List<string> skipList = new List<string>{@"$RECYCLE.BIN", @"System Volume Information"};
  186.          
  187.         // value 的值是 List<string> 类型
  188.         public List<string> SkipList
  189.         {
  190.             get
  191.             {
  192.                 return this.skipList;
  193.             }
  194.             set
  195.             {
  196.                 // 这里可以检查一下列表里面是否已经存在
  197.                 foreach (var str in value)
  198.                 {
  199.                     // 不存在,则加入
  200.                     if (this.skipList.IndexOf(str) == -1)
  201.                         this.skipList.Add(str);
  202.                 }
  203.             }
  204.         }
  205.          
  206.         public bool StartScan()
  207.         {
  208.             DirectoryInfo dir;
  209.             // 处理报错
  210.             try
  211.             {
  212.                 dir = new DirectoryInfo(this.TargetDirectory);        
  213.                 decimal count = 0; // 文件计数
  214.                 bool flag = false;
  215.                 foreach (var each in dir.GetDirectories())
  216.                 {
  217.                     flag = false;
  218.                     // 查询是否包含不可访问目录
  219.                     foreach (var sikp in this.skipList)
  220.                     {
  221.                         if (each.FullName.Contains(sikp))
  222.                         {
  223.                             flag = true;
  224.                             // 开启记录日志
  225.                             Logger.LogMessage(Severity.Warning, "FileInformation", String.Format("{0}  跳过指定目录:{1}", DateTime.Now, each.FullName));
  226.                             break;
  227.                         }
  228.                     }
  229.                      
  230.                     if (flag) continue;
  231.                      
  232.                     DirectoryInfo eachDir = new DirectoryInfo(each.FullName);
  233.                     // 在搜索操作中包括当前目录和所有它的子目录。 此选项在搜索中包括重解析点,比如安装的驱动器和符号链接。
  234.                     foreach (System.IO.FileInfo fi in eachDir.GetFiles("*.*", System.IO.SearchOption.AllDirectories))
  235.                     {
  236.                         count++;
  237.                         // 这里只记录文件大小 小于 指定文件大小
  238.                         // 其他的还可以增加
  239.                         if (fi.Length < this.FilterFileSizeKB)
  240.                             // 开启记录日志
  241.                             Logger.LogMessage(Severity.Information, "FileInformation", String.Format("{0},{1},{2}",count, fi.FullName, fi.Length));
  242.                     }
  243.                 }
  244.             }
  245.             catch (Exception err)
  246.             {
  247.                 Logger.LogMessage(Severity.Error, "FileInformation", err.Message);
  248.                 return false;
  249.             }
  250.             // 暂且返回真
  251.             return true;
  252.         }
  253.          
  254.         // 加入日志
  255.         public void FromLog(FileLogger obj)
  256.         {
  257.             Logger.writeMessage += obj.LogMessage;
  258.         }
  259.          
  260.         // 移除日志
  261.         public void DetachLog(FileLogger obj)
  262.         {
  263.             Logger.writeMessage -= obj.LogMessage;
  264.         }
  265.     }
  266.      
  267.      
  268.     public class Test
  269.     {
  270.         public void fun3(string name)
  271.         {
  272.             MessageBox.Show(String.Format("Hello {0} !", name));
  273.         }
  274.          
  275.         public string fun1(int digits)
  276.         {
  277.             if (digits > 10)
  278.             {
  279.                 return String.Format("{0} > 10", digits);
  280.             }
  281.             else
  282.             {
  283.                 return String.Format("{0} < 10", digits);
  284.             }
  285.         }
  286.          
  287.         public static string fun2(int digits)
  288.         {
  289.             if (digits > 10)
  290.             {
  291.                 return "GEQ";
  292.             }
  293.             else
  294.             {
  295.                 return "LEQ";
  296.             }
  297.         }
  298.     }
  299. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-11 15:32:14 | 显示全部楼层
命令行复制文件和目录
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using Microsoft.VisualBasic.FileIO;

  5. class CopyFileExercise
  6. {
  7.     static void Main (string[] args)
  8.     {
  9.         ConmandLineEventArgs Margs = new ConmandLineEventArgs(args); // 参数实例
  10.          
  11.         ConmandLineParse clp = new ConmandLineParse(); // 解析命令行
  12.         clp.Parse(Margs);
  13.          
  14.         new CopyDirectoryOrFile(Margs).Copy(); // 复制文件
  15.     }
  16.      
  17.     // 储存参数数据
  18.     public class ConmandLineEventArgs : EventArgs
  19.     {
  20.         public readonly string sourceMatch = "-source:"; // 只读字段
  21.         public readonly string destinationMatch = "-destination:";
  22.         public string[] Args;
  23.         public string source{ get; set;} // 主要储存数据字段
  24.         public string destination{ get; set;} // 主要储存数据字段
  25.         public string Error;
  26.          
  27.         public ConmandLineEventArgs(string[] args)
  28.         {
  29.             this.Args = args;
  30.         }
  31.     }
  32.      
  33.     static void CLParseEventHandler(object sender, ConmandLineEventArgs e)
  34.     {
  35.         Console.WriteLine(e.Error);
  36.         Console.WriteLine("参数用法:\n\n\t{0}源文件名(或者源目录名)\n\t{1}新文件名(或者新目录名)", e.source, e.destination);
  37.         Environment.Exit(1); // 退出进程
  38.     }
  39.      
  40.     // 命令行解析
  41.     class ConmandLineParse
  42.     {
  43.          
  44.         public ConmandLineParse()
  45.         {
  46.             this.ErrorEvent += CLParseEventHandler; // 默认订阅事件处理器
  47.         }
  48.          
  49.         public event EventHandler<ConmandLineEventArgs> ErrorEvent; // 错误事件
  50.          
  51.         public void Parse(ConmandLineEventArgs e)
  52.         {
  53.             if (e.Args.Length != 2) // 检查参数数量
  54.             {
  55.                 e.Error = "输入参数数量错误";
  56.                 this.OnErrorEvent(e);
  57.             }
  58.             
  59.             foreach (var n in e.Args) // 分析参数
  60.             {
  61.                 if (n.StartsWith(e.sourceMatch))
  62.                 {
  63.                     e.source =  n.Replace (e.sourceMatch, ""); // 源数据
  64.                 }
  65.                 else if (n.StartsWith(e.destinationMatch))
  66.                 {
  67.                     e.destination = n.Replace (e.destinationMatch, ""); // 目标数据
  68.                 }
  69.             }
  70.             
  71.             // 检查数据是否为空
  72.             if (e.source == "")
  73.             {
  74.                 e.Error = "source为空";
  75.                 this.OnErrorEvent(e);
  76.             }
  77.             if (e.destination == "")
  78.             {
  79.                 e.Error = "destination为空";
  80.                 this.OnErrorEvent(e);
  81.             }
  82.         }
  83.          
  84.         private void OnErrorEvent(ConmandLineEventArgs e) // 参数不正确事件触发
  85.         {
  86.             this.ErrorEvent(this, e);
  87.         }
  88.     }
  89.      
  90.     // 文件或者目录复制
  91.     public class CopyDirectoryOrFile
  92.     {
  93.         private ConmandLineEventArgs e;
  94.         private event EventHandler<ConmandLineEventArgs> ErrorEvent; // 事件
  95.          
  96.         public CopyDirectoryOrFile(ConmandLineEventArgs e)
  97.         {
  98.             this.e = e;
  99.             this.ErrorEvent += CLParseEventHandler; // 事件订阅
  100.         }
  101.          
  102.          
  103.         public void Copy()
  104.         {
  105.             // 调用不同的复制方法
  106.             try
  107.             {
  108.                 if (File.Exists(this.e.source))
  109.                 {
  110.                    FileSystem.CopyFile(this.e.source, this.e.destination, UIOption.AllDialogs, UICancelOption.DoNothing); // 用户取消不引发异常
  111.                 }
  112.                 else if (Directory.Exists(this.e.source))
  113.                 {
  114.                     FileSystem.CopyDirectory(this.e.source, this.e.destination, UIOption.AllDialogs, UICancelOption.DoNothing);
  115.                 }
  116.                 else
  117.                 {
  118.                     this.e.Error = String.Format("{0} 既不是文件也不是目录", e.source);
  119.                     ErrorEvent(this, this.e);
  120.                 }
  121.             }
  122.             catch (IOException E)
  123.             {
  124.                 this.e.Error = E.Message; // 获取异常的信息
  125.                 ErrorEvent(this, this.e);
  126.             }
  127.         }
  128.     }
  129. }
复制代码


batch shell
  1. @echo off

  2. echo;
  3. echo ##############################
  4. echo ## 穿越火线和英雄联盟备份 ###
  5. echo ##############################
  6. echo;

  7. echo 输入 ^<Q^> 退出
  8. echo 输入 ^<L^> 备份英雄联盟
  9. echo 输入 ^<C^> 备份穿越火线
  10. echo 输入 ^<T^> 备份穿越火线和英雄联盟
  11. echo;
  12. echo 请输入字母不区分大小写:
  13. choice /c QLCT /n
  14. set result=%errorlevel%
  15. if %result% equ 1 exit
  16. if %result% equ 2 call :BackupLOL
  17. if %result% equ 3 call :BackupCF
  18. if %result% equ 4 call :BackupTotal

  19. pause
  20. exit 0
  21.   
  22. :BackupLOL
  23.     CopyFile.exe -source:"C:\user\英雄联盟" -destination:"D:\腾讯游戏\英雄联盟"
  24.     exit /b

  25. :BackupCF
  26.     CopyFile.exe -source:"C:\user\穿越火线" -destination:"D:\腾讯游戏\穿越火线"
  27.     exit /b

  28. :BackupTotal
  29.     call :BackupLOL
  30.     call :BackupCF
  31.     exit /b
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-11 18:55:06 From FishC Mobile | 显示全部楼层
学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-3-12 18:04:31 | 显示全部楼层
运算符重载:
  1. using System;

  2. namespace Operator
  3. {
  4.     class Program
  5.     {
  6.         static void Main()
  7.         {
  8.             Box a = new Box(2);
  9.             Box b = new Box(3);
  10.             
  11.             Console.WriteLine(a + b);  // 5 这里返回一个新的 Box 实例,但是被丢弃了
  12.             Console.WriteLine(a >= b); // false
  13.             Console.WriteLine(a <= b); // true
  14.             Console.WriteLine(a++);    // 12 重载时 我们把 Count 的值加上了 10
  15.             Console.WriteLine(a--);    // 7  重载时 我们把 Count 的值减去了 5
  16.             Console.WriteLine(+a);     // 7
  17.             Console.WriteLine(-a);     // -7
  18.             Console.WriteLine(!a);     // False
  19.             a.Count += 7;              // a.Count = 0
  20.             Console.WriteLine(a);      // 0
  21.             Console.WriteLine(!a);     // true
  22.             Console.WriteLine(a++);    // 10
  23.             Console.WriteLine(~a);
  24.             // 取反前二进制码:
  25.             // 00000000,00000000,00000000,00001010
  26.             // 取反后二进制码:
  27.             // 11111111,11111111,11111111,11110101
  28.             // -11
  29.         }
  30.     }
  31.      
  32.      
  33.     class Box
  34.     {
  35.         public int Count {get; set;}
  36.          
  37.         public Box(int v)
  38.         {
  39.             Count = v;
  40.         }
  41.          
  42.         // 重写 ToString 方法 ToString 继承自 object
  43.         // Console.WriteLine 会直接输出 Count 的值;
  44.         public override string ToString()
  45.         {
  46.             return String.Format("{0}", Count);
  47.         }
  48.          
  49.         // 二元操作符 + 需要两个参数
  50.         // 两个相同对象相加,返回相同类型实例 static 修饰符后面的 Box 就是返回值 类型
  51.         public static Box operator + (Box a, Box b)
  52.         {
  53.             Box c = new Box(0);
  54.             c.Count = a.Count + b.Count;
  55.             return c;
  56.         }
  57.          
  58.         // >= 和 <= 必须成对出现,不然会报编译错误 返回值类型为 bool
  59.         public static bool operator >= (Box a, Box b)
  60.         {
  61.             
  62.             if (a.Count >= b.Count)
  63.             {
  64.                 return true;
  65.             }
  66.             else
  67.             {
  68.                 return false;
  69.             }
  70.         }
  71.          
  72.         public static bool operator <= (Box a, Box b)
  73.         {
  74.             if (a.Count <= b.Count) return true;
  75.             else return false;
  76.         }
  77.          
  78.          
  79.          
  80.         // 一元操作符 + 只需要一个参数
  81.         public static Box operator + (Box a)
  82.         {
  83.             a.Count = Math.Abs(a.Count);
  84.             return a;
  85.         }
  86.          
  87.         public static Box operator - (Box a)
  88.         {
  89.             a.Count = 0 - a.Count;
  90.             return a;
  91.         }
  92.          
  93.         // 定义 Count 的值 等于 0 或者 等于 null 时,Count 的值为 false 然后取反 为 true
  94.         public static bool operator ! (Box a)
  95.         {
  96.             if (a.Count == 0 || (int?)a.Count == null) // 值为 false (int?)显示类型转换
  97.             {
  98.                 return true; // 取反返回 true
  99.             }
  100.             else return false;
  101.         }
  102.          
  103.         // 自增运算符不需要创建一个实例,使用它自己
  104.         public static Box operator ++ (Box a)
  105.         {
  106.             a.Count += 10;
  107.             return a;
  108.         }
  109.          
  110.         public static Box operator -- (Box a)
  111.         {
  112.             a.Count -= 5;
  113.             return a;
  114.         }
  115.          
  116.         // 按位取反
  117.         public static Box operator ~ (Box a)
  118.         {
  119.             Console.WriteLine("取反前二进制码:");
  120.             DisplayBinary(a.Count);
  121.             a.Count = ~a.Count;
  122.             Console.WriteLine("取反后二进制码:");
  123.             DisplayBinary(a.Count);
  124.             return a;
  125.         }

  126.         public static void DisplayBinary(int Number) // 显示二进制位
  127.         {
  128.             unsafe // 不安全代码 指针
  129.             {
  130.                 int Count = Number; // int 4 个字节
  131.                 byte* p = (byte*)&Count;  // byte 1个字节
  132.                 for (sbyte i = 3; i >= 0; i--)
  133.                 {
  134.                     for (sbyte j = 7; j >= 0; j--) // 一个字节等于 8 个比特位
  135.                     {
  136.                         if (((*(p + i) & (1<<j))) != 0)
  137.                         {
  138.                             Console.Write(1);
  139.                         }
  140.                         else
  141.                         {
  142.                             Console.Write(0);
  143.                         }
  144.                     }
  145.                     if (i != 0) Console.Write(',');
  146.                 }
  147.             }
  148.             Console.Write('\n');
  149.             
  150.             // for (sbyte i = 31; i >= 0; i--) // 不使用指针版
  151.             // {
  152.                 // if ((Number & (1<<i)) != 0)
  153.                 // {
  154.                     // Console.Write(1);
  155.                 // }
  156.                 // else
  157.                 // {
  158.                     // Console.Write(0);
  159.                 // }
  160.             // }
  161.         }

  162.     }
  163. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-23 18:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表