C#上机

 

1.1

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

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Console.WriteLine("学号:201701060414\n");
            System.Console.WriteLine("姓名:刘月浩\n");
            System.Console.WriteLine("性别:男\n");
            System.Console.WriteLine("年龄:18\n");
            System.Console.WriteLine("专业:计算机科学与技术\n");
            Console.ReadLine();
        }
    }
}
//xh,xm,xb,nl,zy

1.2

 private void button1_Click(object sender, EventArgs e)
        {
            string s;
            s = "学号:" + textBox1.Text + " 姓名:" + textBox2.Text + " 性别:" + textBox3.Text
                + " 年龄:" + textBox4.Text + " 专业:" + textBox5.Text;
            textBox6.Text = s;
        }

2.2

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;

namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "实训2_1";
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            double c = Convert.ToInt32(textBox1.Text);
            double f = 9.0 / 5 * c + 32;
            textBox2.Text = f.ToString();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

2.2

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;

namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "实训2_2";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int money = Convert.ToInt32(textBox1.Text);
            int year = Convert.ToInt32(textBox3.Text);
            double rate = Convert.ToDouble(textBox2.Text) / 100;
            double interset = money * rate * year;
            textBox4.Text = interset.ToString();
            double total = money + interset;
            textBox5.Text = total.ToString();
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

2.3

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;

namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int i = 0;
        double[] a = new double[5];
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "实训2_3";
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            double element = double.Parse(textBox1.Text);
            a[i] = element;
            label4.Text += a[i] + " ";
            i++;
            label2.Text = "请输入第" + (i+1) + "个元素";
        }

        private void label4_Click(object sender, EventArgs e)
        {
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Array.Sort(a);
            label6.Text = a[0] + " " + a[1] + " " + a[2] + " " + a[3] + " " + a[4]; 
        }
    }
}

3.1

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;

namespace WindowsFormsApplication10
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            double x = Convert.ToDouble(textBox1.Text);
            double y;
            if (x < 1) y = x;
            else if (x >= 1 && x < 10)
                y = 2 * x - 1;
            else
                y = 3 * x - 11;
            textBox2.Text = y.ToString();
        }
    }
}

3.2

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;

namespace WindowsFormsApplication10
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            int num = Convert.ToInt32(textBox1.Text);
            int turnNum = 0;
            while (num > 0)
            {
                turnNum = turnNum * 10 + num % 10;
                num /= 10;
            }
            textBox2.Text = turnNum.ToString();
        }
    }
}

3.3

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;

namespace WindowsFormsApplication10
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            int sum ;
            for(int i = 2; i < 1000; i++)
            {
                sum = 0;
                for (int j = 1; j <= i / 2; j++)
                {
                    if (i % j == 0) sum += j;
                }
                if (sum == i)
                {
                    sb.Append(i + "是?一?个?完?数簓:" + i + " =1");
                    for (int j = 2; j <= i / 2; j++)
                    {
                        if (i % j == 0) sb.Append("+" + j);
                    }
                    sb.Append("\n");
                  
                }
            }
            label3.Text = sb.ToString();
        }
    }
}

4.1

using System;
……
namespace SY4_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            AddressBook ab = new AddressBook(nameTex.Text,numTex.Text,emailTex.Text);
            massage.Text = ab.GetMassage();
        }
    }
    class AddressBook
    {
        private string name;
        private string number;
        private string email;
        public AddressBook(string name, string number, string email)
        {
            if (number == "")
                number = null;
            if (email == "")
                email = null;
            this.name = name;
            this.number = number;
            this.email = email;
        }
        public string Name
        {
            get
            {
                return name;
            }
        }
        public string Number
        {
            get
            {
                if (number == null)
                    return "未输入";
                else return number;
            }
            set
            {
                number = value;
            }
        }
        public string Email
        {
            get
            {
                if (email == null)
                    return "未输入";
                else return email;
            }
            set
            {
                email = value;
            }
        }
        public string GetMassage()
        {
            return string.Format("姓名:{0}\r\n电话:{1}\r\nEmail:{2}\r\n", Name, Number, Email);
        }
    }
}

4.2

