计算类的封装

jisuan.cs

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;

 namespace ZY四则运算
 {
     public class jisuan
     {
         public Dictionary<char, int> priorities = null;  //优先级

         public void Calculator()    //添加了四种运算符以及四种运算符的优先级
         {
             priorities = new Dictionary<char, int>();
             priorities.Add();
             priorities.Add();
             priorities.Add();
             priorities.Add();
             priorities.Add();
         }

         const string operators = "+-*/";      //运算符

         public double Compute(double leftNum, double rightNum, char op)  //这是一种方法,用来计算左右两个数的静态方法!
         {
             switch (op)
             {
                 case '+': return leftNum + rightNum;
                 case '-': return leftNum - rightNum;
                 case '*': return leftNum * rightNum;
                 case '/': return leftNum / rightNum;
                 ;
             }
         }

         public bool IsOperator(char op)  //每次判断这个字符是否是运算符?
         {
             ;
         }

         public bool IsAssoc(char op)    //返回一个关联符号
         {
             return op == '+' || op == '-' || op == '*' || op == '/';
         }

         public  Queue<object> QueueSort(string expression)   // 队列排序
         {
             Queue<object> result = new Queue<object>();
             Stack<char> operatorStack = new Stack<char>();   //运算符栈
             operatorStack.Push('#');
             char top, cur, tempChar;                    //top栈顶,current最近的;
             string tempNum;
             , j; i < expression.Length; )     //取出表达式
             {
                 cur = expression[i++];                 //取出表达式的每个字符赋给cur
                 top = operatorStack.Peek();        //栈顶元素赋给top此时为"#"

                 if (cur == '(')         //将左括号压栈,此时栈顶元素为"("
                 {
                     operatorStack.Push(cur);
                 }
                 else
                 {
                     if (IsOperator(cur))       //如果是运算符的话
                     {
                         while (IsOperator(top) && ((IsAssoc(cur) && priorities[cur] <= priorities[top])) || (!IsAssoc(cur) && priorities[cur] < priorities[top]))
                         {
                             result.Enqueue(operatorStack.Pop());     //如果元素为运算符并且优先级小于栈顶元素优先级,出栈
                             top = operatorStack.Peek();      //继续把栈顶元素赋给top
                         }
                         operatorStack.Push(cur);      //把数字压栈
                     }
                     else if (cur == ')')       //将右括号添加到结尾
                     {
                          && (tempChar = operatorStack.Pop()) != '(')
                         {
                             result.Enqueue(tempChar);
                         }
                     }
                     else
                     {
                         tempNum = "" + cur;
                         j = i;
                         ')))
                         {
                             tempNum += expression[j++];
                         }
                         i = j;
                         result.Enqueue(tempNum);
                     }
                 }
             }
             )
             {
                 cur = operatorStack.Pop();
                 if (cur == '#') continue;
                 )
                 {
                     top = operatorStack.Peek();
                 }

                 result.Enqueue(cur);
             }

             return result;
         }

         public  double Calucate(string expression)
         {
             try
             {
                 var rpn = QueueSort(expression);
                 Stack<double> operandStack = new Stack<double>();
                 double left, right;
                 object cur;
                 )
                 {
                     cur = rpn.Dequeue();        //出列
                     if (cur is char)            //如果cur为字符的话
                     {
                         right = operandStack.Pop();   //右边的数字出栈
                         left = operandStack.Pop();    //左边的数字出栈
                         operandStack.Push(Compute(left, right, (char)cur));    //此时调用compute方法
                     }
                     else
                     {
                         operandStack.Push(double.Parse(cur.ToString()));      //是数字就压栈
                     }
                 }
                 return operandStack.Pop();
             }
             catch
             {
                 throw new Exception("表达式不正确!");
             }
         }
     }
 }

Form1.cs

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;
 using System.IO;

 namespace ZY四则运算
 {
     public partial class Form1 : Form
     {
         Form2 frm2 = new Form2();
         public Form1()
         {
             InitializeComponent();
         }
         private void button1_Click_1(object sender, EventArgs e)
         {
             string Express = textBox1.Text;
             frm2.listBox1.Items.Add(Express);
             listBox1.Items.Add(" " + Express + "=");
             textBox1.Clear();
         }
         private void button2_Click(object sender, EventArgs e)
         {
             frm2.ShowDialog();
         }
     }
 }

