算法案例总结

乘法表

 for (int i = 1; i <= 9; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    Console.Write("{0}*{1}={2}\t", j, i, i * j);
                }
                Console.WriteLine();
            }

0到100的素数

素数:只能够被1和自己本身整除的数

 for (int i = 0; i < 100; i++)
            {
                int flag1 = 0;
                for (int j = 2; j < i; j++)
                {
                    if (i % j == 0)
                    {
                        flag1 = 1;
                        break;
                    }
                }
                 if (flag1 == 0 || i == 1) Console.WriteLine("{0}是素数", i);
            }

水仙花数

算法:个位的立方+十位的立方+百位的立方=本数

            for (int i = 0; i < 1000; i++)
            {
                int B = i / 100;
                int S = i % 100 / 10;
                int G = i % 10;
                if (i==B*B*B+S*S*S+G*G*G)
                {
                    Console.WriteLine("{0}是水仙花数", i);
                }
            }

里氏转换

在参数列表中自由输入参数,数量不限制(int,double,string),返回其中的最大值。
例如:int Numbers=GetMax(1,2.3,“666”);

class Program
 {
   //对输入参数进行提取赋值
     public static double  Max(params object[] values)
     {
         int k = 0;
         double [] num=new double[5];
         foreach (object item in values)
         {
             if (item is int)
             {
                 num[k] = (int)item;
             }else if(item is double )
             {
                 num[k] = (double)item;
             }else if(item is string)
             {
                 num[k] = Convert.ToInt32((string)item);
             }
             k++;
         }
         //按大到小进行冒泡排序
         for (int i = 0; i < num.Length-1; i++)
         {
             for (int j = 0; j < num.Length-1-i; j++)
             {
                 double temp;
                 if(num[j]<num[j+1])
                 {
                     temp = num[j];
                     num[j] = num[j + 1];
                     num[j + 1] = temp;
                 }
             }
         }
         return num[0];  //返回排序后第一个最大值
     }
     static void Main(string[] args)
     {
         double  max1 = Max(100, 2000.3, "666");
         Console.WriteLine("最大值为:{0}",max1);
         Console.ReadKey();
     }
 }

接口

利用接口实现输出进销存管理系统中的每月销售明细

   interface Information2
    {
        string Code { get; set; }
        string Name { get; set; }
        void Show();
    }
    class Sell:Information2
    {
        string code, name;

        public string Code { get => code; set => code = value; }
        public string Name { get => name; set => name = value; }
        public Sell(string code,string name)
        {
            Code = code;
            Name = name;
        }
        public void Show() { }
        public static void Show(Sell[] sells)
        {
            foreach (Sell item in sells)
            {
                Console.WriteLine("商品编号:{0}  商品名称:{1}",item.Code,item.Name);
            }
        }
    }
 class work
    {
        static void Main(string[] args)
        {
            Console.WriteLine("*******************销售明细****************************");
            Sell[] sellsJan = new Sell[] { new Sell("T0001", "笔记本电脑"), new Sell("T0002", "华为笔记本"), new Sell("T0003", "联想笔记本"), new Sell("T0004", "小米笔记本") };
            Sell[] sellsFeb = { new Sell("T0006", "华为荣耀9标配版"), new Sell("T0007", "华为荣耀9高配版") };
            Sell[] sellsMar = { new Sell("T0003", "iPad"), new Sell("T0004", "华为荣耀V9"), new Sell("T0008", "一加手机"), new Sell("T0009", "充电宝") };
            try
            {
                Console.Write("请输入要查询的月份:");
                int sel = Convert.ToInt32(Console.ReadLine());
                switch (sel)
                {
                    case 1:
                        Sell.Show(sellsJan);
                        break;
                    case 2:
                        Sell.Show(sellsFeb);
                        break;
                    case 3:
                        Sell.Show(sellsMar);
                        break;
                    default:
                        Console.WriteLine("输入数字超范围");
                        break;
                }
            
            }
            catch (Exception a)
            {

                Console.WriteLine("输入有误:"+a.Message);
            }
            Console.ReadKey();
        }
    }