using System;
……
namespace SY4_2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            Time time = new Time(int.Parse(textH.Text), int.Parse(textM.Text), int.Parse(textS.Text));
            time.Addsecond();
            textH.Text = time.Hour.ToString();
            textM.Text = time.Minute.ToString();
            textS.Text = time.Second.ToString();
        }

    }

    class Time
    {
        private int hour;
        private int minute;
        private int second;
        public Time()
        {
            hour = System.DateTime.Now.Hour;
            minute = System.DateTime.Now.Minute;
            second = System.DateTime.Now.Second;
        }
        public Time(int hour, int minute, int second)
        {
            this.hour = hour;
            this.minute = minute;
            this.second = second;
        }
        public int Hour
        {
            get
            {
                return hour;
            }
        }
        public int Minute
        {
            get
            {
                return minute;
            }
        }
        public int Second
        {
            get
            {
                return second;
            }
        }
        public void Addsecond()
        {
            second++;
            if (second >= 60)
            {
                second = second % 60;
                minute++;
            }
            if (minute >= 60)
            {
                minute = minute % 60;
                hour++;
            }
            if (hour >= 24)
            {
                hour = hour % 24;
            }
        }  
    }
}

4.3

using System;
……
namespace SY4_3
{
    public partial class Form1 : Form
    {
        Account man=null;
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonCreat_Click(object sender, EventArgs e)
        {
            man = new Account();
            massage.Text = string.Format("创建账户成功,\r\n用户卡号为:\r\n{0}", man.CardNo.ToString());
        }

        private void buttonGet_Click(object sender, EventArgs e)
        {
            if (man == null)
            {
                massage.Text = string.Format("取款失败,\r\n请先创建账户。\r\n");
            }
            else if (getTex.Text == "" || int.Parse(getTex.Text)<=0)
            {
                massage.Text = string.Format("取款失败,\r\n请输入正确的取款金额。\r\n");
            }
            else if (int.Parse(getTex.Text) > man.Money)
            {
                massage.Text = string.Format("取款失败,\r\n余额不足。\r\n");
            }
            else
            {
                man.Money = man.Money - int.Parse(getTex.Text);
                massage.Text = string.Format("操作成功!\r\n取款{0}元。\r\n", int.Parse(getTex.Text));
            }
        }

        private void buttonSet_Click(object sender, EventArgs e)
        {
            if (man == null)
            {
                massage.Text = string.Format("存款失败,\r\n请先创建账户。\r\n");
            }
            else if (setTex.Text == "" || int.Parse(setTex.Text) <= 0)
            {
                massage.Text = string.Format("存款失败,\r\n请输入正确的存款金额。\r\n");
            }
            else
            {
                man.Money = man.Money+int.Parse(setTex.Text);
                massage.Text = string.Format("操作成功!\r\n存款{0}元。\r\n", int.Parse(setTex.Text));
            }
        }

        private void buttonCheck_Click(object sender, EventArgs e)
        {
            massage.Text = string.Format("储存卡账户:\r\n{0}\r\n当前余额:{1}",man.CardNo, man.Money);
        }
    }

    public class Account
    {
        private int cardNo;
        private int money;

        public Account()
        {
            Random r = new Random();
            cardNo = r.Next(100000, 500000);
            money = 100;
        }
        public int CardNo
        {
            get
            {
                return cardNo;
            }
        }
        public int Money
        {
            get
            {
                return money;
            }
            set
            {
                money = value;
            }
        }
    }
}

 

5.1

