当前位置:编程学习 > C#/ASP.NET >>

想用C#做一个另类的计算器,但无从下手

--------------------编程问答-------------------- 用栈解决撒 --------------------编程问答--------------------
public int EvaluateExpression()
{
SeqStack<char> optr = new SeqStack <char>(20);
SeqStack<int> opnd = new SeqStack <int>(20);
optr.Push(‘#’);
char c = Console.Read();
char theta = 0;
int a = 0;
int b = 0;
while (c != ‘#’)
{
if((c!=’+’) && (c!=’-‘)
&& (c!=’*’) && (c!=’/’)
&& (c!=’(‘) && (c!=’)’{
optr.Push(c);
}
else
{
switch(Precede(optr.GetTop(), c))
{
Case ‘<’:
optr.Push(c);
c = Console.Read();
break;
case ‘=’:
optr.Pop();
c = Console.Read();
break;
case ‘>’:
theta = optr.Pop();
a = opnd.Pop();
b = opnd.Pop();
opnd.Push(Operate(a,theta,b));
break;
}



Precede判断运算符优先级。 --------------------编程问答-------------------- string s = "";    
object o = new DataTable().Compute(s, "");
--------------------编程问答-------------------- 让我想起js中有个eval函数,呵呵。。。 --------------------编程问答-------------------- 我现在也在自学C#,不过刚写完这个程序,这是我编的第一个程序,需要的话可以发给你参考:

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

namespace 计算器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            time.Text = DateTime.Now.ToString();
        }

        private void time_Click(object sender, EventArgs e)
        {

        }

        private double nPrevValue = 0;       //上一次值
        private bool bAppend = true;      //用户按的数字是否连接在显示的数字后面
        private string strPrevOpar = "";  //上一次的操作符号

        // C按钮
        private void button16_Click(object sender, EventArgs e)
        {
            tbShow.Text = "0";
            strPrevOpar = "";
            bAppend = true;
            nPrevValue = 0;
        }

        //处理数字键
        private void Num_Click(object sender, System.EventArgs e)
        {
            string strNum = ((System.Windows.Forms.Button)sender).Text;
            if (bAppend)
                tbShow.Text = double.Parse(tbShow.Text + strNum).ToString();
            else
            {
                tbShow.Text = strNum;
                bAppend = true;
            }

        }

        //操作符号
        private void Opar_Click(object sender, EventArgs e)
        {
            double nCurValue = double.Parse(tbShow.Text);
            switch (strPrevOpar)
            { 
                case "+":
                    nCurValue += nPrevValue;
                    break;
                case "-":
                    nCurValue = nPrevValue - nCurValue;
                    break;
                case "*":
                    nCurValue *= nPrevValue;
                    break;
                case "/":
                    nCurValue = nPrevValue / nCurValue;
                    break;
                case "sqrt":
                    nCurValue = nCurValue * nCurValue;
                    break;

                default:
                    break;
            }
            strPrevOpar = ((System.Windows.Forms.Button)sender).Text;
            bAppend = false;
            tbShow.Text = nCurValue.ToString();
            nPrevValue = nCurValue;

        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("版本号:1.0.0.0 ");



        }

        private void 作者ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("作者:");
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.tbShow.SelectAll();
            this.tbShow.Copy();
        }

        private void 粘贴ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.tbShow.Paste();
        }

        private void dot_Click(object sender, EventArgs e)
        {
            string strNum = ((System.Windows.Forms.Button)sender).Text;
            tbShow.Text = tbShow.Text + strNum;
        }
--------------------编程问答-------------------- 怎么都放在TextBox中?不是自己找事吗~
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,