1.简单封装

1》计算类

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace 计算
  7. {
  8. class operater1
  9. {
  10. private int x;
  11. private int y;
  12. private string opers;
  13. private int answer;
  14. public int X
  15. {
  16. set
  17. {
  18. x = value;
  19. }
  20. }
  21. public int Y
  22. {
  23. set
  24. {
  25. y = value;
  26. }
  27. }
  28. public string Opers
  29. {
  30. set
  31. {
  32. opers = value;
  33. }
  34. }
  35. public int Answer
  36. {
  37. get
  38. {
  39. return answer;
  40. }
  41. }
  42. public void operation()
  43. {
  44. switch (opers)
  45. {
  46. case "+":
  47. answer = x + y;
  48. break;
  49. case "-":
  50. if (x > y)
  51. {
  52. answer = x - y;
  53. }
  54. else
  55. {
  56. throw new Exception("被减数不能小于减数!");
  57.  
  58. }
  59. break;
  60. case "*":
  61. answer = x * y;
  62. break;
  63. case "/":
  64. if (y == 0)
  65. {
  66. throw new Exception("除数不能为零!");
  67. }
  68. else
  69. {
  70. answer = x / y;
  71. }
  72. break;
  73.  
  74. }
  75.  
  76. }
  77. }
  78.  
  79. }

2》写入类

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace 计算
  8. {
  9. class writes
  10. {
  11. public void inscribe(string a, string b)
  12. {
  13.  
  14. StreamWriter aaa = new StreamWriter(a, true);
  15. aaa.WriteLine(b);
  16. aaa.Close();
  17.  
  18. }
  19. public void cleanup(string c, string d,string e)
  20. {
  21. StreamWriter ddd = new StreamWriter(c);
  22. ddd.WriteLine(" ");
  23. ddd.Close();
  24. StreamWriter aaa = new StreamWriter(d);
  25. aaa.WriteLine("");
  26. aaa.Close();
  27. StreamWriter fff = new StreamWriter(e);
  28. fff.WriteLine("");
  29. fff.Close();
  30. }
  31. }
  32. }

form1代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace 计算
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19.  
  20. private void button1_Click(object sender, EventArgs e)//写入
  21. {
  22. writes writ = new writes();
  23. string fnm = @"one";
  24. string text1=this.textBox1.Text;
  25. writ.inscribe(fnm, text1);
  26. string fnmm = @"tow";
  27. string text2 = this.textBox2.Text;
  28. writ.inscribe(fnmm, text2);
  29. string fnm1 = @"fuhao";
  30. string text3 = this.comboBox1.Text;
  31. writ.inscribe(fnm1, text3);
  32. textBox1.Clear();
  33. textBox2.Clear();
  34.  
  35. }
  36.  
  37. private void button2_Click(object sender, EventArgs e)
  38. {
  39. Form2 fam = new Form2();
  40. fam.ShowDialog();
  41.  
  42. }
  43.  
  44. private void button3_Click(object sender, EventArgs e)//清空题库
  45. {
  46. string a = @"tow";
  47. string b = @"one";
  48. string c = @"fuhao";
  49. writes clean = new writes();
  50. clean.cleanup(a, b, c);
  51. }
  52.  
  53. }
  54. }

form2代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace 出题
  12. {
  13. public partial class Form2 : Form
  14. {
  15. public Form2()
  16. {
  17. InitializeComponent();
  18. }
  19.  
  20. private int i = 1;
  21. public static int count;
  22. public static int right;
  23. private void timer1_Tick(object sender, EventArgs e)
  24. {
  25. try
  26. {
  27. int t = int.Parse(textBox5.Text);
  28. if (t <= 0)
  29. {
  30. timer1.Enabled = false;
  31. textBox5.Enabled = false;
  32. MessageBox.Show("时间到了!");
  33. Form3 fr3 = new Form3();
  34. fr3.ShowDialog();
  35. }
  36. t = t - 1;
  37. textBox5.Text = t.ToString();
  38.  
  39. }
  40. catch
  41. {
  42. }
  43.  
  44. }
  45. private void button1_Click(object sender, EventArgs e)//开始
  46. {
  47. butt();
  48. try
  49. {
  50. string t = textBox5.Text;
  51. textBox5.Text = t;
  52. timer1.Enabled = true;
  53. timer1.Interval = 1000;
  54. timer1.Start();
  55. }
  56. catch
  57. {
  58. }
  59.  
  60. }
  61. private void textBox4_KeyDown(object sender, KeyEventArgs e)//后台代码的应用
  62. {
  63. if (e.KeyCode == Keys.Enter)
  64. {
  65.  
  66. operater1 operater = new operater1();
  67. operater.X = int.Parse(textBox1.Text);
  68. operater.Y = int.Parse(textBox3.Text);
  69. operater.Opers = textBox2.Text;
  70. operater.operation();
  71. if (textBox4.Text == operater.Answer.ToString())
  72. {
  73. MessageBox.Show("回答正确!");
  74. right++;
  75. }
  76. else
  77. {
  78. MessageBox.Show("回答错误!");
  79. }
  80. count++;
  81. textBox4.Clear();
  82. butt();
  83.  
  84. }
  85.  
  86. }//用户的输入
  87. private void button2_Click(object sender, EventArgs e)//停止
  88. {
  89. textBox4.Enabled=false;
  90.  
  91. }
  92. private void butt()
  93. {
  94. string[] line = File.ReadAllLines("one");
  95. if (i < line.Length)
  96. {
  97. textBox1.Text = line[i];
  98. string[] lines = File.ReadAllLines("tow");
  99. textBox3.Text = lines[i];
  100. string[] lin = File.ReadAllLines("fuhao");
  101. textBox2.Text = lin[i];
  102.  
  103. }
  104. i++;
  105. if (i == line.Length + 1)
  106. {
  107. Form3 foo = new Form3();
  108. foo.ShowDialog();
  109. }
  110.  
  111. }//读题
  112. private void button3_Click(object sender, EventArgs e)
  113. {
  114. this.Close();
  115. }//关闭窗体
  116.  
  117. }
  118. }