namespace contest5_1
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string name = txtName.Text;
            int age = int.Parse(txtAge.Text);
            double chinese = double.Parse(t1.Text);
            double math = double.Parse(t2.Text);
            Pupil pupil = new Pupil(name, age, chinese, math);

            string str = "总人数:" + Student.number + ",姓名:" + pupil.Name + ",小学生,平均成绩为:" + pupil.Average() + "\n";
            StringBuilder s = new StringBuilder(9999);
            if (result.Text != null)
            {
                s.Append(result.Text).Append(str);
            }
            else
            {
                s.Append(str);
            }
            
            string ss = s.ToString();
            result.Text = ss;

        }
        
        private void button2_Click(object sender, EventArgs e)
        {
            string name = txtName.Text;
            int age = int.Parse(txtAge.Text);
            double chinese = double.Parse(t1.Text);
            double math = double.Parse(t2.Text);
            double english = double.Parse(t3.Text);
            Junior jun = new Junior(name, age, chinese, math, english);

            string str = "总人数:" + Student.number + ",姓名:" + jun.Name + ",中学生,平均成绩为:" + jun.Average() + "\n";
            StringBuilder s = new StringBuilder(9999);
            s.Append(result.Text).Append(str);
            string ss = s.ToString();
            result.Text = ss;

        }

        private void button3_Click(object sender, EventArgs e)
        {
            string name = txtName.Text;
            int age = int.Parse(txtAge.Text);
            double must = double.Parse(t1.Text);
            double chose = double.Parse(t2.Text);
            Univer uni = new Univer(name, age, must, chose);

            string str = "总人数:" + Student.number + ",姓名:" + uni.Name + ",大学生,平均成绩为:" + uni.Average() + "\n";
            StringBuilder s = new StringBuilder(9999);
            s.Append(result.Text).Append(str);
            string ss = s.ToString();
            result.Text = ss;
        }
    }
    public abstract class Student
    {
        protected string name;
        protected int age;
        public  static int number;
        public Student(string name, int age)
        {
            this.age = age;
            this.name = name;
            number++;

        }
        public string Name
        {
            get { return name; }
        }
        public abstract double Average();
    }

    public class Pupil : Student
    {
        protected double chinese;
        protected double math;
        public Pupil(string name, int age, double chinese, double math) : base(name, age)
        {
            this.chinese = chinese;
            this.math = math;

        }
        public override double Average()
        {
            return (chinese + math) / 2;
        }
    }

    public class Junior : Pupil
    {
        protected double english;
        public Junior(string name, int age, double chinese, double math, double english) : base(name, age, chinese, math)
        {
            this.english = english;

        }
        public override double Average()
        {
            return (chinese + math + english) / 3;
        }
    }

    public class Univer : Student
    {
        protected double chose;
        protected double must;
        public Univer(string name, int age, double must, double chose) : base(name, age)
        {
            this.chose = chose;
            this.must = must;
        }
        public override double Average()
        {
            return (chose + must) / 2;
        }
    }
}

5.2

namespace contest5_2
{
    public partial class Form1 : Form
    {
        public Account account;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            account = new VIPAccount();
            int accountNo = account.CreditNo;
            string msg = String.Format("创建VIP账户成功,用户卡号为{0}",accountNo);
            lblshow.Text = "\n" + msg + "\n";

        }

        private void button2_Click(object sender, EventArgs e)
        {
            account = new Account();
            string msg = string.Format("创建账户成功,用户卡号为:\n{0}", account.CreditNo);
            lblshow.Text = "\n" + msg;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string msg;
            if (account == null)
            {
                msg = "请先创建用户!";
            }
            else
            {
                decimal money = decimal.Parse(tin.Text);
                account.SaveDraw(money, out msg);
            }
            lblshow.Text = "\n" + msg;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string msg;
            if (account == null)
            {
                msg = "请先创建用户!";
            }
            else
            {
                decimal money = decimal.Parse(tout.Text);
                account.WithDraw(money, out msg);
            }
            lblshow.Text = "\n" + msg;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string msg = string.Format("储蓄卡账户:\n" + account.CreditNo + "\n当前余额为:\n" + account.Balance);
            lblshow.Text = "\n" + msg;
        }
    }

    public class Account
    {
        protected int creditNo;
        protected decimal balance;
        public Account()
        {
            Random r = new Random();
            creditNo = r.Next(10000, 50000);
            balance = 100;
        }
        public decimal Balance
        {
            get
            {
                return balance;
            }
        }
        public int CreditNo
        {
            get
            {
                return creditNo;
            }
        }
        public virtual bool WithDraw(decimal money, out string message)
        {
            if (money < 0)
            {
                message = "操作失败!\n输入金额不正确!";
                return false;
            }
            else if (balance >= money)
            {
                balance -= money;
                message = "操作成功!\n取款" + money + "元";
                return true;
            }
            else
            {
                message = "操作失败\n余额不足!";
                return true;
            }
        }
        public bool SaveDraw(decimal money, out string message)
        {
            if (money <= 0)
            {
                message = "操作失败!\n输入金额不正确!";
                return false;
            }
            else
            {
                balance += money;
                message = "操作成功!\n存款" + money + "元";
                return true;
            }
        }
    }

    public class VIPAccount : Account
    {
        public VIPAccount()
        {
            Random r = new Random();
            creditNo = r.Next(500000, 1000000);
            balance = 10000;
        }

        public override bool WithDraw(decimal money, out string message)
        {
            if (money < 0)
            {
                message = "操作失败!\n输入金额不正确!";
                return false;
            }
            else if (balance >= money)
            {
                balance -= money;
                message = "操作成功!\n取款" + money + "元";
                return true;
            }
            else if (balance + 1000 > money) 
            {
                balance -= money;
                message = "操作成功!\n取款" + money + "元,透支" + (-balance) + "元";
                return true;
            }
            else
            {
                message = "操作失败\n余额不足!";
                return true;
            }
        }
    }
}

