计算类的封装

jisuan.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ZY四则运算
  7. {
  8. public class jisuan
  9. {
  10. public Dictionary<char, int> priorities = null; //优先级
  11.  
  12. public void Calculator() //添加了四种运算符以及四种运算符的优先级
  13. {
  14. priorities = new Dictionary<char, int>();
  15. priorities.Add();
  16. priorities.Add();
  17. priorities.Add();
  18. priorities.Add();
  19. priorities.Add();
  20. }
  21.  
  22. const string operators = "+-*/"; //运算符
  23.  
  24. public double Compute(double leftNum, double rightNum, char op) //这是一种方法,用来计算左右两个数的静态方法!
  25. {
  26. switch (op)
  27. {
  28. case '+': return leftNum + rightNum;
  29. case '-': return leftNum - rightNum;
  30. case '*': return leftNum * rightNum;
  31. case '/': return leftNum / rightNum;
  32. ;
  33. }
  34. }
  35.  
  36. public bool IsOperator(char op) //每次判断这个字符是否是运算符?
  37. {
  38. ;
  39. }
  40.  
  41. public bool IsAssoc(char op) //返回一个关联符号
  42. {
  43. return op == '+' || op == '-' || op == '*' || op == '/';
  44. }
  45.  
  46. public Queue<object> QueueSort(string expression) // 队列排序
  47. {
  48. Queue<object> result = new Queue<object>();
  49. Stack<char> operatorStack = new Stack<char>(); //运算符栈
  50. operatorStack.Push('#');
  51. char top, cur, tempChar; //top栈顶,current最近的;
  52. string tempNum;
  53. , j; i < expression.Length; ) //取出表达式
  54. {
  55. cur = expression[i++]; //取出表达式的每个字符赋给cur
  56. top = operatorStack.Peek(); //栈顶元素赋给top此时为"#"
  57.  
  58. if (cur == '(') //将左括号压栈,此时栈顶元素为"("
  59. {
  60. operatorStack.Push(cur);
  61. }
  62. else
  63. {
  64. if (IsOperator(cur)) //如果是运算符的话
  65. {
  66. while (IsOperator(top) && ((IsAssoc(cur) && priorities[cur] <= priorities[top])) || (!IsAssoc(cur) && priorities[cur] < priorities[top]))
  67. {
  68. result.Enqueue(operatorStack.Pop()); //如果元素为运算符并且优先级小于栈顶元素优先级,出栈
  69. top = operatorStack.Peek(); //继续把栈顶元素赋给top
  70. }
  71. operatorStack.Push(cur); //把数字压栈
  72. }
  73. else if (cur == ')') //将右括号添加到结尾
  74. {
  75. && (tempChar = operatorStack.Pop()) != '(')
  76. {
  77. result.Enqueue(tempChar);
  78. }
  79. }
  80. else
  81. {
  82. tempNum = "" + cur;
  83. j = i;
  84. ')))
  85. {
  86. tempNum += expression[j++];
  87. }
  88. i = j;
  89. result.Enqueue(tempNum);
  90. }
  91. }
  92. }
  93. )
  94. {
  95. cur = operatorStack.Pop();
  96. if (cur == '#') continue;
  97. )
  98. {
  99. top = operatorStack.Peek();
  100. }
  101.  
  102. result.Enqueue(cur);
  103. }
  104.  
  105. return result;
  106. }
  107.  
  108. public double Calucate(string expression)
  109. {
  110. try
  111. {
  112. var rpn = QueueSort(expression);
  113. Stack<double> operandStack = new Stack<double>();
  114. double left, right;
  115. object cur;
  116. )
  117. {
  118. cur = rpn.Dequeue(); //出列
  119. if (cur is char) //如果cur为字符的话
  120. {
  121. right = operandStack.Pop(); //右边的数字出栈
  122. left = operandStack.Pop(); //左边的数字出栈
  123. operandStack.Push(Compute(left, right, (char)cur)); //此时调用compute方法
  124. }
  125. else
  126. {
  127. operandStack.Push(double.Parse(cur.ToString())); //是数字就压栈
  128. }
  129. }
  130. return operandStack.Pop();
  131. }
  132. catch
  133. {
  134. throw new Exception("表达式不正确!");
  135. }
  136. }
  137. }
  138. }

