zoukankan      html  css  js  c++  java
  • 简易计算器(代码+封装+策略模式)

    具体设计思路:
    1.先创建一个Windows窗体。
    2.添加所需控件,修改相应的属性值。
    3.对控件编写代码,使之实现相应的功能。
    4.设计出一个四则运算雏形后再根据需求完善代码。
    5.进行测试分析。
    6.对程序进行PSP耗时分析。

    运行结果:
    代码如下:
     
    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 sizeyunsuan
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            string path = "E: tf";
            public static int Count = 0;
            public static int zhengque = 0;
            public static int lefttime;
            public static int time;
            public static int sum;
            int i = 1;
            Class1 mm = new Class1();
            private void Form1_Load(object sender, EventArgs e)
            {
                if (File.Exists(path))
                {
                    this.richTextBox1.LoadFile(path, RichTextBoxStreamType.RichText);
                    open.Enabled = false;
                }
                save.Enabled = false;
            }
            private void open_Click(object sender, EventArgs e) //打开文件
            {
                OpenFileDialog TxTOpenDialog = new OpenFileDialog();
                TxTOpenDialog.Filter = "RTF文件(*.RTF)|*.RTF";
                if (TxTOpenDialog.ShowDialog() == DialogResult.OK)
                {
                    path = TxTOpenDialog.FileName;
                    this.richTextBox1.LoadFile(TxTOpenDialog.FileName, RichTextBoxStreamType.RichText);
                    save.Enabled = false;
                    open.Enabled = false;
                    MessageBox.Show("读取成功""提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
     
            private void save_Click(object sender, EventArgs e) //保存文件
            {
                SaveFileDialog TxtSaveDialog = new SaveFileDialog();
                TxtSaveDialog.Filter = "RTF文件(*.RTF)|*.RTF";
                if (File.Exists(path))
                {
     
                    this.richTextBox1.LoadFile(path, RichTextBoxStreamType.RichText);
                    MessageBox.Show("保存成功""提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                   
                    save.Enabled = false;
                }
                else
                {
                    if (TxtSaveDialog.ShowDialog() == DialogResult.OK)
                    {
     
                        this.richTextBox1.SaveFile(TxtSaveDialog.FileName, RichTextBoxStreamType.RichText);
                        MessageBox.Show("保存成功""提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                         
                        save.Enabled = false;
                    }
                }
            }
     
            private void richTextBox1_TextChanged(object sender, EventArgs e)
            {
                save.Enabled = true;
                if (this.richTextBox1.Text == "" || this.richTextBox1.Text == null)
                {
                    open.Enabled = true;
                }
            }
            public void shizi() //录入题目
            {
                    left.Text = "";
                    fuhao.Text = "";
                    right.Text = "";  
            }
            private void open2_Click(object sender, EventArgs e) //打开试题
            {
                OpenFileDialog TxTOpenDialog = new OpenFileDialog();
                TxTOpenDialog.Filter = "RTF文件(*.RTF)|*.RTF";
                if (TxTOpenDialog.ShowDialog() == DialogResult.OK)
                {
                    path = TxTOpenDialog.FileName;
                    this.richTextBox2.LoadFile(TxTOpenDialog.FileName, RichTextBoxStreamType.RichText);
                    save.Enabled = false;
                    open.Enabled = false;
                    MessageBox.Show("导入成功""提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
     
            private void daoru_Click(object sender, EventArgs e) //导入试题
            {
                richTextBox2.Text = richTextBox1.Text;
            }
     
            private void daan1_Click(object sender, EventArgs e)
            {
                if (daan1.Text == "显示答案")
                {
                    daan.PasswordChar = Convert.ToChar(0);
                    daan1.Text = "隐藏答案";
                }
                else if (daan1.Text == "隐藏答案")
                {
                    daan.PasswordChar = '.';
                    daan1.Text = "显示答案";
     
                }
            }
     
            private void kaishi_Click(object sender, EventArgs e)
            {
     
                string[] ll = new string[100];
                ll = File.ReadAllLines("writer.txt");
                textBox1.Text = ll[0];
     
                string[] lli = new string[100];
                lli = File.ReadAllLines("writer1.txt");
                textBox2.Text = lli[0];
     
                string[] llp = new string[100];
                llp = File.ReadAllLines("writer2.txt");
                textBox3.Text = llp[0];
               
                
                int minute;
                try
                {
                    minute = int.Parse(this.shijian.Text);
                }
                catch (System.Exception ex)
                {
                    this.shijian1.Text = "输入错误";
                    return;
                }
                lefttime = minute;
                this.timer1.Interval = 1000;
                this.timer1.Enabled = true;
                this.timer1.Start();
                
            }
     
            private void timer1_Tick(object sender, EventArgs e)
            {
                if (lefttime <= 0)
                {
                    timer1.Enabled = false;
                    MessageBox.Show("时间到!");
                    Form2 frm2 = new Form2();
                    frm2.ShowDialog();
                     
                }
                this.shijian1.Text = "剩余时间" + lefttime.ToString() + "秒";
                lefttime--;
            }
     
            private void jieshu_Click(object sender, EventArgs e)
            {
                Form2 frm2 = new Form2();
                frm2.ShowDialog();
            }
     
            private void button1_Click(object sender, EventArgs e) //下一题
            {
                Count++;
                ti.Text = Count.ToString();
                StreamWriter writer = File.AppendText("writer.txt");
               writer.WriteLine(left.Text);
                writer.Close();
                StreamWriter writer1 = File.AppendText("writer1.txt");
                writer1.WriteLine(fuhao.Text);
                writer1.Close();
                StreamWriter writer2 = File.AppendText("writer2.txt");
                writer2.WriteLine(right.Text);
                writer2.Close();
                richTextBox1.Text += left.Text + fuhao.Text + right.Text + label2.Text + "" " ";
                mm.m= int.Parse(left.Text);
                mm.n = int.Parse(right.Text);
                daan.Text = Convert.ToString(mm.c);
                daan.Text += sum + " ";
                 
                
                mm.d = Convert.ToChar(fuhao.Text);
                mm.Calue();
             }
            private void textBox4_KeyDown(object sender, KeyEventArgs e)
            {
     
                string m = textBox2.Text;
                int result;
                switch (m)
                {
                    case "+":
                        sum = int.Parse(textBox1.Text) + int.Parse(textBox3.Text);
     
                        break;
                    case "-":
                        sum = int.Parse(textBox1.Text) - int.Parse(textBox3.Text);
                        break;
                    case "*":
                        sum = int.Parse(textBox1.Text) * int.Parse(textBox3.Text);
                        break;
                    case "/":
                        sum = int.Parse(textBox1.Text) / int.Parse(textBox3.Text);
                        break;
                    default:
                        break;
                }
                if (e.KeyCode == Keys.Enter)
                {
     
                    if (int.TryParse(textBox4.Text, out result) == false)
                    {
                        MessageBox.Show("请输入数字");
     
                    }
                    if (textBox4.Text == sum.ToString())
                    {
     
                        MessageBox.Show("答对啦!");
                        zhengque++;
     
                    }
                    else
                    {
     
                        MessageBox.Show("答错了,继续加油!");
                    }
                    
                    
                    textBox4.Clear();
     
                    string[] ll = new string[100];
                    ll = File.ReadAllLines("writer.txt");
                    textBox1.Text = ll[i];
                    string[] lli = new string[100];
                    lli = File.ReadAllLines("writer1.txt");
                    textBox2.Text = lli[i];
                    string[] llp = new string[100];
                    llp = File.ReadAllLines("writer2.txt");
                    textBox3.Text = llp[i];
                    i++;
     
                }
            }
     
            private void button2_Click(object sender, EventArgs e)
            {
                File.WriteAllText("writer.txt"string.Empty);
                File.WriteAllText("writer1.txt"string.Empty);
                File.WriteAllText("writer2.txt"string.Empty);
            }
      
        }
    }
    封装:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace sizeyunsuan
    {
        class Class1
        {
           public string a="";
           public string b="";
           public char d;
           public int m=0;
           public int n = 0;
           public int c = 0;      
           public void Calue()
            {          
                if(d=='+')
                { 
                     c=m+n;
                     a = "";
                     b = "";
                    
                }
                if (d == '-')
                {
                   c= m -  n ;
                   a = "";
                   b = "";
                  
                  
                }
                if (d == '*')
                {
                  c = m * n;
                  a = "";
                  b = "";
                    }
               if (d == '/')
                {
                   c =m / n;
                   a = "";
                   b = "";

    }
             }
    }

    }

    策略封装:
    using system;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    namespace shuxuefudao
        public interface ITaxStragety
            {
            int Calcul(int a,int b);
            
            }
       class Class2 : ITaxStragety
        {
           
          public  int Calcul(int a, int b)
            {
                return  a + b;
            }
     
        }
       class sub : ITaxStragety
        {
          public int Calcul(int a, int b)
           {
               return  a - b;
           }
        
       }
       class sheng : ITaxStragety
       {
          public int Calcul(int a, int b)
           {
               return a *b;
                            
           }
        
       }
        class chu : ITaxStragety
       {
         public int Calcul(int a, int b )
           {
               return a / b;
     
           }
       }
       
         class tiaoyong
        {               
             public int c=0;
             public int n=0;
             public void yunsuan(string m)
             {
                 if(m=="+")
                 {
                     Class2 ss = new Class2();
                     ss.Calcul(c,n);
                                                       
                 }
                 if(m=="-")
                 {
                     sub ww = new sub();
                     ww.Calcul(c,n);
     
                     
                 }
                 if(m=="*")
                 {
                     sheng qq = new sheng();
                     qq.Calcul(c,n);
                                
                 }
                 if(m=="/")
                 {
     
                     chu pp = new chu();
                     pp.Calcul(c,n);
                  
                 }
              
             }
        }
    }
    psp耗时分析:

                                                                           PSP耗时分析

      Personal Software Process Stages Time(%) Senior Student Time(h) SDE
    Planning 计划 10 6
      .Estimate   .估计这个任务需要多少时间 30 6
    Development 开发 50 15
      .Analysis   .需求分析 8 6
      .Design Spec   .生成设计文档 10 5
      .Design Review   .设计复审(和同事审核设计文档) 5 4
      .Coding Standard   .代码规范(为目前的开发制定合适的规范) 3 3
      .Design   .具体设计 20 10
      .Coding   .具体编码 24 11
    Code Review   .代码复审 7 8

  • 相关阅读:
    Docker学习总结(一)--Docker简介
    Liunx软件安装之Zabbix监控软件
    Liunx软件安装之Nginx
    Liunx软件安装之Redis
    Liunx软件安装之Tomcat
    Liunx软件安装之JDK
    Liunx软件安装之MySQL
    Liunx学习总结(九)--防火墙
    tensorflow 错误
    anaconda安装失败
  • 原文地址:https://www.cnblogs.com/gyhlkq/p/5059298.html
Copyright © 2011-2022 走看看