5.3

namespace contest5_3
{
    public partial class Form1 : Form
    {
        IPlayer iplayer;
        MP3 mp3;
        AVI avi;

        public Form1()
        {
            InitializeComponent();
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            mp3 = new MP3();
            iplayer = (IPlayer)mp3;

        }
        
        private void button2_Click(object sender, EventArgs e)
        {
            avi = new AVI();
            iplayer = (IPlayer)avi;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            lblshow.Text = iplayer.Pre();
        }
       
        private void button4_Click(object sender, EventArgs e)
        {
            lblshow.Text = iplayer.Next();
        }
        
        private void button5_Click(object sender, EventArgs e)
        {
            lblshow.Text = iplayer.Stop();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            lblshow.Text = iplayer.Play();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            lblshow.Text = iplayer.Pause();
        }

    }

    interface IPlayer
    {
        string Play();
        string Stop();
        string Pause();
        string Pre();
        string Next();
    }

    public class MP3 : IPlayer
    {
        string IPlayer.Next()
        {
            return "播放下一首MP3歌曲!";
        }

        string IPlayer.Pause()
        {
            return "暂停播放MP3歌曲!";
        }

        string IPlayer.Play()
        {
            return "正在播放MP3歌曲!";
        }

        string IPlayer.Pre()
        {
            return "播放上一首MP3歌曲!";
        }

        string IPlayer.Stop()
        {
            return "停止播放MP3歌曲!";
        }
    }

    public class AVI : IPlayer
    {
        string IPlayer.Next()
        {
            return "播放下一首AVI歌曲!";
        }

        string IPlayer.Pause()
        {
            return "暂停播放AVI歌曲!";
        }

        string IPlayer.Play()
        {
            return "正在播放AVI歌曲!";
        }

        string IPlayer.Pre()
        {
            return "播放上一首AVI歌曲!";
        }

        string IPlayer.Stop()
        {
            return "停止播放AVI歌曲!";
        }
    }
}

6.1

using System.Collections;

namespace contest6_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        ArrayList list =new ArrayList();

        private void btmadd_Click(object sender, EventArgs e)
        {
            string name = txt1.Text;
            string position = txt2.Text;
            Teacher t = new Teacher(name, position);
            list.Add(t);
            lblshow.Text = "";
            myForeach();

        }

        private void btmdelete_Click(object sender, EventArgs e)
        {
            int mak = int.Parse(txt3.Text);
            list.RemoveAt(mak);
            lblshow.Text = "";
            myForeach();

        }

        private void myForeach()
        {
            foreach(object obj in list)
            {
                Teacher t = (Teacher)obj;
                lblshow.Text += t.GetMessage();
            }
        }
    }

    public class Teacher
    {
        public Teacher(string name,string position)
        {
            this.name = name;
            this.position = position;
        }
        private string name;
        private string position;
        public string GetMessage()
        {
            return string.Format("教师姓名:{0} 职称:{1}\n",this.name,this.position);

        }
    }
}

6.2