Form2.cs

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;
 using System.IO;

 namespace ZY四则运算
 {
     public partial class Form2 : Form
     {
         public Form2()
         {
             InitializeComponent();
         }
         ; //倒计时
         ;
         ;
         private void Form2_Load(object sender, EventArgs e)
         {
             lblTime.Text = "剩余时间:";
             timer1.Enabled = false;
             timer1.Interval = ;
         }
         private void timer1_Tick(object sender, EventArgs e)
         {
             int tm = time--;
             lblTime.Text = "剩余时间:" + tm.ToString() + "秒";
             )
             {
                 timer1.Enabled = false;
                 MessageBox.Show("时间已到", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
             }
         }
         private void button1_Click_1(object sender, EventArgs e)
         {
             timer1.Stop();
             MessageBox.Show(label1.Text);
         }
         private void button2_Click_1(object sender, EventArgs e)
         {
             timer1.Start();
         }
         private void button3_Click_1(object sender, EventArgs e)
         {
             sfd.Filter = "(*.txt)|*.txt";
             if (sfd.ShowDialog() == DialogResult.OK)
             {
                 string sPath = sfd.FileName;
                 FileStream fs = new FileStream(sPath, FileMode.Create);
                 StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
                 ;
                 ; i <= iCount; i++)
                 {
                     sw.WriteLine(listBox2.Items[i].ToString());
                 }
                 sw.Flush();
                 sw.Close();
                 fs.Close();
             }
         }
         private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
         {
             )
             {
                 textBox1.Text = listBox1.Items[].ToString();
             }
             else
             {
                 MessageBox.Show("答题结束");
             }
         }
         private void textBox2_KeyDown(object sender, KeyEventArgs e)
         {
             jisuan js = new jisuan();
             if (e.KeyCode == Keys.Enter)
             {
                 string result = textBox1.Text;
                 if (textBox2.Text.Trim() == string.Empty)            //去除空格之后,如果没答题给出提示。
                 {
                     MessageBox.Show("您尚未答题", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                     return;
                 }
                 Count++;
                 if (textBox2.Text ==js.Calucate(result).ToString())   //直接调用Calucate这个方法计算result的值并与输入的值进行比较
                 {
                     MessageBox.Show("回答正确!");
                     listBox2.Items.Add(result + "=" + textBox2.Text + "  " + "√");//若答对直接后面打个对勾。
                     listBox1.Items.Remove(listBox1.SelectedItem);
                     right++;
                 }

                 else
                 {
                     MessageBox.Show("答题错误!");
                     listBox2.Items.Add(result + "=" + textBox2.Text + "  " + "×");//若答错就在后面打个错号。
                     listBox1.Items.Remove(listBox1.SelectedItem);
                 }
                 label1.Text = ).PadRight(, , ) + "%";
                 textBox1.Clear();
                 textBox2.Clear();
             }
         }
     }
 }

运行测试:

出题界面:

答题界面:

提示结束:

保存:

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. SQL Server 存储过程(转)

    Transact-SQL中的存储过程,非常类似于Java语言中的方法,它可以重复调用.当存储过程执行一次后,可以将语句缓存中,这样下次执行的时候直接使用缓存中的语句.这样就可以提高存储过程的性能. Ø ...

  2. Codeforces Round #350 (Div. 2) D2 二分

    五一期间和然然打的团队赛..那时候用然然的号打一场掉一场...七出四..D1是个数据规模较小的题 写了一个暴力过了 面对数据如此大的D2无可奈何 现在回来看 一下子就知道解法了 二分就可以 二分能做多 ...

  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. Hibernate - lazy, fetch, inverse, cascade

    Inverse是hibernate双向关系中的基本概念.inverse的真正作用就是指定由哪一方来维护之间的关联关系.当一方中指定了"inverse=false"(默认),那么那一 ...

  5. iOS中文API之NSLayoutconstraint

    AutoLayout为开发者提供了一种不同于传统对于UI元素位置指定的布局方法.以前,不论是在IB里拖放,还是在代码中写,每个UIView都会有自己的frame属性,来定义其在当前视图中的位置和尺寸. ...

  6. open-falcon 安装

    openfalcon 规划 拓扑图 transfer 配置文件 [root@openfalcon_transfer1 transfer]# cat cfg.json { "debug&quo ...

  7. HTML两张图片叠加问题的进一步修改

    要想两张图片叠加,只需在circle添加一个Position:absolute就OK了 以上几步,很多大侠前辈都已经说过,相信不用再啰嗦,我想说的是一种扩展,将示例放到右边,可能因为我悟性低,研究了一 ...

  8. svg学习(一)

    SVG 是使用 XML 来描述二维图形和绘图程序的语言. 什么是SVG? SVG 指可伸缩矢量图形 (Scalable Vector Graphics) SVG 用来定义用于网络的基于矢量的图形 SV ...

  9. 使用pgstatspack分析PostgreSQL数据库性能

    pgstatspack [root@test01 soft]# wget http://pgfoundry.org/frs/download.php/3151/pgstatspack_version_ ...

  10. Java基本语法

    一:跨行 Java变量不能跨行,如:String na me = “张三"; 字符串不能跨行,如:String a = "xxxxxxxxxx yyyyyyyy"; 二: ...