2.策略模式

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace 计算
  7. {
  8. interface operater1
  9. {
  10.  
  11. int calculate(int a, int b);
  12.  
  13. }
  14. class Add : operater1
  15. {
  16.  
  17. public int calculate(int a, int b)
  18. {
  19.  
  20. return a + b;
  21.  
  22. }
  23.  
  24. }
  25. class Sub : operater1
  26. {
  27. public int calculate(int a, int b)
  28. {
  29. return a - b;
  30. }
  31. }
  32. class Mul : operater1
  33. {
  34. public int calculate(int a, int b)
  35. {
  36. return a * b;
  37. }
  38. }
  39. class Div : operater1
  40. {
  41. public int calculate(int a, int b)
  42. {
  43. if (b == 0)
  44. {
  45. throw new Exception("除数不能为零!");
  46. }
  47. else
  48. {
  49. return a / b;
  50. }
  51. }
  52. }
  53. }

实现策略

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace 计算
  7. {
  8. public class Clacuter
  9. {
  10. private operater1 oper1;
  11. public Clacuter(string aSS)
  12. {
  13. switch (aSS)
  14. {
  15. case "+":
  16. oper1 = new Add();
  17. break;
  18. case "-":
  19. oper1 = new Sub();
  20. break;
  21. case "*":
  22. oper1 = new Mul();
  23. break;
  24. case "/":
  25. oper1 = new Div();
  26. break;
  27. }
  28.  
  29. }
  30. public int Calculation(int a,int b)
  31. {
  32. return oper1.calculate(a, b);
  33. }
  34. }
  35. }

2,》写入类

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6.  
  7. namespace 出题
  8. {
  9. class writes
  10. {
  11. public void inscribe(string a, string b)
  12. {
  13.  
  14. StreamWriter aaa = new StreamWriter(a, true);
  15. aaa.WriteLine(b);
  16. aaa.Close();
  17.  
  18. }
  19. public void cleanup(string c, string d,string e)
  20. {
  21. StreamWriter ddd = new StreamWriter(c);
  22. ddd.WriteLine(" ");
  23. ddd.Close();
  24. StreamWriter aaa = new StreamWriter(d);
  25. aaa.WriteLine("");
  26. aaa.Close();
  27. StreamWriter fff = new StreamWriter(e);
  28. fff.WriteLine("");
  29. fff.Close();
  30. }
  31. }
  32. }