namespace contest6_2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Classlist classlist = new Classlist(3);
        private void button1_Click(object sender, EventArgs e)
        {
            int index = Convert.ToInt32(txtno.Text) - 1;
            Student t = classlist[index];
            if (t == null)
            {
                lblshow.Text = string.Format("没有第{0}号学生", index + 1 );
            }
            else
            {
                lblshow.Text = string.Format("第{0}号学生姓名为:{1}", index + 1 , t.Name);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string name = txtname.Text;
            Student s = classlist[name];
            if (s == null)
            {
                lblshow.Text = string.Format("未找到姓名为{0}的学生",name);
            }
            else
            {
                lblshow.Text = string.Format("找到姓名为{0}的学生,学号为{1}",name,s.No);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string name = txtname.Text;
            string no = txtno.Text ;
            int index = Convert.ToInt32(txtno.Text) - 1;
            Student t = new Student(no, name);
            classlist[index] = t;
            lblshow.Text = string.Format("学生添加成功!");

        }
    }

    public class Student
    {
        private string no;
        private string name;
        public Student(string no,string name)
        {
            this.name = name;
            this.no = no;
        }
        public string Name
        {
            get
            {
                return name;
            }
        }
        public string No
        {
            get
            {
                return no;
            }
        }
    }

    public class Classlist
    {
        private Student []stu;
        public Classlist(int n)
        {
            stu = new Student[n];
        }

        public Student this[string s]
        {
            get
            {
                {
                    foreach (Student p in stu)
                        {
                        if (p.Name == s)
                            {
                                return p;
                            }
                        }
                }
                return null;
            }
        }
        public Student this[int index]
        {
            get
            {
                if (index < 0 || index >= stu.Length)
                {
                    return null;
                }
                return stu[index];  
            }
            set
            {
                if (index < 0 || index >= stu.Length)
                {
                    return;
                }
                stu[index] = value;
            }
        }
    }
}

6.3

namespace contest6_3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Cart<Product> prot = new Cart<Product>();
        private void button1_Click(object sender, EventArgs e)
        {
            prot.Carts.Add(new Appliance(txtname.Text));
            lblshow.Text += string.Format("\n添加电器{0}成功", txtname.Text);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            prot.Carts.Add(new Commody(txtname.Text));
            lblshow.Text += string.Format("\n添加日用百货{0}成功", txtname.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            prot.Carts.Add(new Clothes(txtname.Text));
            lblshow.Text += string.Format("\n添加衣服{0}成功", txtname.Text);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            lblshow.Text = prot.CartTo();
        }
    }

    public abstract class Product
    {
        protected string name;
        public Product(string name)
        {
            this.name = name;
        }
        public abstract string Info();

    }

    public class Appliance : Product
    {
        public Appliance(string name) : base(name) { }
        public override string Info()
        {
            return string.Format("{0}是电器", name);
        }
    }

    public class Commody : Product
    {
        public Commody(string name) : base(name) { }
        public override string Info()
        {
            return string.Format("{0}是日用百货", name);
        }
    }

    public class Clothes : Product
    {
        public Clothes(string name) : base(name) { }
        public override string Info()
        {
            return string.Format("{0}是衣服", name);     
        }
    }

    public class Cart<T> where T : Product
    {
        private List<T> pros = new List<T>();
        public List<T> Carts
        {
            get
            {
                return pros;
            }
        }
        public string CartTo()
        {
            string msg = string.Empty;
            foreach(T pro in pros)
            {
                msg += "\n" + pro.Info();
            }
            return msg;
        }
    }
}

8.1

namespace contest8_1
{
    public partial class calculator : Form
    {
        public delegate int Caculate(int x, int y);
        public Caculate handler;
        MyMath math = new MyMath();
        int operand1;
        int operand2;
        bool isContinue = true;
        public calculator()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            btnNum_Click(sender, e);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            btnNum_Click(sender, e);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            btnNum_Click(sender, e);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            btnNum_Click(sender, e);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            btnNum_Click(sender, e);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            btnNum_Click(sender, e);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            btnNum_Click(sender, e);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            btnNum_Click(sender, e);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            btnNum_Click(sender, e);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            btnNum_Click(sender,e);
        }

        private void button11_Click(object sender, EventArgs e)
        {
            btnCalc_Click(sender, e);
        }

        private void button12_Click(object sender, EventArgs e)
        {
            btnCalc_Click(sender, e);
        }

        private void button13_Click(object sender, EventArgs e)
        {
            btnCalc_Click(sender, e);
        }

        private void button14_Click(object sender, EventArgs e)
        {
            btnCalc_Click(sender, e);
        }

        private void button15_Click(object sender, EventArgs e)
        {
            btnCalc_Click(sender, e);
        }