银行管理系统(接口实现)

    public interface IBankAccount
    {
        void PayIn(double amount);//存款
        bool Withdraw(double amount);//取款
    }

    public interface ITransferBankAccount : IBankAccount
    {
        //转账: 要转的账号, 金额
       bool TransferTo(IBankAccount destination, double amount);
    }

    public class CurrentAccount : ITransferBankAccount
    {
        private double _balance;//字段
        public double Balance { get { return _balance; } } //余额
        public CurrentAccount() { }
        public CurrentAccount(double value)
        {
            this._balance = value;
        }
        public void PayIn(double amount)//存款
        {
            _balance += amount;
        }
        public bool Withdraw(double amount)//取款
        {
            if (_balance >= amount)
            {
                //账户的余额足够
                _balance -= amount;
                return true;
            }
            else
            {
                Console.WriteLine("余额不足,取款失败!");
                return false;
            }
        }
        //转账
        public bool TransferTo(IBankAccount destination, double amount)
        {
            //判断一下余额是否充足
            if (this.Withdraw(amount) == true)//扣款成功判断
            {
                destination.PayIn(amount);//往账号存钱
                return true;
            }
            else
            {
                return false;//余额不足
            }
        }

        public override string ToString()
        {
            return "Current Bank Account:Balance=" + this._balance;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("=============账户存款==============");
            IBankAccount account1 = new CurrentAccount(200); //本账户
            Console.WriteLine("账户1存200");
            ITransferBankAccount account2 = new CurrentAccount(500);//要转账
            Console.WriteLine("账户2存500");
            Console.WriteLine("=============转账操作==============");
            Console.WriteLine("账户2转账100元到账户1");
            account2.TransferTo(account1, 100);//账户2对账户1转账
            Console.WriteLine("=============显示账户余额==============");
            Console.WriteLine("account1's" + account1.ToString());
            Console.WriteLine("account2's" + account2.ToString());

            Console.ReadKey();
        }
    }

账号密码校对(面向对象方法)

    public class Adminstrator
    {
        //字段
        string _longinName;//账号
        string _loginPwd;//密码
        string _control;//权限

        //属性
        /// <summary>
        /// 账号
        /// </summary>
        public string LonginName { get => _longinName; set => _longinName = value; }
        /// <summary>
        /// 密码
        /// </summary>
        public string LoginPwd { get => _loginPwd; set => _loginPwd = value; }
        /// <summary>
        /// 权限
        /// </summary>
        public string Control { get => _control; set => _control = value; }

        //构造函数
        public Adminstrator() { Console.WriteLine("无参的构造函数"); }
        public Adminstrator(string Name, string LoginPwd)
        {
            Console.WriteLine("有参构造函数");
            this.LonginName = Name; this.LoginPwd = LoginPwd;
        }

        //构建一个静态方法进行查询
        public static Adminstrator Login(Adminstrator objadmin)
        {
            if (objadmin.LonginName == "admin" && objadmin.LoginPwd == "admin")
            {
                objadmin.Control = "管理员";
                return objadmin;
            }
            else if (objadmin.LonginName == "SomOne" && objadmin.LoginPwd == "123456")
            {
                objadmin.Control = "普通用户";
                return objadmin;
            }
            else
            {
                return null;
            }
        }
    }

Winform 通过构造函数值传递

通过form2操控form1按钮属性

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

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 fr2 = new Form2(this);
            fr2.Show();
        }
    }
//----------------------------------------------------------------------------
    public partial class Form2 : Form
    {
        private Form1 fr1;
        public Form2()
        {
            InitializeComponent();
        }
        public Form2(Form1 fr):this()
        {
            this.fr1 = fr;
        }
        //启用
        private void button1_Click(object sender, EventArgs e)
        {
            this.fr1.Enabled = true;
        }
        //停用
        private void button2_Click(object sender, EventArgs e)
        {
            this.fr1.Enabled = false;
        }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值