3》form2代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10.  
  11. namespace 计算
  12. {
  13. public partial class Form2 : Form
  14. {
  15. public Form2()
  16. {
  17. InitializeComponent();
  18. }
  19.  
  20. private int i = 1;
  21. public static int count;
  22. public static int right;
  23. private void timer1_Tick(object sender, EventArgs e)
  24. {
  25. try
  26. {
  27. int t = int.Parse(textBox5.Text);
  28. if (t <= 0)
  29. {
  30. timer1.Enabled = false;
  31. textBox5.Enabled = false;
  32. MessageBox.Show("时间到了!");
  33. Form3 fr3 = new Form3();
  34. fr3.ShowDialog();
  35. }
  36. t = t - 1;
  37. textBox5.Text = t.ToString();
  38.  
  39. }
  40. catch
  41. {
  42. }
  43.  
  44. }
  45. private void button1_Click(object sender, EventArgs e)//开始
  46. {
  47. butt();
  48. try
  49. {
  50. string t = textBox5.Text;
  51. textBox5.Text = t;
  52. timer1.Enabled = true;
  53. timer1.Interval = 1000;
  54. timer1.Start();
  55. }
  56. catch
  57. {
  58. }
  59.  
  60. }
  61. private void textBox4_KeyDown(object sender, KeyEventArgs e)//策略模式代码的实现
  62. {
  63. if (e.KeyCode == Keys.Enter)
  64. {
  65. Clacuter clacuter=new Clacuter(textBox2.Text);
  66. int B = clacuter.Calculation(int.Parse(textBox1.Text), int.Parse(textBox3.Text));
  67.  
  68. if (textBox4.Text ==B.ToString())
  69. {
  70. MessageBox.Show("回答正确!");
  71. right++;
  72. }
  73. else
  74. {
  75. MessageBox.Show("回答错误!");
  76. }
  77. count++;
  78. textBox4.Clear();
  79. butt();
  80.  
  81. }
  82.  
  83. }//用户的输入
  84. private void button2_Click(object sender, EventArgs e)//停止
  85. {
  86. textBox4.Enabled=false;
  87.  
  88. }
  89. private void butt()
  90. {
  91. string[] line = File.ReadAllLines("one");
  92. if (i < line.Length)
  93. {
  94. textBox1.Text = line[i];
  95. string[] lines = File.ReadAllLines("tow");
  96. textBox3.Text = lines[i];
  97. string[] lin = File.ReadAllLines("fuhao");
  98. textBox2.Text = lin[i];
  99.  
  100. }
  101. i++;
  102. if (i == line.Length + 1)
  103. {
  104. Form3 foo = new Form3();
  105. foo.ShowDialog();
  106. }
  107.  
  108. }//读题
  109. private void button3_Click(object sender, EventArgs e)
  110. {
  111. this.Close();
  112. }//关闭窗体
  113.  
  114. }
  115. }

3.Asp换脸

1》后台代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5.  
  6. /// <summary>
  7. ///operater1 的摘要说明
  8. /// </summary>
  9. interface operater1
  10. {
  11.  
  12. int calculate(int a, int b);
  13.  
  14. }
  15. class Add : operater1
  16. {
  17.  
  18. public int calculate(int a, int b)
  19. {
  20.  
  21. return a + b;
  22.  
  23. }
  24.  
  25. }
  26. class Sub : operater1
  27. {
  28. public int calculate(int a, int b)
  29. {
  30. return a - b;
  31. }
  32. }
  33. class Mul : operater1
  34. {
  35. public int calculate(int a, int b)
  36. {
  37. return a * b;
  38. }
  39. }
  40. class Div : operater1
  41. {
  42. public int calculate(int a, int b)
  43. {
  44. if (b == 0)
  45. {
  46. throw new Exception("除数不能为零!");
  47. }
  48. else
  49. {
  50. return a / b;
  51. }
  52. }
  53. }
  54. public class Clacuter
  55. {
  56. private operater1 oper1;
  57. public Clacuter(string aSS)
  58. {
  59. switch (aSS)
  60. {
  61. case "+":
  62. oper1 = new Add();
  63. break;
  64. case "-":
  65. oper1 = new Sub();
  66. break;
  67. case "*":
  68. oper1 = new Mul();
  69. break;
  70. case "/":
  71. oper1 = new Div();
  72. break;
  73. }
  74.  
  75. }
  76. public int Calculation(int a,int b)
  77. {
  78. return oper1.calculate(a, b);
  79. }
  80. }

web代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. public partial class Default2 : System.Web.UI.Page
  9. {
  10. protected void Page_Load(object sender, EventArgs e)
  11. {
  12. if (!IsPostBack)
  13. {
  14. chuti();
  15. }
  16. }
  17. protected void Button1_Click(object sender, EventArgs e)
  18. {
  19. int a = int.Parse(TextBox1.Text);
  20. int b = int.Parse(TextBox2.Text);
  21. Clacuter claacuter = new Clacuter(TextBox3.Text);
  22. string answer = claacuter.Calculation(a, b).ToString();
  23. if (TextBox4.Text == answer)
  24. {
  25. Response.Write("回答正确!");
  26. }
  27. else
  28. {
  29. Response.Write("回答错误!");
  30. Response.Write(answer);
  31. }
  32.  
  33. }
  34. protected void Button2_Click(object sender, EventArgs e)
  35. {
  36. chuti();
  37. }
  38. private void chuti()
  39. {
  40. Random random = new Random();
  41. TextBox1.Text = random.Next(1, 100).ToString();
  42. TextBox2.Text = random.Next(1, 100).ToString();
  43. string[] arry = new string[] { "+", "-", "*", "/" };
  44. TextBox3.Text = arry[random.Next(0, 4)];
  45.  
  46. }
  47. }