        private void btnNum_Click(object sender, EventArgs s)
        {
            if (sender is Button)
            {
                Button btn = (Button)sender;
                if (isContinue)
                    textResult.Text += btn.Text;
                else
                {
                    isContinue = true;
                    textResult.Text = btn.Text;
                }
            }
        }

        private void btnCalc_Click(object sender,EventArgs e)
        {
            isContinue = false;
            if (handler == null)
            {
                operand1 = Convert.ToInt32(textResult.Text);
            }
            else
            {
                operand2 = Convert.ToInt32(textResult.Text);
                operand1 = handler(operand1, operand2);
                textResult.Text = operand1.ToString();
            }

            if (sender is Button)
            {
                Button btn = (Button)sender;
                switch (btn.Text)
                {
                    case "+":
                        handler += new Caculate(math.Add); break;
                    case "-":
                        handler += new Caculate(math.Sub); break;
                    case "*":
                        handler += new Caculate(math.Mul); break;
                    case "/":
                        handler += new Caculate(math.Div); break;
                    case "=":
                        handler = null; break;
                }
            }
        }
    }

    class MyMath
    {
        public int Add(int x, int y) { return x + y; }
        public int Mul(int x, int y) { return x * y; }
        public int Sub(int x, int y) { return x - y; }
        public int Div(int x, int y) { return x / y; }
    }
}

8.2

namespace contest8_2
{
    public partial class Form1 : Form
    {
        private Stock myStock;
        Random r;
        private EventHandler<StockEventArgs> myStock_OnStockFall;
        public Form1()
        {
            InitializeComponent();
            int price = Convert.ToInt32(txtStartPrice.Text);
            myStock = new Stock(price);
            r = new Random();
        }

        void Change(object sender,StockEventArgs e)
        {
            this.txtPresentPrice.Text = myStock.Price.ToString();
            lblshow.AppendText("\n当前股价:" + myStock.Price);
        }
        void Fall(object sender, StockEventArgs e)
        {
            int buy = Convert.ToInt32(txtbuy.Text);
            if (myStock.Price <= buy)
            {
                lblshow.Text += "\n买入,买入价:" + myStock.Price;
                myStock.OnStockFall -= new EventHandler<StockEventArgs>(Fall);
                txtbuy.Text = "";
            }
        }
        void Rise(object sender, StockEventArgs e)
        {
            int sell = Convert.ToInt32(txtsell.Text);
            if (myStock.Price >= sell)
            {
                lblshow.Text += "\n卖出,卖出价:" + myStock.Price;
                myStock.OnStockRise -= new EventHandler<StockEventArgs>(Rise);
                txtsell.Text = "";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            myStock.OnStockChange += new EventHandler<StockEventArgs>(Change);
            timer1.Enabled = true;
            txtPresentPrice.Text = txtStartPrice.Text;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            myStock.OnStockFall += new EventHandler<StockEventArgs>(Fall);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            myStock.OnStockFall += new EventHandler<StockEventArgs>(Rise);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int nowPrice;
            nowPrice = Convert.ToInt32(txtPresentPrice.Text);
            int change = r.Next(-3, 4);
            myStock.Price = nowPrice + change;
           
        }
    }

    class StockEventArgs : EventArgs
    {
        int stockPrice;
        public StockEventArgs(int stockPrice)
        {
            this.stockPrice = stockPrice;
        }
        public int StockPrice
        {
            get
            {
                return stockPrice;
            }
        }
    }

    class Stock
    {
        public event EventHandler<StockEventArgs> OnStockChange;
        public event EventHandler<StockEventArgs> OnStockRise;
        public event EventHandler<StockEventArgs> OnStockFall;
        private int stockPrice;
        public Stock(int price)
        {
            stockPrice = price;
        }
        public int Price
        {
            set
            {
                decimal oldPrice = stockPrice;
                stockPrice = value;
                if(OnStockChange!=null && oldPrice != value)
                {
                    OnStockChange(this, new StockEventArgs(stockPrice));
                }
                if (OnStockFall != null && oldPrice > value)
                {
                    OnStockFall(this, new StockEventArgs(stockPrice));
                }
                else if (OnStockRise != null && oldPrice < value)
                {
                    OnStockRise(this, new StockEventArgs(stockPrice));
                }
            }
            get
            {
                return stockPrice;
            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值