C#学习

为期5节课的C#课程培训,今天正式结束了.

李老师主要教我们如何用工具编写一个计算器,拥有可视化的UI界面的WPF插件比起JAVA开发真是快多了.

我的成果:


这个计算器可以实现简单的运算,并且记录计算过程,以日志的形式保存和读写,下面为主要的代码:


Class1.cs

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

namespace Calculator
{
    public class MyCalculator
    {
        private float _operatorX;

        private float _operatorY;
    
        public MyCalculator(float operatorX,float operatorY)
        {
            _operatorX = operatorX;
            _operatorY = operatorY;
        }

        public float Add()
        {
            return _operatorX + _operatorY;
        }

        public float Sub()
        {
            return _operatorX - _operatorY;
        }

        public float Mul()
        {
            return _operatorX * _operatorY;
        }

        public float Div()
        {
            if (_operatorY==0)
                return 0;
            else
                return _operatorX / _operatorY;
        }

    }
}

Calculator.IO.cs
using System;
using System.IO;

namespace Calculator.IO
{
    public class IODeal
    {
        /// <summary>
        /// 字段,日志文件名称
        /// </summary>
        const string path = @"CalculateRecord.txt";

        /// <summary>
        /// 写入信息
        /// </summary>
        /// <param name="message"></param>
        public void Write(string message)
        {
            //构造消息
            var msg = string.Format("时间:{0}{1}算式:{2}{3}", DateTime.Now, Environment.NewLine, message, Environment.NewLine);
            //写入文件
            File.AppendAllText(path, msg);
        }

        /// <summary>
        /// 读取信息
        /// </summary>
        /// <returns></returns>
        public string Read()
        {
            if (File.Exists(path))
            {
                var message = File.ReadAllText(path);
                return message;
            }
            else
            {
                return "日志文件不存在";
            }            
        }
    }
}

UnitTestProject1.cs
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Calculator;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var Calculator = new MyCalculator(5.3f, 2.3f);
            float result = Calculator.Add();
            Assert.AreEqual(result,7.6f);
        }
    }
}

MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Calculator;
using Calculator.IO;

namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private int[] temp = new int[16] ;//以数组形式保存输入的操作数
        private static int _flag=0;//数组的下标
        private float _operatorX;//第一个操作数
        private float _operatorY;//第二个操作数
        private float num;//对输入的数字进行保存,生成操作数
        private int _int_flag;//记录进行的运算(加,减,乘,除)
        private int count=0;//对连续运算的处理标记
        private string show_record;//对计算过程的记录,仿微软的计算器
        private string txtRecord;
        private string newRecord;
        private string oldRecord = null;
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 用来显示输入的操作数
        /// </summary>
        private void Show_Num()
        {
            string s_temp = null;
            int show_count=0;//对小数点的标记
            for (int i = 1; i <= _flag; i++)
            {
                if (temp[1] == 10)
                    s_temp = s_temp + '0' + '.';
                else if (temp[i] != 10)
                    s_temp = s_temp + temp[i];
                else if (temp[i] == 10 && show_count == 0) 
                {
                    s_temp = s_temp + ".";
                    show_count++;//防止出现两个小数点
                }
            }
            float.TryParse(s_temp,out num);
            Show.Text = s_temp;
        }

        private void Num_1_Click(object sender, RoutedEventArgs e)
        {
            if (_flag == 15)//输入数不能超过15位
                Show.Text = "错误";
            else
            {
                temp[++_flag] = 1;
                Show_Num();//每次输入一个数会实时更新输入的数
            }
        }

        private void Num_2_Click(object sender, RoutedEventArgs e)
        {
            if (_flag == 15)
                Show.Text = "错误";
            else
            {
                temp[++_flag] = 2;
                Show_Num();
            }
        }

        private void Num_3_Click(object sender, RoutedEventArgs e)
        {
            if (_flag == 15)
                Show.Text = "错误";
            else
            {
                temp[++_flag] = 3;
                Show_Num();
            }
        }

        private void Num_4_Click(object sender, RoutedEventArgs e)
        {
            if (_flag == 15)
                Show.Text = "错误";
            else
            {
                temp[++_flag] = 4;
                Show_Num();
            }
        }

        private void Num_5_Click(object sender, RoutedEventArgs e)
        {
            if (_flag == 15)
                Show.Text = "错误";
            else
            {
                temp[++_flag] = 5;
                Show_Num();
            }
        }

        private void Num_6_Click(object sender, RoutedEventArgs e)
        {
            if (_flag == 15)
                Show.Text = "错误";
            else
            {
                temp[++_flag] = 6;
                Show_Num();
            }
        }

        private void Num_7_Click(object sender, RoutedEventArgs e)
        {
            if (_flag == 15)
                Show.Text = "错误";
            else
            {
                temp[++_flag] = 7;
                Show_Num();
            }
        }

        private void Num_8_Click(object sender, RoutedEventArgs e)
        {
            if (_flag == 15)
                Show.Text = "错误";
            else
            {
                temp[++_flag] = 8;
                Show_Num();
            }
        }

        private void Num_9_Click(object sender, RoutedEventArgs e)
        {
            if (_flag == 15)
                Show.Text = "错误";
            else
            {
                temp[++_flag] = 9;
                Show_Num();
            }
        }

        private void Num_0_Click(object sender, RoutedEventArgs e)
        {
            if (_flag == 15)
                Show.Text = "错误";
            else
            {
                temp[++_flag] = 0;
                Show_Num();
            }
        }

        private void Num_dian_Click(object sender, RoutedEventArgs e)
        {
            if (_flag == 15)
                Show.Text = "错误";
            else
            {
                temp[++_flag] = 10;//用10来代表小数点,易于处理
                Show_Num();
            }
        }

        /// <summary>
        /// 对加法的处理
        /// </summary>
        private void Add_Click(object sender, RoutedEventArgs e)
        {
            if(count==0)
                _operatorX = num;
            _flag = 0;
            _int_flag = 1;
            newRecord = _operatorX.ToString();
            show_record = show_record + _operatorX + '+';//记录计算过程
            record.Text = show_record;//显示计算过程
        }

        /// <summary>
        /// 对减法的处理
        /// </summary>
        private void Sub_Click(object sender, RoutedEventArgs e)
        {
            if (count == 0)
                _operatorX = num;
            _flag = 0;
            _int_flag = 2;
            newRecord = _operatorX.ToString();
           show_record = show_record + _operatorX + '-';
           record.Text = show_record;
        }

        /// <summary>
        /// 对乘法的处理
        /// </summary>
        private void Mul_Click(object sender, RoutedEventArgs e)
        {
            if (count == 0)
                _operatorX = num;
            _flag = 0;
            _int_flag = 3;
            newRecord = _operatorX.ToString();
           show_record = show_record + _operatorX + '*';
           record.Text = show_record;
        }

        /// <summary>
        /// 对除法的处理
        /// </summary>
        private void Div_Click(object sender, RoutedEventArgs e)
        {
            if (count == 0)
                _operatorX = num;
            _flag = 0;
            _int_flag = 4;
            newRecord = _operatorX.ToString();
           show_record = show_record + _operatorX + '/';
           record.Text = show_record;
        }

        /// <summary>
        /// 退格键
        /// </summary>
        private void Back_Click(object sender, RoutedEventArgs e)
        {
            _flag--;
            Show_Num();
        }

        /// <summary>
        /// 置空显示端,重新进行运算
        /// </summary>
        private void Clear_Click(object sender, RoutedEventArgs e)
        {
            _flag = 0;
            count = 0;
            Show.Text = "0";
            show_record = null;
            record.Text=show_record;
        }

        /// <summary>
        /// 写日志
        /// </summary>
        private void WriteLog()
        {
            var operation = txtRecord;
            var provider = new Calculator.IO.IODeal();
            provider.Write(operation);
        }

        /// <summary>
        /// 对结果的处理及显示
        /// </summary>
        private void Result_Click(object sender, RoutedEventArgs e)
        {
            _operatorY = num;
            var My_Calculator = new MyCalculator(_operatorX, _operatorY);
            char c_flag='_';
            float result = 0;
            //对各种运算的处理及实现
            switch (_int_flag)
            {
                case 1:
                    {
                        result = My_Calculator.Add();
                        Show.Text = result.ToString();
                        c_flag = '+';
                        break;
                    }
                case 2:
                    {
                        result = My_Calculator.Sub();
                        Show.Text = result.ToString();
                        c_flag = '-';
                        break;
                    }
                case 3:
                    {
                        result = My_Calculator.Mul();
                        Show.Text = result.ToString();
                        c_flag = '*';
                        break;
                    }
                case 4:
                    {
                        result = My_Calculator.Div();
                        Show.Text = result.ToString();
                        c_flag = '/';
                        break;
                    }
                default:
                    {
                        Show.Text = "错误";
                        break;
                    }
            }
            _operatorX = result;//保证运算的连续进行
            if (count == 0)
            {
                txtRecord = newRecord + c_flag + _operatorY + '=' + result;
                oldRecord = result.ToString();
            }
            else
            {
                txtRecord = oldRecord + c_flag + _operatorY + '=' + result;
                oldRecord = result.ToString();
            }
            count = 2;//虽然保证了连续运算,但重新运算需先置空运算器
            _flag = 0;//计算完成,对存数的数组置空
            show_record = null;//计算过程置空处理
            record.Text = show_record;//更新计算过程
            WriteLog();
        }

        /// <summary>
        /// 显示历史计算过程
        /// </summary>
        private void history_Click(object sender, RoutedEventArgs e)
        {
            //清空文本
            txtLog.Clear();
            //读日志
            var provider = new Calculator.IO.IODeal();
            var message = provider.Read();
            txtLog.Text = message;
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值