c#作业第一次

这是第一题:

 

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 AddressBook
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }
        private void button_add_Click(object sender, EventArgs e)
        {
            string name = newName.Text;
            string phone = newPhone.Text;
            string email = newEmail.Text;
            AddressBook book = new AddressBook(name,phone,email);
            show.Text = book.GetMessage();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
    class AddressBook 
    {
        private string name;
        private string phone;
        private string email;
        public AddressBook(string name, string phone, string email)
        {
            this.name = name;
            this.phone = phone;
            this.email = email;
        }
        public string Name
        {
            get { return name; }
        }
        public string Phone
        {
            get
            {
                if (phone == "" || phone == null) return "未输入";
                else return phone;
            }
            set
            {
                phone = value;
            }
        }
        public string Email
        {
            get
            {
                if (email == "" || email == null) return "未输入";
                else return email;
            }
            set
            {
                email = value;
            }
        }
        public string GetMessage()
        {
            return string.Format("姓名:{0}
电话:{1}
Email:{2}",Name,Phone,Email);
        }
    }
}

这是第二题:

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

namespace AccounDemo //这是命名空间,用来装几个类
{
    public partial class Form1 : Form //也是一个类,应该是一个窗口类,基类是From,public 表示类的访问级别 公开 partial 表示部分类  就是说这些代码只是类的一部分 (还有另一部分在其他地方) Form1 类名: 在这里表示继承   Form 也是类名(此处表示基类)
    {
        Account account = null;//定义一个类的对象              
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)//创建账户按钮的点击事件,实例化Account类
        {
            account = new Account();
            string message = string.Format("创建用户成功,用户卡号为:{0}", account.CreditNo);//eg:这里的creditNo是私有的,需要使用CreditNo进行访问
            lblShow.Text = message;
        }

        private void btnWithDraw_Click(object sender, EventArgs e)//取款按钮的点击事件
        {
            string message;
            if(account==null)
            {
                message = "请先创建账户!";
            }
            else if(txtWithDraw.Text=="" || txtWithDraw.Text==null)
            {
                message = "请输入取款金额!";
            }
            else
            {
                decimal money = decimal.Parse(txtWithDraw.Text);
                account.WithDraw(money,out message);
            }
            lblShow.Text = "
" + message + "
";
        }

        private void btnWithIn_Click(object sender, EventArgs e)//存款按钮的点击事件
        {
            string message;
            if (account == null)
            {
                message = "请先创建账户!";
            }
            else if (txtWithIn.Text == "" || txtWithIn.Text == null)
            {
                message = "请输入存款金额!";
            }
            else
            {
                decimal money = decimal.Parse(txtWithIn.Text);
                account.WithIn(money, out message);
            }
            lblShow.Text = "
" + message + "
";
        }

        private void btnWithShow_Click(object sender, EventArgs e)//查询余额按钮的点击事件
        {
            string message;
            if (account==null)
            {
                message = "请先创建账户!";
            }
            else
            {
                message = "
" + "储蓄卡账户:" + "
" + account.CreditNo + "
" + "当前的余额为:" + account.Balance;
            }            
            lblShow.Text = message;
        }
    }
    public class Account
    {
        private int creditNo;
        private decimal balance;
        public Account()//没有参数的构造函数,默认的构造函数
        {
            Random r = new Random();//实例化一个可以生成随机数的类
            creditNo = r.Next(100000,500000);//设置生成的随机数的范围
            balance = 100;
        }
        public decimal Balance //访问私有成员变量
        {
                get { return this.balance; }
        }
        public int CreditNo //访问私有成员变量
        {
            get { return this.creditNo;  }
        }
        public bool WithDraw(decimal money,out string message)//成员函数:取款操作
        {
            if(money<0)
            {
                message = "操作失败!
输入金额不正确!";
                return false;
            }
            else if(balance>=money)
            {
                balance -= money;
                message = "操作成功!
取款" + money + "";
                return true;
            }
            else
            {
                message = "操作失败!
余额不足!";
                return false;
            }
        }
        public bool WithIn(decimal money,out string message)//成员函数:存款操作
        {
            if(money<0)
            {
                message = "操作失败!
输入金额不正确!";
                return false;
            }
            else
            {
                balance += money;
                message = "操作成功!
存款" + money + "";
                return true;
            }
        }
    }

}

 这是第三题:

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

