C#小练习09

定义一个公司员工类,包含员工的员工号,姓名,性别,出生年月,年龄,工资金额等字段和属性,其中规定姓名不能为空,工资金额不低于当地最低工资600元,员工号从1000到5999,并给出将员工所有信息打印出来的ToString()方法。  

 

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

namespace CSharp_9_01
{
    class Employee
    {
        private static int Id;

        public static int Id1
        {
            get
            {
                if (Employee.Id >= 1000 && Employee.Id <= 5999)
                {
                    return Employee.Id;
                }
                else
                {
                    Console.WriteLine("员工工号应为1000到5999之间,已经初始化为1000");
                    return 1000;
                }
            }
            set { Employee.Id = value; }
        }
        private static string Name;

        public static string Name1
        {
            get
            {
                if (Employee.Name != null)
                {
                    return Employee.Name;
                }
                else
                    Console.WriteLine("姓名不能为空,已将姓名初始化为小明");
                return "小明";
            }
            set
            {
                if (Employee.Name != null)
                {
                    Employee.Name = value;
                }
                else
                {
                    Console.WriteLine("姓名不能为空,已将姓名初始化为小明");
                    Employee.Name = "小明";
                }
            }
        }
        private static char Sex = '男';
        private static int Year = 1997;
        private static int Month = 09;
        private static int Day = 09;
        private static int Age = 20;
        private static float Salary;

        public static float Salary1
        {
            get
            {
                if (Employee.Salary >= 600)
                {
                    return Employee.Salary;
                }
                else
                {
                    Console.WriteLine("最低工资不低于600,已经初始化为600");
                    return 600;
                }
            }
            set
            {
                if (Employee.Salary >= 600)
                {
                    Employee.Salary = value;
                }
                else
                {
                    Console.WriteLine("最低工资不低于600,已经初始化为600");
                    Employee.Salary = 600;
                }
            }
        }
        public override string ToString()
        {

            return (string.Format("员工工号为:{0}\n员工姓名为:{1}\n员工性别为:{2}\n员工出生年月日为:{3}年{4}月{5}日\n员工年龄为:{6}\n员工工资为:{7}\n", Id1, Name1, Sex, Year, Month, Day, Age, Salary1));
        }
       
        static void Main(string[] args)
        {
            Employee e = new Employee();
            Console.WriteLine(e);
           
        }
    }
}

定义一个电视机类,具有以下特性:

1、生产厂家;

2、是否支持数字信号;

3、价格;

4、当前频道(最多有100个频道);

一台电视机必须有以下操作功能:

1、打开电视;

2、关闭电视;

3、选台;

4、显示当前频道;

 

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

namespace CSharp_9_02
{
    class Tv
    {
        static int num = 0;
        static string factory="熊猫牌";
        static bool IssupNub=true;
        static float Price=1999;
        static int[] Channel = new int[100];
        static void OpenTv()
        {
            Console.WriteLine("输入要打开的频道数字:");
            num++;
            Channel[num] = int.Parse(Console.ReadLine());
            Console.WriteLine("正在打开电视即将打开:频道{0}……", Channel[num]);
            Console.WriteLine("打开成功!");
        }
        
        static void ChangeChannel()
        {
            Console.WriteLine("输入要切换的频道数字:");
            num++;
            Channel[num] = int.Parse(Console.ReadLine());
            Console.WriteLine("正在切换到频道{0}……", Channel[num]);
        }
        static void ShowTv()
        {
            Console.WriteLine("当前所有已打开频道列表为:");
            for (int i = 1; i <= num; i++)
            {
                Console.WriteLine("channel:{0}",Channel[i]);
            } 
            Console.WriteLine("当前正在收看频道为:{0}", Channel[num]);
           
        }
        static void CloseTv()
        {

            num--;
            Console.WriteLine("正在关闭电视……");
        }

        static void Main(string[] args)
        {
            OpenTv();
           
            ChangeChannel();
            ShowTv();
            CloseTv();
            
        }
    }
}
    
    


定义复数类complex,使用运算符重载实现复数相加、想减、相乘和相除的运算。

 

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

namespace CSharp_9_03
{
    public struct Complex
    {

        public double real;
        public double image;
        public Complex(double real, double image)
        {
            this.real = real;
            this.image = image;

        }
        //运算符重载+
        public static Complex operator +(Complex c1, Complex c2)
        {
            return new Complex(c1.real + c2.real, c1.image + c2.image);
        }
        //运算符重载-
        public static Complex operator -(Complex c1, Complex c2)
        {
            return new Complex(c1.real - c2.real, c1.image - c2.image);
        }
        //运算符重载* (a+bi)(c+di)=(ac-bd)-(bc+ad)i
        public static Complex operator *(Complex c1, Complex c2)
        {
            double a=c1.real * c2.real - c1.image * c2.image;
            double b= c1.image * c2.real + c1.real * c2.image;
            return new Complex(a,b);
        }
        //运算符重载/
        public static Complex operator /(Complex c1, Complex c2)
        {
            double a = (c1.real * c2.real + c1.image * c2.image) / (c2.image * c2.image + c2.real * c2.real);
            double b = (c1.image * c2.real - c1.real * c2.image) / (c2.real * c2.real + c2.image * c2.image);
            return new Complex(a, b);
        }
        public override string ToString()
        {
            if (image < 0) return (string.Format("{0}{1}i", real, image));
            return (string.Format("{0}+{1}i", real, image));
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Complex num1 = new Complex(2, 3);
            Complex num2 = new Complex(3, 4);
            Console.WriteLine("复数1:{0:f2}", num1);
            Console.WriteLine("复数2:{0:f2}", num1);
            Console.WriteLine("复数相加:{0:f2}", num1 + num2);
            Console.WriteLine("复数相减:{0:f2}", num1 - num2);
            Console.WriteLine("复数相乘:{0:f2}", num1 * num2);
            Console.WriteLine("复数相除:{0:f2}", num1 / num2);
        }
    }
}

某公司雇员(Employee)包括经理(Manager),技术人员(Technician)和销售员(Salesman)。开发部经理(DeveloperManger),既是经理也是技术人员。销售部经理(SalesManager),既是经理也是销售员。 以Employee类为虚基类派生出Manager,Technician和Salesman类;再进一步派生出Developermanager和Salesmanager类。 Employee类的属性包括姓名、职工号、工资级别,月薪(实发基本工资加业绩工资)。操作包括月薪计算函数(pay()),该函数要求输入请假天数,扣去应扣工资后,得出实发基本工资。 Technician类派生的属性有每小时附加酬金和当月工作时数,及研究完成进度系数。业绩工资为三者之积。也包括同名的pay()函数,工资总额为基本工资加业绩工资。 Salesman类派生的属性有当月销售额和酬金提取百分比,业绩工资为两者之积。也包括同名的pay()函数,工资总额为基本工资加业绩工资。 Manager类派生属性有固定奖金额和业绩系数,业绩工资为两者之积。工资总额也为基本工资加业绩工资。 而DeveloperManager类,pay()函数是将作为经理和作为技术人员业绩工资之和的一半作为业绩工资。 SalesManager类,pay()函数则是经理的固定奖金额的一半,加上部门总销售额与提成比例之积,这是业绩工资。 编程实现工资管理。特别注意pay()的定义和调用方法:先用同名覆盖,再用运行时多态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值