运行图片

封装,策略模式,Asp换脸的更多相关文章

  1. 封装,策略,Asp换脸

    封装.策略 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespac ...

  2. 计算器软件的代码实现 (策略模式+asp.net)

    一 策略模式代码的编写 using System; using System.Collections.Generic; using System.Linq; using System.Web; /// ...

  3. 计算器软件实现系列(五)策略模式+asp.net

    一 策略模式代码的编写 using System; using System.Collections.Generic; using System.Linq; using System.Web; /// ...

  4. Wpf+数据库代码封装+策略模式封装

    运行界面: 数据库保存的题: 数据库封装代码: using System; using System.Collections.Generic; using System.Linq; using Sys ...

  5. ASP.NET四则运算--策略模式

    在ASP.NET中实现四则运算,同样使用了类的封装,以及策略模式.只不过是把封装的类.前台代码以及后台的代码分离开来,但同样是要达到功能的实现. Calculator.cs using System; ...

  6. ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现

    ASP.NET MVC 学习笔记-2.Razor语法   1.         表达式 表达式必须跟在“@”符号之后, 2.         代码块 代码块必须位于“@{}”中,并且每行代码必须以“: ...

  7. ASP.net之策略模式

    设计思路: 用ASP.net设计,调用策略模式.在第一个数和第二个数的文本框中输入数值,单击录题按钮,数值保存在n1,n2文档中,把要做的题都保存完后,单击开始按钮,开始做题,做完单击判断按钮,进行判 ...

  8. 【Android】策略模式封装百度地图路线规划模块

    百度地图的Demo里有个路线规划的功能,但是,这个功能和Activity耦合性太高,所以需要单独抽离出路径规划功能,进行"解耦". 注:由于项目原因,本文只针对驾车路线规划进行封装 ...

  9. Android 设计模式实战之关于封装计费代码库的策略模式详谈

    写在之前 这周生活上出现了很多的不如意,从周一开始就觉得哪里出现了问题,然后就是各种烦躁的情绪,后来事情还真是如预感的那样发生了,很是心痛,但也无可奈何,希望大家都好好珍惜自己身边的人:友人,亲人,家 ...

随机推荐

  1. php内容

    PHP语言原理:先把代码显示在源代码中,再通过浏览器解析在网页上 PHP中关键字通常分为四种类型: 1. 用于数据类型定义的关键字,如:int,string,bool,classic,object和a ...

  2. 如何将cmd中命令输出保存为TXT文本文件

    在使用Windows 中的cmd.exe工具时,有时候我们想要把我们的输入命令及结果保存起来, 但是用复制的方法过于麻烦:有时输出数据条数过大,会造成内容自动滚出屏幕,无法阅读,我们可将命令运行的结果 ...

  3. JAVA中使用JSON进行数据传递

    最近在做一个基于JAVA Servlet的WEB应用以及对应的Anroid应用客户端的开发工作. 其中,在接口的访问和数据的传输方面使用的比较多的是使用JSON对象来操作格式化数据:在服务器端采用JS ...

  4. 用户交互与while循环<代码>

    #用户交互1 age_oldboy = 56 guess_age = int(input(">>:")) if guess_age == age_oldboy: pri ...

  5. 1011 最大公约数GCD

    1011 最大公约数GCD 基准时间限制:1 秒 空间限制:131072 KB 输入2个正整数A,B,求A与B的最大公约数. Input 2个数A,B,中间用空格隔开.(1<= A,B < ...

  6. Java学习-038-JavaWeb_007 -- JSP 动作标识 - plugin

    plugin 动作时用来在 JSP 页面中加载 Java Applet 或者 JavaBean 组件,语法格式如下所示: <jsp:plugin type="bean|applet&q ...

  7. @SuppressWarnings

    http://www.cnblogs.com/fsjohnhuang/p/4040785.html 一.前言 编码时我们总会发现如下变量未被使用的警告提示: 上述代码编译通过且可以运行,但每行前面的“ ...

  8. Upgrade Image&ntext to varbinarymax&nvarchar(max)

    CREATE PROCEDURE SP_EXEC_WITH_LOG(@I_TICKETNO VARCHAR(10),@I_SQLSTR nvarchar(max))ASBEGIN    DECLARE ...

  9. Nested transactions in stored procedure of SQLServer

    question: if the nested transaction encountered an exception, then rollbacked. How about the outer t ...

  10. iOS:FFmpeg视频播放和直播框架

    视频直播和播放转码器框架 介绍: FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序.采用LGPL或GPL许可证. 它提供了录制.转换以及流化音视频的完整解决方案.它 ...