namespace MaxDemo
{
    public partial class Form1 : Form
    {
        Maxer M = new Maxer();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int a = Convert.ToInt32(txtb1.Text);
            int b = Convert.ToInt32(txtb2.Text);
            int ans = M.Max(a,b);
            lblShow.Text = "最大值:" + ans + "
";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            double a = Convert.ToDouble(txtb1.Text);
            double b = Convert.ToDouble(txtb2.Text);
            double ans = M.Max(a,b);
            lblShow.Text = "最大值:" + ans + "
";

        }

        private void button3_Click(object sender, EventArgs e)
        {
            int a = Convert.ToInt32(txtb1.Text);
            int b = Convert.ToInt32(txtb2.Text);
            int c = Convert.ToInt32(txtb3.Text);
            int ans = M.Max(a, b,c);
            lblShow.Text = "最大值:" + ans + "
";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string s1 = txtb1.Text;
            string s2 = txtb2.Text;
            string s3 = txtb3.Text;
            string ans = M.Max(s1, s2, s3);
            lblShow.Text = "最大字符串:" + ans + "
";
        }
    }
    class Maxer
    {
        public int Max(int a,int b)
        {
            return a > b ? a : b;
        }
        public int Max(int a,int b,int c)
        {
            int max = a;
            if (b > max) max = b;
            if (c > max) max = c;
            return max;
        }
        public double Max(double a,double b)
        {
            return a > b ? a : b;
        }
        public string Max(string s1,string s2,string s3)
        {
            string max = s1;
            max = Max(max,s2);
            max = Max(max,s3);
            return max;
        }
        public string Max(string s1,string s2)
        {
            int len1 = s1.Length, len2 = s2.Length;
            for(int i=0 ; i<len1 && i<len2 ; i++)
            {
                if (s1[i] < s2[i]) return s2;
                else if (s1[i] > s2[i]) return s1;
            }
            if (len1 < len2) return s2;
            else return s1;
        }

    }

}

这是第四题:

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

namespace 书籍
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Book book;
            if (txtTitle.Text == "" || txtTitle.Text==null)
            {
                if (txtPrice.Text == "" || txtPrice.Text==null)
                {
                    lblShow.Text = "调用无参构造函数:";
                    book = new Book();
                }
                else
                {
                    lblShow.Text = "调用有一个只有价格参数的构造函数";
                    book = new Book(double.Parse(txtPrice.Text));
                }
            }
            else 
            {
                if(txtPrice.Text=="" || txtPrice.Text==null)
                {
                    lblShow.Text = "调用有一个只有书名参数的构造函数";
                    book = new Book(txtTitle.Text);
                }
                else
                {
                    double price = Convert.ToDouble(txtPrice.Text);
                    lblShow.Text = "调用有两个参数的构造函数";
                    book = new Book(txtTitle.Text, price);
                }
                
            }
            lblShow.Text += "
" + book.GetMessage();
        }

        private void lblShow_Click(object sender, EventArgs e)
        {

        }
    }
    public class Book
    {
        private string title;
        private double price;
        public Book() : this("无名", 20) { }
        public Book(string title, double price)
        {
            this.title = title;
            this.price = price;
        }
        public Book(double price) : this("无名", price) { }
        public Book(string title) : this(title, 20) { }
        public string GetMessage()
        {
            return string.Format("书名:{0}
价格:{1}元。", this.title, this.price);
        }
    }
}
原文地址:https://www.cnblogs.com/zuimeiyujianni/p/8835321.html