Form1.cs

  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 ZY四则运算
  12. {
  13. public partial class Form1 : Form
  14. {
  15. Form2 frm2 = new Form2();
  16. public Form1()
  17. {
  18. InitializeComponent();
  19. }
  20. private void button1_Click_1(object sender, EventArgs e)
  21. {
  22. string Express = textBox1.Text;
  23. frm2.listBox1.Items.Add(Express);
  24. listBox1.Items.Add(" " + Express + "=");
  25. textBox1.Clear();
  26. }
  27. private void button2_Click(object sender, EventArgs e)
  28. {
  29. frm2.ShowDialog();
  30. }
  31. }
  32. }

Form2.cs

  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 ZY四则运算
  12. {
  13. public partial class Form2 : Form
  14. {
  15. public Form2()
  16. {
  17. InitializeComponent();
  18. }
  19. ; //倒计时
  20. ;
  21. ;
  22. private void Form2_Load(object sender, EventArgs e)
  23. {
  24. lblTime.Text = "剩余时间:";
  25. timer1.Enabled = false;
  26. timer1.Interval = ;
  27. }
  28. private void timer1_Tick(object sender, EventArgs e)
  29. {
  30. int tm = time--;
  31. lblTime.Text = "剩余时间:" + tm.ToString() + "秒";
  32. )
  33. {
  34. timer1.Enabled = false;
  35. MessageBox.Show("时间已到", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  36. }
  37. }
  38. private void button1_Click_1(object sender, EventArgs e)
  39. {
  40. timer1.Stop();
  41. MessageBox.Show(label1.Text);
  42. }
  43. private void button2_Click_1(object sender, EventArgs e)
  44. {
  45. timer1.Start();
  46. }
  47. private void button3_Click_1(object sender, EventArgs e)
  48. {
  49. sfd.Filter = "(*.txt)|*.txt";
  50. if (sfd.ShowDialog() == DialogResult.OK)
  51. {
  52. string sPath = sfd.FileName;
  53. FileStream fs = new FileStream(sPath, FileMode.Create);
  54. StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
  55. ;
  56. ; i <= iCount; i++)
  57. {
  58. sw.WriteLine(listBox2.Items[i].ToString());
  59. }
  60. sw.Flush();
  61. sw.Close();
  62. fs.Close();
  63. }
  64. }
  65. private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
  66. {
  67. )
  68. {
  69. textBox1.Text = listBox1.Items[].ToString();
  70. }
  71. else
  72. {
  73. MessageBox.Show("答题结束");
  74. }
  75. }
  76. private void textBox2_KeyDown(object sender, KeyEventArgs e)
  77. {
  78. jisuan js = new jisuan();
  79. if (e.KeyCode == Keys.Enter)
  80. {
  81. string result = textBox1.Text;
  82. if (textBox2.Text.Trim() == string.Empty) //去除空格之后,如果没答题给出提示。
  83. {
  84. MessageBox.Show("您尚未答题", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  85. return;
  86. }
  87. Count++;
  88. if (textBox2.Text ==js.Calucate(result).ToString()) //直接调用Calucate这个方法计算result的值并与输入的值进行比较
  89. {
  90. MessageBox.Show("回答正确!");
  91. listBox2.Items.Add(result + "=" + textBox2.Text + " " + "√");//若答对直接后面打个对勾。
  92. listBox1.Items.Remove(listBox1.SelectedItem);
  93. right++;
  94. }
  95.  
  96. else
  97. {
  98. MessageBox.Show("答题错误!");
  99. listBox2.Items.Add(result + "=" + textBox2.Text + " " + "×");//若答错就在后面打个错号。
  100. listBox1.Items.Remove(listBox1.SelectedItem);
  101. }
  102. label1.Text = ).PadRight(, , ) + "%";
  103. textBox1.Clear();
  104. textBox2.Clear();
  105. }
  106. }
  107. }
  108. }

运行测试:

出题界面:

答题界面:

提示结束:

保存:

C#带小括号的运算的更多相关文章

  1. bash 括号(小括号,双小括号,中括号,双中括号,大括号)

    小括号()和大括号{} 主要包括一下几种 ${var} $(cmd) ()和{} $((exp)) ${var:-string},${var:+string},${var:=string},${var ...

  2. shell中的括号(小括号,中括号,大括号)及单引号、 双引号,反引号(``)

    一.小括号,园括号() 1.单小括号 () ①命令组.括号中的命令将会新开一个子shell顺序执行,所以括号中的变量不能够被脚本余下的部分使用.括号中多个命令之间用分号隔开,最后一个命令可以没有分号, ...

  3. Javascript小括号“()”的多义性

    摘要:本文主要介绍JavaScript中小括号有五种语义. Javascript中小括号有五种语义 语义1,函数声明时参数表 function func(arg1,arg2){ // ... } 语义 ...

  4. shell中的大括号和小括号

      1.shell中的大括号和小括号   1.${var} 2.$(cmd) 3.() 和 {} 4.${var:-string}, ${var:+string},  ${var:=string}, ...

  5. Javascript 中的小括号 “()” 的多义性

    Javascript 中小括号有5 种语义 语义1:函数声明时参数表 1 function func(arg1, arg2){  2    // ...  3  }    语义2:和一些语句联合使用以 ...

  6. js小括号的作用

    js中小括号()的用法详解:对于小括号无论是菜鸟还是高手一定都不会陌生,可以说它几乎是随处可见,虽然熟悉但并非真正的理解,由此可能会产生很多莫名其妙的错误,下面就通过代码实例详细介绍一下小括号的用法. ...

  7. JavaScript小括号、中括号、大括号的多义性

    语义1,函数声明时参数表 func(arg1,arg2){ // ... } 语义2,和一些语句联合使用以达到某些限定作用 // 和for in一起使用 for(var a in obj){ // . ...

  8. javascript 函数后面有多个小括号f( )( )( )...

    有时我们看见js函数后面跟着多个小括号是什么意思?f( )( )( )... f()执行f函数,返回子函数 f()()执行子函数,返回孙函数 f()()()执行孙函数,返回重孙函数 ... ... 但 ...

  9. javascript小括号、中括号、大括号学习总结

    作为一名编程人员,和括号打交道是必不可少的.你可知道在不同的上下文中,括号的作用是不一样的,今天就让我们简单总结下javascript小括号.中括号.大括号的用法. 总的来说,JavaScript中小 ...

随机推荐

  1. Empire C:Basic 3

    首先我们定义一个表示年龄的指针: int* page: 这就是定义了一个指针,和定义普通变量就多了一个*符号而已. 为什么变量名用了p开头,这里引用了英文pointer(指向),表示它是一个指针,而非 ...

  2. SQL语句中count(1)count(*)count(字段)用法的区别

    SQL语句中count(1)count(*)count(字段)用法的区别 在SQL语句中count函数是最常用的函数之一,count函数是用来统计表中记录数的一个函数, 一. count(1)和cou ...

  3. A trip through the Graphics Pipeline 2011_05

    After the last post about texture samplers, we’re now back in the 3D frontend. We’re done with verte ...

  4. 酷友观点/经验:支付接口返回数据接收地址,session数据丢失(或者说失效)的问题浅析(原创文章)

    酷友观点/经验:支付接口返回数据接收地址,session数据丢失(或者说失效)的问题浅析(原创文章)   最近手头在开发一个游戏官网,在支付模块采用神州付技术支持,神州付数据表单中要求提供服务器返回地 ...

  5. Maven:解决-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match.

    1.添加M2_HOME的环境变量 2.Preference->Java->Installed JREs->Edit 选择一个jdk, 添加  -Dmaven.multiModuleP ...

  6. 在Altium_Designer_PCB_中插入图片的方法

    详细请看PDF: http://files.cnblogs.com/files/BinB-W/在Altium_Designer_PCB_中插入图片的方法.pdf 配套文件: http://files. ...

  7. English随笔1

    英语中的基本五大句型  1.Subject (主语) + Verb (谓语) Li Ming works The accident happened 2.Subject (主语) + Link. V( ...

  8. Echarts3 使用教程

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. Postgres Plus Advanced Server installation

    # setenforce Permissive # ./ppasmeta-9.3.1.3-linux-x64.run --mode text Installation Directory [/opt/ ...

  10. PHP Apache服务配置

    第一步:在C:\Windows\System32\drivers\etc中找到hosts配置服务 127.0.0.1 localhost 127.0.0.1 www.psq.com 保存即可 第二步: ...